From fd3f1b95474dea1e05d8ae6448c5969480313ee0 Mon Sep 17 00:00:00 2001 From: Jakub Wartak Date: Fri, 27 Nov 2026 14:15:30 +0100 Subject: [PATCH v02122026 3/4] WAIT FOR LSN: reduce per record overhead during WAL replay Replace the three separate WaitLSNWakeup() calls during WAL replay with a single call to WaitLSNWakeupAllStandby(), which issues one full memory barrier (e.g., a locked instruction on x86) followed by atomic reads of the three minWaitedLSN values. Author: Jakub Wartak Co-authored-by: Xuneng Zhou Reviewed-by: Xuneng Zhou Discussion: https://postgr.es/m/CAKZiRmzEs%2BS60pW_gbjBjSrRYLKfKRf08dTcmN-7%2Bqh8aDFPsw%40mail.gmail.com --- src/backend/access/transam/xlog.c | 9 ++------ src/backend/access/transam/xlogrecovery.c | 7 +----- src/backend/access/transam/xlogwait.c | 36 ++++++++++++++++++++++++++----- src/include/access/xlogwait.h | 6 +++++- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -6942,13 +6942,8 @@ */ WakeupCheckpointer(); - /* - * Wake up all waiters. They need to report an error that recovery was - * ended before reaching the target LSN. - */ - WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, InvalidXLogRecPtr); - WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, InvalidXLogRecPtr); - WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, InvalidXLogRecPtr); + /* Wake all standby waiters so they can recheck after recovery ends. */ + WaitLSNWakeupAllStandby(InvalidXLogRecPtr); /* * Shutdown the recovery environment. This must occur after diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -1809,12 +1809,7 @@ * 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); + WaitLSNWakeupAllStandby(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 --- a/src/backend/access/transam/xlogwait.c +++ b/src/backend/access/transam/xlogwait.c @@ -213,7 +213,10 @@ minWaitedLSN = procInfo->waitLSN; } - /* Pairs with pg_atomic_read_membarrier_u64() in WaitLSNWakeup(). */ + /* + * Pairs with the full barriers in WaitLSNWakeup() and + * WaitLSNWakeupAllStandby(). + */ pg_atomic_write_membarrier_u64(&waitLSNState->minWaitedLSN[i], minWaitedLSN); } @@ -355,7 +358,8 @@ } /* - * Wake up processes waiting for LSN to reach currentLSN + * Wake waiters of the given type whose target LSN has been reached. + * Passing InvalidXLogRecPtr wakes every waiter of that type. */ void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN) @@ -365,15 +369,37 @@ Assert(i >= 0 && i < WAIT_LSN_TYPE_COUNT); /* - * Fast path check. Skip if currentLSN is InvalidXLogRecPtr, which means - * "wake all waiters" (e.g., during promotion when recovery ends). Pairs - * with pg_atomic_write_membarrier_u64() in updateMinWaitedLSN(). + * For a valid LSN, skip the heap if its smallest target is still ahead. + * The read pairs with pg_atomic_write_membarrier_u64() in + * updateMinWaitedLSN(). */ if (XLogRecPtrIsValid(currentLSN) && pg_atomic_read_membarrier_u64(&waitLSNState->minWaitedLSN[i]) > currentLSN) return; wakeupWaiters(lsnType, currentLSN); +} + +/* + * Check all standby wait types and wake waiters whose target LSN has + * been reached. Pass InvalidXLogRecPtr to wake all standby waiters. + */ +void +WaitLSNWakeupAllStandby(XLogRecPtr currentLSN) +{ + /* + * Order the replay-position update before all three minimum checks. + * This pairs with pg_atomic_write_membarrier_u64() in + * updateMinWaitedLSN(). + */ + pg_memory_barrier(); + + for (int i = WAIT_LSN_TYPE_STANDBY_REPLAY; i <= WAIT_LSN_TYPE_STANDBY_FLUSH; i++) + { + if (!XLogRecPtrIsValid(currentLSN) || + pg_atomic_read_u64(&waitLSNState->minWaitedLSN[i]) <= currentLSN) + wakeupWaiters(i, currentLSN); + } } /* diff --git a/src/include/access/xlogwait.h b/src/include/access/xlogwait.h --- a/src/include/access/xlogwait.h +++ b/src/include/access/xlogwait.h @@ -35,7 +35,10 @@ */ typedef enum WaitLSNType { - /* Standby wait types (walreceiver/startup wakes) */ + /* + * Standby wait types (walreceiver/startup wakes). Keep these contiguous + * because WaitLSNWakeupAllStandby() iterates from REPLAY through FLUSH. + */ WAIT_LSN_TYPE_STANDBY_REPLAY, WAIT_LSN_TYPE_STANDBY_WRITE, WAIT_LSN_TYPE_STANDBY_FLUSH, @@ -102,6 +105,7 @@ extern XLogRecPtr GetCurrentLSNForWaitType(WaitLSNType lsnType); extern void WaitLSNWakeup(WaitLSNType lsnType, XLogRecPtr currentLSN); +extern void WaitLSNWakeupAllStandby(XLogRecPtr currentLSN); extern void WaitLSNCleanup(void); extern WaitLSNResult WaitForLSN(WaitLSNType lsnType, XLogRecPtr targetLSN, int timeout);