public inbox for [email protected]  
help / color / mirror / Atom feed
From: =?utf-8?B?Y2NhNTUwNw==?= <[email protected]>
To: =?utf-8?B?cGdzcWwtaGFja2Vycw==?= <[email protected]>
Subject: Re: [BUG] Take a long time to reach consistent after pg_rewind
Date: Mon, 13 Apr 2026 21:14:19 +0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

> Possible fix:
> 
> The pg_rewind use pg_current_wal_insert_lsn() to set the min recovery point, which calls
> GetXLogInsertRecPtr() and returns the latest wal insert pointer. Maybe we should use
> GetXLogInsertEndRecPtr() which returns the latest wal record end pointer.
> 
> Thoughts?

Another solution:

If minRecoveryPoint is just after a xlog page header, we can move it to the begin of
the page. It's safe because we just skip the xlog page header. Do I miss something?

Attach a patch done like this.

--
Regards,
ChangAo Chen


Attachments:

  [application/octet-stream] v1-0001-Introduce-GetEffectiveMinRecoveryPoint.patch (2.1K, 2-v1-0001-Introduce-GetEffectiveMinRecoveryPoint.patch)
  download | inline diff:
From 76e75797819d01e36b61ed3a74c51e821a5f2370 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <[email protected]>
Date: Mon, 13 Apr 2026 20:51:55 +0800
Subject: [PATCH v1] Introduce GetEffectiveMinRecoveryPoint()

If minRecoveryPoint is just after a xlog page header, we can move
it to the begin of the page. It's safe because we just skip the
xlog page header. Without this, it may take a long time to reach
a consistent state (e.g. the primary doesn't have any xlog record
after the minRecoveryPoint).
---
 src/backend/access/transam/xlogrecovery.c | 25 ++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index c236e2b7969..63e8409eab9 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -2139,6 +2139,29 @@ CheckTablespaceDirectory(void)
 	}
 }
 
+/*
+ * If minRecoveryPoint is just after a xlog page header, we return a pointer
+ * that points to the begin of the page, otherwise return minRecoveryPoint.
+ *
+ * The returned pointer is used for checking whether we can reach a consistent
+ * state. It's safe because we just skip the xlog page header.
+ */
+static XLogRecPtr
+GetEffectiveMinRecoveryPoint(void)
+{
+	XLogRecPtr	ptr = minRecoveryPoint;
+	uint64		pageno = XLogSegmentOffset(ptr, wal_segment_size) / XLOG_BLCKSZ;
+	uint64		pageoff = ptr % XLOG_BLCKSZ;
+
+	if (pageno == 0 && pageoff == SizeOfXLogLongPHD)
+		return ptr - SizeOfXLogLongPHD;
+
+	if (pageno > 0 && pageoff == SizeOfXLogShortPHD)
+		return ptr - SizeOfXLogShortPHD;
+
+	return ptr;
+}
+
 /*
  * Checks if recovery has reached a consistent state. When consistency is
  * reached and we have a valid starting standby snapshot, tell postmaster
@@ -2199,7 +2222,7 @@ CheckRecoveryConsistency(void)
 	 * All we know prior to that is that we're not consistent yet.
 	 */
 	if (!reachedConsistency && !backupEndRequired &&
-		minRecoveryPoint <= lastReplayedEndRecPtr)
+		GetEffectiveMinRecoveryPoint() <= lastReplayedEndRecPtr)
 	{
 		/*
 		 * Check to see if the XLOG sequence contained any unresolved
-- 
2.34.1



reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: [BUG] Take a long time to reach consistent after pg_rewind
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox