From 6730d101000ada2a1aa1fe8d5bd17f47b96f4b8b Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Tue, 9 Jun 2026 13:01:19 +0900
Subject: [PATCH 2/2] Fix race condition in streaming replication timeline
 selection during promotion

---
 src/backend/replication/walsender.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index e80ed052077..f2e9bbe9d95 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -3389,7 +3389,7 @@ XLogSendPhysical(void)
 		 */
 		bool		becameHistoric = false;
 
-		SendRqstPtr = GetStandbyFlushRecPtr(&SendRqstTLI);
+		SendRqstPtr = GetStandbyFlushRecPtr(NULL);
 
 		if (!RecoveryInProgress())
 		{
@@ -3400,9 +3400,22 @@ XLogSendPhysical(void)
 		}
 		else
 		{
+			TimeLineID	insertTLI;
+
+			/*
+			 * If the insertion timeline has already been set, use it. See
+			 * logical_read_xlog_page() for details.
+			 */
+			insertTLI = GetWALInsertionTimeLine();
+
+			if (insertTLI != 0)
+				SendRqstTLI = insertTLI;
+			else
+				GetXLogReplayRecPtr(&SendRqstTLI);
+
 			/*
-			 * Still a cascading standby. But is the timeline we're sending
-			 * still the one recovery is recovering from?
+			 * Is the timeline we're sending still the one recovery is
+			 * recovering from?
 			 */
 			if (sendTimeLine != SendRqstTLI)
 				becameHistoric = true;
-- 
2.52.0

