From fb688d863ca214ac0f5a27aabbe39bb1c3a8448d Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Fri, 30 Jan 2026 15:50:02 +0900
Subject: [PATCH v1] Wake up backends immediately when sync standbys decrease

When synchronous_standby_names is changed to require fewer sync standbys
(e.g., from "FIRST 2 (s1,s2)" to "FIRST 1 (s1)"), backends waiting for
synchronous replication were not woken up immediately upon config
reload. They remained blocked until some message arrived from a standby,
even though the new configuration's requirements were already satisfied.

This patch adds SyncRepReleaseWaiters() calls after config reload in
walsender, allowing waiters to be released as soon as the new settings
take effect.

Author: Shinya Kato <shinya11.kato@gmail.com>
Reviewed-by:
Discussion: https://postgr.es/m/
---
 src/backend/replication/walsender.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 1ab09655a70..a4b9e8f5166 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -1652,6 +1652,13 @@ ProcessPendingWrites(void)
 			ConfigReloadPending = false;
 			ProcessConfigFile(PGC_SIGHUP);
 			SyncRepInitConfig();
+
+			/*
+			 * Recheck and release any now-satisfied waiters after config
+			 * reload reduces required sync standbys (e.g., FIRST 2 -> FIRST 1).
+			 */
+			if (!am_cascading_walsender)
+				SyncRepReleaseWaiters();
 		}
 
 		/* Try to flush pending output to the client */
@@ -1860,6 +1867,13 @@ WalSndWaitForWal(XLogRecPtr loc)
 			ConfigReloadPending = false;
 			ProcessConfigFile(PGC_SIGHUP);
 			SyncRepInitConfig();
+
+			/*
+			 * Recheck and release any now-satisfied waiters after config
+			 * reload reduces required sync standbys (e.g., FIRST 2 -> FIRST 1).
+			 */
+			if (!am_cascading_walsender)
+				SyncRepReleaseWaiters();
 		}
 
 		/* Check for input from the client */
@@ -2905,6 +2919,13 @@ WalSndLoop(WalSndSendDataCallback send_data)
 			ConfigReloadPending = false;
 			ProcessConfigFile(PGC_SIGHUP);
 			SyncRepInitConfig();
+
+			/*
+			 * Recheck and release any now-satisfied waiters after config
+			 * reload reduces required sync standbys (e.g., FIRST 2 -> FIRST 1).
+			 */
+			if (!am_cascading_walsender)
+				SyncRepReleaseWaiters();
 		}
 
 		/* Check for input from the client */
-- 
2.47.3

