From 79afaee0d8b270e65ca8774c30303e97b23ea110 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 20 Nov 2026 18:11:48 +0900
Subject: [PATCH 2/3] Use BackgroundPsql::quit(1) to wait for

---
 .../t/010_index_concurrently_upsert.pl        |  2 +-
 .../recovery/t/051_effective_wal_level.pl     | 37 +++----------------
 2 files changed, 7 insertions(+), 32 deletions(-)

diff --git a/src/test/modules/test_misc/t/010_index_concurrently_upsert.pl b/src/test/modules/test_misc/t/010_index_concurrently_upsert.pl
index 50a0e7db8f7..db3d6d8d19a 100644
--- a/src/test/modules/test_misc/t/010_index_concurrently_upsert.pl
+++ b/src/test/modules/test_misc/t/010_index_concurrently_upsert.pl
@@ -944,7 +944,7 @@ sub safe_quit
 	$stderr =~ s/$banner_match//;
 
 	# Close the session
-	$session->quit;
+	$session->quit(1);
 
 	return $stderr;
 }
diff --git a/src/test/recovery/t/051_effective_wal_level.pl b/src/test/recovery/t/051_effective_wal_level.pl
index 5129e6eb638..ffb817b5b1e 100644
--- a/src/test/recovery/t/051_effective_wal_level.pl
+++ b/src/test/recovery/t/051_effective_wal_level.pl
@@ -32,19 +32,6 @@ sub wait_for_logical_decoding_disabled
 		qq[select current_setting('effective_wal_level') = 'replica';]);
 }
 
-# Close a background psql session and wait for its server backend to finish
-# exit processing so that its local injection point names can be safely reused.
-sub quit_and_wait
-{
-	my ($node, $background_psql, $backend_pid) = @_;
-
-	die "invalid backend PID: $backend_pid" unless $backend_pid =~ /^\d+$/;
-	$background_psql->quit;
-	$node->poll_query_until('postgres',
-		qq[select count(*) = 0 from pg_stat_activity where pid = $backend_pid]
-	) or die "timed out waiting for backend $backend_pid to exit";
-}
-
 # Initialize the primary server with wal_level = 'replica'.
 my $primary = PostgreSQL::Test::Cluster->new('primary');
 $primary->init(allows_streaming => 1);
@@ -429,8 +416,6 @@ if (   $ENV{enable_injection_points} eq 'yes'
 
 	# Start a psql session and stop it in the middle of the activation process.
 	my $psql_create_slot = $primary->background_psql('postgres');
-	my $psql_create_slot_pid =
-	  $psql_create_slot->query_safe('select pg_backend_pid()');
 	$psql_create_slot->query_until(
 		qr/create_slot_1/,
 		q(\echo create_slot_1
@@ -458,7 +443,7 @@ select pg_create_logical_replication_slot('slot_1', 'test_decoding');
 		qq[select injection_points_wakeup('logical-decoding-activation')]);
 
 	# Let the released backend finish creating its slot.
-	quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
+	$psql_create_slot->quit(1);
 
 	# Decode from slot_2, whose start point precedes where a redundant
 	# status-change record would have been written; this fails in xlog_decode()
@@ -485,8 +470,6 @@ select pg_drop_replication_slot('slot_2');
 	# interrupted.
 	$psql_create_slot =
 	  $primary->background_psql('postgres', on_error_stop => 0);
-	$psql_create_slot_pid =
-	  $psql_create_slot->query_safe('select pg_backend_pid()');
 
 	# Start the logical decoding activation process upon creating the logical
 	# slot, but it will wait due to the injection point.
@@ -514,13 +497,11 @@ select pg_cancel_backend(pid) from pg_stat_activity where query ~ 'slot_canceled
 	wait_for_logical_decoding_disabled($primary);
 	pass("the activation process aborted");
 
-	quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
+	$psql_create_slot->quit(1);
 
 	# Test concurrent activation processes run and one is interrupted.
 	$psql_create_slot =
 	  $primary->background_psql('postgres', on_error_stop => 0);
-	$psql_create_slot_pid =
-	  $psql_create_slot->query_safe('select pg_backend_pid()');
 
 	# Start a psql session and stops in the middle of the activation
 	# process.
@@ -559,7 +540,7 @@ select pg_cancel_backend(pid) from pg_stat_activity where query ~ 'slot_canceled
 		"effective_wal_level remains 'logical' even after the concurrent activation is interrupted"
 	);
 
-	quit_and_wait($primary, $psql_create_slot, $psql_create_slot_pid);
+	$psql_create_slot->quit(1);
 
 	# Test races between the deactivation of logical decoding and slot
 	# creation during recovery. Logical decoding can be deactivated while a
@@ -615,8 +596,6 @@ hot_standby_feedback = on
 	# information but before creating the local slot.  Note that this point
 	# waits when reaching the creation of slot "sync_slot".
 	my $psql_sync_slot = $standby5->background_psql('postgres');
-	my $psql_sync_slot_pid =
-	  $psql_sync_slot->query_safe('select pg_backend_pid()');
 	$psql_sync_slot->query_until(
 		qr/sync_slots/,
 		q(\echo sync_slots
@@ -643,7 +622,7 @@ select pg_sync_replication_slots();
 		qq[select injection_points_wakeup('replication-slot-create-begin')]);
 	$standby5->wait_for_log(
 		qr/could not synchronize replication slot "sync_slot"/, $log_offset);
-	quit_and_wait($standby5, $psql_sync_slot, $psql_sync_slot_pid);
+	$psql_sync_slot->quit(1);
 	is( $standby5->safe_psql(
 			'postgres', qq[select count(*) from pg_replication_slots]),
 		'0',
@@ -664,8 +643,6 @@ select pg_sync_replication_slots();
 	);
 
 	$psql_sync_slot = $standby5->background_psql('postgres');
-	$psql_sync_slot_pid =
-	  $psql_sync_slot->query_safe('select pg_backend_pid()');
 	$psql_sync_slot->query_until(
 		qr/sync_slots/,
 		q(\echo sync_slots
@@ -719,7 +696,7 @@ select pg_create_logical_replication_slot('test_slot4', 'test_decoding');
 select pg_drop_replication_slot('sync_slot');
 ]);
 	$primary->wait_for_replay_catchup($standby5);
-	quit_and_wait($standby5, $psql_sync_slot, $psql_sync_slot_pid);
+	$psql_sync_slot->quit(1);
 
 	# The slot created on retry may or may not have been persisted by the
 	# time the remote slot was dropped. One more synchronization cycle
@@ -757,8 +734,6 @@ select pg_drop_replication_slot('sync_slot');
 	# Start creating a logical slot, stopping it before creating the slot.
 	$psql_create_slot =
 	  $standby5->background_psql('postgres', on_error_stop => 0);
-	$psql_create_slot_pid =
-	  $psql_create_slot->query_safe('select pg_backend_pid()');
 	# Note that this point waits when reaching the creation of slot
 	# "standby5_slot".
 	$psql_create_slot->query_until(
@@ -786,7 +761,7 @@ select pg_create_logical_replication_slot('standby5_slot', 'test_decoding');
 	$standby5->wait_for_log(
 		qr/ERROR: .* logical decoding on standby requires "effective_wal_level" >= "logical" on the primary/,
 		$log_offset);
-	quit_and_wait($standby5, $psql_create_slot, $psql_create_slot_pid);
+	$psql_create_slot->quit(1);
 	is( $standby5->safe_psql(
 			'postgres', qq[select count(*) from pg_replication_slots]),
 		'0',
-- 
2.52.0

