From 541bd745ea85cc0409a166eec98c0e9a4dd0868a Mon Sep 17 00:00:00 2001
From: Shlok Kyal <shlok.kyal.oss@gmail.com>
Date: Wed, 24 Sep 2025 15:26:37 +0530
Subject: [PATCH v4 2/2] Add test for new stats for slot sync skip

---
 src/backend/replication/logical/slotsync.c |   5 +
 src/test/recovery/meson.build              |   3 +-
 src/test/recovery/t/049_slot_skip_stats.pl | 180 +++++++++++++++++++++
 3 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 src/test/recovery/t/049_slot_skip_stats.pl

diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index 6259fad894c..4359120165e 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -64,6 +64,7 @@
 #include "storage/procarray.h"
 #include "tcop/tcopprot.h"
 #include "utils/builtins.h"
+#include "utils/injection_point.h"
 #include "utils/pg_lsn.h"
 #include "utils/ps_status.h"
 #include "utils/timeout.h"
@@ -994,6 +995,10 @@ synchronize_slots(WalReceiverConn *wrconn)
 	if (started_tx)
 		CommitTransactionCommand();
 
+#ifdef USE_INJECTION_POINTS
+	INJECTION_POINT("slot-sync-skip", NULL);
+#endif
+
 	return some_slot_updated;
 }
 
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..3aa63911207
--- /dev/null
+++ b/src/test/recovery/t/049_slot_skip_stats.pl
@@ -0,0 +1,180 @@
+# 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',
+	auth_extra => [ '--create-role' => 'repl_role' ]);
+$primary->append_conf(
+	'postgresql.conf', qq{
+autovacuum = off
+max_prepared_transactions = 1
+});
+$primary->start;
+
+# 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,
+	has_restoring => 1);
+
+my $connstr_1 = $primary->connstr;
+$standby1->append_conf(
+	'postgresql.conf', qq(
+hot_standby_feedback = on
+primary_slot_name = 'sb1_slot'
+primary_conninfo = '$connstr_1 dbname=postgres'
+));
+
+# Create a physical replication slot on primary for standby
+$primary->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");
+
+# Simulate standby connection failure by modifying pg_hba.conf
+unlink($primary->data_dir . '/pg_hba.conf');
+$primary->append_conf('pg_hba.conf',
+	qq{local   all             all                                     trust}
+);
+$primary->restart;
+
+# 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);
+));
+$primary->safe_psql('postgres',
+	"SELECT pg_replication_slot_advance('slot_sync', pg_current_wal_lsn());");
+
+# Attempt to sync replication slots while standby is behind
+$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
+$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");
+
+# Restore connectivity between primary and standby
+unlink($primary->data_dir . '/pg_hba.conf');
+$primary->append_conf(
+	'pg_hba.conf',
+	qq{
+local   all             all                                     trust
+local   replication     all                                     trust
+});
+$primary->restart;
+
+# 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();");
+
+# Create a new logical slot for testing injection point
+$primary->safe_psql('postgres',
+	"SELECT pg_create_logical_replication_slot('slot_sync', 'test_decoding', false, false, true)"
+);
+
+# Attach injection point to simulate wait
+my $standby_psql = $standby1->background_psql('postgres');
+$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

