From 8657138424888ab25ac2f15f9819e994e7ff4a69 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 16 Feb 2026 11:58:11 +0900
Subject: [PATCH v1] Stabilize 009_twophase.pl

009_twophase.pl does switchover several times, but sometimes the old primary
could not follow the new primary, with the log like below:

```
LOG: fetching timeline history file for timeline 2 from primary server
LOG:  started streaming WAL from primary at 0/03000000 on timeline 1
LOG: replication terminated by primary server
DETAIL:  End of WAL reached on timeline 1 at 0/03022070.
LOG:  new timeline 2 forked off current database system timeline  1 before current recovery point 0/030220B8
LOG: restarted WAL streaming at 0/03000000 on timeline 1
LOG: replication terminated by primary server
```

This issue could occur for two reasons.

1) An old primary shuts down before all changes are replicated.
2) A background writer on the old primary generated the RUNNING_XACTS record,
and the node shut down before sending it.

This commit addresses both. Regarding the first issue, wait_for_replay_catchup()
has been added to ensure that all changes are replicated.
As for the second issue, the injection_points extension is used to suppress the
generation of RUNNING_XACTS records. For now, this test can run without
injection_points, but random failures can still occur in such environments.

Author: Hayato Kuroda <kuroda.hayato@fujitsu.com>
---
 src/test/recovery/t/009_twophase.pl | 34 +++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/src/test/recovery/t/009_twophase.pl b/src/test/recovery/t/009_twophase.pl
index 879e493b5b8..0b541c7bf31 100644
--- a/src/test/recovery/t/009_twophase.pl
+++ b/src/test/recovery/t/009_twophase.pl
@@ -63,6 +63,16 @@ my $cur_primary_name = $cur_primary->name;
 # Create table we'll use in the test transactions
 $cur_primary->psql('postgres', "CREATE TABLE t_009_tbl (id int, msg text)");
 
+# Check if the extension injection_points is available, as it may be
+# possible that this script is run with installcheck, where the module
+# would not be installed by default.
+my $injection_points_supported = $cur_primary->check_extension('injection_points');
+
+if ($injection_points_supported != 0)
+{
+	$cur_primary->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
+}
+
 ###############################################################################
 # Check that we can commit and abort transaction after soft restart.
 # Here checkpoint happens before shutdown and no WAL replay will occur at next
@@ -215,6 +225,13 @@ is($psql_out, '0',
 # Check that prepared transactions can be committed on promoted standby.
 ###############################################################################
 
+# skip writing xl_running_xacts record from here to avoid random failures
+if ($injection_points_supported != 0)
+{
+	$cur_primary->safe_psql('postgres',
+		"SELECT injection_points_attach('skip-log-running-xacts', 'error');");
+}
+
 $cur_primary->psql(
 	'postgres', "
 	BEGIN;
@@ -222,6 +239,7 @@ $cur_primary->psql(
 	SAVEPOINT s1;
 	INSERT INTO t_009_tbl VALUES (22, 'issued to ${cur_primary_name}');
 	PREPARE TRANSACTION 'xact_009_10';");
+$cur_primary->wait_for_replay_catchup($cur_standby);
 $cur_primary->teardown_node;
 $cur_standby->promote;
 
@@ -247,6 +265,13 @@ $cur_standby->start;
 # consistent.
 ###############################################################################
 
+# skip writing xl_running_xacts record from here to avoid random failures
+if ($injection_points_supported != 0)
+{
+	$cur_primary->safe_psql('postgres',
+		"SELECT injection_points_attach('skip-log-running-xacts', 'error');");
+}
+
 $cur_primary->psql(
 	'postgres', "
 	BEGIN;
@@ -254,6 +279,7 @@ $cur_primary->psql(
 	SAVEPOINT s1;
 	INSERT INTO t_009_tbl VALUES (24, 'issued to ${cur_primary_name}');
 	PREPARE TRANSACTION 'xact_009_11';");
+$cur_primary->wait_for_replay_catchup($cur_standby);
 $cur_primary->stop;
 $cur_standby->restart;
 $cur_standby->promote;
@@ -281,6 +307,13 @@ $cur_primary->psql('postgres', "COMMIT PREPARED 'xact_009_11'");
 # restart while primary is down.
 ###############################################################################
 
+# skip writing xl_running_xacts record from here to avoid random failures
+if ($injection_points_supported != 0)
+{
+	$cur_primary->safe_psql('postgres',
+		"SELECT injection_points_attach('skip-log-running-xacts', 'error');");
+}
+
 $cur_primary->psql(
 	'postgres', "
 	BEGIN;
@@ -289,6 +322,7 @@ $cur_primary->psql(
 	INSERT INTO t_009_tbl VALUES (26, 'issued to ${cur_primary_name}');
 	PREPARE TRANSACTION 'xact_009_12';
 	");
+$cur_primary->wait_for_replay_catchup($cur_standby);
 $cur_primary->stop;
 $cur_standby->teardown_node;
 $cur_standby->start;
-- 
2.47.3

