From 1a8b75dce65bab25506b285bf2aa287fa1c24259 Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Mon, 16 Feb 2026 11:58:11 +0900 Subject: [PATCH v2] 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 Reviewed-by: Alexander Lakhin #fix --- src/test/recovery/t/009_twophase.pl | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/test/recovery/t/009_twophase.pl b/src/test/recovery/t/009_twophase.pl index 879e493b5b8..e0065a4a4ce 100644 --- a/src/test/recovery/t/009_twophase.pl +++ b/src/test/recovery/t/009_twophase.pl @@ -37,6 +37,8 @@ $node_london->append_conf( 'postgresql.conf', qq( max_prepared_transactions = 10 log_checkpoints = true + autovacuum = off + checkpoint_timeout = 1h )); $node_london->start; $node_london->backup('london_backup'); @@ -48,6 +50,8 @@ $node_paris->init_from_backup($node_london, 'london_backup', $node_paris->append_conf( 'postgresql.conf', qq( subtransaction_buffers = 32 + autovacuum = off + checkpoint_timeout = 1h )); $node_paris->start; @@ -63,6 +67,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 @@ -158,6 +172,14 @@ $cur_primary->psql( COMMIT PREPARED 'xact_009_6';"); $cur_primary->teardown_node; $cur_primary->start; + +# skip writing xl_running_xacts record to avoid random failures +if ($injection_points_supported != 0) +{ + $cur_primary->safe_psql('postgres', + "SELECT injection_points_attach('skip-log-running-xacts', 'error');"); +} + $psql_rc = $cur_primary->psql( 'postgres', " BEGIN; @@ -222,6 +244,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; @@ -230,6 +253,13 @@ note "Now paris is primary and london is standby"; ($cur_primary, $cur_standby) = ($node_paris, $node_london); $cur_primary_name = $cur_primary->name; +# skip writing xl_running_xacts record to avoid random failures +if ($injection_points_supported != 0) +{ + $cur_primary->safe_psql('postgres', + "SELECT injection_points_attach('skip-log-running-xacts', 'error');"); +} + # because london is not running at this point, we can't use syncrep commit # on this command $psql_rc = $cur_primary->psql('postgres', @@ -254,6 +284,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; @@ -263,6 +294,13 @@ note "Now london is primary and paris is standby"; ($cur_primary, $cur_standby) = ($node_london, $node_paris); $cur_primary_name = $cur_primary->name; +# skip writing xl_running_xacts record 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', "SELECT count(*) FROM pg_prepared_xacts", @@ -289,6 +327,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