From 306f570d4eff2e1437bce387da867a94ad25ae7b Mon Sep 17 00:00:00 2001
From: Shlok Kyal <shlok.kyal.oss@gmail.com>
Date: Wed, 15 Oct 2025 15:36:34 +0530
Subject: [PATCH v5 2/2] Add test for new stats for slot sync skip

---
 src/test/recovery/meson.build              |   3 +-
 src/test/recovery/t/049_slot_skip_stats.pl | 193 +++++++++++++++++++++
 2 files changed, 195 insertions(+), 1 deletion(-)
 create mode 100644 src/test/recovery/t/049_slot_skip_stats.pl

diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build
index 52993c32dbb..83a6c4b5c17 100644
--- a/src/test/recovery/meson.build
+++ b/src/test/recovery/meson.build
@@ -56,7 +56,8 @@ tests += {
       't/045_archive_restartpoint.pl',
       't/046_checkpoint_logical_slot.pl',
       't/047_checkpoint_physical_slot.pl',
-      't/048_vacuum_horizon_floor.pl'
+      't/048_vacuum_horizon_floor.pl',
+      't/049_slot_skip_stats.pl'
     ],
   },
 }
diff --git a/src/test/recovery/t/049_slot_skip_stats.pl b/src/test/recovery/t/049_slot_skip_stats.pl
new file mode 100644
index 00000000000..74e49751348
--- /dev/null
+++ b/src/test/recovery/t/049_slot_skip_stats.pl
@@ -0,0 +1,193 @@
+# Copyright (c) 2024-2025, PostgreSQL Global Development Group
+
+use strict;
+use warnings FATAL => 'all';
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+# Skip all tests if injection points are not supported in this build
+if ($ENV{enable_injection_points} ne 'yes')
+{
+	plan skip_all => 'Injection points not supported by this build';
+}
+
+# Initialize the primary cluster
+my $primary = PostgreSQL::Test::Cluster->new('publisher');
+$primary->init(allows_streaming => 'logical');
+$primary->append_conf(
+	'postgresql.conf', qq{
+autovacuum = off
+});
+$primary->start;
+
+# 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.
+if (!$primary->check_extension('injection_points'))
+{
+	plan skip_all => 'Extension injection_points not installed';
+}
+
+# Load the injection_points extension
+$primary->safe_psql('postgres', q(CREATE EXTENSION injection_points));
+
+# Take a backup of the primary for standby initialization
+my $backup_name = 'backup';
+$primary->backup($backup_name);
+
+# Initialize standby from primary backup
+my $standby1 = PostgreSQL::Test::Cluster->new('standby1');
+$standby1->init_from_backup($primary, $backup_name, has_streaming => 1);
+
+my $connstr = $primary->connstr;
+$standby1->append_conf(
+	'postgresql.conf', qq(
+hot_standby_feedback = on
+primary_slot_name = 'sb1_slot'
+primary_conninfo = '$connstr dbname=postgres'
+));
+
+# Create a physical replication slot on primary for standby
+$primary->safe_psql('postgres',
+	q{SELECT pg_create_physical_replication_slot('sb1_slot');});
+
+$standby1->start;
+
+# Create a logical replication slot on primary for testing
+$primary->safe_psql('postgres',
+	"SELECT pg_create_logical_replication_slot('slot_sync', 'test_decoding', false, false, true)"
+);
+
+# Wait for standby to catch up
+$primary->wait_for_replay_catchup($standby1);
+
+# Initial sync of replication slots
+$standby1->safe_psql('postgres', "SELECT pg_sync_replication_slots();");
+
+# Verify that initially there is no skip reason
+my $result = $standby1->safe_psql(
+	'postgres',
+	"SELECT slot_sync_skip_reason FROM pg_replication_slots
+     WHERE slot_name = 'slot_sync' AND synced"
+);
+is($result, 'none', "slot sync reason is none");
+
+# Hold walreciever so WAL is not flushed on standby
+my $standby_psql = $standby1->background_psql('postgres');
+$standby_psql->query_safe(
+	q(select injection_points_attach('walreceiver','wait')));
+
+# Advance the failover slot so that confirmed flush LSN of remote slot become
+# ahead of standby's flushed LSN
+$primary->safe_psql(
+	'postgres', qq(
+    CREATE TABLE t1(a int);
+    INSERT INTO t1 VALUES(1);
+));
+
+# Wait for backend to reach injection point
+$standby1->wait_for_event('walreceiver', 'walreceiver');
+
+$primary->safe_psql('postgres',
+	"SELECT pg_replication_slot_advance('slot_sync', pg_current_wal_lsn());");
+
+my ($stdout, $stderr);
+# Attempt to sync replication slots while standby is behind
+($result, $stdout, $stderr) =
+  $standby1->psql('postgres', "SELECT pg_sync_replication_slots();");
+
+# Check skip reason and count when standby is behind
+$result = $standby1->safe_psql(
+	'postgres',
+	"SELECT slot_sync_skip_reason FROM pg_replication_slots
+     WHERE slot_name = 'slot_sync' AND synced AND NOT temporary"
+);
+is($result, 'missing_wal_record', "slot sync skip when standby is behind");
+
+$result = $standby1->safe_psql('postgres',
+	"SELECT slot_sync_skip_count FROM pg_stat_replication_slots WHERE slot_name = 'slot_sync'"
+);
+is($result, '1', "check slot sync skip count");
+
+# Repeat sync to ensure skip count increments
+($result, $stdout, $stderr) =
+  $standby1->psql('postgres', "SELECT pg_sync_replication_slots();");
+
+$result = $standby1->safe_psql(
+	'postgres',
+	"SELECT slot_sync_skip_reason FROM pg_replication_slots
+     WHERE slot_name = 'slot_sync' AND synced AND NOT temporary"
+);
+is($result, 'missing_wal_record', "slot sync skip when standby is behind");
+
+$result = $standby1->safe_psql('postgres',
+	"SELECT slot_sync_skip_count FROM pg_stat_replication_slots WHERE slot_name = 'slot_sync'"
+);
+is($result, '2', "check slot sync skip count");
+
+# Detach injection point
+$standby1->safe_psql(
+	'postgres', q{
+	SELECT injection_points_detach('walreceiver');
+	SELECT injection_points_wakeup('walreceiver');
+});
+
+# Check that skip reason is reset after successful sync
+$standby1->safe_psql('postgres', "SELECT pg_sync_replication_slots();");
+$result = $standby1->safe_psql(
+	'postgres',
+	"SELECT slot_sync_skip_reason FROM pg_replication_slots
+     WHERE slot_name = 'slot_sync' AND synced AND NOT temporary"
+);
+is($result, 'none', "slot_sync_skip_reason is reset after successful sync");
+
+# Cleanup: drop the logical slot and ensure standby catches up
+$primary->safe_psql('postgres',
+	"SELECT pg_drop_replication_slot('slot_sync')");
+$primary->wait_for_replay_catchup($standby1);
+
+$standby1->safe_psql('postgres', "SELECT pg_sync_replication_slots();");
+
+# Test for case when slot sync is skipped when the remote slot is
+# behind the local slot.
+$primary->safe_psql('postgres',
+	"SELECT pg_create_logical_replication_slot('slot_sync', 'test_decoding', false, false, true)"
+);
+
+# Attach injection point to simulate wait
+$standby_psql->query_safe(
+	q(select injection_points_attach('slot-sync-skip','wait')));
+
+# Initiate sync of failover slots
+$standby_psql->query_until(
+	qr/slot_sync/,
+	q(
+\echo slot_sync
+select pg_sync_replication_slots();
+));
+
+# Wait for backend to reach injection point
+$standby1->wait_for_event('client backend', 'slot-sync-skip');
+
+# Logical slot is temporary and sync will skip because remote is behind
+$result = $standby1->safe_psql(
+	'postgres',
+	"SELECT slot_sync_skip_reason FROM pg_replication_slots
+     WHERE slot_name = 'slot_sync' AND synced AND temporary"
+);
+is($result, 'remote_behind', "slot sync skip as remote is behind");
+
+$result = $standby1->safe_psql('postgres',
+	"SELECT slot_sync_skip_count FROM pg_stat_replication_slots WHERE slot_name = 'slot_sync'"
+);
+is($result, '1', "check slot sync skip count");
+
+# Detach injection point
+$standby1->safe_psql(
+	'postgres', q{
+	SELECT injection_points_detach('slot-sync-skip');
+	SELECT injection_points_wakeup('slot-sync-skip');
+});
+
+done_testing();
-- 
2.34.1

