From 45090e9e2b94d53016118c5bf28b912c7ed6610d Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Wed, 25 Nov 2026 14:37:55 +0100 Subject: [PATCH v1] Update minRecoveryPoint at every shutdown restartpoint Previously, pg_rewind could report "no rewind required" for a cleanly stopped standby that had replayed WAL past the point of divergence. As a result, the node could not follow the new primary. pg_rewind takes minRecoveryPoint as the end of WAL of a standby, but a shutdown restartpoint set it to the replay position only when the restartpoint itself was skipped. When one was performed, only the flush advanced it, and only if something written carried a newer LSN. A replayed abort, for example, leaves none behind. Fix this by setting minRecoveryPoint to the replay position at every shutdown restartpoint, just like when the restartpoint is skipped. --- src/backend/access/transam/xlog.c | 15 ++++- src/bin/pg_rewind/meson.build | 1 + src/bin/pg_rewind/t/012_standby_shutdown.pl | 75 +++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/bin/pg_rewind/t/012_standby_shutdown.pl diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index fd37ea4f607..a7ac46fe5c8 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -8532,9 +8532,9 @@ CreateRestartPoint(int flags) * before the last shutdown. Such time travel is still possible in case of * immediate shutdown, though. * - * We don't explicitly advance minRecoveryPoint when we do create a - * restartpoint. It's assumed that flushing the buffers will do that as a - * side-effect. + * When we do create a restartpoint, flushing the buffers advances + * minRecoveryPoint as a side-effect, but not necessarily up to the last + * record replayed. A shutdown restartpoint takes care of that below. */ if (!XLogRecPtrIsValid(lastCheckPointRecPtr) || lastCheckPoint.redo <= ControlFile->checkPointCopy.redo) @@ -8646,6 +8646,15 @@ CreateRestartPoint(int flags) */ INJECTION_POINT("create-restart-point", NULL); + /* + * The flush above advanced minRecoveryPoint only if something written + * carried a newer LSN, which not every record leaves behind, e.g. an + * abort. At shutdown, make it cover everything replayed, as pg_rewind + * takes it for the end of WAL of a standby. + */ + if (flags & CHECKPOINT_IS_SHUTDOWN) + UpdateMinRecoveryPoint(InvalidXLogRecPtr, true); + /* * Remember the prior checkpoint's redo ptr for * UpdateCheckPointDistanceEstimate() diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build index 52a6ab0a515..2caf529da15 100644 --- a/src/bin/pg_rewind/meson.build +++ b/src/bin/pg_rewind/meson.build @@ -45,6 +45,7 @@ tests += { 't/009_growing_files.pl', 't/010_keep_recycled_wals.pl', 't/011_wal_copy.pl', + 't/012_standby_shutdown.pl', ], }, } diff --git a/src/bin/pg_rewind/t/012_standby_shutdown.pl b/src/bin/pg_rewind/t/012_standby_shutdown.pl new file mode 100644 index 00000000000..eb3ccc8928c --- /dev/null +++ b/src/bin/pg_rewind/t/012_standby_shutdown.pl @@ -0,0 +1,75 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Test that pg_rewind rewinds a cleanly stopped standby that has replayed past +# the point of divergence, when the last record it replayed is an abort. +# +# This test does not make use of RewindTest as it requires three nodes. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node_a = PostgreSQL::Test::Cluster->new('node_a'); +$node_a->init(allows_streaming => 1); +$node_a->append_conf( + 'postgresql.conf', qq( +autovacuum = off +checkpoint_timeout = '1h' +)); +$node_a->start; + +my $backup_name = 'my_backup'; +$node_a->backup($backup_name); + +my $node_s1 = PostgreSQL::Test::Cluster->new('node_s1'); +$node_s1->init_from_backup($node_a, $backup_name, has_streaming => 1); +$node_s1->start; + +my $node_s2 = PostgreSQL::Test::Cluster->new('node_s2'); +$node_s2->init_from_backup($node_a, $backup_name, has_streaming => 1); +$node_s2->start; + +# Restart S2, so that it has no dirty buffers and its minRecoveryPoint is +# at the replay position. +$node_a->wait_for_replay_catchup($node_s2); +$node_s2->restart; + +# Give S2 a checkpoint record to create its shutdown restartpoint from, and +# promote S1 right after it. +$node_a->safe_psql('postgres', 'CHECKPOINT'); +$node_a->wait_for_replay_catchup($node_s1); +$node_s1->promote; + +# Only S2 replays this abort. Unlike a commit, an abort does not advance +# minRecoveryPoint when it is written out, and it modifies no data page. It +# is not flushed at once, hence wait for the insert position. +$node_a->safe_psql('postgres', + 'BEGIN; SELECT pg_current_xact_id(); ROLLBACK;'); +my $abort_lsn = $node_a->lsn('insert'); +$node_a->wait_for_catchup($node_s2, 'replay', $abort_lsn); +$node_s2->stop; + +my ($stdout, $stderr) = + run_command([ 'pg_controldata', $node_s2->data_dir ]); +my ($min_recovery_lsn) = + $stdout =~ /^Minimum recovery ending location:\s+(\S+)$/m; +is( $node_a->safe_psql( + 'postgres', + "SELECT '$min_recovery_lsn'::pg_lsn >= '$abort_lsn'::pg_lsn"), + 't', + 'minRecoveryPoint covers the replayed abort after shutdown'); + +command_checks_all( + [ + 'pg_rewind', + '--source-server' => $node_s1->connstr('postgres'), + '--target-pgdata' => $node_s2->data_dir, + ], + 0, + [], + [qr/rewinding from last common checkpoint/], + 'pg_rewind rewinds the standby'); + +done_testing(); -- 2.47.3