From ced53920dbdc77215c7121cde165c2d500f85253 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Thu, 7 Sep 2023 16:00:20 +0900
Subject: [PATCH v1] Stabilize subscription stats test.

The new test added by commits 68a59f9e9 et al disables the
subscription and manually drops the associated replication
slot. However, since disabling the subsubscription doesn't wait for a
walsender to release the replication slot and exit,
pg_drop_replication_slot() could fail. Avoid failure by adding a wait
for the replication slot to become inactive.

Reported-by: Hou Zhijie, as per buildfarm
Reviewed-by: Hou Zhijie
Discussion: https://postgr.es/m/OS0PR01MB571682316378379AA34854F694E9A%40OS0PR01MB5716.jpnprd01.prod.outlook.com
Backpatch-through: 15
---
 src/test/subscription/t/026_stats.pl | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/test/subscription/t/026_stats.pl b/src/test/subscription/t/026_stats.pl
index 0bcda006cd..61d9578b65 100644
--- a/src/test/subscription/t/026_stats.pl
+++ b/src/test/subscription/t/026_stats.pl
@@ -285,6 +285,13 @@ is( $node_subscriber->safe_psql(
 		$db, qq(SELECT pg_stat_have_stats('subscription', 0, $sub2_oid))),
 	qq(f),
 	qq(Subscription stats for subscription '$sub2_name' should be removed.));
+
+# Since disabling subscription doesn't wait for walsender to release the replication
+# slot and exit, wait for the slot to become inactive.
+$node_publisher->poll_query_until($db,
+	qq(SELECT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = '$sub2_name' AND active_pid IS NULL))
+) or die "slot never became inactive";
+
 $node_publisher->safe_psql($db,
 	qq(SELECT pg_drop_replication_slot('$sub2_name')));
 
-- 
2.31.1

