From 3b57dbff6412f3864633eecd0d153d862e1737af Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Tue, 2 Sep 2025 10:01:17 -0400
Subject: [PATCH v1 5/9] Fix XLogNeedsFlush() for checkpointer

XLogNeedsFlush() takes an LSN and compares it to either the flush pointer or the
min recovery point, depending on whether it is in normal operation or recovery.

Even though it is technically recovery, the checkpointer must flush WAL during
an end-of-recovery checkpoint, so in this case, it should compare the provided
LSN to the flush pointer and not the min recovery point.

If it compares the LSN to the min recovery point when the control file's min
recovery point has been updated to an incorrect value, XLogNeedsFlush() can
return an incorrect result of true -- even after just having flushed WAL.

Change this to only compare the LSN to min recovery point -- and, potentially
update the local copy of min recovery point, when xlog inserts are allowed --
which is true for the checkpointer during an end-of-recovery checkpoint, but
false during crash recovery otherwise.

Author: Melanie Plageman <melanieplageman@gmail.com>
Reported-by: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/CAAKRu_a1vZRZRWO3_jv_X13RYoqLRVipGO0237g5PKzPa2YX6g%40mail.gmail.com
---
 src/backend/access/transam/xlog.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 7ffb2179151..16ef6d2cd64 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3115,7 +3115,7 @@ XLogNeedsFlush(XLogRecPtr record)
 	 * instead. So "needs flush" is taken to mean whether minRecoveryPoint
 	 * would need to be updated.
 	 */
-	if (RecoveryInProgress())
+	if (RecoveryInProgress() && !XLogInsertAllowed())
 	{
 		/*
 		 * An invalid minRecoveryPoint means that we need to recover all the
-- 
2.43.0

