From: Soumyadeep Chakraborty <soumyadeep2007@gmail.com>
Date: Mon, 23 Nov 2026 11:50:21 -0800
Subject: [PATCH] Test for parent timeline reuse after interrupted promotion

During promotion, startup removes standby.signal and writes the child
timeline's history file before it writes the end-of-recovery record and
updates pg_control. If a crash or other unclean shutdown interrupts
promotion after standby.signal is durably removed but before pg_control
records the new timeline, the node can restart as a primary on the
parent timeline.

Pause promotion just after the history file is written, stop the standby
in immediate mode, and restart it. Startup comes up as a primary on the
parent timeline and pg_ctl rejects a second promotion request.

Commit a different row on each node and run pg_rewind with the old
primary as the target. Both nodes report the same timeline, so pg_rewind
reports "no rewind required" and exits successfully. On restart, the
target still has its own row and lacks the promoted node's row.

---

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fd37ea4f607f827072ea441e381befa8082f1c1d..8a4eb46d1a64ed8076122b389e43fb30cf8d36e8 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -6692,6 +6692,8 @@ StartupXLOG(void)
 		writeTimeLineHistory(newTLI, recoveryTargetTLI,
 							 EndOfLog, endOfRecoveryInfo->recoveryStopReason);
 
+		INJECTION_POINT("promotion-after-timeline-history", NULL);
+
 		ereport(LOG,
 				(errmsg("archive recovery complete")));
 	}
diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index cb3df6ed895023737566919bd86cdbf0261c6d95..e775254b1b1821bc8d535353d9cd440983cf3200 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -70,6 +70,7 @@ tests += {
       't/059_remote_apply_status_interval.pl',
       't/060_multixact_crashed_updater.pl',
       't/061_recovery_target_prefetch.pl',
+      't/062_interrupted_promotion.pl',
     ],
   },
 }
diff --git a/src/test/recovery/t/062_interrupted_promotion.pl b/src/test/recovery/t/062_interrupted_promotion.pl
new file mode 100644
index 0000000000000000000000000000000000000000..05764afd24a341f47ed428373389a197cec32ed0
--- /dev/null
+++ b/src/test/recovery/t/062_interrupted_promotion.pl
@@ -0,0 +1,64 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# The standby restarts on the old primary's timeline after an interrupted
+# promotion. pg_rewind then misses the divergence between the two nodes.
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $primary = PostgreSQL::Test::Cluster->new('primary');
+$primary->init(allows_streaming => 1);
+$primary->start;
+$primary->safe_psql('postgres', 'CREATE EXTENSION injection_points');
+$primary->safe_psql('postgres', 'CREATE TABLE branch (name text)');
+$primary->backup('backup');
+
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($primary, 'backup', has_streaming => 1);
+$standby->start;
+$primary->wait_for_replay_catchup($standby);
+$primary->stop;
+
+# Interrupt promotion after startup removes standby.signal and writes the
+# child timeline's history file. The end-of-recovery record is still
+# unwritten and pg_control still points to the parent timeline.
+my $point = 'promotion-after-timeline-history';
+$standby->safe_psql('postgres',
+	"SELECT injection_points_attach('$point', 'wait')");
+$standby->safe_psql('postgres', 'SELECT pg_promote(false)');
+$standby->wait_for_event('startup', $point);
+$standby->stop('immediate');
+$standby->start;
+
+# Startup came up as a primary on the parent timeline, so pg_ctl rejects
+# the second promotion request.
+command_ok(
+	[ 'pg_ctl', '--pgdata' => $standby->data_dir, 'promote' ],
+	'can retry the interrupted promotion after restart');
+
+# Commit a distinct row on each node so the target has a change to discard
+# and the source has a change the target must recover.
+$standby->safe_psql('postgres', q{INSERT INTO branch VALUES ('promoted')});
+$standby->stop;
+$primary->start;
+$primary->safe_psql('postgres', q{INSERT INTO branch VALUES ('old primary')});
+$primary->stop;
+
+# Both nodes report the same timeline ID. pg_rewind reports "no rewind
+# required" and exits successfully despite the divergent rows.
+is( system_log(
+		'pg_rewind',
+		'--source-pgdata' => $standby->data_dir,
+		'--target-pgdata' => $primary->data_dir),
+	0, 'pg_rewind exits successfully');
+
+$primary->start(options => '-p ' . $primary->port);
+# pg_rewind skipped the divergent WAL, so the target still reads
+# 'old primary' where it should read 'promoted'.
+is($primary->safe_psql('postgres', 'SELECT name FROM branch ORDER BY name'),
+	'promoted', 'rewound target contains only the promoted node\'s row');
+
+done_testing();
