From 316ee3b4605eb70df02dfb215e85841f8f285682 Mon Sep 17 00:00:00 2001
From: Stepan Neretin <slpmcf@gmail.com>
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

