v6-0005-Fix-XLogNeedsFlush-for-checkpointer.patch

text/x-patch

Filename: v6-0005-Fix-XLogNeedsFlush-for-checkpointer.patch
Type: text/x-patch
Part: 2
Message: Re: Checkpointer write combining

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v6-0005
Subject: Fix XLogNeedsFlush() for checkpointer
File+
src/backend/access/transam/xlog.c 12 7
From 9a3f592e5dc7933decad22747d0a4335429d2170 Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Tue, 2 Sep 2025 10:01:17 -0400
Subject: [PATCH v6 5/9] Fix XLogNeedsFlush() for checkpointer

In normal operation, XLogNeedsFlush() returns true if the flush ptr has
not been advanced past the provided LSN. During normal recovery on a
standby (not crash recovery), it return true if the minimum recovery
point hasn't been advanced past the provided LSN.

However, during an end-of-recovery checkpoint, the checkpointer flushes
WAL, so XLogNeedsFlush() should compare the provided location with the
flush pointer.

Correct the logic in XLogNeedsFlush() to compare the LSN to the flush
pointer when WAL inserts are allowed and the minimum recovery point
otherwise.

This is not an active bug because no current users of XLogNeedsFlush()
temporarily allowed WAL inserts during recovery.

Author: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Jeff Davis <pgsql@j-davis.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/flat/CAAKRu_a1vZRZRWO3_jv_X13RYoqLRVipGO0237g5PKzPa2YX6g%40mail.gmail.com
Discussion: https://postgr.es/m/flat/CAAKRu_bcWRvRwZUop_d9vzF9nHAiT%2B-uPzkJ%3DS3ShZ1GqeAYOw%40mail.gmail.com
---
 src/backend/access/transam/xlog.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 0baf0ac6160..62923d33b79 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -3102,21 +3102,26 @@ XLogBackgroundFlush(void)
 }
 
 /*
- * Test whether XLOG data has been flushed up to (at least) the given position.
+ * Test whether XLOG data has been flushed up to (at least) the given position
+ * or whether the minimum recovery point is updated past the given position.
  *
- * Returns true if a flush is still needed.  (It may be that someone else
- * is already in process of flushing that far, however.)
+ * Returns true if a flush is still needed or if the minimum recovery point
+ * must be updated.
+ *
+ * It is possible that someone else is already in the process of flushing that
+ * far or updating the minimum recovery point that far.
  */
 bool
 XLogNeedsFlush(XLogRecPtr record)
 {
 	/*
-	 * During recovery, we don't flush WAL but update minRecoveryPoint
-	 * instead. So "needs flush" is taken to mean whether minRecoveryPoint
-	 * would need to be updated.
+	 * During recovery, when WAL inserts are forbidden, "needs flush" is taken
+	 * to mean whether minRecoveryPoint would need to be updated.
 	 */
-	if (RecoveryInProgress())
+	if (!XLogInsertAllowed())
 	{
+		Assert(RecoveryInProgress());
+
 		/*
 		 * An invalid minRecoveryPoint means that we need to recover all the
 		 * WAL, i.e., we're doing crash recovery.  We never modify the control
-- 
2.43.0