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 <[email protected]>
Author: Michael Paquier <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Reviewed-by: Joao Rito <[email protected]>
Discussion: 
https://postgr.es/m/CAGRkXqQ=W7TE2c+hpcBADdRYs0Vvdvcz51aV3gSwtQeBsPL=x...@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(-)

Reply via email to