Rethink the delay-checkpoint-end mechanism in the back-branches.
authorRobert Haas
Thu, 14 Apr 2022 15:10:07 +0000 (11:10 -0400)
committerRobert Haas
Thu, 14 Apr 2022 15:10:07 +0000 (11:10 -0400)
The back-patch of commit bbace5697df12398e87ffd9879171c39d27f5b33 had
the unfortunate effect of changing the layout of PGPROC in the
back-branches, which could break extensions. This happened because it
changed the delayChkpt from type bool to type int. So, change it back,
and add a new bool delayChkptEnd field instead. The new field should
fall within what used to be padding space within the struct, and so
hopefully won't cause any extensions to break.

Per report from Markus Wanner and discussion with Tom Lane and others.

Patch originally by me, somewhat revised by Markus Wanner per a
suggestion from Michael Paquier. A very similar patch was developed
by Kyotaro Horiguchi, but I failed to see the email in which that was
posted before writing one of my own.

Discussion: http://postgr.es/m/CA+Tgmoao-kUD9c5nG5sub3F7tbo39+cdr8jKaOVEs_1aBWcJ3Q@mail.gmail.com
Discussion: http://postgr.es/m/20220406.164521.17171257901083417[email protected]

src/backend/access/transam/multixact.c
src/backend/access/transam/twophase.c
src/backend/access/transam/xact.c
src/backend/access/transam/xlog.c
src/backend/access/transam/xloginsert.c
src/backend/catalog/storage.c
src/backend/storage/buffer/bufmgr.c
src/backend/storage/ipc/procarray.c
src/include/storage/proc.h
src/include/storage/procarray.h

index 50d8bab9e213e24a203a683b98c22a7e6515361a..b643564f16a4ef220004e9855f27c06531456c57 100644 (file)
@@ -3075,8 +3075,8 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
     * crash/basebackup, even though the state of the data directory would
     * require it.
     */
-   Assert((MyProc->delayChkpt & DELAY_CHKPT_START) == 0);
-   MyProc->delayChkpt |= DELAY_CHKPT_START;
+   Assert(!MyProc->delayChkpt);
+   MyProc->delayChkpt = true;
 
    /* WAL log truncation */
    WriteMTruncateXlogRec(newOldestMultiDB,
@@ -3102,7 +3102,7 @@ TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
    /* Then offsets */
    PerformOffsetsTruncation(oldestMulti, newOldestMulti);
 
-   MyProc->delayChkpt &= ~DELAY_CHKPT_START;
+   MyProc->delayChkpt = false;
 
    END_CRIT_SECTION();
    LWLockRelease(MultiXactTruncationLock);
index dea3f485f7ac76501e1be4253fae90bf8696e576..b0aaad65957ed4036e0a1425755c1e846d3ee286 100644 (file)
@@ -474,8 +474,9 @@ MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid, const char *gid,
    }
    proc->xid = xid;
    Assert(proc->xmin == InvalidTransactionId);
-   proc->delayChkpt = 0;
+   proc->delayChkpt = false;
    proc->statusFlags = 0;
+   proc->delayChkptEnd = false;
    proc->pid = 0;
    proc->databaseId = databaseid;
    proc->roleId = owner;
@@ -1165,8 +1166,8 @@ EndPrepare(GlobalTransaction gxact)
 
    START_CRIT_SECTION();
 
-   Assert((MyProc->delayChkpt & DELAY_CHKPT_START) == 0);
-   MyProc->delayChkpt |= DELAY_CHKPT_START;
+   Assert(!MyProc->delayChkpt);
+   MyProc->delayChkpt = true;
 
    XLogBeginInsert();
    for (record = records.head; record != NULL; record = record->next)
@@ -1209,7 +1210,7 @@ EndPrepare(GlobalTransaction gxact)
     * checkpoint starting after this will certainly see the gxact as a
     * candidate for fsyncing.
     */
-   MyProc->delayChkpt &= ~DELAY_CHKPT_START;
+   MyProc->delayChkpt = false;
 
    /*
     * Remember that we have this GlobalTransaction entry locked for us.  If
@@ -2276,8 +2277,8 @@ RecordTransactionCommitPrepared(TransactionId xid,
    START_CRIT_SECTION();
 
    /* See notes in RecordTransactionCommit */
-   Assert((MyProc->delayChkpt & DELAY_CHKPT_START) == 0);
-   MyProc->delayChkpt |= DELAY_CHKPT_START;
+   Assert(!MyProc->delayChkpt);
+   MyProc->delayChkpt = true;
 
    /*
     * Emit the XLOG commit record. Note that we mark 2PC commits as
@@ -2325,7 +2326,7 @@ RecordTransactionCommitPrepared(TransactionId xid,
    TransactionIdCommitTree(xid, nchildren, children);
 
    /* Checkpoint can proceed now */
