pgsql: Forget the prefetcher's current record in XLogPrefetcherBeginRea

From: Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Forget the prefetcher's current record in XLogPrefetcherBeginRea
Date: 2026-11-19 18:24:18
Message-ID: E1xU6nx-00000000Pp9-2rjO@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Forget the prefetcher's current record in XLogPrefetcherBeginRead()

XLogPrefetcherBeginRead() reset the decoder's record queue, but not
prefetcher->record, whose block references XLogPrefetcherNextBlock()
examines. The next read then continued with the remaining block
references of that stale record, before decoding the record at the new
read position.

FinishWalRecovery() re-reads the last replayed record to find the end
of WAL. When recovery stops at a recovery target, the prefetcher may
be part-way through a later record, and if its remaining block
references fill up the I/O queue, no record is decoded. This fails an
assertion in XLogPrefetcherReadRecord(). Without assertions, the start
of the last replayed record silently becomes the end of WAL, the new
timeline branches off at the wrong point, and the end-of-recovery
checkpoint fails with "xlog flush request ... is not satisfied". The
recovery signal file is gone by then, so a restart performs crash
recovery past the recovery target.

This mostly happens with maintenance_io_concurrency set to 7 or less,
and a recovery target shortly before an XLOG_FPI record written by an
index build, CLUSTER or similar operation, as such records carry up to
XLR_MAX_BLOCK_ID block references that need no I/O.

Fix by forgetting the current record in XLogPrefetcherBeginRead(). The
other callers run before the redo loop, when there is no current record.

Add a TAP test that stops recovery shortly before a multi-block XLOG_FPI
record of an SP-GiST index build, with maintenance_io_concurrency = 1.
The test uses SP-GiST rather than B-tree, so that it works unchanged in
the back branches. B-tree builds before 17 WAL-log each page in a
separate XLOG_FPI record with a single block reference, so the
prefetcher is never part-way through such a record.

Author: Tomas Vondra <tomas(at)vondra(dot)me>
Reviewed-by: surya poondla <suryapoondla4(at)gmail(dot)com>
Reviewed-by: shihao zhong <zhong950419(at)gmail(dot)com>
Reviewed-by: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Discussion: https://postgr.es/m/a797f236-286a-43e6-9b82-5f3d70bd5e99@vondra.me
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9d92394052773ce52ea2ee39bf9678a8bffe119d

Modified Files
--------------
src/backend/access/transam/xlogprefetcher.c | 3 +
src/test/recovery/meson.build | 1 +
.../recovery/t/061_recovery_target_prefetch.pl | 94 ++++++++++++++++++++++
3 files changed, 98 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Tomas Vondra 2026-11-19 18:24:25 pgsql: Forget the prefetcher's current record in XLogPrefetcherBeginRea
Previous Message Tomas Vondra 2026-11-19 18:24:11 pgsql: Forget the prefetcher's current record in XLogPrefetcherBeginRea