Hi Hackers,

When you run pg_rewind with --source-pgdata and the source is a cleanly
stopped standby, it sets the target's minRecoveryPoint to the source's
last restartpoint instead of the source's minRecoveryPoint. This causes
the target to be marked consistent too soon.

Problem
=======

For a local source, pg_rewind assumes there was a shutdown checkpoint
and uses the latest checkpoint location as the point the target must
replay to. But it also accepts a source that is shut down in recovery,
and in that case, the standby's data files include changes up to its
minRecoveryPoint. When using --source-server, pg_rewind already uses
minRecoveryPoint for a standby source.

To reproduce this, it is enough that the source standby replays a
transaction after its last restartpoint before it is stopped. In my
test, it replayed a CHECKPOINT followed by a transaction:

Latest checkpoint location:           0/030001A8
Minimum recovery ending location:     0/03000388

After pg_rewind, the control file of the target contains the checkpoint
location of the source, not its minRecoveryPoint:

Minimum recovery ending location:     0/030001A8

This is important if recovery ends at the consistency point, such as
with recovery_target = 'immediate'. In this case, the target has blocks
from the source that are newer than the WAL it has replayed. As a
result, a transaction that was not replayed can become partially
visible: once a new transaction on the target gets the same xid, its
changes in the copied blocks show up, while its changes in other blocks
are missing.

Patch
=====

The attached patch makes pg_rewind use the source's minRecoveryPoint if
it is later than the last checkpoint. It also adds a TAP test for this case.

Comments welcome.

Best regards
   Jan

-- 
Jan Nidzwetzki
PlanetScale
From 94392e6df4e3ea38c1f747b74507bf51cd7d2e54 Mon Sep 17 00:00:00 2001
From: Jan Nidzwetzki <[email protected]>
Date: Fri, 27 Nov 2026 14:51:17 +0100
Subject: [PATCH] pg_rewind: Use minRecoveryPoint of a local standby source

Previously, if the source was a local data directory from a cleanly
stopped standby, pg_rewind set the target's minRecoveryPoint to the
source's last restartpoint. However, some blocks copied from the source
could be newer, which meant the target was marked as consistent too
soon. If recovery finished at that point, such as when using
recovery_target = 'immediate', the target might show changes from
transactions it had not replayed.

To fix this, pg_rewind now uses the source's minRecoveryPoint if it is
later than its last checkpoint. This matches the approach already used
when the standby source is accessed through a connection.

Reported-by: Tom Pang <[email protected]>
Author: Jan Nidzwetzki <[email protected]>
---
 src/bin/pg_rewind/meson.build                 |   1 +
 src/bin/pg_rewind/pg_rewind.c                 |  16 ++-
 .../pg_rewind/t/012_local_standby_source.pl   | 111 ++++++++++++++++++
 3 files changed, 125 insertions(+), 3 deletions(-)
 create mode 100644 src/bin/pg_rewind/t/012_local_standby_source.pl

diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build
index 52a6ab0a515..552511833cb 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_local_standby_source.pl',
     ],
   },
 }
diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index d2521dab333..2f6d19ea76f 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -773,10 +773,20 @@ perform_rewind(filemap_t *filemap, rewind_source *source,
        {
                /*
                 * Source is a local data directory. It should've shut down 
cleanly,
-                * and we must replay to the latest shutdown checkpoint.
+                * and we must replay to the latest shutdown checkpoint, or to
+                * minRecoveryPoint if it was a standby.
                 */
-               endrec = ControlFile_source_after.checkPoint;
-               endtli = ControlFile_source_after.checkPointCopy.ThisTimeLineID;
+               if (ControlFile_source_after.minRecoveryPoint >
+                       ControlFile_source_after.checkPoint)
+               {
+                       endrec = ControlFile_source_after.minRecoveryPoint;
+                       endtli = ControlFile_source_after.minRecoveryPointTLI;
+               }
+               else
+               {
+                       endrec = ControlFile_source_after.checkPoint;
+                       endtli = 
ControlFile_source_after.checkPointCopy.ThisTimeLineID;
+               }
        }
 
        memcpy(&ControlFile_new, &ControlFile_source_after, 
sizeof(ControlFileData));
diff --git a/src/bin/pg_rewind/t/012_local_standby_source.pl 
b/src/bin/pg_rewind/t/012_local_standby_source.pl
new file mode 100644
index 00000000000..1653688d4cb
--- /dev/null
+++ b/src/bin/pg_rewind/t/012_local_standby_source.pl
@@ -0,0 +1,111 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Test that pg_rewind with a cleanly stopped standby as local source makes the
+# target replay to the minRecoveryPoint of the source.
+#
+# Node A is the primary, B a standby that is used as source, and C a promoted
+# standby that is rewound.
+#
+# 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;
+
+use File::Copy;
+
+my $tmp_folder = PostgreSQL::Test::Utils::tempdir;
+
+# Return the minRecoveryPoint stored in the control file of a node.
+sub min_recovery_point
+{
+       my $node = shift;
+       my ($stdout, $stderr) =
+         run_command([ 'pg_controldata', $node->data_dir ]);
+       my ($lsn) = $stdout =~ /^Minimum recovery ending location:\s+(\S+)$/m;
+       return $lsn;
+}
+
+my $node_a = PostgreSQL::Test::Cluster->new('node_a');
+$node_a->init(allows_streaming => 1);
+$node_a->append_conf(
+       'postgresql.conf', qq(
+wal_keep_size = '100MB'
+));
+$node_a->start;
+
+# Both tables get a row, so that their pages exist before the nodes diverge.
+$node_a->safe_psql('postgres',
+       'CREATE TABLE t1 AS SELECT 1 AS i; CREATE TABLE t2 AS SELECT 1 AS i;');
+
+my $backup_name = 'my_backup';
+$node_a->backup($backup_name);
+
+my $node_b = PostgreSQL::Test::Cluster->new('node_b');
+$node_b->init_from_backup($node_a, $backup_name, has_streaming => 1);
+$node_b->start;
+
+my $node_c = PostgreSQL::Test::Cluster->new('node_c');
+$node_c->init_from_backup($node_a, $backup_name, has_streaming => 1);
+$node_c->start;
+
+# Promote C and insert into t1 on it.  pg_rewind copies only pages that the
+# target has changed, so it will copy the page of t1 from B, but not the page
+# of t2.
+$node_a->wait_for_replay_catchup($node_c);
+$node_c->promote;
+$node_c->safe_psql('postgres', 'INSERT INTO t1 VALUES (2)');
+$node_c->stop;
+
+# On A, run a checkpoint followed by a transaction that changes both tables.
+# B replays both, so the restartpoint of its shutdown is older than the
+# transaction, while its minRecoveryPoint covers it.
+$node_a->safe_psql('postgres',
+       'CHECKPOINT; BEGIN; INSERT INTO t1 VALUES (3); INSERT INTO t2 VALUES 
(3); COMMIT;'
+);
+$node_a->wait_for_replay_catchup($node_b);
+$node_b->stop;
+
+# Keep a temporary postgresql.conf or it would be overwritten during the 
rewind.
+my $node_c_pgdata = $node_c->data_dir;
+copy("$node_c_pgdata/postgresql.conf", "$tmp_folder/postgresql.conf.tmp");
+
+command_ok(
+       [
+               'pg_rewind',
+               '--source-pgdata' => $node_b->data_dir,
+               '--target-pgdata' => $node_c_pgdata,
+               '--no-sync',
+       ],
+       'pg_rewind with a stopped standby as local source');
+
+move("$tmp_folder/postgresql.conf.tmp", "$node_c_pgdata/postgresql.conf");
+
+is( min_recovery_point($node_c),
+       min_recovery_point($node_b),
+       'target replays to the minRecoveryPoint of the source standby');
+
+# C now has the page of t1 with the row of that transaction, but the old page
+# of t2.  End recovery as soon as C is consistent.  If that is before the
+# transaction, the row never reaches t2, while the row in t1 shows up as soon
+# as a new transaction on C gets the same xid.
+$node_c->append_conf(
+       'postgresql.conf', qq(
+recovery_target = 'immediate'
+recovery_target_action = 'promote'
+));
+$node_c->start;
+$node_c->poll_query_until('postgres', 'SELECT NOT pg_is_in_recovery()')
+  or die "Timed out while waiting for promotion";
+$node_c->safe_psql('postgres', 'SELECT pg_current_xact_id()');
+
+is( $node_c->safe_psql(
+               'postgres', q{
+SELECT (SELECT string_agg(i::text, ',' ORDER BY i) FROM t1),
+       (SELECT string_agg(i::text, ',' ORDER BY i) FROM t2)}),
+       '1,3|1,3',
+       'target shows the whole transaction replayed by the source standby');
+
+done_testing();
-- 
2.47.3

Reply via email to