From 6b677889bbf77f1104c922eb744424730949c5f1 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Mon, 18 Oct 2021 18:18:05 -0400
Subject: [PATCH 2/2] Pretend it's not valid until XLogBeginInsert.

---
 src/backend/access/transam/xlog.c       | 11 +++++------
 src/backend/access/transam/xlogfuncs.c  |  2 +-
 src/backend/access/transam/xloginsert.c |  1 +
 src/backend/access/transam/xlogutils.c  | 10 +++++-----
 src/backend/replication/walsender.c     | 10 +++++-----
 5 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 5f1e6d360f..864d76da54 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2241,7 +2241,7 @@ AdvanceXLInsertBuffer(XLogRecPtr upto, bool opportunistic)
 		NewPage->xlp_magic = XLOG_PAGE_MAGIC;
 
 		/* NewPage->xlp_info = 0; */	/* done by memset */
-		NewPage->xlp_tli = ThisTimeLineIDChecked;
+		NewPage->xlp_tli = ThisTimeLineID; //  XXX wouldn't be OK if we deferred until XLogBeginInsert
 		NewPage->xlp_pageaddr = NewPageBeginPtr;
 
 		/* NewPage->xlp_rem_len = 0; */	/* done by memset */
@@ -3294,7 +3294,7 @@ XLogFileInitInternal(XLogSegNo logsegno, bool *added, char *path)
 	int			fd;
 	int			save_errno;
 
-	XLogFilePath(path, ThisTimeLineIDChecked, logsegno, wal_segment_size);
+	XLogFilePath(path, ThisTimeLineID, logsegno, wal_segment_size); // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 	/*
 	 * Try to use existent file (checkpoint maker may have created it already)
@@ -3653,7 +3653,7 @@ InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
 	char		path[MAXPGPATH];
 	struct stat stat_buf;
 
-	XLogFilePath(path, ThisTimeLineIDChecked, *segno, wal_segment_size);
+	XLogFilePath(path, ThisTimeLineID, *segno, wal_segment_size);  // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 	LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
 	if (!XLogCtl->InstallXLogFileSegmentActive)
@@ -8602,7 +8602,6 @@ InitXLOGAccess(void)
 
 	/* ThisTimeLineID doesn't change so we need no lock to copy it */
 	ThisTimeLineID = XLogCtl->ThisTimeLineID;
-	ThisTimeLineIDValid = true;
 	Assert(ThisTimeLineID != 0 || IsBootstrapProcessingMode());
 
 	/* set wal_segment_size */
@@ -9135,11 +9134,11 @@ CreateCheckPoint(int flags)
 	if (flags & CHECKPOINT_END_OF_RECOVERY)
 		LocalSetXLogInsertAllowed();
 
-	checkPoint.ThisTimeLineID = ThisTimeLineIDChecked;
+	checkPoint.ThisTimeLineID = ThisTimeLineID;	// XXX wouldn't be OK if we deferred until XLogBeginInsert
 	if (flags & CHECKPOINT_END_OF_RECOVERY)
 		checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
 	else
-		checkPoint.PrevTimeLineID = ThisTimeLineIDChecked;
+		checkPoint.PrevTimeLineID = ThisTimeLineID; // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 	checkPoint.fullPageWrites = Insert->fullPageWrites;
 
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c
index dada5675e5..4815fd723c 100644
--- a/src/backend/access/transam/xlogfuncs.c
+++ b/src/backend/access/transam/xlogfuncs.c
@@ -511,7 +511,7 @@ pg_walfile_name(PG_FUNCTION_ARGS)
 						 "pg_walfile_name()")));
 
 	XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
-	XLogFileName(xlogfilename, ThisTimeLineIDChecked, xlogsegno, wal_segment_size);
+	XLogFileName(xlogfilename, ThisTimeLineID, xlogsegno, wal_segment_size); // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 	PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
 }
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index b492c656d7..2c2e9ad28d 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -145,6 +145,7 @@ XLogBeginInsert(void)
 	if (begininsert_called)
 		elog(ERROR, "XLogBeginInsert was already called");
 
+	ThisTimeLineIDValid = true;
 	begininsert_called = true;
 }
 
diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
index 802517c881..ec16bd0ae0 100644
--- a/src/backend/access/transam/xlogutils.c
+++ b/src/backend/access/transam/xlogutils.c
@@ -737,7 +737,7 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa
 	 * it looked up the timeline. There's nothing we can do about it if
 	 * StartupXLOG() renames it to .partial concurrently.
 	 */
