1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-22 02:52:08 +03:00

Fix possible recovery trouble if TRUNCATE overlaps a checkpoint.

If TRUNCATE causes some buffers to be invalidated and thus the
checkpoint does not flush them, TRUNCATE must also ensure that the
corresponding files are truncated on disk. Otherwise, a replay
from the checkpoint might find that the buffers exist but have
the wrong contents, which may cause replay to fail.

Report by Teja Mupparti. Patch by Kyotaro Horiguchi, per a design
suggestion from Heikki Linnakangas, with some changes to the
comments by me. Review of this and a prior patch that approached
the issue differently by Heikki Linnakangas, Andres Freund, Álvaro
Herrera, Masahiko Sawada, and Tom Lane.

Discussion: http://postgr.es/m/BYAPR06MB6373BF50B469CA393C614257ABF00@BYAPR06MB6373.namprd06.prod.outlook.com
This commit is contained in:
Robert Haas
2022-03-24 14:49:08 -04:00
parent 2121d58091
commit 118f1a332b
11 changed files with 117 additions and 29 deletions

View File

@ -3069,8 +3069,8 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
* crash/basebackup, even though the state of the data directory would * crash/basebackup, even though the state of the data directory would
* require it. * require it.
*/ */
Assert(!MyPgXact->delayChkpt); Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) == 0);
MyPgXact->delayChkpt = true; MyPgXact->delayChkpt |= DELAY_CHKPT_START;
/* WAL log truncation */ /* WAL log truncation */
WriteMTruncateXlogRec(newOldestMultiDB, WriteMTruncateXlogRec(newOldestMultiDB,
@ -3096,7 +3096,7 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
/* Then offsets */ /* Then offsets */
PerformOffsetsTruncation(oldestMulti, newOldestMulti); PerformOffsetsTruncation(oldestMulti, newOldestMulti);
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt &= ~DELAY_CHKPT_START;
END_CRIT_SECTION(); END_CRIT_SECTION();
LWLockRelease(MultiXactTruncationLock); LWLockRelease(MultiXactTruncationLock);

View File

@ -476,7 +476,7 @@ MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid, const char *gid,
} }
pgxact->xid = xid; pgxact->xid = xid;
pgxact->xmin = InvalidTransactionId; pgxact->xmin = InvalidTransactionId;
pgxact->delayChkpt = false; pgxact->delayChkpt = 0;
pgxact->vacuumFlags = 0; pgxact->vacuumFlags = 0;
proc->pid = 0; proc->pid = 0;
proc->databaseId = databaseid; proc->databaseId = databaseid;
@ -1175,7 +1175,8 @@ EndPrepare(GlobalTransaction gxact)
START_CRIT_SECTION(); START_CRIT_SECTION();
MyPgXact->delayChkpt = true; Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) == 0);
MyPgXact->delayChkpt |= DELAY_CHKPT_START;
XLogBeginInsert(); XLogBeginInsert();
for (record = records.head; record != NULL; record = record->next) for (record = records.head; record != NULL; record = record->next)
@ -1218,7 +1219,7 @@ EndPrepare(GlobalTransaction gxact)
* checkpoint starting after this will certainly see the gxact as a * checkpoint starting after this will certainly see the gxact as a
* candidate for fsyncing. * candidate for fsyncing.
*/ */
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt &= ~DELAY_CHKPT_START;
/* /*
* Remember that we have this GlobalTransaction entry locked for us. If * Remember that we have this GlobalTransaction entry locked for us. If
@ -2352,7 +2353,8 @@ RecordTransactionCommitPrepared(TransactionId xid,
START_CRIT_SECTION(); START_CRIT_SECTION();
/* See notes in RecordTransactionCommit */ /* See notes in RecordTransactionCommit */
MyPgXact->delayChkpt = true; Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) == 0);
MyPgXact->delayChkpt |= DELAY_CHKPT_START;
/* /*
* Emit the XLOG commit record. Note that we mark 2PC commits as * Emit the XLOG commit record. Note that we mark 2PC commits as
@ -2400,7 +2402,7 @@ RecordTransactionCommitPrepared(TransactionId xid,
TransactionIdCommitTree(xid, nchildren, children); TransactionIdCommitTree(xid, nchildren, children);
/* Checkpoint can proceed now */ /* Checkpoint can proceed now */
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt &= ~DELAY_CHKPT_START;
END_CRIT_SECTION(); END_CRIT_SECTION();

View File

