pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid DELAY_CHKPT_START in smgr_bulk_finish()
Date: Mon, 23 Nov 2026 22:59:41 +0000
Message-ID: <E1xVd0f-000000004eR-04sH@gemulon.postgresql.org> (raw)

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@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Surya Poondla <suryapoondla4@gmail.com>
Reviewed-by: Joao Rito <joaovictor.rito@gmail.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(-)



Message-ID: <E1xVd0f-000000004eR-04sH@gemulon.postgresql.org>
Permalink:  ../E1xVd0f-000000004eR-04sH@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xVd0f-000000004eR-04sH@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: michael@paquier.xyz
  Subject: Re: pgsql: Avoid DELAY_CHKPT_START in smgr_bulk_finish()
  In-Reply-To: <E1xVd0f-000000004eR-04sH@gemulon.postgresql.org>
  References: <E1xVd0f-000000004eR-04sH@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox