From cecba2265e0218cdb433bd86787bfc15b67d4c45 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 2 Nov 2026 23:22:36 -0500 Subject: [PATCH 1/2] Avoid DELAY_CHKPT_START in smgr_bulk_finish() Register the sync request before checking the redo pointer, so an error caught by a savepoint cannot leak the flag. Backpatch-through: 17 --- src/backend/storage/smgr/bulk_write.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/backend/storage/smgr/bulk_write.c b/src/backend/storage/smgr/bulk_write.c index f3c24082a69..8a9471d8715 100644 --- a/src/backend/storage/smgr/bulk_write.c +++ b/src/backend/storage/smgr/bulk_write.c @@ -40,7 +40,6 @@ #include "access/xlogrecord.h" #include "storage/bufpage.h" #include "storage/bulk_write.h" -#include "storage/proc.h" #include "storage/smgr.h" #include "utils/rel.h" @@ -197,27 +196,25 @@ smgr_bulk_finish(BulkWriteState *bulkstate) */ /* - * Prevent a checkpoint from starting between the GetRedoRecPtr() and - * smgrregistersync() calls. + * Register the sync request before checking the redo pointer. If the + * redo pointer has not moved, any checkpoint that sets a new redo + * pointer after the check will absorb the request before it syncs. In + * this order there is no need to hold off checkpoints with + * DELAY_CHKPT_START, which would stay set if smgrregistersync() + * failed inside a subtransaction. */ - Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0); - MyProc->delayChkptFlags |= DELAY_CHKPT_START; + smgrregistersync(bulkstate->smgr, bulkstate->forknum); if (bulkstate->start_RedoRecPtr != GetRedoRecPtr()) { /* * A checkpoint occurred and it didn't know about our writes, so - * fsync() the relation ourselves. + * fsync() the relation ourselves. The sync request registered + * above is then redundant, but harmless. */ - MyProc->delayChkptFlags &= ~DELAY_CHKPT_START; smgrimmedsync(bulkstate->smgr, bulkstate->forknum); elog(DEBUG1, "flushed relation because a checkpoint occurred concurrently"); } - else - { - smgrregistersync(bulkstate->smgr, bulkstate->forknum); - MyProc->delayChkptFlags &= ~DELAY_CHKPT_START; - } } } -- 2.37.1 (Apple Git-137.1)