Skip to content

Commit 18e0913

Browse files
committed
StartupXLOG: Don't repeatedly disable/enable local xlog insertion.
All the code that runs in the startup process to write WAL records before that's allowed generally is now consecutive, so there's no reason to shut the facility to write WAL locally off and then turn it on again three times in a row. Unfortunately, this requires a slight kludge in the checkpointer, which needs to separately enable writing WAL in order to write the checkpoint record. Because that code might run in the same process as StartupXLOG() if we are in single-user mode, we must save/restore the state of the LocalXLogInsertAllowed flag. Hopefully, we'll be able to eliminate this wart in further refactoring, but it's not too bad anyway. Amul Sul, with modifications by me. Discussion: http://postgr.es/m/CAAJ_b97fysj6sRSQEfOHj-y8Jfd5uPqOgO74qast89B4WfD+TA@mail.gmail.com
1 parent a75dbf7 commit 18e0913

File tree

1 file changed

+12
-11
lines changed
  • src/backend/access/transam

1 file changed

+12
-11
lines changed

src/backend/access/transam/xlog.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ static void checkTimeLineSwitch(XLogRecPtr lsn, TimeLineID newTLI,
905905
TimeLineID prevTLI);
906906
static void VerifyOverwriteContrecord(xl_overwrite_contrecord *xlrec,
907907
XLogReaderState *state);
908-
static void LocalSetXLogInsertAllowed(void);
908+
static int LocalSetXLogInsertAllowed(void);
909909
static void CreateEndOfRecoveryRecord(void);
910910
static XLogRecPtr CreateOverwriteContrecordRecord(XLogRecPtr aborted_lsn);
911911
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags);
@@ -8062,6 +8062,7 @@ StartupXLOG(void)
80628062
}
80638063
XLogReaderFree(xlogreader);
80648064

8065+
/* Enable WAL writes for this backend only. */
80658066
LocalSetXLogInsertAllowed();
80668067

80678068
/* If necessary, write overwrite-contrecord before doing anything else */
@@ -8080,7 +8081,6 @@ StartupXLOG(void)
80808081
*/
80818082
Insert->fullPageWrites = lastFullPageWrites;
80828083
UpdateFullPageWrites();
8083-
LocalXLogInsertAllowed = -1;
80848084

80858085
/*
80868086
* Emit checkpoint or end-of-recovery record in XLOG, if required.
@@ -8098,7 +8098,6 @@ StartupXLOG(void)
80988098
* If any of the critical GUCs have changed, log them before we allow
80998099
* backends to write WAL.
81008100
*/
8101-
LocalSetXLogInsertAllowed();
81028101
XLogReportParameters();
81038102

81048103
/* If this is archive recovery, perform post-recovery cleanup actions. */
@@ -8467,15 +8466,20 @@ XLogInsertAllowed(void)
84678466
*
84688467
* Note: it is allowed to switch LocalXLogInsertAllowed back to -1 later,
84698468
* and even call LocalSetXLogInsertAllowed() again after that.
8469+
*
8470+
* Returns the previous value of LocalXLogInsertAllowed.
84708471
*/
8471-
static void
8472+
static int
84728473
LocalSetXLogInsertAllowed(void)
84738474
{
8474-
Assert(LocalXLogInsertAllowed == -1);
8475+
int oldXLogAllowed = LocalXLogInsertAllowed;
8476+
84758477
LocalXLogInsertAllowed = 1;
84768478

84778479
/* Initialize as RecoveryInProgress() would do when switching state */
84788480
InitXLOGAccess();
8481+
8482+
return oldXLogAllowed;
84798483
}
84808484

84818485
/*
@@ -9020,6 +9024,7 @@ CreateCheckPoint(int flags)
90209024
XLogRecPtr last_important_lsn;
90219025
VirtualTransactionId *vxids;
90229026
int nvxids;
9027+
int oldXLogAllowed;
90239028

90249029
/*
90259030
* An end-of-recovery checkpoint is really a shutdown checkpoint, just
@@ -9127,7 +9132,7 @@ CreateCheckPoint(int flags)
91279132
* initialized, which we need here and in AdvanceXLInsertBuffer.)
91289133
*/
91299134
if (flags & CHECKPOINT_END_OF_RECOVERY)
9130-
LocalSetXLogInsertAllowed();
9135+
oldXLogAllowed = LocalSetXLogInsertAllowed();
91319136

91329137
checkPoint.ThisTimeLineID = ThisTimeLineID;
91339138
if (flags & CHECKPOINT_END_OF_RECOVERY)
@@ -9307,7 +9312,7 @@ CreateCheckPoint(int flags)
93079312
if (shutdown)
93089313
{
93099314
if (flags & CHECKPOINT_END_OF_RECOVERY)
9310-
LocalXLogInsertAllowed = -1; /* return to "check" state */
9315+
LocalXLogInsertAllowed = oldXLogAllowed;
93119316
else
93129317
LocalXLogInsertAllowed = 0; /* never again write WAL */
93139318
}
@@ -9447,8 +9452,6 @@ CreateEndOfRecoveryRecord(void)
94479452
xlrec.PrevTimeLineID = XLogCtl->PrevTimeLineID;
94489453
WALInsertLockRelease();
94499454

9450-
LocalSetXLogInsertAllowed();
9451-
94529455
START_CRIT_SECTION();
94539456

94549457
XLogBeginInsert();
@@ -9469,8 +9472,6 @@ CreateEndOfRecoveryRecord(void)
94699472
LWLockRelease(ControlFileLock);
94709473

94719474
END_CRIT_SECTION();
9472-
9473-
LocalXLogInsertAllowed = -1; /* return to "check" state */
94749475
}
94759476

94769477
/*

0 commit comments

Comments
 (0)