Hi,

pg_rewind still trips this when the target is a direct ancestor of the
source and stopped earlier on the common timeline.

  Assert(target_wal_endrec == divergerec)

An older base backup of the primary is enough to get there.
Assertion builds crash.  Otherwise we already say "no rewind
required", which is correct.

Heikki called the Assert bogus in BUG #18575 and posted a remove-it
patch [1], but it never landed.  Maxim bumped that thread later [2].
Srinath's recent file-sync / findLastCheckpoint report [3] quotes the
same Assert, though that thread is about the "== divergerec" early
exit, not this "< divergerec" crash.

Attached is a small rebase on current master, with the TAP coverage
folded into "005_same_timeline.pl".  No behaviour change outside
assertion builds.

Does this still seem worth committing?

[1]
https://www.postgresql.org/message-id/[email protected]
[2]
https://www.postgresql.org/message-id/[email protected]
[3]
https://www.postgresql.org/message-id/cafc+b6qpqh4e22-paemsby8yv_2z_8puy8xrumpw9ot_efk...@mail.gmail.com

Regards,
Stepan Neretin
From 316ee3b4605eb70df02dfb215e85841f8f285682 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 02:12:33 +0700
Subject: [PATCH 2/2] Extend same-timeline TAP for ancestor no-rewind case

Cover a direct-ancestor target whose WAL stopped short of the
divergence LSN in "005_same_timeline.pl", next to the existing
same-timeline case.
---
 src/bin/pg_rewind/t/005_same_timeline.pl | 51 +++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_rewind/t/005_same_timeline.pl b/src/bin/pg_rewind/t/005_same_timeline.pl
index 95a40c3b270..b5367039f24 100644
--- a/src/bin/pg_rewind/t/005_same_timeline.pl
+++ b/src/bin/pg_rewind/t/005_same_timeline.pl
@@ -2,11 +2,13 @@
 # Copyright (c) 2021-2026, PostgreSQL Global Development Group
 
 #
-# Test that running pg_rewind with the source and target clusters
-# on the same timeline runs successfully.
+# Cases where pg_rewind needs no rewind.  Same timeline, and a target
+# that is a direct ancestor whose WAL stopped short of the divergence
+# LSN.  The latter used to trip Assert "target_wal_endrec == divergerec".
 #
 use strict;
 use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
 use PostgreSQL::Test::Utils;
 use Test::More;
 
@@ -15,10 +17,55 @@ use lib $FindBin::RealBin;
 
 use RewindTest;
 
+# Same timeline
 RewindTest::setup_cluster();
 RewindTest::start_primary();
 RewindTest::create_standby();
 RewindTest::run_pg_rewind('local');
 RewindTest::clean_rewind_test();
 
+# Direct ancestor whose WAL ends before divergence
+my $anc_primary = PostgreSQL::Test::Cluster->new('anc_primary');
+$anc_primary->init(allows_streaming => 1);
+$anc_primary->append_conf(
+	'postgresql.conf', qq(
+wal_keep_size = '64MB'
+wal_log_hints = on
+));
+$anc_primary->start;
+
+$anc_primary->safe_psql('postgres',
+	"CREATE TABLE early AS SELECT generate_series(1, 1000); CHECKPOINT;");
+
+my $anc_target = PostgreSQL::Test::Cluster->new('anc_target');
+$anc_primary->backup('early');
+$anc_target->init_from_backup($anc_primary, 'early');
+$anc_target->start;
+$anc_target->stop('fast');
+
+$anc_primary->safe_psql('postgres',
+	"CREATE TABLE late AS SELECT generate_series(1, 1000); CHECKPOINT;");
+
+my $anc_source = PostgreSQL::Test::Cluster->new('anc_source');
+$anc_primary->backup('late');
+$anc_source->init_from_backup($anc_primary, 'late', has_streaming => 1);
+$anc_source->start;
+$anc_primary->wait_for_catchup($anc_source);
+$anc_source->promote;
+
+command_checks_all(
+	[
+		'pg_rewind',
+		'--dry-run',
+		'--source-server' => $anc_source->connstr('postgres'),
+		'--target-pgdata' => $anc_target->data_dir,
+	],
+	0,
+	[qr//],
+	[qr/no rewind required/],
+	'pg_rewind reports "no rewind required" for ancestor target');
+
+$anc_primary->stop;
+$anc_source->stop;
+
 done_testing();
-- 
2.56.0

From baa752e1a27191848a20299fd0fc2e0b0b62fc93 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <[email protected]>
Date: Tue, 17 Nov 2026 02:12:33 +0700
Subject: [PATCH 1/2] Remove bogus Assert in pg_rewind ancestor check

If the target is a direct ancestor of the source, its WAL may end
before the divergence LSN, for example after an older base backup of
the primary.  Assert "target_wal_endrec == divergerec" then crashes
assertion builds.  Otherwise we already report that no rewind is
required.

Drop the Assert.
---
 src/bin/pg_rewind/pg_rewind.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index d2521dab333..fd5c0e031db 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -452,9 +452,11 @@ main(int argc, char **argv)
 		}
 		else
 		{
-			/* the last common checkpoint record must be part of target WAL */
-			Assert(target_wal_endrec == divergerec);
-
+			/*
+			 * Target stopped at or before the divergence point, so it is
+			 * an ancestor and needs no rewind.  Stopping earlier is fine
+			 * too, for example after an older base backup of the primary.
+			 */
 			rewind_needed = false;
 		}
 	}
-- 
2.56.0

Reply via email to