Skip to content

Commit 7e59b12

Browse files
committed
Fix control file update done in restartpoints still running after promotion
If a cluster is promoted (aka the control file shows a state different than DB_IN_ARCHIVE_RECOVERY) while CreateRestartPoint() is still processing, this function could miss an update of the control file for "checkPoint" and "checkPointCopy" but still do the recycling and/or removal of the past WAL segments, assuming that the to-be-updated LSN values should be used as reference points for the cleanup. This causes a follow-up restart attempting crash recovery to fail with a PANIC on a missing checkpoint record if the end-of-recovery checkpoint triggered by the promotion did not complete while the cluster abruptly stopped or crashed before the completion of this checkpoint. The PANIC would be caused by the redo LSN referred in the control file as located in a segment already gone, recycled by the previous restartpoint with "checkPoint" out-of-sync in the control file. This commit fixes the update of the control file during restartpoints so as "checkPoint" and "checkPointCopy" are updated even if the cluster has been promoted while a restartpoint is running, to be on par with the set of WAL segments actually recycled in the end of CreateRestartPoint(). 7863ee4 has fixed this problem already on master, but the release timing of the latest point versions did not let me enough time to study and fix that on all the stable branches. Reported-by: Fujii Masao, Rui Zhao Author: Kyotaro Horiguchi Reviewed-by: Nathan Bossart, Michael Paquier Discussion: https://postgr.es/m/20220316.102444.2193181487576617583.horikyota.ntt@gmail.com Backpatch-through: 10
1 parent 301b91c commit 7e59b12

File tree

1 file changed

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

1 file changed

+29
-15
lines changed

src/backend/access/transam/xlog.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9370,21 +9370,26 @@ CreateRestartPoint(int flags)
93709370
PriorRedoPtr = ControlFile->checkPointCopy.redo;
93719371

93729372
/*
9373-
* Update pg_control, using current time. Check that it still shows
9374-
* IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
9375-
* this is a quick hack to make sure nothing really bad happens if somehow
9376-
* we get here after the end-of-recovery checkpoint.
9373+
* Update pg_control, using current time. Check that it still shows an
9374+
* older checkpoint, else do nothing; this is a quick hack to make sure
9375+
* nothing really bad happens if somehow we get here after the
9376+
* end-of-recovery checkpoint.
93779377
*/
93789378
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9379-
if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY &&
9380-
ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
9379+
if (ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
93819380
{
9381+
/*
9382+
* Update the checkpoint information. We do this even if the cluster
9383+
* does not show DB_IN_ARCHIVE_RECOVERY to match with the set of WAL
9384+
* segments recycled below.
9385+
*/
93829386
ControlFile->checkPoint = lastCheckPointRecPtr;
93839387
ControlFile->checkPointCopy = lastCheckPoint;
93849388
ControlFile->time = (pg_time_t) time(NULL);
93859389

93869390
/*
9387-
* Ensure minRecoveryPoint is past the checkpoint record. Normally,
9391+
* Ensure minRecoveryPoint is past the checkpoint record and update it
9392+
* if the control file still shows DB_IN_ARCHIVE_RECOVERY. Normally,
93889393
* this will have happened already while writing out dirty buffers,
93899394
* but not necessarily - e.g. because no buffers were dirtied. We do
93909395
* this because a non-exclusive base backup uses minRecoveryPoint to
@@ -9393,18 +9398,27 @@ CreateRestartPoint(int flags)
93939398
* at a minimum. Note that for an ordinary restart of recovery there's
93949399
* no value in having the minimum recovery point any earlier than this
93959400
* anyway, because redo will begin just after the checkpoint record.
9401+
* this because a non-exclusive base backup uses minRecoveryPoint to
9402+
* determine which WAL files must be included in the backup, and the
9403+
* file (or files) containing the checkpoint record must be included,
9404+
* at a minimum. Note that for an ordinary restart of recovery there's
9405+
* no value in having the minimum recovery point any earlier than this
9406+
* anyway, because redo will begin just after the checkpoint record.
93969407
*/
9397-
if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
9408+
if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY)
93989409
{
9399-
ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
9400-
ControlFile->minRecoveryPointTLI = lastCheckPoint.ThisTimeLineID;
9410+
if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
9411+
{
9412+
ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
9413+
ControlFile->minRecoveryPointTLI = lastCheckPoint.ThisTimeLineID;
94019414

9402-
/* update local copy */
9403-
minRecoveryPoint = ControlFile->minRecoveryPoint;
9404-
minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
9415+
/* update local copy */
9416+
minRecoveryPoint = ControlFile->minRecoveryPoint;
9417+
minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
9418+
}
9419+
if (flags & CHECKPOINT_IS_SHUTDOWN)
9420+
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
94059421
}
9406-
if (flags & CHECKPOINT_IS_SHUTDOWN)
9407-
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
94089422
UpdateControlFile();
94099423
}
94109424
LWLockRelease(ControlFileLock);

0 commit comments

Comments
 (0)