From 70374fd88eebae77b53f65957624b1e9929f1685 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Mon, 8 Jun 2026 07:04:43 +0000
Subject: [PATCH v1 3/4] Apply the same timeline fix to
 read_local_xlog_page_guts()

read_local_xlog_page_guts() has the same race as logical_read_xlog_page() (that
has been fixed in commit xxxxx): when RecoveryInProgress() returns true during
promotion, it gets the old timeline from GetXLogReplayRecPtr() while old WAL
segments may already be removed.

This affects SQL-callable logical decoding functions (pg_logical_slot_get_changes,
pg_logical_slot_peek_changes) running on a standby during promotion.

Apply the same fix: use GetWALInsertionTimeLineIfSet() to detect that the
insertion timeline is already established and use it directly.

Reported-by: Xuneng Zhou <xunengzhou@gmail.com>
Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by:
Discussion: https://postgr.es/m/7daef094-abf3-4672-bc23-3df4763b16a3%40gmail.com
---
 src/backend/access/transam/xlogutils.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 100.0% src/backend/access/transam/

diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index 5fbe39133b8..fdc341d8fa4 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -896,7 +896,19 @@ read_local_xlog_page_guts(XLogReaderState *state, XLogRecPtr targetPagePtr,
 		if (!RecoveryInProgress())
 			read_upto = GetFlushRecPtr(&currTLI);
 		else
+		{
+			TimeLineID	insertTLI;
+
 			read_upto = GetXLogReplayRecPtr(&currTLI);
+
+			/*
+			 * If the insertion timeline has already been set, use it. See
+			 * logical_read_xlog_page() for details.
+			 */
+			insertTLI = GetWALInsertionTimeLineIfSet();
+			if (insertTLI != 0)
+				currTLI = insertTLI;
+		}
 		tli = currTLI;
 
 		/*
-- 
2.34.1

