Reorder XLogNeedsFlush() checks to be more consistent
authorMichael Paquier <[email protected]>
Tue, 30 Sep 2025 00:38:32 +0000 (09:38 +0900)
committerMichael Paquier <[email protected]>
Tue, 30 Sep 2025 00:38:32 +0000 (09:38 +0900)
During recovery, XLogNeedsFlush() checks the minimum recovery LSN point
instead of the flush LSN point.  The same condition checks are used when
updating the minimum recovery point in UpdateMinRecoveryPoint(), but are
written in reverse order.

This commit makes the order of the checks consistent between
XLogNeedsFlush() and UpdateMinRecoveryPoint(), improving the code
clarity.  Note that the second check (as ordered by this commit) relies
on InRecovery, which is true only in the startup process.  So this makes
XLogNeedsFlush() cheaper in the startup process with the first check
acting as a shortcut while doing crash recovery, where
LocalMinRecoveryPoint is an invalid LSN.

Author: Melanie Plageman <[email protected]>
Reviewed-by: Dilip Kumar <[email protected]>
Discussion: https://postgr.es/m/aMIHNRTP6Wj6vw1s%40paquier.xyz

src/backend/access/transam/xlog.c

index 109713315c0700ca2b3fb4a30d395b1e10ad6dd7..eceab3412558bcac6cfd0a343ee336c25d38971e 100644 (file)
@@ -3134,6 +3134,10 @@ XLogNeedsFlush(XLogRecPtr record)
     */
    if (!XLogInsertAllowed())
    {
+       /* Quick exit if already known to be updated or cannot be updated */
+       if (!updateMinRecoveryPoint || record <= LocalMinRecoveryPoint)
+           return false;
+
        /*
         * An invalid minRecoveryPoint means that we need to recover all the
         * WAL, i.e., we're doing crash recovery.  We never modify the control
@@ -3143,11 +3147,10 @@ XLogNeedsFlush(XLogRecPtr record)
         * it has not replayed all WAL available when doing crash recovery.
         */
        if (XLogRecPtrIsInvalid(LocalMinRecoveryPoint) && InRecovery)
+       {
            updateMinRecoveryPoint = false;
-
-       /* Quick exit if already known to be updated or cannot be updated */
-       if (record <= LocalMinRecoveryPoint || !updateMinRecoveryPoint)
            return false;
+       }
 
        /*
         * Update local copy of minRecoveryPoint. But if the lock is busy,