-   MyProc->delayChkpt &= ~DELAY_CHKPT_START;
+   MyProc->delayChkpt = false;
 
    END_CRIT_SECTION();
 
index c5e7261921dfb248c3bb64707b368beaa07bd95c..e8523693c0aac19ca4b5ece59111f7d6beb65e88 100644 (file)
@@ -1335,9 +1335,9 @@ RecordTransactionCommit(void)
         * This makes checkpoint's determination of which xacts are delayChkpt
         * a bit fuzzy, but it doesn't matter.
         */
-       Assert((MyProc->delayChkpt & DELAY_CHKPT_START) == 0);
+       Assert(!MyProc->delayChkpt);
        START_CRIT_SECTION();
-       MyProc->delayChkpt |= DELAY_CHKPT_START;
+       MyProc->delayChkpt = true;
 
        SetCurrentTransactionStopTimestamp();
 
@@ -1438,7 +1438,7 @@ RecordTransactionCommit(void)
     */
    if (markXidCommitted)
    {
-       MyProc->delayChkpt &= ~DELAY_CHKPT_START;
+       MyProc->delayChkpt = false;
        END_CRIT_SECTION();
    }
 
index d889d69387ef9e8ee77e2afde17c9eb51f2961ac..8edf0fa6461a8cd90264164e9ea5f57ffb09619f 100644 (file)
@@ -9228,27 +9228,25 @@ CreateCheckPoint(int flags)
     * and we will correctly flush the update below.  So we cannot miss any
     * xacts we need to wait for.
     */
-   vxids = GetVirtualXIDsDelayingChkpt(&nvxids, DELAY_CHKPT_START);
+   vxids = GetVirtualXIDsDelayingChkpt(&nvxids);
    if (nvxids > 0)
    {
        do
        {
            pg_usleep(10000L);  /* wait for 10 msec */
-       } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
-                                             DELAY_CHKPT_START));
+       } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids));
    }
    pfree(vxids);
 
    CheckPointGuts(checkPoint.redo, flags);
 
-   vxids = GetVirtualXIDsDelayingChkpt(&nvxids, DELAY_CHKPT_COMPLETE);
+   vxids = GetVirtualXIDsDelayingChkptEnd(&nvxids);
    if (nvxids > 0)
    {
        do
        {
            pg_usleep(10000L);  /* wait for 10 msec */
-       } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids,
-                                             DELAY_CHKPT_COMPLETE));
+       } while (HaveVirtualXIDsDelayingChkptEnd(vxids, nvxids));
    }
    pfree(vxids);
 
index 1af4a90c41990f1657b4fe780f8b1daa1e923b2f..b153fad594d896b3ad01a36c65b188e6cb1e42cf 100644 (file)
@@ -925,7 +925,7 @@ XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
    /*
     * Ensure no checkpoint can change our view of RedoRecPtr.
     */
-   Assert((MyProc->delayChkpt & DELAY_CHKPT_START) != 0);
+   Assert(MyProc->delayChkpt);
 
    /*
     * Update RedoRecPtr so that we can make the right decision
index fa5682dce804540fb06d291de26d1a4d9406d36e..b7fc491a6841b02df1bac74d787c7633490defe0 100644 (file)
@@ -338,8 +338,8 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
     * the blocks to not exist on disk at all, but not for them to have the
     * wrong contents.
     */
-   Assert((MyProc->delayChkpt & DELAY_CHKPT_COMPLETE) == 0);
-   MyProc->delayChkpt |= DELAY_CHKPT_COMPLETE;
+   Assert(!MyProc->delayChkptEnd);
+   MyProc->delayChkptEnd = true;
 
    /*
     * We WAL-log the truncation before actually truncating, which means
@@ -387,7 +387,7 @@ RelationTruncate(Relation rel, BlockNumber nblocks)
    smgrtruncate(rel->rd_smgr, forks, nforks, blocks);
 
    /* We've done all the critical work, so checkpoints are OK now. */
-   MyProc->delayChkpt &= ~DELAY_CHKPT_COMPLETE;
+   MyProc->delayChkptEnd = false;
 
    /*
     * Update upper-level FSM pages to account for the truncation. This is
index a55545a1875eb646add72e0f5b049edccc694271..49561ae794d98926a2d0ad4f9a10cc5c310311a1 100644 (file)
@@ -3946,8 +3946,8 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
             * essential that CreateCheckpoint waits for virtual transactions
             * rather than full transactionids.
             */
-           Assert((MyProc->delayChkpt & DELAY_CHKPT_START) == 0);
-           MyProc->delayChkpt |= DELAY_CHKPT_START;
+           Assert(!MyProc->delayChkpt);
+           MyProc->delayChkpt = true;
            delayChkpt = true;
            lsn = XLogSaveBufferForHint(buffer, buffer_std);
        }
