From c263f1d1e044d9d3bc03fa4928c94d9951788ace Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Wed, 24 Sep 2025 16:57:40 -0400
Subject: [PATCH] Reorder XLogNeedsFlush() checks to be more consistent

In recovery XLogNeedsFlush() checks the minimum recovery point instead
of the flush pointer. The same condition checks are used when updating
the minimum recovery point but written in reverse order. Make the order
of checks consistent between XLogNeedsFlush() and
UpdateMinRecoveryPoint() -- which is invoked during recovery by
XLogFlush().

This improves clarity and does not change functionality.

Discussion: https://postgr.es/m/aMIHNRTP6Wj6vw1s%40paquier.xyz
---
 src/backend/access/transam/xlog.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index eac1de75ed0..3929b2ff224 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -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,
-- 
2.43.0

