From 79ca2d385beda379ccddebdb5ae43d5d26f3ca09 Mon Sep 17 00:00:00 2001 From: Dilip Kumar Date: Fri, 12 Sep 2025 08:37:15 +0530 Subject: [PATCH] Make XLogFlush() and XLogNeedsFlush() decision more consistent Make XLogFlush() and XLogNeedsFlush() more consistent in their decision-making process for flushing the WAL. Currently, XLogFlush() uses XLogInsertAllowed() to determine whether to flush the WAL or just update minRecoveryPoint. In contrast, XLogNeedsFlush() checks RecoveryInProgress(). This can lead to inconsistencies. For example, during an end-of-recovery checkpoint, the checkpointer is allowed to flush the WAL, so XLogFlush() will proceed with the flush. However, XLogNeedsFlush() will still check RecoveryInProgress(), which returns true, leading it to mistakenly rely on minRecoveryPoint. This commit resolves the inconsistency by having XLogNeedsFlush() also base its decision on XLogInsertAllowed(). To further improve consistency, XLogFlush() is modified to directly call XLogNeedsFlush() to determine if a WAL flush is necessary, instead of checking LogwrtResult.Flush position. This simplifies the logic and ensures both functions align on the same criteria for flushing. --- src/backend/access/transam/xlog.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 0baf0ac6160..969706dc34f 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -2797,7 +2797,7 @@ XLogFlush(XLogRecPtr record) } /* Quick exit if already known flushed */ - if (record <= LogwrtResult.Flush) + if (!XLogNeedsFlush(record)) return; #ifdef WAL_DEBUG @@ -3111,11 +3111,11 @@ 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. + * If Xlog insert is not allowed, we don't flush WAL but update + * minRecoveryPoint instead. So "needs flush" is taken to mean whether + * minRecoveryPoint would need to be updated. */ - if (RecoveryInProgress()) + if (!XLogInsertAllowed()) { /* * An invalid minRecoveryPoint means that we need to recover all the -- 2.49.0