@@ -3981,7 +3981,7 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
        UnlockBufHdr(bufHdr, buf_state);
 
        if (delayChkpt)
-           MyProc->delayChkpt &= ~DELAY_CHKPT_START;
+           MyProc->delayChkpt = false;
 
        if (dirtied)
        {
index ae71d7538be7f72604558d8e76396343a7eb6a8c..127be9c017ad89495db298a2427f8471d6a8bc2c 100644 (file)
@@ -330,6 +330,11 @@ static void DisplayXidCache(void);
 #define xc_slow_answer_inc()       ((void) 0)
 #endif                         /* XIDCACHE_DEBUG */
 
+static VirtualTransactionId *GetVirtualXIDsDelayingChkptGuts(int *nvxids,
+                                                            int type);
+static bool HaveVirtualXIDsDelayingChkptGuts(VirtualTransactionId *vxids,
+                                            int nvxids, int type);
+
 /* Primitives for KnownAssignedXids array handling for standby */
 static void KnownAssignedXidsCompress(bool force);
 static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
@@ -690,8 +695,9 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
        proc->lxid = InvalidLocalTransactionId;
        proc->xmin = InvalidTransactionId;
 
-       /* be sure this is cleared in abort */
-       proc->delayChkpt = 0;
+       /* be sure these are cleared in abort */
+       proc->delayChkpt = false;
+       proc->delayChkptEnd = false;
 
        proc->recoveryConflictPending = false;
 
@@ -732,8 +738,9 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
    proc->lxid = InvalidLocalTransactionId;
    proc->xmin = InvalidTransactionId;
 
-   /* be sure this is cleared in abort */
-   proc->delayChkpt = 0;
+   /* be sure these are cleared in abort */
+   proc->delayChkpt = false;
+   proc->delayChkptEnd = false;
 
    proc->recoveryConflictPending = false;
 
@@ -3045,26 +3052,28 @@ GetOldestSafeDecodingTransactionId(bool catalogOnly)
 }
 
 /*
- * GetVirtualXIDsDelayingChkpt -- Get the VXIDs of transactions that are
- * delaying checkpoint because they have critical actions in progress.
+ * GetVirtualXIDsDelayingChkptGuts -- Get the VXIDs of transactions that are
+ * delaying the start or end of a checkpoint because they have critical
+ * actions in progress.
  *
  * Constructs an array of VXIDs of transactions that are currently in commit
- * critical sections, as shown by having specified delayChkpt bits set in their
- * PGPROC.
+ * critical sections, as shown by having delayChkpt or delayChkptEnd set in
+ * their PGPROC.
  *
  * Returns a palloc'd array that should be freed by the caller.
  * *nvxids is the number of valid entries.
  *
- * Note that because backends set or clear delayChkpt without holding any lock,
- * the result is somewhat indeterminate, but we don't really care.  Even in
- * a multiprocessor with delayed writes to shared memory, it should be certain
- * that setting of delayChkpt will propagate to shared memory when the backend
- * takes a lock, so we cannot fail to see a virtual xact as delayChkpt if
- * it's already inserted its commit record.  Whether it takes a little while
- * for clearing of delayChkpt to propagate is unimportant for correctness.
+ * Note that because backends set or clear delayChkpt and delayChkptEnd
+ * without holding any lock, the result is somewhat indeterminate, but we
+ * don't really care.  Even in a multiprocessor with delayed writes to
+ * shared memory, it should be certain that setting of delayChkpt will
+ * propagate to shared memory when the backend takes a lock, so we cannot
+ * fail to see a virtual xact as delayChkpt if it's already inserted its
+ * commit record.  Whether it takes a little while for clearing of
+ * delayChkpt to propagate is unimportant for correctness.
  */
