]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Remove redundant WAIT FOR LSN caller-side pre-checks
authorAlexander Korotkov <akorotkov@postgresql.org>
Sun, 3 May 2026 13:17:32 +0000 (16:17 +0300)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sun, 3 May 2026 13:22:02 +0000 (16:22 +0300)
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>
src/backend/access/transam/xlog.c
src/backend/access/transam/xlogrecovery.c
src/backend/replication/walreceiver.c

index 18d5dee06e0cf7cf69526b6e377d233a1bcb7b03..f0434da40c94534299478e54c45e5b80ebae03d4 100644 (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
index c236e2b796928a76a0823ea4a2f65ce5de4e2303..4f2eaa369904c9cd16e1a4cd099e92dd47a1e2d7 100644 (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))
index 9e40f066c978d39b04268ea1fba6e6d61558e439..07eac07b9ce4c8d45823dd3b4b9c1e7e456c5280 100644 (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();