From 960bb34c8b1551f4061d13f9102837091d27f12f Mon Sep 17 00:00:00 2001
From: Hou Zhijie <houzj.fnst@cn.fujitsu.com>
Date: Mon, 8 Apr 2024 14:06:03 +0800
Subject: [PATCH v1] stablize the test

---
 .../t/040_standby_failover_slots_sync.pl      | 56 +++++++------------
 1 file changed, 20 insertions(+), 36 deletions(-)

diff --git a/src/test/recovery/t/040_standby_failover_slots_sync.pl b/src/test/recovery/t/040_standby_failover_slots_sync.pl
index 3234b1ad7d..f5ee7d4fec 100644
--- a/src/test/recovery/t/040_standby_failover_slots_sync.pl
+++ b/src/test/recovery/t/040_standby_failover_slots_sync.pl
@@ -127,7 +127,7 @@ ok( $stderr =~
 # Configure standby1 to replicate and synchronize logical slots configured
 # for failover on the primary
 #
-#              failover slot lsub1_slot ->| ----> subscriber1 (connected via logical replication)
+#              failover slot lsub1_slot   |       inactive
 #              failover slot lsub2_slot   |       inactive
 # primary --->                            |
 #              physical slot sb1_slot --->| ----> standby1 (connected via streaming replication)
@@ -159,6 +159,16 @@ log_min_messages = 'debug2'
 $primary->append_conf('postgresql.conf', "log_min_messages = 'debug2'");
 $primary->reload;
 
+# Drop the subscription to prevent further advancement of the restart_lsn for
+# the lsub1_slot.
+$subscriber1->safe_psql('postgres', "DROP SUBSCRIPTION regress_mysub1;");
+
+# To ensure that restart_lsn has moved to a recent WAL position, we re-create
+# the lsub1_slot.
+$primary->psql('postgres',
+	q{SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true);}
+);
+
 $primary->psql('postgres',
 	q{SELECT pg_create_logical_replication_slot('lsub2_slot', 'test_decoding', false, false, true);}
 );
@@ -169,25 +179,13 @@ $primary->psql('postgres',
 # Start the standby so that slot syncing can begin
 $standby1->start;
 
-$primary->wait_for_catchup('regress_mysub1');
-
-# Do not allow any further advancement of the restart_lsn for the lsub1_slot.
-$subscriber1->safe_psql('postgres',
-	"ALTER SUBSCRIPTION regress_mysub1 DISABLE");
-
-# Wait for the replication slot to become inactive on the publisher
-$primary->poll_query_until(
-	'postgres',
-	"SELECT COUNT(*) FROM pg_catalog.pg_replication_slots WHERE slot_name = 'lsub1_slot' AND active = 'f'",
-	1);
-
 # Capture the inactive_since of the slot from the primary. Note that the slot
-# will be inactive since the corresponding subscription is disabled.
+# will be inactive since the corresponding subscription was dropped.
 my $inactive_since_on_primary =
 	$primary->validate_slot_inactive_since('lsub1_slot', $slot_creation_time_on_primary);
 
 # Wait for the standby to catch up so that the standby is not lagging behind
-# the subscriber.
+# the failover slots.
 $primary->wait_for_replay_catchup($standby1);
 
 # Synchronize the primary server slots to the standby.
@@ -262,39 +260,26 @@ $standby1->append_conf('postgresql.conf', 'max_slot_wal_keep_size = -1');
 $standby1->reload;
 
 # Capture the time before the logical failover slot is created on the primary.
-# Note that the subscription creates the slot again on the primary.
 $slot_creation_time_on_primary = $publisher->safe_psql(
 	'postgres', qq[
     SELECT current_timestamp;
 ]);
 
 # To ensure that restart_lsn has moved to a recent WAL position, we re-create
-# the subscription and the logical slot.
-$subscriber1->safe_psql(
+# the lsub1_slot.
+$primary->safe_psql(
 	'postgres', qq[
-	DROP SUBSCRIPTION regress_mysub1;
-	CREATE SUBSCRIPTION regress_mysub1 CONNECTION '$publisher_connstr' PUBLICATION regress_mypub WITH (slot_name = lsub1_slot, copy_data = false, failover = true);
+	SELECT pg_drop_replication_slot('lsub1_slot');
+	SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true);
 ]);
 
-$primary->wait_for_catchup('regress_mysub1');
-
-# Do not allow any further advancement of the restart_lsn for the lsub1_slot.
-$subscriber1->safe_psql('postgres',
-	"ALTER SUBSCRIPTION regress_mysub1 DISABLE");
-
-# Wait for the replication slot to become inactive on the publisher
-$primary->poll_query_until(
-	'postgres',
-	"SELECT COUNT(*) FROM pg_catalog.pg_replication_slots WHERE slot_name = 'lsub1_slot' AND active = 'f'",
-	1);
-
 # Capture the inactive_since of the slot from the primary. Note that the slot
-# will be inactive since the corresponding subscription is disabled.
+# will be inactive since the corresponding subscription was dropped.
 $inactive_since_on_primary =
 	$primary->validate_slot_inactive_since('lsub1_slot', $slot_creation_time_on_primary);
 
 # Wait for the standby to catch up so that the standby is not lagging behind
-# the subscriber.
+# the failover slots.
 $primary->wait_for_replay_catchup($standby1);
 
 my $log_offset = -s $standby1->logfile;
@@ -571,8 +556,7 @@ $primary->safe_psql(
 $subscriber1->safe_psql(
 	'postgres', qq[
 	CREATE TABLE tab_int (a int PRIMARY KEY);
-	ALTER SUBSCRIPTION regress_mysub1 ENABLE;
-	ALTER SUBSCRIPTION regress_mysub1 REFRESH PUBLICATION;
+	CREATE SUBSCRIPTION regress_mysub1 CONNECTION '$publisher_connstr' PUBLICATION regress_mypub WITH (slot_name = lsub1_slot, failover = true, create_slot = false);
 ]);
 
 $subscriber1->wait_for_subscription_sync;
-- 
2.30.0.windows.2

