A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

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

A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:
Вложения:
Hi,

While hacking on an improvement for the recovery prefetcher, I ran into
a pre-existing bug present since PG 15. The brief explanation is that
XLogPrefetcherBeginRead resets a bunch of stuff, but not the pointer
prefetcher->record. In some circumstances (full I/O queue, ...) that
happens to confuse XLogPrefetcherReadRecord, triggering failures:

- assert failures on development builds

  Assert(lrq_completed(prefetcher->streaming_read) == 0);

- a bunch of weird/uncommon errors on regular builds

  LOG:  request to flush past end of generated WAL; request 0/030BBD90,
        current position 0/030B9D48
  ERROR:  xlog flush request 0/030BBD90 is not satisfied --- flushed
          only to 0/030B9D48
  FATAL:  checkpoint request failed

AFAICS it can happen only with reduced maintenance_io_concurrency, but
I'm not sure about that. Maybe there's a way to generate WAL records
with enough block images.

The attached patch fixes this by simply resetting the record to NULL in
XLogPrefetcherBeginRead. It also introduces a new TAP test triggering
this, using a SP-GiST index built etc.

I'm not sure if we want to add the TAP test, I'm attaching it more as a
way to reproduce the issue. Without the fix in XLogPrefetcherBeginRead,
it crashes with errors similar to the attached logs.

regards

-- 
Tomas Vondra

Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Chao Li <li.evan.chao@gmail.com>
Дата:


> On Oct 26, 2026, at 07:37, Tomas Vondra  wrote:
> 
> Hi,
> 
> While hacking on an improvement for the recovery prefetcher, I ran into
> a pre-existing bug present since PG 15. The brief explanation is that
> XLogPrefetcherBeginRead resets a bunch of stuff, but not the pointer
> prefetcher->record. In some circumstances (full I/O queue, ...) that
> happens to confuse XLogPrefetcherReadRecord, triggering failures:
> 
> - assert failures on development builds
> 
>  Assert(lrq_completed(prefetcher->streaming_read) == 0);
> 
> - a bunch of weird/uncommon errors on regular builds
> 
>  LOG:  request to flush past end of generated WAL; request 0/030BBD90,
>        current position 0/030B9D48
>  ERROR:  xlog flush request 0/030BBD90 is not satisfied --- flushed
>          only to 0/030B9D48
>  FATAL:  checkpoint request failed
> 
> AFAICS it can happen only with reduced maintenance_io_concurrency, but
> I'm not sure about that. Maybe there's a way to generate WAL records
> with enough block images.
> 
> The attached patch fixes this by simply resetting the record to NULL in
> XLogPrefetcherBeginRead. It also introduces a new TAP test triggering
> this, using a SP-GiST index built etc.
> 
> I'm not sure if we want to add the TAP test, I'm attaching it more as a
> way to reproduce the issue. Without the fix in XLogPrefetcherBeginRead,
> it crashes with errors similar to the attached logs.
> 
> regards
> 
> -- 
> Tomas Vondra
> <0001-Forget-the-prefetcher-s-current-record-in-XLogPrefet.patch>

I confirmed that the new test from v1 failed without the fix, and passed after patching v1.

So, the bug is that the prefetcher continued examining the stale prefetcher->record and filled the I/O queue without decoding the record at the new read position. The resulting assertion failure terminated the startup process.

The fix is straightforward and looks good to me. Although the failure may be an edge case, clearing the stale pointer is certainly the right thing to do.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

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

+1 for this patch,

I tested it and it fixes 15-master. 

I have a question here, I saw you posted a few patches related
to replay prefetcher, do you have any plans in this area?

Thanks,
Shihao



Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

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

Hi Tomas,

Thanks for the patch. I went through it and the fix looks right to me.

I also looked at the other callers of XLogPrefetcherBeginRead(), and I agree with the commit message: apart from FinishWalRecovery(), each of them runs after at most one
record has been read and returned, so prefetcher->record is already NULL there.

