From de673ec025074cd95ad4a4e53e2c26fcc14d5a4a Mon Sep 17 00:00:00 2001
From: alterego655 <824662526@qq.com>
Date: Fri, 14 Nov 2025 09:34:03 +0800
Subject: [PATCH v1] Fix WaitLSNWakeup() fast-path check for InvalidXLogRecPtr

WaitLSNWakeup() incorrectly returned early when called with
InvalidXLogRecPtr (meaning "wake all waiters"), because the fast-path
check compared minWaitedLSN > 0 without validating currentLSN first.
This caused WAIT FOR LSN commands to wait indefinitely during standby
promotion until random signals woke them.

Add XLogRecPtrIsValid() check before the comparison so InvalidXLogRecPtr
bypasses the fast-path and wakes all waiters immediately.
---
 src/backend/access/transam/xlogwait.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 34fa41ed9b2..78de93db47f 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -270,8 +270,12 @@ WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN)
 
 	Assert(i >= 0 && i < (int) WAIT_LSN_TYPE_COUNT);
 
-	/* Fast path check */
-	if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[i]) > currentLSN)
+	/*
+	 * Fast path check.  Skip if currentLSN is InvalidXLogRecPtr, which means
+	 * "wake all waiters" (e.g., during promotion when recovery ends).
+	 */
+	if (XLogRecPtrIsValid(currentLSN) &&
+		pg_atomic_read_u64(&waitLSNState->minWaitedLSN[i]) > currentLSN)
 		return;
 
 	wakeupWaiters(lsnType, currentLSN);
-- 
2.51.0

