0001-top-up-changes.patch.txt

text/plain

Filename: 0001-top-up-changes.patch.txt
Type: text/plain
Part: 0
Message: Re: [PATCH] Release replication slot on error in SQL-callable slot functions
From fd4e8f731fbfa5ccef88338d6fa39fe2b2a1820b Mon Sep 17 00:00:00 2001
From: Shveta Malik <shveta.malik@gmail.com>
Date: Wed, 27 May 2026 09:18:50 +0530
Subject: [PATCH] top-up changes

---
 contrib/test_decoding/sql/slot.sql  | 11 +++++++++--
 src/backend/replication/slotfuncs.c |  3 +++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/contrib/test_decoding/sql/slot.sql b/contrib/test_decoding/sql/slot.sql
index eabbe05cc90..d8e0adccbfb 100644
--- a/contrib/test_decoding/sql/slot.sql
+++ b/contrib/test_decoding/sql/slot.sql
@@ -206,14 +206,18 @@ EXCEPTION WHEN OTHERS THEN
     RAISE NOTICE 'caught: %', SQLERRM;
 END;
 $$;
+
+-- the concerned slot must not exist (it was dropped on error)
 SELECT count(*) = 0 AS slot_was_dropped FROM pg_replication_slots
     WHERE slot_name = 'regression_slot_error';
 
-SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t3', 'test_decoding', true);
+-- the session is still usable
+SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t3', 'test_decoding', false);
 
 -- pg_replication_slot_advance: error after acquiring the slot should
 -- release it so the session stays usable.
 SELECT slot_name FROM pg_replication_slot_advance('regression_slot_t3', pg_current_wal_lsn());
+
 DO $$
 BEGIN
     PERFORM pg_replication_slot_advance('regression_slot_t3', '0/1');
@@ -221,7 +225,8 @@ EXCEPTION WHEN OTHERS THEN
     RAISE NOTICE 'caught expected error';
 END;
 $$;
--- the session is still healthy
+
+-- the session is still usable
 SELECT slot_name FROM pg_replication_slot_advance('regression_slot_t3', pg_current_wal_lsn());
 
 -- pg_copy_logical_replication_slot: error after creating the destination
@@ -233,9 +238,11 @@ EXCEPTION WHEN OTHERS THEN
     RAISE NOTICE 'caught: %', SQLERRM;
 END;
 $$;
+
 -- the destination slot must not exist (it was dropped on error)
 SELECT count(*) = 0 AS dst_slot_dropped FROM pg_replication_slots
     WHERE slot_name = 'regression_slot_dst';
+
 -- the session is still usable
 SELECT count(*) >= 0 AS changes_ok FROM pg_logical_slot_get_changes('regression_slot_t3', NULL, NULL);
 
diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c
index 9ba9bd892e6..d32e9187b95 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -194,6 +194,7 @@ create_logical_replication_slot(char *name, char *plugin,
 		 */
 		if (MyReplicationSlot != NULL)
 			ReplicationSlotDropAcquired();
+
 		PG_RE_THROW();
 	}
 	PG_END_TRY();
@@ -633,6 +634,7 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
 	{
 		if (MyReplicationSlot != NULL)
 			ReplicationSlotRelease();
+
 		PG_RE_THROW();
 	}
 	PG_END_TRY();
@@ -896,6 +898,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
 		 */
 		if (MyReplicationSlot != NULL)
 			ReplicationSlotDropAcquired();
+
 		PG_RE_THROW();
 	}
 	PG_END_TRY();
-- 
2.34.1