The reason this goes unnoticed on non-assert builds is that FinishWalRecovery() throws away the result of ReadRecord(). Even with emode = PANIC, ReadRecord() just returns NULL quietly when there's no errormsg, and EndRecPtr is still the start of the record, which matches what you describe. I couldn't find a case where re-reading the last replayed record is expected to fail, so maybe it's worth adding a check there, probably as a separate patch:

        XLogPrefetcherBeginRead(xlogprefetcher, lastRec);
        if (ReadRecord(xlogprefetcher, PANIC, false, lastRecTLI) == NULL)
                elog(PANIC, "could not re-read last replayed record at %X/%08X",
                         LSN_FORMAT_ARGS(lastRec));

That way, if something like this ever happens again, it would fail loudly.

Regards,
Surya Poondla

Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 10/27/26 23:50, surya poondla wrote:
> 
> Hi Tomas,
> 
> Thanks for the patch. I went through it and the fix looks right to me.
> 
> I also looked at the other callers of XLogPrefetcherBeginRead(), and I
> agree with the commit message: apart from FinishWalRecovery(), each of
> them runs after at most one
> record has been read and returned, so prefetcher->record is already NULL
> there.
> 
> The reason this goes unnoticed on non-assert builds is that
> FinishWalRecovery() throws away the result of ReadRecord(). Even with
> emode = PANIC, ReadRecord() just returns NULL quietly when there's no
> errormsg, and EndRecPtr is still the start of the record, which matches
> what you describe. I couldn't find a case where re-reading the last
> replayed record is expected to fail, so maybe it's worth adding a check
> there, probably as a separate patch:
> 
>         XLogPrefetcherBeginRead(xlogprefetcher, lastRec);
>         if (ReadRecord(xlogprefetcher, PANIC, false, lastRecTLI) == NULL)
>                 elog(PANIC, "could not re-read last replayed record at
> %X/%08X",
>                          LSN_FORMAT_ARGS(lastRec));
> 
> That way, if something like this ever happens again, it would fail loudly.
> 
> Regards,
> Surya Poondla

Thanks for the reviews. I've pushed and backpatched this. I've been
wondering whether to include the TAP test, or whether it was only useful
when investigating the issue. I ended up committing it too. It's pretty
quick (at least compared to some other TAP tests for recovery), and it
seems useful enough.


regards

-- 
Tomas Vondra



Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 3:29 AM Tomas Vondra  wrote:
> I ended up committing it too.

Thanks for working on this!

The new test fails on some buildfarm members, such as indri on v17 [1].
It sets maintenance_io_concurrency = 1, but v17 and earlier reject
nonzero values on platforms without posix_fadvise(), causing the standby
to fail to start.

In v18, this restriction was removed for AIO support, so the setting is
accepted, although recovery prefetching remains disabled on these platforms.

Should we skip the test when recovery prefetching is unavailable?

Regards,

[1] https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=indri&dt=2026-11-19%2021%3A49%3A19

-- 
Fujii Masao


Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 11/20/26 00:07, Fujii Masao wrote:
> On Fri, Nov 20, 2026 at 3:29 AM Tomas Vondra  wrote:
>> I ended up committing it too.
> 
> Thanks for working on this!
> 
> The new test fails on some buildfarm members, such as indri on v17 [1].
> It sets maintenance_io_concurrency = 1, but v17 and earlier reject
> nonzero values on platforms without posix_fadvise(), causing the standby
> to fail to start.
> 
> In v18, this restriction was removed for AIO support, so the setting is
> accepted, although recovery prefetching remains disabled on these platforms.
> 
> Should we skip the test when recovery prefetching is unavailable?
> 

D'oh! I completely forgot about this. Yes, we should skip the test for
these machines. I'll look into that tomorrow morning (unless you want to
take care of it).

What's a good way to check if a machine supports posix_fadvise? I don't
see any TAP test already doing that. I guess we could try assigning into
effective_io_concurrency, and skip the test if it fails.


regards

