From: Michael Paquier Date: Thu, 11 Jun 2026 05:43:07 +0000 (+0900) Subject: Backport GetWALInsertionTimeLineIfSet() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a04624daa32a5ada3708f488b7f787de8b8f306;p=thirdparty%2Fpostgresql.git Backport GetWALInsertionTimeLineIfSet() 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 --- diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 03d26e24d2c..d9a2ce5e40d 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -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 diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 6fd5b3dfa66..287b8af4350 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -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);