Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d2ddfa6

Browse files
committed
xlog.c: Remove global variables ReadRecPtr and EndRecPtr.
In most places, the variables necessarily store the same value as the eponymous members of the XLogReaderState that we use during WAL replay, because ReadRecord() assigns the values from the structure members to the global variables just after XLogReadRecord() returns. However, XLogBeginRead() adjusts the structure members but not the global variables, so after XLogBeginRead() and before the completion of XLogReadRecord() the values can differ. Otherwise, they must be identical. According to my analysis, the only place where either variable is referenced at a point where it might not have the same value as the structure member is the refrence to EndRecPtr within XLogPageRead. Therefore, at every other place where we are using the global variable, we can just switch to using the structure member instead, and remove the global variable. However, we can, and in fact should, do this in XLogPageRead() as well, because at that point in the code, the global variable will actually store the start of the record we want to read - either because it's where the last WAL record ended, or because the read position has been changed using XLogBeginRead since the last record was read. The structure member, on the other hand, will already have been updated to point to the end of the record we just read. Elsewhere, the latter is what we use as an argument to emode_for_corrupt_record(), so we should do the same here. This part of the patch is perhaps a bug fix, but I don't think it has any important consequences, so no back-patch. The point here is just to continue to whittle down the entirely excessive use of global variables in xlog.c. Discussion: http://postgr.es/m/CA+Tgmoao96EuNeSPd+hspRKcsCddu=b1h-QNRuKfY8VmfNQdfg@mail.gmail.com
1 parent e7ea2fa commit d2ddfa6

File tree

1 file changed

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

1 file changed

+24
-29
lines changed

src/backend/access/transam/xlog.c

+24-29
Original file line numberDiff line numberDiff line change
@@ -850,10 +850,6 @@ typedef struct XLogPageReadPrivate
850850
static TimestampTz XLogReceiptTime = 0;
851851
static XLogSource XLogReceiptSource = XLOG_FROM_ANY;
852852

853-
/* State information for XLOG reading */
854-
static XLogRecPtr ReadRecPtr; /* start of last record read */
855-
static XLogRecPtr EndRecPtr; /* end+1 of last record read */
856-
857853
/*
858854
* Local copies of equivalent fields in the control file. When running
859855
* crash recovery, minRecoveryPoint is set to InvalidXLogRecPtr as we
@@ -4475,8 +4471,6 @@ ReadRecord(XLogReaderState *xlogreader, int emode,
44754471
char *errormsg;
44764472

44774473
record = XLogReadRecord(xlogreader, &errormsg);
4478-
ReadRecPtr = xlogreader->ReadRecPtr;
4479-
EndRecPtr = xlogreader->EndRecPtr;
44804474
if (record == NULL)
44814475
{
44824476
/*
@@ -4505,7 +4499,7 @@ ReadRecord(XLogReaderState *xlogreader, int emode,
45054499
* shouldn't loop anymore in that case.
45064500
*/
45074501
if (errormsg)
4508-
ereport(emode_for_corrupt_record(emode, EndRecPtr),
4502+
ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
45094503
(errmsg_internal("%s", errormsg) /* already translated */ ));
45104504
}
45114505

