postgres.git / summary / log / commit / refs

commit    36ff18f948418e059b5dee49a01928dd9d983004
Author:   Tomas Vondra <tomas.vondra@postgresql.org>
Date:     Thu Nov 19 16:48:52 2026 +0000

    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@vondra.me>
    Reviewed-by: surya poondla <suryapoondla4@gmail.com>
    Reviewed-by: shihao zhong <zhong950419@gmail.com>
    Reviewed-by: Chao Li <li.evan.chao@gmail.com>
    Discussion: https://postgr.es/m/a797f236-286a-43e6-9b82-5f3d70bd5e99@vondra.me
    Backpatch-through: 15


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(+) diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c index 7735562db01..2abce2eea2d 100644 --- a/src/backend/access/transam/xlogprefetcher.c +++ b/src/backend/access/transam/xlogprefetcher.c @@ -969,6 +969,9 @@ XLogPrefetcherBeginRead(XLogPrefetcher *prefetcher, XLogRecPtr recPtr) prefetcher->no_readahead_until = 0; + /* Forget the record we were examining, if any. */ + prefetcher->record = NULL; + /* This will forget about any queued up records in the decoder. */ XLogBeginRead(prefetcher->reader, recPtr); } diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index cbd701891ed..005f06f43d4 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -63,6 +63,7 @@ tests += { 't/056_standby_snapshot_export.pl', 't/057_snapshot_commit_race.pl', 't/060_multixact_crashed_updater.pl', + 't/061_recovery_target_prefetch.pl', ], }, } diff --git a/src/test/recovery/t/061_recovery_target_prefetch.pl b/src/test/recovery/t/061_recovery_target_prefetch.pl new file mode 100644 index 00000000000..96f94d31b20 --- /dev/null +++ b/src/test/recovery/t/061_recovery_target_prefetch.pl @@ -0,0 +1,94 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test that recovery ends correctly when a recovery target is reached while +# the WAL prefetcher is part-way through a later record with many block +# references. + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node_primary = PostgreSQL::Test::Cluster->new('primary'); +$node_primary->init(has_archiving => 1, allows_streaming => 1); +$node_primary->start; + +$node_primary->safe_psql( + 'postgres', + "CREATE TABLE t (i int, v text); + INSERT INTO t SELECT i, md5(i::text) FROM generate_series(1, 10000) i;" +); +$node_primary->backup('backup'); + +# An SP-GiST build WAL-logs the new index with log_newpage_range(), in +# XLOG_FPI records with up to XLR_MAX_BLOCK_ID block references each. +my $start_lsn = + $node_primary->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()'); +$node_primary->safe_psql('postgres', + 'CREATE INDEX t_v ON t USING spgist (v)'); +my $end_lsn = + $node_primary->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()'); +my $walfile = $node_primary->safe_psql('postgres', + 'SELECT pg_walfile_name(pg_current_wal_lsn())'); +$node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); +$node_primary->poll_query_until('postgres', + "SELECT last_archived_wal >= '$walfile' FROM pg_stat_archiver") + or die "timed out waiting for WAL to be archived"; + +# Stop recovery before the record preceding the first FPI record with many +# block references, rather than before the FPI record itself, because the +# prefetcher forgets about a record once it has been read (even if it is not +# replayed). With maintenance_io_concurrency = 1, the prefetcher only looks a +# few block references ahead, so it is part-way through the FPI record when +# recovery ends. +my ($stdout, $stderr) = run_command( + [ + 'pg_waldump', + '--path' => $node_primary->archive_dir, + '--start' => $start_lsn, + '--end' => $end_lsn, + ]); +$stdout =~ /prev ([0-9A-F]+\/[0-9A-F]+), desc: FPI , .*blkref #8: / + or die "could not find an FPI record with many block references"; +my $target_lsn = $1; + +my $node_standby = PostgreSQL::Test::Cluster->new('standby'); +$node_standby->init_from_backup( + $node_primary, 'backup', + has_restoring => 1, + standby => 0); +$node_standby->append_conf( + 'postgresql.conf', qq( +maintenance_io_concurrency = 1 +recovery_target_lsn = '$target_lsn' +recovery_target_inclusive = off +recovery_target_action = 'promote' +)); +$node_standby->start; +$node_standby->poll_query_until('postgres', 'SELECT NOT pg_is_in_recovery()') + or die "timed out waiting for promotion"; + +# Compare the LSNs as pg_lsn, so as not to depend on how they're formatted. +my ($stop_lsn) = + slurp_file($node_standby->logfile) =~ + /recovery stopping before WAL location \(LSN\) "([0-9A-F]+\/[0-9A-F]+)"/ + or die "could not find where recovery stopped in the server log"; +is( $node_standby->safe_psql( + 'postgres', "SELECT '$stop_lsn'::pg_lsn = '$target_lsn'::pg_lsn"), + 't', + 'recovery stopped at the target'); +is( $node_standby->safe_psql( + 'postgres', "SELECT count(*), to_regclass('t_v') IS NULL FROM t"), + '10000|t', + 'data matches the recovery target'); + +# Check that the WAL written on the new timeline can be replayed. +$node_standby->safe_psql('postgres', 'INSERT INTO t VALUES (0)'); +$node_standby->stop('immediate'); +$node_standby->start; +is($node_standby->safe_psql('postgres', 'SELECT count(*) FROM t'), + '10001', 'crash recovery after promotion'); + +done_testing(); [parent: ecfe3a44dab7]