10000 Don't delete unarchived WAL files during crash recovery. · danielcode/postgres@fea9346 · GitHub
[go: up one dir, main page]

Skip to content

Commit fea9346

Browse files
committed
Don't delete unarchived WAL files during crash recovery.
Bug reported by Jehan-Guillaume (ioguix) de Rorthais. This was introduced with the change to keep WAL files restored from archive in pg_xlog, in 9.2.
1 parent 7b058a2 commit fea9346

File tree

1 file changed

+29
-1
lines changed
  • src/backend/access/transam

1 file changed

+29
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ typedef struct XLogCtlData
418418
* recovery. Protected by info_lck.
419419
*/
420420
bool SharedRecoveryInProgress;
421+
bool SharedInArchiveRecovery;
421422

422423
/*
423424
* SharedHotStandbyActive indicates if we're still in crash or archive
@@ -622,6 +623,7 @@ static void XLogArchiveCleanup(const char *xlog);
622623
static void readRecoveryCommandFile(void);
623624
static void exitArchiveRecovery(TimeLineID endTLI,
624625
uint32 endLogId, uint32 endLogSeg);
626+
static bool ArchiveRecoveryInProgress(void);
625627
static bool recoveryStopsHere(XLogRecord *record, bool *includeThis);
626628
static void recoveryPausesHere(void);
627629
static void SetLatestXTime(TimestampTz xtime);
@@ -3571,7 +3573,7 @@ RemoveOldXlogFiles(uint32 log, uint32 seg, XLogRecPtr endptr)
35713573
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
35723574
strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
35733575
{
3574-
if (RecoveryInProgress() || XLogArchiveCheckDone(xlde->d_name))
3576+
if (ArchiveRecoveryInProgress() || XLogArchiveCheckDone(xlde->d_name))
35753577
{
35763578
snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
35773579

@@ -5289,6 +5291,7 @@ XLOGShmemInit(void)
52895291
*/
52905292
XLogCtl->XLogCacheBlck = XLOGbuffers - 1;
52915293
XLogCtl->SharedRecoveryInProgress = true;
5294+
XLogCtl->SharedInArchiveRecovery = false;
52925295
XLogCtl->SharedHotStandbyActive = false;
52935296
XLogCtl->WalWriterSleeping = false;
52945297
XLogCtl->Insert.currpage = (XLogPageHeader) (XLogCtl->pages);
@@ -5680,6 +5683,7 @@ readRecoveryCommandFile(void)
56805683

56815684
/* Enable fetching from archive recovery area */
56825685
InArchiveRecovery = true;
5686+
XLogCtl->SharedInArchiveRecovery = true;
56835687

56845688
/*
56855689
* If user specified recovery_target_timeline, validate it or compute the
@@ -5718,11 +5722,16 @@ exitArchiveRecovery(TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg)
57185722
{
57195723
char recoveryPath[MAXPGPATH];
57205724
char xlogpath[MAXPGPATH];
5725+
/* use volatile pointer to prevent code rearrangement */
5726+
volatile XLogCtlData *xlogctl = XLogCtl;
57215727

57225728
/*
57235729
* We are no longer in archive recovery state.
57245730
*/
57255731
InArchiveRecovery = false;
5732+
SpinLockAcquire(&xlogctl->info_lck);
5733+
xlogctl->SharedInArchiveRecovery = false;
5734+
SpinLockRelease(&xlogctl->info_lck);
57265735

57275736
/*
57285737
* Update min recovery point one last time.
@@ -7314,6 +7323,25 @@ RecoveryInProgress(void)
73147323
}
73157324
}
73167325

7326+
/*
7327+
* Are we currently in archive recovery? In the startup process, you can just
7328+
* check InArchiveRecovery variable instead.
7329+
*/
7330+
static bool
7331+
ArchiveRecoveryInProgress()
7332+
{
7333+
bool result;
7334+
/* use volatile pointer to prevent code rearrangement */
7335+
volatile XLogCtlData *xlogctl = XLogCtl;
7336+
7337+
/* spinlock is essential on machines with weak memory ordering! */
7338+
SpinLockAcquire(&xlogctl->info_lck);
7339+
result = xlogctl->SharedInArchiveRecovery;
7340+
SpinLockRelease(&xlogctl->info_lck);
7341+
7342+
return result;
7343+
}
7344+
73177345
/*
73187346
* Is HotStandby active yet? This is only important in special backends
73197347
* since normal backends won't ever be able to connect until this returns

0 commit comments

Comments
 (0)
0