]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Backport GetWALInsertionTimeLineIfSet()
authorMichael Paquier <michael@paquier.xyz>
Thu, 11 Jun 2026 05:43:07 +0000 (14:43 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 11 Jun 2026 05:43:07 +0000 (14:43 +0900)
This routine is able to return the WAL insertion timeline.  Contrary to
GetWALInsertionTimeLine(), this can be called before recovery is marked
as done in shared memory, just after InsertTimeLineID has been set.  So
it can offer more flexibility in terms of timeline detection without
depending on RecoveryInProgress().

This routine will be used in a follow-up fix that will be applied down
to v16, and it has been introduced in 53b327f83ea2 (v17~).

Discussion: https://postgr.es/m/7daef094-abf3-4672-bc23-3df4763b16a3@gmail.com
Backpatch-through: 16

src/backend/access/transam/xlog.c
src/include/access/xlog.h

index 03d26e24d2c86d8bea97b7f91caa57bde6b456e7..d9a2ce5e40d9b1536de608317713750168f15c46 100644 (file)
@@ -6147,6 +6147,25 @@ GetWALInsertionTimeLine(void)
        return XLogCtl->InsertTimeLineID;
 }
 
+/*
+ * GetWALInsertionTimeLineIfSet -- If the system is not in recovery, returns
+ * the WAL insertion timeline; else, returns 0. Wherever possible, use
+ * GetWALInsertionTimeLine() instead, since it's cheaper. Note that this
+ * function decides recovery has ended as soon as the insert TLI is set, which
+ * happens before we set XLogCtl->SharedRecoveryState to RECOVERY_STATE_DONE.
+ */
+TimeLineID
+GetWALInsertionTimeLineIfSet(void)
+{
+       TimeLineID      insertTLI;
+
+       SpinLockAcquire(&XLogCtl->info_lck);
+       insertTLI = XLogCtl->InsertTimeLineID;
+       SpinLockRelease(&XLogCtl->info_lck);
+
+       return insertTLI;
+}
+
 /*
  * GetLastImportantRecPtr -- Returns the LSN of the last important record
  * inserted. All records not explicitly marked as unimportant are considered
index 6fd5b3dfa6625f89186770852aba8032bc0a84ca..287b8af4350bc306b2cab52c491c4418324eb56b 100644 (file)
@@ -245,6 +245,7 @@ extern XLogRecPtr GetRedoRecPtr(void);
 extern XLogRecPtr GetInsertRecPtr(void);
 extern XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI);
 extern TimeLineID GetWALInsertionTimeLine(void);
+extern TimeLineID GetWALInsertionTimeLineIfSet(void);
 extern XLogRecPtr GetLastImportantRecPtr(void);
 
 extern void SetWalWriterSleeping(bool sleeping);