From ac080100b692700688054630d77190d63027798c Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Fri, 7 Feb 2020 12:40:56 +0900
Subject: [PATCH] Fix handling of tentative state after chainging
 synchronous_standby_names

When reloading config after updating synchronous_standby_names,
walsenders don't recalculate their sync-priority exactly at the same
time. SyncRepGetSyncStandbysPriority may fail under such inconsistent
situation.  Fix that by assuming a walsender as non-sync if it has a
priority number that is impossible against the
synchronous_standby_names.
---
 src/backend/replication/syncrep.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index c284103b54..2689421bcd 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -842,6 +842,16 @@ SyncRepGetSyncStandbysPriority(bool *am_sync)
 		if (this_priority == 0)
 			continue;
 
+		/*
+		 * Walsenders don't reload configuration at the same time. A walsender
+		 * may have out-of-range priority before updating by the reload. Treat
+		 * them the same way as non-syncronous standbys.  Priority is going to
+		 * be fixed soon and we can work with consistent priorities at the next
+		 * standby-reply message.
+		 */
+		if (this_priority > lowest_priority)
+			continue;
+		
 		/* Must have a valid flush position */
 		if (XLogRecPtrIsInvalid(flush))
 			continue;
-- 
2.18.2