@@ -4523,7 +4517,7 @@ ReadRecord(XLogReaderState *xlogreader, int emode,
45234517
wal_segment_size);
45244518
XLogFileName(fname, xlogreader->seg.ws_tli, segno,
45254519
wal_segment_size);
4526-
ereport(emode_for_corrupt_record(emode, EndRecPtr),
4520+
ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
45274521
(errmsg("unexpected timeline ID %u in log segment %s, offset %u",
45284522
xlogreader->latestPageTLI,
45294523
fname,
@@ -4565,9 +4559,9 @@ ReadRecord(XLogReaderState *xlogreader, int emode,
45654559
/* initialize minRecoveryPoint to this record */
45664560
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
45674561
ControlFile->state = DB_IN_ARCHIVE_RECOVERY;
4568-
if (ControlFile->minRecoveryPoint < EndRecPtr)
4562+
if (ControlFile->minRecoveryPoint < xlogreader->EndRecPtr)
45694563
{
4570-
ControlFile->minRecoveryPoint = EndRecPtr;
4564+
ControlFile->minRecoveryPoint = xlogreader->EndRecPtr;
45714565
ControlFile->minRecoveryPointTLI = replayTLI;
45724566
}
45734567
/* update local copy */
@@ -7501,7 +7495,7 @@ StartupXLOG(void)
75017495
if (checkPoint.redo < RecPtr)
75027496
XLogCtl->replayEndRecPtr = checkPoint.redo;
75037497
else
7504-
XLogCtl->replayEndRecPtr = EndRecPtr;
7498+
XLogCtl->replayEndRecPtr = xlogreader->EndRecPtr;
75057499
XLogCtl->replayEndTLI = replayTLI;
75067500
XLogCtl->lastReplayedEndRecPtr = XLogCtl->replayEndRecPtr;
75077501
XLogCtl->lastReplayedTLI = XLogCtl->replayEndTLI;
@@ -7557,7 +7551,7 @@ StartupXLOG(void)
75577551

75587552
ereport(LOG,
75597553
(errmsg("redo starts at %X/%X",
7560-
LSN_FORMAT_ARGS(ReadRecPtr))));
7554+
LSN_FORMAT_ARGS(xlogreader->ReadRecPtr))));
75617555

75627556
/* Prepare to report progress of the redo phase. */
75637557
if (!StandbyMode)
@@ -7572,7 +7566,7 @@ StartupXLOG(void)
75727566

75737567
if (!StandbyMode)
75747568
ereport_startup_progress("redo in progress, elapsed time: %ld.%02d s, current LSN: %X/%X",
7575-
LSN_FORMAT_ARGS(ReadRecPtr));
7569+
LSN_FORMAT_ARGS(xlogreader->ReadRecPtr));
75767570

75777571
#ifdef WAL_DEBUG
75787572
if (XLOG_DEBUG ||
@@ -7686,7 +7680,8 @@ StartupXLOG(void)
76867680
if (newReplayTLI != replayTLI)
76877681
{
76887682
/* Check that it's OK to switch to this TLI */
7689-
checkTimeLineSwitch(EndRecPtr, newReplayTLI,
7683+
checkTimeLineSwitch(xlogreader->EndRecPtr,
7684+
newReplayTLI,
76907685
prevReplayTLI, replayTLI);
76917686

76927687
/* Following WAL records should be run with new TLI */
@@ -7700,7 +7695,7 @@ StartupXLOG(void)
77007695
* so that XLogFlush will update minRecoveryPoint correctly.
77017696
*/
77027697
SpinLockAcquire(&XLogCtl->info_lck);
7703-
XLogCtl->replayEndRecPtr = EndRecPtr;
7698+
XLogCtl->replayEndRecPtr = xlogreader->EndRecPtr;
77047699
XLogCtl->replayEndTLI = replayTLI;
77057700
SpinLockRelease(&XLogCtl->info_lck);
77067701

@@ -7732,7 +7727,7 @@ StartupXLOG(void)
77327727
* successfully replayed.
77337728
*/
77347729
SpinLockAcquire(&XLogCtl->info_lck);
7735-
XLogCtl->lastReplayedEndRecPtr = EndRecPtr;
7730+
XLogCtl->lastReplayedEndRecPtr = xlogreader->EndRecPtr;
77367731
XLogCtl->lastReplayedTLI = replayTLI;
77377732
SpinLockRelease(&XLogCtl->info_lck);
77387733

@@ -7748,7 +7743,7 @@ StartupXLOG(void)
77487743
}
77497744

77507745
/* Remember this record as the last-applied one */
7751-
LastRec = ReadRecPtr;
7746+
LastRec = xlogreader->ReadRecPtr;
77527747

77537748
/* Allow read-only connections if we're consistent now */
77547749
CheckRecoveryConsistency();
@@ -7761,7 +7756,7 @@ StartupXLOG(void)
77617756
* (possibly bogus) future WAL segments on the old
77627757
* timeline.
77637758
*/
7764-
RemoveNonParentXlogFiles(EndRecPtr, replayTLI);
7759+
RemoveNonParentXlogFiles(xlogreader->EndRecPtr, replayTLI);
77657760

77667761
/*
77677762
* Wake up any walsenders to notice that we are on a new
@@ -7829,7 +7824,7 @@ StartupXLOG(void)
78297824

78307825
ereport(LOG,
78317826
(errmsg("redo done at %X/%X system usage: %s",
7832-
LSN_FORMAT_ARGS(ReadRecPtr),
7827+
LSN_FORMAT_ARGS(xlogreader->ReadRecPtr),
78337828
pg_rusage_show(&ru0))));
78347829
xtime = GetLatestXTime();
78357830
if (xtime)
@@ -7904,7 +7899,7 @@ StartupXLOG(void)
79047899
*/
79057900
XLogBeginRead(xlogreader, LastRec);
79067901
record = ReadRecord(xlogreader, PANIC, false, replayTLI);
7907-
EndOfLog = EndRecPtr;
7902+
EndOfLog = xlogreader->EndRecPtr;
79087903

79097904
/*
79107905
* EndOfLogTLI is the TLI in the filename of the XLOG segment containing
@@ -8013,7 +8008,7 @@ StartupXLOG(void)
80138008
* between here and writing the end-of-recovery record.
80148009
*/
80158010
writeTimeLineHistory(newTLI, recoveryTargetTLI,
8016-
EndRecPtr, reason);
8011+
xlogreader->EndRecPtr, reason);
80178012

80188013
/*
80198014
* Since there might be a partial WAL segment named RECOVERYXLOG, get
@@ -9647,7 +9642,7 @@ CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
96479642
* startup process.)
96489643
*/
96499644
static void
9650-
RecoveryRestartPoint(const CheckPoint *checkPoint)
9645+
RecoveryRestartPoint(const CheckPoint *checkPoint, XLogReaderState *record)
96519646
{
96529647
/*
96539648
* Also refrain from creating a restartpoint if we have seen any
@@ -9670,8 +9665,8 @@ RecoveryRestartPoint(const CheckPoint *checkPoint)
96709665
* work out the next time it wants to perform a restartpoint.
96719666
*/
96729667
SpinLockAcquire(&XLogCtl->info_lck);
9673-
XLogCtl->lastCheckPointRecPtr = ReadRecPtr;
9674-
XLogCtl->lastCheckPointEndPtr = EndRecPtr;
9668+
XLogCtl->lastCheckPointRecPtr = record->ReadRecPtr;
9669+
XLogCtl->lastCheckPointEndPtr = record->EndRecPtr;
96759670
XLogCtl->lastCheckPoint = *checkPoint;
96769671
SpinLockRelease(&XLogCtl->info_lck);
96779672
}
@@ -10468,7 +10463,7 @@ xlog_redo(XLogReaderState *record)
1046810463
(errmsg("unexpected timeline ID %u (should be %u) in checkpoint record",
1046910464
checkPoint.ThisTimeLineID, replayTLI)));
1047010465

