pgsql: Avoid DELAY_CHKPT_START in smgr_bulk_finish()

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Avoid DELAY_CHKPT_START in smgr_bulk_finish()
Date: 2026-11-23 22:59:41
Message-ID: E1xVd0f-000000004eR-04sH@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid DELAY_CHKPT_START in smgr_bulk_finish()

smgr_bulk_finish() sets DELAY_CHKPT_START around GetRedoRecPtr() and
smgrregistersync(), so that no checkpoint could start between reading
the redo pointer and queueing the sync request. Unlike all the other
users of this flag, this code was not in a critical section. If
smgrregistersync() fails (lseek failure, or some OOM), and because the
flag is only cleared at the end of a top-level transaction, an ERROR
caught in a savepoint or in a PL/pgSQL exception block would leave the
flag set. Then every checkpoint waited for this backend until the
transaction ended.

The redo pointer is now checked twice: a first time to register a sync
request, and a second time to flush the segment if the redo has moved
in-between, eliminating the need for setting DELAY_CHKPT_START. This
is less optimal than the previous code if a checkpoint runs between the
smgrregistersync() and the smgrimmedsync() calls, by causing unnecessary
flushes, but the window is short enough that it should not be a problem
in practice, and the new code is safer in terms of ERROR handling.

The problem would only show up due to some environment failure, if
hitting this specific code path. Like any failure of this class no
backpatch is done as it would be unlikely hit in practice.

Author: Shihao Zhong <zhong950419(at)gmail(dot)com>
Author: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Surya Poondla <suryapoondla4(at)gmail(dot)com>
Reviewed-by: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAGRkXqQ=W7TE2c+hpcBADdRYs0Vvdvcz51aV3gSwtQeBsPL=xg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/9931482364a938af876307e84ae88ff4839838b0

Modified Files
--------------
src/backend/storage/smgr/bulk_write.c | 33 ++++++++++++++-------------------
1 file changed, 14 insertions(+), 19 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Masahiko Sawada 2026-11-23 23:11:04 pgsql: Add an xid argument to the logical decoding message callbacks.
Previous Message Tom Lane 2026-11-23 22:54:17 pgsql: Fix overflow problems in btree_gist's penalty and distance funct