Checkpoints stuck after a failed bulk write inside a savepoint

Поиск
Список
Период
Сортировка
Искать

Checkpoints stuck after a failed bulk write inside a savepoint

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi hackers,

smgr_bulk_finish() sets DELAY_CHKPT_START outside a critical section
and then calls smgrregistersync(). That call can raise an ERROR, for
example when lseek() fails on a bad disk or memory runs out. 

If a savepoint catches that error, the flag stays set. AbortSubTransaction() 
does not clear the flag. So every checkpoint waits in CheckpointDelayStart
until the transaction ends. With assertions enabled, COMMIT fails an Assert.

TRAP: failed Assert("(MyProc->delayChkptFlags & DELAY_CHKPT_IN_COMMIT) == 0"),
File: "xact.c", Line: 1473

The flag closes a gap between reading the redo pointer and registering
the sync request. 

0001 removes the gap instead. It registers the request first and then checks 
the redo pointer. If the pointer has not moved, a later checkpoint will pick up 
our request before it syncs. If it has moved, we fsync the file ourselves as 
before. So the flag is not needed.

The patch is the fix only and the noncfbot-repro.patch include the injection point
and reproduction.

Thanks,
Shihao

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
Joao Rito <joaovictor.rito@gmail.com>
Дата:
Hi Shihao,

Thank you for the patch. I tested v1-0001 together with the test in
noncfbot-repro.patch. It worked for me. These are my results.

Setup: master at 5ffc82103773 (2026-11-16), Debian, gcc 14,
--enable-cassert --enable-injection-points --enable-tap-tests.

Without the fix, the test cannot run as posted, because
noncfbot-repro.patch places the injection point in the fixed code.
So I added the injection point by hand in master, just before
smgrregistersync(), inside the DELAY_CHKPT_START window. The test
then hangs: the CHECKPOINT after ROLLBACK TO SAVEPOINT does not
finish and the TAP timeout fires (about 62 seconds in my run).
This matches your description, but it is my own placement of the
injection point, not the one in your patch.

With v1-0001 and the test:
 - t/016_bulk_write_savepoint.pl passes.
 - The other 15 test_misc tests pass (257 tests in total).
 - make check passes (243 tests).

On the idea: registering the sync request first and checking the
redo pointer after it looks right to me. It removes the flag, so
nothing can leak when an ERROR is caught by a savepoint.

A small remark. If the redo pointer did move, the request registered
earlier is redundant, as the new comment says. I assume that costs
at most one extra fsync in a checkpoint, but I did not measure it.

What I did not test:
 - Real I/O failure (lseek or out of memory). I used only the
   injection point.
 - Concurrent checkpoints racing with smgr_bulk_finish(). I read the
   new ordering but did not stress it.
 - The backpatch to 17. I used master only.
 - wal_level=minimal, meson build, other platforms.
 - Crash recovery after the sequence.
 - A proof that the new ordering never loses an fsync. I only read it.
 - The Assert at COMMIT (xact.c): the test failed first in my red
   run, so I did not see it.

Thanks again.

Best regards,
Joao Rito


Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Joao,

Thanks for testing

> I assume that costs
> at most one extra fsync in a checkpoint, but I did not measure it.

Yes, the cost is one extra fsync per segment on a file we just
flushed. It only happens when a checkpoint runs during the bulk write.

Thanks,
Shihao

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Mon, Nov 16, 2026 at 12:20:36PM -0500, shihao zhong wrote:
> Yes, the cost is one extra fsync per segment on a file we just
> flushed. It only happens when a checkpoint runs during the bulk write.

Hmm.  I am not sure to like that much, as it is wasteful, at least.
How about checking the redo LSN twice instead?  I would imagine, let's
say:
- First check the redo LSN.
- If it does not match with what's in the bulk state, register a sync
request.
- Within the code block that registered the sync request, re-check the
redo LSN vs the bulk state LSN.
- If the redo LSN and the bulk state LSN in the second check do not
match, do smgrimmedsync().

That's still not better than what's on HEAD, but it reduces the window
of the useless syncs to be close to zero in practice, only when a
checkpoint had the idea to run between the smgrregistersync() and the
smgrregistersync() calls.  And we don't lose track of the flag value
due to the fact that we don't use it.

The bulk loading facility exists down to v17, but I'm not convinced
that this is worth a backpatch: this is very unlikely going to show up
in practice.
--
Michael

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Nov 20, 2026 at 04:40:53PM +0900, Michael Paquier wrote:
> That's still not better than what's on HEAD, but it reduces the window
> of the useless syncs to be close to zero in practice, only when a
> checkpoint had the idea to run between the smgrregistersync() and the
> smgrregistersync() calls.  And we don't lose track of the flag value
> due to the fact that we don't use it.