10471-
RecoveryRestartPoint(&checkPoint);
10466+
RecoveryRestartPoint(&checkPoint, record);
1047210467
}
1047310468
else if (info == XLOG_CHECKPOINT_ONLINE)
1047410469
{
@@ -10524,7 +10519,7 @@ xlog_redo(XLogReaderState *record)
1052410519
(errmsg("unexpected timeline ID %u (should be %u) in checkpoint record",
1052510520
checkPoint.ThisTimeLineID, replayTLI)));
1052610521

10527-
RecoveryRestartPoint(&checkPoint);
10522+
RecoveryRestartPoint(&checkPoint, record);
1052810523
}
1052910524
else if (info == XLOG_OVERWRITE_CONTRECORD)
1053010525
{
@@ -10690,8 +10685,8 @@ xlog_redo(XLogReaderState *record)
1069010685
if (!fpw)
1069110686
{
1069210687
SpinLockAcquire(&XLogCtl->info_lck);
10693-
if (XLogCtl->lastFpwDisableRecPtr < ReadRecPtr)
10694-
XLogCtl->lastFpwDisableRecPtr = ReadRecPtr;
10688+
if (XLogCtl->lastFpwDisableRecPtr < record->ReadRecPtr)
10689+
XLogCtl->lastFpwDisableRecPtr = record->ReadRecPtr;
1069510690
SpinLockRelease(&XLogCtl->info_lck);
1069610691
}
1069710692

@@ -12586,7 +12581,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
1258612581
* errmsg_internal() because the message was already translated.
1258712582
*/
1258812583
if (xlogreader->errormsg_buf[0])
12589-
ereport(emode_for_corrupt_record(emode, EndRecPtr),
12584+
ereport(emode_for_corrupt_record(emode, xlogreader->EndRecPtr),
1259012585
(errmsg_internal("%s", xlogreader->errormsg_buf)));
1259112586

1259212587
/* reset any error XLogReaderValidatePageHeader() might have set */

0 commit comments

Comments
 (0)