-- 
Tomas Vondra



Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 8:22 AM Tomas Vondra  wrote:
> What's a good way to check if a machine supports posix_fadvise? I don't
> see any TAP test already doing that. I guess we could try assigning into
> effective_io_concurrency, and skip the test if it fails.

In v18 and later, setting effective_io_concurrency would succeed even on
the platform not supporting posix_fadvise(), such as Windows?
If so, checking recovery_prefetch = on might be a better approach?
Because its check hook, check_recovery_prefetch(), seems to always
return false when USE_PREFETCH is not enabled.

Regards,

-- 
Fujii Masao


Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:


On 11/20/26 01:14, Fujii Masao wrote:
> On Fri, Nov 20, 2026 at 8:22 AM Tomas Vondra  wrote:
>> What's a good way to check if a machine supports posix_fadvise? I don't
>> see any TAP test already doing that. I guess we could try assigning into
>> effective_io_concurrency, and skip the test if it fails.
> 
> In v18 and later, setting effective_io_concurrency would succeed even on
> the platform not supporting posix_fadvise(), such as Windows?
> If so, checking recovery_prefetch = on might be a better approach?
> Because its check hook, check_recovery_prefetch(), seems to always
> return false when USE_PREFETCH is not enabled.
> 

Actually, there's an easier way. We already have check_pg_config(), so
we can do just this:

# Prefetching requires posix_fadvise().  Without it, the server rejects the
# non-zero maintenance_io_concurrency this test relies on.
if (!check_pg_config("#define HAVE_POSIX_FADVISE 1"))
{
  plan skip_all => 'posix_fadvise() not supported by this platform';
}

Will push the fix in the morning.

-- 
Tomas Vondra



Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 10:54 AM Tomas Vondra  wrote:
> # Prefetching requires posix_fadvise().  Without it, the server rejects the
> # non-zero maintenance_io_concurrency this test relies on.
> if (!check_pg_config("#define HAVE_POSIX_FADVISE 1"))
> {
>   plan skip_all => 'posix_fadvise() not supported by this platform';
> }

This check seems to skip the test on macOS because posix_fadvise() is not
supported there. But, v18 and later have a platform-specific prefetch
for macOS, so maintenance_io_concurrency = 1 can be set there.

So, this check might be insufficient for macOS?

Regards,

-- 
Fujii Masao


Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:
On 11/20/26 03:44, Fujii Masao wrote:
> On Fri, Nov 20, 2026 at 10:54 AM Tomas Vondra  wrote:
>> # Prefetching requires posix_fadvise().  Without it, the server rejects the
>> # non-zero maintenance_io_concurrency this test relies on.
>> if (!check_pg_config("#define HAVE_POSIX_FADVISE 1"))
>> {
>>   plan skip_all => 'posix_fadvise() not supported by this platform';
>> }
> 
> This check seems to skip the test on macOS because posix_fadvise() is not
> supported there. But, v18 and later have a platform-specific prefetch
> for macOS, so maintenance_io_concurrency = 1 can be set there.
> 
> So, this check might be insufficient for macOS?
> 

I'm planning to add the skip only on 15-17, so that on 18+ the test
keeps running everywhere. I believe that should still work even on
macos, but I've only tested it on Linux built without fadvise by:

    ./configure ac_cv_func_posix_fadvise=no


regards

-- 
Tomas Vondra



Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 6:46 PM Tomas Vondra  wrote:
> I'm planning to add the skip only on 15-17, so that on 18+ the test
> keeps running everywhere.

Sounds good to me. Thanks for working on this!

Regards,

-- 
Fujii Masao


Re: A bug in recovery prefetcher, triggering asserts / FATAL: checkpoint request failed

От:
Tomas Vondra <tomas@vondra.me>
Дата:


On 11/20/26 11:44, Fujii Masao wrote:
> On Fri, Nov 20, 2026 at 6:46 PM Tomas Vondra  wrote:
>> I'm planning to add the skip only on 15-17, so that on 18+ the test
>> keeps running everywhere.
> 
> Sounds good to me. Thanks for working on this!
> 

Done this way.

Thanks!

-- 
Tomas Vondra



FAQ