Ahem.. between the smgrregistersync() and the smgrimmedsync() calls.
Bad copy-paste.
--
Michael

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Nov 20, 2026 at 05:00:49PM +0900, Michael Paquier wrote:
> On Fri, Nov 20, 2026 at 04:40:53PM +0900, Michael Paquier wrote:
>> That's still not better than what's on HEAD, but it reduces the window
>> of the useless syncs to be close to zero in practice, only when a
>> checkpoint had the idea to run between the smgrregistersync() and the
>> smgrregistersync() calls.  And we don't lose track of the flag value
>> due to the fact that we don't use it.
> 
> Ahem.. between the smgrregistersync() and the smgrimmedsync() calls.
> Bad copy-paste.

Please find attached to show the idea.  I have not spent much time
testing it.  (Note that the CI has failed once on Windows, not sure if
this was related to this patch or not.)
--
Michael

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Sat, Nov 21, 2026 at 07:34:36AM +0900, Michael Paquier wrote:
> (Note that the CI has failed once on Windows, not sure if
> this was related to this patch or not.)

That was an unrelated failure; the ECPG tests have timed out.
--
Michael

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
shihao zhong <zhong950419@gmail.com>
Дата:
> Please find attached to show the idea.  I have not spent much time
> testing it.

Thanks, v2 looks good to me. I ran a checkpoint at each point around
the two redo checks and could not find a race.

Thanks,
Shihao 

Re: Checkpoints stuck after a failed bulk write inside a savepoint

От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Michael,

Please find attached to show the idea.  I have not spent much time
testing it.  (Note that the CI has failed once on Windows, not sure if
this was related to this patch or not.)


I have tested v2 on master, with two injection points added locally
just before and just after smgrregistersync().

The double check of the redo pointer looks right to me.  It works
because CreateCheckPoint() publishes XLogCtl->RedoRecPtr before
CheckPointGuts() calls ProcessSyncRequests(), and that absorbs the queued requests before it bumps the cycle counter.  
So if the second check still sees the old redo pointer, any checkpoint that moves it
later will pick up our request.  If the second check sees a new redo
pointer, we fsync the relation ourselves.  The flag used to enforce this ordering explicitly.  Now it is implicit, so it may be worth one
sentence in the comment, in case CreateCheckPoint() is reordered later.

What I tested:
1. Error inside a savepoint for each caller of the bulk write facility: btree CREATE INDEX and REINDEX, sorted GiST build, btree
and SP-GiST on an unlogged table (init fork), ALTER TABLE ... TYPE (index rebuild), CLUSTER, and ALTER TABLE SET TABLESPACE in both
directions.  On master the CHECKPOINT after ROLLBACK TO SAVEPOINT
blocks in all cases.  With an assert-enabled build, a second CREATE INDEX in the same transaction fails the Assert.
With v2 the CHECKPOINT completes in a few ms, and the retry and COMMIT work.

2. A checkpoint forced while the backend waits at each injection point.  In both windows the backend notices that the redo pointer
moved and calls smgrimmedsync().  Without a checkpoint there is no immediate fsync.

3. Four sessions running REINDEX, CLUSTER, SET TABLESPACE and GiST index rebuilds in a loop (one of them on an unlogged table), with an error injected and caught in one of them, and a fifth session running CHECKPOINT in a loop (about 1100 checkpoints).  No hang and no failure.  amcheck (heapallindexed) and GiST scan counts are fine, before and after crash recovery with an immediate stop.

4. make check passes, and so does Shihao's TAP test from noncfbot-repro.patch, adapted to v2.

Regarding the backpatch, I agree that the error itself is rare.  But no explicit SAVEPOINT is
needed, a PL/pgSQL EXCEPTION block around the DDL is enough, and that
pattern is common.  When it happens, no checkpoint can complete until the transaction ends.
I tested a DO block that catches the error and keeps running, with
max_wal_size = 32MB, while another session generated WAL.  On master
pg_wal grew from 16MB to 192MB and no checkpoint completed.  With v2 pg_wal stayed at 32MB .
So I would lean towards backpatching down to 17, but I leave that to you.

If you want a test, the two injection points around
smgrregistersync() cover the savepoint case and both windows.  I can send them.  
Note that the test in noncfbot-repro.patch does not apply on v2, because its injection point is placed in the v1 code.

Regards,
Surya Poondla 
FAQ