Skip to content

Commit 92929a3

Browse files
committed
Fix possible pg_basebackup failure on standby with "include WAL".
If a restartpoint flushed no dirty buffers, it could fail to update the minimum recovery point, leading to a minimum recovery point prior to the starting REDO location. perform_base_backup() would interpret that as meaning that no WAL files at all needed to be included in the backup, failing an internal sanity check. To fix, have restartpoints always update the minimum recovery point to just after the checkpoint record itself, so that the file (or files) containing the checkpoint record will always be included in the backup. Code by Amit Kapila, per a design suggestion by me, with some additional work on the code comment by me. Test case by Michael Paquier. Report by Kyotaro Horiguchi.
1 parent 44c56d3 commit 92929a3

File tree

1 file changed

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

1 file changed

+28
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,14 @@ typedef struct XLogCtlData
471471

472472
/*
473473
* During recovery, we keep a copy of the latest checkpoint record here.
474-
* Used by the background writer when it wants to create a restartpoint.
474+
* lastCheckPointRecPtr points to start of checkpoint record and
475+
* lastCheckPointEndPtr points to end+1 of checkpoint record. Used by the
476+
* background writer when it wants to create a restartpoint.
475477
*
476478
* Protected by info_lck.
477479
*/
478480
XLogRecPtr lastCheckPointRecPtr;
481+
XLogRecPtr lastCheckPointEndPtr;
479482
CheckPoint lastCheckPoint;
480483

481484
/*
@@ -7480,6 +7483,7 @@ RecoveryRestartPoint(const CheckPoint *checkPoint)
74807483
*/
74817484
SpinLockAcquire(&xlogctl->info_lck);
74827485
xlogctl->lastCheckPointRecPtr = ReadRecPtr;
7486+
xlogctl->lastCheckPointEndPtr = EndRecPtr;
74837487
xlogctl->lastCheckPoint = *checkPoint;
74847488
SpinLockRelease(&xlogctl->info_lck);
74857489
}
@@ -7499,6 +7503,7 @@ bool
74997503
CreateRestartPoint(int flags)
75007504
{
75017505
XLogRecPtr lastCheckPointRecPtr;
7506+
XLogRecPtr lastCheckPointEndPtr;
75027507
CheckPoint lastCheckPoint;
75037508
XLogSegNo _logSegNo;
75047509
TimestampTz xtime;
@@ -7515,6 +7520,7 @@ CreateRestartPoint(int flags)
75157520
/* Get a local copy of the last safe checkpoint record. */
75167521
SpinLockAcquire(&xlogctl->info_lck);
75177522
lastCheckPointRecPtr = xlogctl->lastCheckPointRecPtr;
7523+
lastCheckPointEndPtr = xlogctl->lastCheckPointEndPtr;
75187524
lastCheckPoint = xlogctl->lastCheckPoint;
75197525
SpinLockRelease(&xlogctl->info_lck);
75207526

@@ -7615,6 +7621,27 @@ CreateRestartPoint(int flags)
76157621
ControlFile->checkPoint = lastCheckPointRecPtr;
76167622
ControlFile->checkPointCopy = lastCheckPoint;
76177623
ControlFile->time = (pg_time_t) time(NULL);
7624+
7625+
/*
7626+
* Ensure minRecoveryPoint is past the checkpoint record. Normally,
7627+
* this will have happened already while writing out dirty buffers,
7628+
* but not necessarily - e.g. because no buffers were dirtied. We do
7629+
* this because a non-exclusive base backup uses minRecoveryPoint to
7630+
* determine which WAL files must be included in the backup, and the
7631+
* file (or files) containing the checkpoint record must be included,
7632+
* at a minimum. Note that for an ordinary restart of recovery there's
7633+
* no value in having the minimum recovery point any earlier than this
7634+
* anyway, because redo will begin just after the checkpoint record.
7635+
*/
7636+
if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
7637+
{
7638+
ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
7639+
ControlFile->minRecoveryPointTLI = lastCheckPoint.ThisTimeLineID;
7640+
7641+
/* update local copy */
7642+
minRecoveryPoint = ControlFile->minRecoveryPoint;
7643+
minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
7644+
}
76187645
if (flags & CHECKPOINT_IS_SHUTDOWN)
76197646
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
76207647
UpdateControlFile();

0 commit comments

Comments
 (0)