@@ -433,6 +433,7 @@ typedef struct XLogCtlData
433433 * recovery. Protected by info_lck.
434434 */
435435 bool SharedRecoveryInProgress ;
436+ bool SharedInArchiveRecovery ;
436437
437438 /*
438439 * SharedHotStandbyActive indicates if we're still in crash or archive
@@ -619,6 +620,7 @@ static bool bgwriterLaunched = false;
619620
620621static void readRecoveryCommandFile (void );
621622static void exitArchiveRecovery (TimeLineID endTLI , XLogSegNo endLogSegNo );
623+ static bool ArchiveRecoveryInProgress (void );
622624static bool recoveryStopsHere (XLogRecord * record , bool * includeThis );
623625static void recoveryPausesHere (void );
624626static void SetLatestXTime (TimestampTz xtime );
@@ -2923,7 +2925,7 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr endptr)
29232925 strspn (xlde -> d_name , "0123456789ABCDEF" ) == 24 &&
29242926 strcmp (xlde -> d_name + 8 , lastoff + 8 ) <= 0 )
29252927 {
2926- if (RecoveryInProgress () || XLogArchiveCheckDone (xlde -> d_name ))
2928+ if (ArchiveRecoveryInProgress () || XLogArchiveCheckDone (xlde -> d_name ))
29272929 {
29282930 snprintf (path , MAXPGPATH , XLOGDIR "/%s" , xlde -> d_name );
29292931
@@ -3869,6 +3871,7 @@ XLOGShmemInit(void)
38693871 */
38703872 XLogCtl -> XLogCacheBlck = XLOGbuffers - 1 ;
38713873 XLogCtl -> SharedRecoveryInProgress = true;
3874+ XLogCtl -> SharedInArchiveRecovery = false;
38723875 XLogCtl -> SharedHotStandbyActive = false;
38733876 XLogCtl -> WalWriterSleeping = false;
38743877 XLogCtl -> Insert .currpage = (XLogPageHeader ) (XLogCtl -> pages );
@@ -4262,6 +4265,7 @@ readRecoveryCommandFile(void)
42624265
42634266 /* Enable fetching from archive recovery area */
42644267 InArchiveRecovery = true;
4268+ XLogCtl -> SharedInArchiveRecovery = true;
42654269
42664270 /*
42674271 * If user specified recovery_target_timeline, validate it or compute the
@@ -4300,11 +4304,16 @@ exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo)
43004304{
43014305 char recoveryPath [MAXPGPATH ];
43024306 char xlogpath [MAXPGPATH ];
4307+ /* use volatile pointer to prevent code rearrangement */
4308+ volatile XLogCtlData * xlogctl = XLogCtl ;
43034309
43044310 /*
43054311 * We are no longer in archive recovery state.
43064312 */
43074313 InArchiveRecovery = false;
4314+ SpinLockAcquire (& xlogctl -> info_lck );
4315+ xlogctl -> SharedInArchiveRecovery = false;
4316+ SpinLockRelease (& xlogctl -> info_lck );
43084317
43094318 /*
43104319 * Update min recovery point one last time.
@@ -6101,6 +6110,25 @@ RecoveryInProgress(void)
61016110 }
61026111}
61036112
6113+ /*
6114+ * Are we currently in archive recovery? In the startup process, you can just
6115+ * check InArchiveRecovery variable instead.
6116+ */
6117+ static bool
6118+ ArchiveRecoveryInProgress ()
6119+ {
6120+ bool result ;
6121+ /* use volatile pointer to prevent code rearrangement */
6122+ volatile XLogCtlData * xlogctl = XLogCtl ;
6123+
6124+ /* spinlock is essential on machines with weak memory ordering! */
6125+ SpinLockAcquire (& xlogctl -> info_lck );
6126+ result = xlogctl -> SharedInArchiveRecovery ;
6127+ SpinLockRelease (& xlogctl -> info_lck );
6128+
6129+ return result ;
6130+ }
6131+
61046132/*
61056133 * Is HotStandby active yet? This is only important in special backends
61066134 * since normal backends won't ever be able to connect until this returns
0 commit comments