-VirtualTransactionId *
-GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
+static VirtualTransactionId *
+GetVirtualXIDsDelayingChkptGuts(int *nvxids, int type)
 {
    VirtualTransactionId *vxids;
    ProcArrayStruct *arrayP = procArray;
@@ -3084,7 +3093,8 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
        int         pgprocno = arrayP->pgprocnos[index];
        PGPROC     *proc = &allProcs[pgprocno];
 
-       if ((proc->delayChkpt & type) != 0)
+       if (((type & DELAY_CHKPT_START) && proc->delayChkpt) ||
+           ((type & DELAY_CHKPT_COMPLETE) && proc->delayChkptEnd))
        {
            VirtualTransactionId vxid;
 
@@ -3100,6 +3110,26 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
    return vxids;
 }
 
+/*
+ * GetVirtualXIDsDelayingChkpt - Get the VXIDs of transactions that are
+ * delaying the start of a checkpoint.
+ */
+VirtualTransactionId *
+GetVirtualXIDsDelayingChkpt(int *nvxids)
+{
+   return GetVirtualXIDsDelayingChkptGuts(nvxids, DELAY_CHKPT_START);
+}
+
+/*
+ * GetVirtualXIDsDelayingChkptEnd - Get the VXIDs of transactions that are
+ * delaying the end of a checkpoint.
+ */
+VirtualTransactionId *
+GetVirtualXIDsDelayingChkptEnd(int *nvxids)
+{
+   return GetVirtualXIDsDelayingChkptGuts(nvxids, DELAY_CHKPT_COMPLETE);
+}
+
 /*
  * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying?
  *
@@ -3109,8 +3139,9 @@ GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
  * Note: this is O(N^2) in the number of vxacts that are/were delaying, but
  * those numbers should be small enough for it not to be a problem.
  */
-bool
-HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
+static bool
+HaveVirtualXIDsDelayingChkptGuts(VirtualTransactionId *vxids, int nvxids,
+                                int type)
 {
    bool        result = false;
    ProcArrayStruct *arrayP = procArray;
@@ -3128,7 +3159,8 @@ HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
 
        GET_VXID_FROM_PGPROC(vxid, *proc);
 
-       if ((proc->delayChkpt & type) != 0 &&
+       if ((((type & DELAY_CHKPT_START) && proc->delayChkpt) ||
+            ((type & DELAY_CHKPT_COMPLETE) && proc->delayChkptEnd)) &&
            VirtualTransactionIdIsValid(vxid))
        {
            int         i;
@@ -3151,6 +3183,28 @@ HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
    return result;
 }
 
+/*
+ * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying
+ * the start of a checkpoint?
+ */
+bool
+HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
+{
+   return HaveVirtualXIDsDelayingChkptGuts(vxids, nvxids,
+                                           DELAY_CHKPT_START);
+}
+
+/*
+ * HaveVirtualXIDsDelayingChkptEnd -- Are any of the specified VXIDs delaying
+ * the end of a checkpoint?
+ */
+bool
+HaveVirtualXIDsDelayingChkptEnd(VirtualTransactionId *vxids, int nvxids)
+{
+   return HaveVirtualXIDsDelayingChkptGuts(vxids, nvxids,
+                                           DELAY_CHKPT_COMPLETE);
+}
+
 /*
  * BackendPidGetProc -- get a backend's PGPROC given its PID
  *
index b78012ec2b6126d23d2af54a7e30cc5cf5f998a2..11b514c9ae8a4bef54394c9c6710da1624b16541 100644 (file)
@@ -87,36 +87,8 @@ struct XidCache
 #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.
+ * Flags used only for type of internal functions
+ * GetVirtualXIDsDelayingChkptGuts and HaveVirtualXIDsDelayingChkptGuts.
  */
 #define DELAY_CHKPT_START      (1<<0)
 #define DELAY_CHKPT_COMPLETE   (1<<1)
@@ -226,11 +198,12 @@ struct PGPROC
    pg_atomic_uint64 waitStart; /* time at which wait for lock acquisition
                                 * started */
 
-   int         delayChkpt;     /* for DELAY_CHKPT_* flags */
+   bool        delayChkpt;     /* true if this proc delays checkpoint start */
 
    uint8       statusFlags;    /* this backend's status flags, see PROC_*
                                 * above. mirrored in
                                 * ProcGlobal->statusFlags[pgxactoff] */
+   bool        delayChkptEnd;  /* true if this proc delays checkpoint end */
 
    /*
     * Info to allow us to wait for synchronous replication, if needed.
index 93de230a3290577a39d1dcde0f57bbb80a221a42..f5d7d4109860ffad6f149ccea05b2acd7a66bf3b 100644 (file)
@@ -59,9 +59,12 @@ extern TransactionId GetOldestActiveTransactionId(void);
 extern TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly);
 extern void GetReplicationHorizons(TransactionId *slot_xmin, TransactionId *catalog_xmin);
 
-extern VirtualTransactionId *GetVirtualXIDsDelayingChkpt(int *nvxids, int type);
+extern VirtualTransactionId *GetVirtualXIDsDelayingChkpt(int *nvxids);
+extern VirtualTransactionId *GetVirtualXIDsDelayingChkptEnd(int *nvxids);
 extern bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids,
-                                        int nvxids, int type);
+                                        int nvxids);
+extern bool HaveVirtualXIDsDelayingChkptEnd(VirtualTransactionId *vxids,
+                                           int nvxids);
 
 extern PGPROC *BackendPidGetProc(int pid);
 extern PGPROC *BackendPidGetProcWithLock(int pid);