Skip to content

Commit

Permalink
Merge pull request #198 from Mixton/priority
Browse files Browse the repository at this point in the history
Fix promotion score computation.

The score were not sorted correctly, making the node with the higher lag the best known one. Fortunately, these scores are just hints for the election. Should a failover occurs, the election process would refuse to promote the wrong node and promote the best one soon after.
  • Loading branch information
ioguix authored Sep 17, 2021
2 parents 31750fa + 78328c6 commit 7ebb25b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions script/pgsqlms
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ sub _get_lag_scores {

# We check locations of connected standbies by querying the
# "pg_stat_replication" view.
# The row_number applies on the result set ordered on write_location ASC so
# the highest row_number should be given to the closest node from the
# primary, then the lowest node name (alphanumeric sort) in case of
# equality. The result set itself is order by priority DESC to process best
# The row_number applies on the result set ordered on write_location DESC so
# the smallest row_number should be given to the closest node from the
# primary (1), then the lowest node name (alphanumeric sort) in case of
# equality. This row_number - 1 is then used to decrease the priority (score) by
# step of 10 units starting from 1000.
# E.g. row_number = 1 and maxlag = 0, ( 1000 - (row_number - 1) * 10 ) * 1 = 1000
# The result set itself is order by priority DESC to process best
# known candidate first.
$query = qq{
SELECT application_name, priority, location, state, current_lag
Expand All @@ -238,7 +241,7 @@ sub _get_lag_scores {
(1000 - (
row_number() OVER (
PARTITION BY state IN ('startup', 'backup')
ORDER BY location ASC, application_name ASC
ORDER BY location DESC, application_name ASC
) - 1
) * 10
) * CASE WHEN ( $maxlag > 0
Expand Down

0 comments on commit 7ebb25b

Please sign in to comment.