From 7880bc6d59ac78474daeaf8cf6cbfeb75857c431 Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Fri, 27 Nov 2026 14:15:30 +0100
Subject: [PATCH v27112026 3/3] WAIT FOR LSN: reduce per record overhead during
 WAL replay

Replace the three separate WaitLSNWakeup() calls during WAL replay with single
WaitLSNWakeupStandby() which does a just single memory barrier (e.g. x86 "lock"
instruction) followed by plain reads of the three minWaitedLSN values.

Author: Jakub Wartak <jakub.wartak@enterprisedb.com>
Discussion:
---
 src/backend/access/transam/xlogrecovery.c |  7 +-----
 src/backend/access/transam/xlogwait.c     | 26 +++++++++++++++++++++++
 src/include/access/xlogwait.h             |  1 +
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index e091a3e178b..4e0c03af07d 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1809,12 +1809,7 @@ PerformWalRecovery(void)
 			 * WAL was already written and flushed to disk, so write and flush
 			 * waiters can be woken at the replay position too.
 			 */
-			WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY,
-						  XLogRecoveryCtl->lastReplayedEndRecPtr);
-			WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE,
-						  XLogRecoveryCtl->lastReplayedEndRecPtr);
-			WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH,
-						  XLogRecoveryCtl->lastReplayedEndRecPtr);
+			WaitLSNWakeupStandby(XLogRecoveryCtl->lastReplayedEndRecPtr);
 
 			/* Exit loop if we reached inclusive recovery target */
 			if (recoveryStopsAfter(xlogreader))
diff --git a/src/backend/access/transam/xlogwait.c b/src/backend/access/transam/xlogwait.c
index 7170921ecdf..855fea3780f 100644
--- a/src/backend/access/transam/xlogwait.c
+++ b/src/backend/access/transam/xlogwait.c
@@ -376,6 +376,32 @@ WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN)
 	wakeupWaiters(lsnType, currentLSN);
 }
 
+/*
+ * Wake up the all the standby replay, write and flush waiters for currentLSN.
+ *
+ * Speedy variant: use just single memory barrier for read (e.g. single "lock"
+ * x86 instruction) rather than three as it would happen with three
+ * (WAIT_LSN_TYPE_COUNT) separate WaitLSNWakeup() calls.
+ */
+void
+WaitLSNWakeupStandby(XLogRecPtr currentLSN)
+{
+	if (!XLogRecPtrIsValid(currentLSN))
+		return;
+
+	/* Pairs with pg_atomic_write_membarrier_u64() in updateMinWaitedLSN(). */
+	pg_memory_barrier();
+
+	if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_REPLAY]) <= currentLSN)
+		wakeupWaiters(WAIT_LSN_TYPE_STANDBY_REPLAY, currentLSN);
+
+	if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_WRITE]) <= currentLSN)
+		wakeupWaiters(WAIT_LSN_TYPE_STANDBY_WRITE, currentLSN);
+
+	if (pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_FLUSH]) <= currentLSN)
+		wakeupWaiters(WAIT_LSN_TYPE_STANDBY_FLUSH, currentLSN);
+}
+
 /*
  * Clean up any LSN wait state for the current process.
  */
diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h
index 2bf0263e9e2..1f9862f5a69 100644
--- a/src/include/access/xlogwait.h
+++ b/src/include/access/xlogwait.h
@@ -102,6 +102,7 @@ extern PGDLLIMPORT WaitLSNState *waitLSNState;
 
 extern XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType);
 extern void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN);
+extern void WaitLSNWakeupStandby(XLogRecPtr currentLSN);
 extern void WaitLSNCleanup(void);
 extern WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN,
 								int timeout);
-- 
2.43.0

