Skip to content

Commit

Permalink
change priority query: the node with the highest write_lsn / write_lo…
Browse files Browse the repository at this point in the history
…ation (the closest to the master node) will also have the highest priority
  • Loading branch information
Mixton committed Sep 15, 2021
1 parent 31750fa commit 78328c6
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 78328c6

Please sign in to comment.