From 0443844f652286cececba39575ebd70bdf489ca0 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Thu, 16 Feb 2017 17:07:34 +0100 Subject: [PATCH] Make it possible to compare xlog location against replica (#396) --- patroni/scripts/wale_restore.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/patroni/scripts/wale_restore.py b/patroni/scripts/wale_restore.py index 78ac48e7..e2b9099b 100755 --- a/patroni/scripts/wale_restore.py +++ b/patroni/scripts/wale_restore.py @@ -133,8 +133,16 @@ class WALERestore(object): with psycopg2.connect(self.master_connection) as con: con.autocommit = True with con.cursor() as cur: - cur.execute("SELECT pg_xlog_location_diff(pg_current_xlog_location(), %s)", - (backup_start_lsn,)) + cur.execute("""SELECT CASE WHEN pg_is_in_recovery() + THEN GREATEST( + pg_xlog_location_diff(COALESCE( + pg_last_xlog_receive_location(), '0/0'), %s)::bigint, + pg_xlog_location_diff( + pg_last_xlog_replay_location(), %s)::bigint) + ELSE pg_xlog_location_diff( + pg_current_xlog_location(), %s)::bigint + END""", (backup_start_lsn, backup_start_lsn, backup_start_lsn)) + diff_in_bytes = int(cur.fetchone()[0]) except psycopg2.Error: logger.exception('could not determine difference with the master location')