@ -1239,8 +1239,9 @@ RecordTransactionCommit(void)
* This makes checkpoint's determination of which xacts are delayChkpt * This makes checkpoint's determination of which xacts are delayChkpt
* a bit fuzzy, but it doesn't matter. * a bit fuzzy, but it doesn't matter.
*/ */
Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) == 0);
START_CRIT_SECTION(); START_CRIT_SECTION();
MyPgXact->delayChkpt = true; MyPgXact->delayChkpt |= DELAY_CHKPT_START;
SetCurrentTransactionStopTimestamp(); SetCurrentTransactionStopTimestamp();
@ -1341,7 +1342,7 @@ RecordTransactionCommit(void)
*/ */
if (markXidCommitted) if (markXidCommitted)
{ {
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt &= ~DELAY_CHKPT_START;
END_CRIT_SECTION(); END_CRIT_SECTION();
} }

View File

@ -9064,18 +9064,30 @@ CreateCheckPoint(int flags)
* and we will correctly flush the update below. So we cannot miss any * and we will correctly flush the update below. So we cannot miss any
* xacts we need to wait for. * xacts we need to wait for.
*/ */
vxids = GetVirtualXIDsDelayingChkpt(&nvxids); vxids = GetVirtualXIDsDelayingChkpt(&nvxids, DELAY_CHKPT_START);
if (nvxids > 0) if (nvxids > 0)
{ {
do do
{ {
pg_usleep(10000L); /* wait for 10 msec */ pg_usleep(10000L); /* wait for 10 msec */
} while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids)); } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
DELAY_CHKPT_START));
} }
pfree(vxids); pfree(vxids);
CheckPointGuts(checkPoint.redo, flags); CheckPointGuts(checkPoint.redo, flags);
vxids = GetVirtualXIDsDelayingChkpt(&nvxids, DELAY_CHKPT_COMPLETE);
if (nvxids > 0)
{
do
{
pg_usleep(10000L); /* wait for 10 msec */
} while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
DELAY_CHKPT_COMPLETE));
}
pfree(vxids);
/* /*
* Take a snapshot of running transactions and write this to WAL. This * Take a snapshot of running transactions and write this to WAL. This
* allows us to reconstruct the state of running transactions during * allows us to reconstruct the state of running transactions during

View File

@ -899,7 +899,7 @@ XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
/* /*
* Ensure no checkpoint can change our view of RedoRecPtr. * Ensure no checkpoint can change our view of RedoRecPtr.
*/ */
Assert(MyPgXact->delayChkpt); Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) != 0);
/* /*
* Update RedoRecPtr so that we can make the right decision * Update RedoRecPtr so that we can make the right decision

View File

@ -27,6 +27,7 @@
#include "catalog/storage.h" #include "catalog/storage.h"
#include "catalog/storage_xlog.h" #include "catalog/storage_xlog.h"
#include "storage/freespace.h" #include "storage/freespace.h"
#include "storage/proc.h"
#include "storage/smgr.h" #include "storage/smgr.h"
#include "utils/memutils.h" #include "utils/memutils.h"
#include "utils/rel.h" #include "utils/rel.h"
@ -248,6 +249,22 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
if (vm) if (vm)
visibilitymap_truncate(rel, nblocks); visibilitymap_truncate(rel, nblocks);
/*
* Make sure that a concurrent checkpoint can't complete while truncation
* is in progress.
*
* The truncation operation might drop buffers that the checkpoint
* otherwise would have flushed. If it does, then it's essential that
* the files actually get truncated on disk before the checkpoint record
* is written. Otherwise, if reply begins from that checkpoint, the
* to-be-truncated blocks might still exist on disk but have older
* contents than expected, which can cause replay to fail. It's OK for
* the blocks to not exist on disk at all, but not for them to have the
* wrong contents.
*/
Assert((MyPgXact->delayChkpt & DELAY_CHKPT_COMPLETE) == 0);
MyPgXact->delayChkpt |= DELAY_CHKPT_COMPLETE;
/* /*
* We WAL-log the truncation before actually truncating, which means * We WAL-log the truncation before actually truncating, which means
* trouble if the truncation fails. If we then crash, the WAL replay * trouble if the truncation fails. If we then crash, the WAL replay
@ -286,8 +303,15 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
XLogFlush(lsn); XLogFlush(lsn);
} }
/* Do the real work */ /*
* This will first remove any buffers from the buffer pool that should no
* longer exist after truncation is complete, and then truncate the
* corresponding files on disk.
*/
smgrtruncate(rel->rd_smgr, MAIN_FORKNUM, nblocks); smgrtruncate(rel->rd_smgr, MAIN_FORKNUM, nblocks);
/* We've done all the critical work, so checkpoints are OK now. */
MyPgXact->delayChkpt &= ~DELAY_CHKPT_COMPLETE;
} }
/* /*

View File

@ -3471,7 +3471,9 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
* essential that CreateCheckpoint waits for virtual transactions * essential that CreateCheckpoint waits for virtual transactions
* rather than full transactionids. * rather than full transactionids.
*/ */
MyPgXact->delayChkpt = delayChkpt = true; Assert((MyPgXact->delayChkpt & DELAY_CHKPT_START) == 0);
MyPgXact->delayChkpt |= DELAY_CHKPT_START;
delayChkpt = true;
lsn = XLogSaveBufferForHint(buffer, buffer_std); lsn = XLogSaveBufferForHint(buffer, buffer_std);
} }
@ -3504,7 +3506,7 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
UnlockBufHdr(bufHdr, buf_state); UnlockBufHdr(bufHdr, buf_state);
if (delayChkpt) if (delayChkpt)
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt &= ~DELAY_CHKPT_START;
if (dirtied) if (dirtied)
{ {

View File

@ -433,7 +433,10 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
pgxact->xmin = InvalidTransactionId; pgxact->xmin = InvalidTransactionId;
/* must be cleared with xid/xmin: */ /* must be cleared with xid/xmin: */
pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK; pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
pgxact->delayChkpt = false; /* be sure this is cleared in abort */
/* be sure this is cleared in abort */
pgxact->delayChkpt = 0;
proc->recoveryConflictPending = false; proc->recoveryConflictPending = false;
Assert(pgxact->nxids == 0); Assert(pgxact->nxids == 0);
@ -455,7 +458,10 @@ ProcArrayEndTransactionInternal(PGPROC *proc, PGXACT *pgxact,
pgxact->xmin = InvalidTransactionId; pgxact->xmin = InvalidTransactionId;
/* must be cleared with xid/xmin: */ /* must be cleared with xid/xmin: */
pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK; pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
pgxact->delayChkpt = false; /* be sure this is cleared in abort */
/* be sure this is cleared in abort */
pgxact->delayChkpt = 0;
proc->recoveryConflictPending = false; proc->recoveryConflictPending = false;
/* Clear the subtransaction-XID cache too while holding the lock */ /* Clear the subtransaction-XID cache too while holding the lock */
@ -2267,7 +2273,8 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
* delaying checkpoint because they have critical actions in progress. * delaying checkpoint because they have critical actions in progress.
* *
* Constructs an array of VXIDs of transactions that are currently in commit * Constructs an array of VXIDs of transactions that are currently in commit
* critical sections, as shown by having delayChkpt set in their PGXACT. * critical sections, as shown by having specified delayChkpt bits set in their
* PGXACT.
* *
* Returns a palloc'd array that should be freed by the caller. * Returns a palloc'd array that should be freed by the caller.
* *nvxids is the number of valid entries. * *nvxids is the number of valid entries.
@ -2281,13 +2288,15 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
* for clearing of delayChkpt to propagate is unimportant for correctness. * for clearing of delayChkpt to propagate is unimportant for correctness.
*/ */
VirtualTransactionId * VirtualTransactionId *
GetVirtualXIDsDelayingChkpt(int *nvxids) GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
{ {
VirtualTransactionId *vxids; VirtualTransactionId *vxids;
ProcArrayStruct *arrayP = procArray; ProcArrayStruct *arrayP = procArray;
int count = 0; int count = 0;
int index; int index;
Assert(type != 0);
/* allocate what's certainly enough result space */ /* allocate what's certainly enough result space */
vxids = (VirtualTransactionId *) vxids = (VirtualTransactionId *)
palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs); palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
@ -2300,7 +2309,7 @@ GetVirtualXIDsDelayingChkpt(int *nvxids)
volatile PGPROC *proc = &allProcs[pgprocno]; volatile PGPROC *proc = &allProcs[pgprocno];
volatile PGXACT *pgxact = &allPgXact[pgprocno]; volatile PGXACT *pgxact = &allPgXact[pgprocno];
if (pgxact->delayChkpt) if ((pgxact->delayChkpt & type) != 0)
{ {
VirtualTransactionId vxid; VirtualTransactionId vxid;
@ -2326,12 +2335,14 @@ GetVirtualXIDsDelayingChkpt(int *nvxids)
* those numbers should be small enough for it not to be a problem. * those numbers should be small enough for it not to be a problem.
*/ */
bool bool
HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids) HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
{ {
bool result = false; bool result = false;
ProcArrayStruct *arrayP = procArray; ProcArrayStruct *arrayP = procArray;
int index; int index;
Assert(type != 0);
LWLockAcquire(ProcArrayLock, LW_SHARED); LWLockAcquire(ProcArrayLock, LW_SHARED);
for (index = 0; index < arrayP->numProcs; index++) for (index = 0; index < arrayP->numProcs; index++)
@ -2343,7 +2354,8 @@ HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
GET_VXID_FROM_PGPROC(vxid, *proc); GET_VXID_FROM_PGPROC(vxid, *proc);
if (pgxact->delayChkpt && VirtualTransactionIdIsValid(vxid)) if ((pgxact->delayChkpt & type) != 0 &&
VirtualTransactionIdIsValid(vxid))
{ {
int i; int i;

View File

@ -380,7 +380,7 @@ InitProcess(void)
MyProc->roleId = InvalidOid; MyProc->roleId = InvalidOid;
MyProc->tempNamespaceId = InvalidOid; MyProc->tempNamespaceId = InvalidOid;
MyProc->isBackgroundWorker = IsBackgroundWorker; MyProc->isBackgroundWorker = IsBackgroundWorker;
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt = 0;
MyPgXact->vacuumFlags = 0; MyPgXact->vacuumFlags = 0;
/* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */ /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
if (IsAutoVacuumWorkerProcess()) if (IsAutoVacuumWorkerProcess())
@ -562,7 +562,7 @@ InitAuxiliaryProcess(void)
MyProc->roleId = InvalidOid; MyProc->roleId = InvalidOid;
MyProc->tempNamespaceId = InvalidOid; MyProc->tempNamespaceId = InvalidOid;
MyProc->isBackgroundWorker = IsBackgroundWorker; MyProc->isBackgroundWorker = IsBackgroundWorker;
MyPgXact->delayChkpt = false; MyPgXact->delayChkpt = 0;
MyPgXact->vacuumFlags = 0; MyPgXact->vacuumFlags = 0;
MyProc->lwWaiting = false; MyProc->lwWaiting = false;
MyProc->lwWaitMode = 0; MyProc->lwWaitMode = 0;

View File

@ -76,6 +76,41 @@ struct XidCache
*/ */
#define INVALID_PGPROCNO PG_INT32_MAX #define INVALID_PGPROCNO PG_INT32_MAX
/*
* Flags for PGPROC.delayChkpt
*
* These flags can be used to delay the start or completion of a checkpoint
* for short periods. A flag is in effect if the corresponding bit is set in
* the PGPROC of any backend.
*
* For our purposes here, a checkpoint has three phases: (1) determine the
* location to which the redo pointer will be moved, (2) write all the
* data durably to disk, and (3) WAL-log the checkpoint.
*
* Setting DELAY_CHKPT_START prevents the system from moving from phase 1
* to phase 2. This is useful when we are performing a WAL-logged modification
* of data that will be flushed to disk in phase 2. By setting this flag
* before writing WAL and clearing it after we've both written WAL and
* performed the corresponding modification, we ensure that if the WAL record
* is inserted prior to the new redo point, the corresponding data changes will
* also be flushed to disk before the checkpoint can complete. (In the
* extremely common case where the data being modified is in shared buffers
* and we acquire an exclusive content lock on the relevant buffers before
* writing WAL, this mechanism is not needed, because phase 2 will block
* until we release the content lock and then flush the modified data to
* disk.)
*
* Setting DELAY_CHKPT_COMPLETE prevents the system from moving from phase 2
* to phase 3. This is useful if we are performing a WAL-logged operation that
* might invalidate buffers, such as relation truncation. In this case, we need
* to ensure that any buffers which were invalidated and thus not flushed by
* the checkpoint are actaully destroyed on disk. Replay can cope with a file
* or block that doesn't exist, but not with a block that has the wrong
* contents.
*/
#define DELAY_CHKPT_START (1<<0)
#define DELAY_CHKPT_COMPLETE (1<<1)
/* /*
* Each backend has a PGPROC struct in shared memory. There is also a list of * Each backend has a PGPROC struct in shared memory. There is also a list of
* currently-unused PGPROC structs that will be reallocated to new backends. * currently-unused PGPROC structs that will be reallocated to new backends.
@ -232,8 +267,7 @@ typedef struct PGXACT
uint8 vacuumFlags; /* vacuum-related flags, see above */ uint8 vacuumFlags; /* vacuum-related flags, see above */
bool overflowed; bool overflowed;
bool delayChkpt; /* true if this proc delays checkpoint start; int delayChkpt; /* for DELAY_CHKPT_* flags */
* previously called InCommit */
uint8 nxids; uint8 nxids;
} PGXACT; } PGXACT;

View File

@ -92,8 +92,9 @@ extern TransactionId GetOldestXmin(Relation rel, int flags);
extern TransactionId GetOldestActiveTransactionId(void); extern TransactionId GetOldestActiveTransactionId(void);
extern TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly); extern TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly);
extern VirtualTransactionId *GetVirtualXIDsDelayingChkpt(int *nvxids); extern VirtualTransactionId *GetVirtualXIDsDelayingChkpt(int *nvxids, int type);
extern bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids); extern bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids,
int nvxids, int type);
extern PGPROC *BackendPidGetProc(int pid); extern PGPROC *BackendPidGetProc(int pid);
extern PGPROC *BackendPidGetProcWithLock(int pid); extern PGPROC *BackendPidGetProcWithLock(int pid);