-	if (state->currTLI == ThisTimeLineIDChecked && wantPage >= lastReadPage)
+	if (state->currTLI == ThisTimeLineID && wantPage >= lastReadPage) // XXX wouldn't be OK if we deferred until XLogBeginInsert
 	{
 		Assert(state->currTLIValidUntil == InvalidXLogRecPtr);
 		return;
@@ -749,7 +749,7 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa
 	 * the current segment we can just keep reading.
 	 */
 	if (state->currTLIValidUntil != InvalidXLogRecPtr &&
-		state->currTLI != ThisTimeLineIDChecked &&
+		state->currTLI != ThisTimeLineID && // XXX wouldn't be OK if we deferred until XLogBeginInsert
 		state->currTLI != 0 &&
 		((wantPage + wantLength) / state->segcxt.ws_segsize) <
 		(state->currTLIValidUntil / state->segcxt.ws_segsize))
@@ -772,7 +772,7 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa
 		 * We need to re-read the timeline history in case it's been changed
 		 * by a promotion or replay from a cascaded replica.
 		 */
-		List	   *timelineHistory = readTimeLineHistory(ThisTimeLineIDChecked);
+		List	   *timelineHistory = readTimeLineHistory(ThisTimeLineID);  // XXX wouldn't be OK if we deferred until XLogBeginInsert
 		XLogRecPtr	endOfSegment;
 
 		endOfSegment = ((wantPage / state->segcxt.ws_segsize) + 1) *
@@ -874,7 +874,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr,
 			read_upto = GetXLogReplayRecPtr(&ThisTimeLineID);
 			ThisTimeLineIDValid = true;
 		}
-		tli = ThisTimeLineIDChecked;
+		tli = ThisTimeLineID; // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 		/*
 		 * Check which timeline to get the record from.
@@ -902,7 +902,7 @@ read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr,
 		 */
 		XLogReadDetermineTimeline(state, targetPagePtr, reqLen);
 
-		if (state->currTLI == ThisTimeLineIDChecked)
+		if (state->currTLI == ThisTimeLineID)  // XXX wouldn't be OK if we deferred until XLogBeginInsert
 		{
 
 			if (loc <= read_upto)
diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 73a6e06e6b..25dc7e4206 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -440,7 +440,7 @@ IdentifySystem(void)
 	values[0] = CStringGetTextDatum(sysid);
 
 	/* column 2: timeline */
-	values[1] = Int32GetDatum(ThisTimeLineIDChecked);
+	values[1] = Int32GetDatum(ThisTimeLineID); // XXX wouldn't be OK if we deferred until XLogBeginInsert
 
 	/* column 3: wal location */
 	values[2] = CStringGetTextDatum(xloc);
@@ -628,7 +628,7 @@ StartReplication(StartReplicationCmd *cmd)
 		XLogRecPtr	switchpoint;
 
 		sendTimeLine = cmd->timeline;
-		if (sendTimeLine == ThisTimeLineIDChecked)
+		if (sendTimeLine == ThisTimeLineID) // XXX wouldn't be OK if we deferred until XLogBeginInsert
 		{
 			sendTimeLineIsHistoric = false;
 			sendTimeLineValidUpto = InvalidXLogRecPtr;
@@ -643,7 +643,7 @@ StartReplication(StartReplicationCmd *cmd)
 			 * Check that the timeline the client requested exists, and the
 			 * requested start location is on that timeline.
 			 */
-			timeLineHistory = readTimeLineHistory(ThisTimeLineIDChecked);
+			timeLineHistory = readTimeLineHistory(ThisTimeLineID); // XXX wouldn't be OK if we deferred until XLogBeginInsert
 			switchpoint = tliSwitchPoint(cmd->timeline, timeLineHistory,
 										 &sendTimeLineNextTLI);
 			list_free_deep(timeLineHistory);
@@ -682,7 +682,7 @@ StartReplication(StartReplicationCmd *cmd)
 	}
 	else
 	{
-		sendTimeLine = ThisTimeLineIDChecked;
+		sendTimeLine = ThisTimeLineID;  // XXX wouldn't be OK if we deferred until XLogBeginInsert
 		sendTimeLineValidUpto = InvalidXLogRecPtr;
 		sendTimeLineIsHistoric = false;
 	}
@@ -812,7 +812,7 @@ logical_read_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int req
 	XLogSegNo	segno;
 
 	XLogReadDetermineTimeline(state, targetPagePtr, reqLen);
-	sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineIDChecked);
+	sendTimeLineIsHistoric = (state->currTLI != ThisTimeLineID);  // XXX wouldn't be OK if we deferred until XLogBeginInsert
 	sendTimeLine = state->currTLI;
 	sendTimeLineValidUpto = state->currTLIValidUntil;
 	sendTimeLineNextTLI = state->nextTLI;
-- 
2.24.3 (Apple Git-128)

