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 53b327f83e (v17~).

Discussion: https://postgr.es/m/7daef094-abf3-4672-bc23-3df4763b16a3@gmail.com
Backpatch-through: 16
This commit is contained in:
Michael Paquier 2026-06-11 14:43:07 +09:00
parent 18955d412c
commit 2a04624daa
2 changed files with 20 additions and 0 deletions

View 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

View 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);