Remove redundant WAIT FOR LSN caller-side pre-checks

All five wakeup call sites duplicate WaitLSNWakeup()'s internal
fast-path minWaitedLSN check and add an unnecessary NULL check
on waitLSNState.

Remove the inline pre-checks and call WaitLSNWakeup() directly.
The fast-path check inside WaitLSNWakeup() already returns early
when no waiter's target has been reached, so there is no
performance difference.

The waitLSNState NULL checks are also unnecessary: shared memory
is fully initialized before any backend or auxiliary process
starts, so waitLSNState is always non-NULL at these call sites.

Reported-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/jzq5shdewncpxc35r3s2mcfsmo4bjovkza5mnqf5bdfumhfi3g%40bglckf7dxmw5
Author: Xuneng Zhou <xunengzhou@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>
This commit is contained in:
Alexander Korotkov 2026-05-03 16:17:32 +03:00
parent a80a593ab6
commit df9f938ca2
3 changed files with 16 additions and 28 deletions

View file

@ -2936,12 +2936,10 @@ XLogFlush(XLogRecPtr record)
WalSndWakeupProcessRequests(true, !RecoveryInProgress());
/*
* If we flushed an LSN that someone was waiting for, notify the waiters.
* Wake up processes waiting for primary flush LSN to reach current flush
* position.
*/
if (waitLSNState &&
(LogwrtResult.Flush >=
pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_PRIMARY_FLUSH])))
WaitLSNWakeup(WAIT_LSN_TYPE_PRIMARY_FLUSH, LogwrtResult.Flush);
WaitLSNWakeup(WAIT_LSN_TYPE_PRIMARY_FLUSH, LogwrtResult.Flush);
/*
* If we still haven't flushed to the request point then we have a
@ -3126,12 +3124,10 @@ XLogBackgroundFlush(void)
WalSndWakeupProcessRequests(true, !RecoveryInProgress());
/*
* If we flushed an LSN that someone was waiting for, notify the waiters.
* Wake up processes waiting for primary flush LSN to reach current flush
* position.
*/
if (waitLSNState &&
(LogwrtResult.Flush >=
pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_PRIMARY_FLUSH])))
WaitLSNWakeup(WAIT_LSN_TYPE_PRIMARY_FLUSH, LogwrtResult.Flush);
WaitLSNWakeup(WAIT_LSN_TYPE_PRIMARY_FLUSH, LogwrtResult.Flush);
/*
* Great, done. To take some work off the critical path, try to initialize

View file

@ -1782,14 +1782,11 @@ PerformWalRecovery(void)
ApplyWalRecord(xlogreader, record, &replayTLI);
/*
* If we replayed an LSN that someone was waiting for then walk
* over the shared memory array and set latches to notify the
* waiters.
* Wake up processes waiting for standby replay LSN to reach
* current replay position.
*/
if (waitLSNState &&
(XLogRecoveryCtl->lastReplayedEndRecPtr >=
pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_REPLAY])))
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY, XLogRecoveryCtl->lastReplayedEndRecPtr);
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_REPLAY,
XLogRecoveryCtl->lastReplayedEndRecPtr);
/* Exit loop if we reached inclusive recovery target */
if (recoveryStopsAfter(xlogreader))

View file

@ -981,12 +981,10 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
pg_atomic_write_membarrier_u64(&WalRcv->writtenUpto, LogstreamResult.Write);
/*
* If we wrote an LSN that someone was waiting for, notify the waiters.
* Wake up processes waiting for standby write LSN to reach current write
* position.
*/
if (waitLSNState &&
(LogstreamResult.Write >=
pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_WRITE])))
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, LogstreamResult.Write);
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_WRITE, LogstreamResult.Write);
/*
* Close the current segment if it's fully written up in the last cycle of
@ -1028,13 +1026,10 @@ XLogWalRcvFlush(bool dying, TimeLineID tli)
SpinLockRelease(&walrcv->mutex);
/*
* If we flushed an LSN that someone was waiting for, notify the
* waiters.
* Wake up processes waiting for standby flush LSN to reach current
* flush position.
*/
if (waitLSNState &&
(LogstreamResult.Flush >=
pg_atomic_read_u64(&waitLSNState->minWaitedLSN[WAIT_LSN_TYPE_STANDBY_FLUSH])))
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, LogstreamResult.Flush);
WaitLSNWakeup(WAIT_LSN_TYPE_STANDBY_FLUSH, LogstreamResult.Flush);
/* Signal the startup process and walsender that new WAL has arrived */
WakeupRecovery();