Hi all,

I noticed two places in slotfuncs.c that reimplement helpers we already
have.

copy_replication_slot() scans ReplicationSlotCtl->replication_slots[]
itself to find the source slot. SearchNamedReplicationSlot() performs
exactly that lookup, and the caller already holds
ReplicationSlotControlLock, so it can be called with
need_lock = false.

pg_get_replication_slots() compares slot_contents.data.database with
InvalidOid three times to distinguish physical from logical slots.
SlotIsPhysical() expresses the same condition, and the same function
already uses this macro elsewhere.

No functional change. make check passes, and the patch applies to
master at cb94cb41. pgindent is clean. No backpatch is intended.

Comments welcome.

Regards,
Tristan Su
From a9cb713b8d4e83a5e6259aef8b4b0a4fd03001e2 Mon Sep 17 00:00:00 2001
From: Tristan Su <[email protected]>
Date: Tue, 1 Dec 2026 20:24:03 +0800
Subject: [PATCH] Reuse slot helpers in pg_get_replication_slots and slot copy

Use SearchNamedReplicationSlot() instead of open-coding the shared
memory scan in copy_replication_slot(), and use SlotIsPhysical()
instead of testing data.database directly in
pg_get_replication_slots().

No functional change.
---
 src/backend/replication/slotfuncs.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/backend/replication/slotfuncs.c 
b/src/backend/replication/slotfuncs.c
index fdeb6a23d7b..0fbdabc48ad 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -304,17 +304,17 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
                i = 0;
                values[i++] = NameGetDatum(&slot_contents.data.name);
 
-               if (slot_contents.data.database == InvalidOid)
+               if (SlotIsPhysical(&slot_contents))
                        nulls[i++] = true;
                else
                        values[i++] = NameGetDatum(&slot_contents.data.plugin);
 
-               if (slot_contents.data.database == InvalidOid)
+               if (SlotIsPhysical(&slot_contents))
                        values[i++] = CStringGetTextDatum("physical");
                else
                        values[i++] = CStringGetTextDatum("logical");
 
-               if (slot_contents.data.database == InvalidOid)
+               if (SlotIsPhysical(&slot_contents))
                        nulls[i++] = true;
                else
                        values[i++] = 
ObjectIdGetDatum(slot_contents.data.database);
@@ -675,19 +675,14 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool 
logical_slot)
         * managed to create the new slot, we advance the new slot's restart_lsn
         * to the source slot's updated restart_lsn the second time we lock it.
         */
-       for (int i = 0; i < max_replication_slots + 
max_repack_replication_slots; i++)
-       {
-               ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i];
+       src = SearchNamedReplicationSlot(NameStr(*src_name), false);
 
-               if (s->in_use && strcmp(NameStr(s->data.name), 
NameStr(*src_name)) == 0)
-               {
-                       /* Copy the slot contents while holding spinlock */
-                       SpinLockAcquire(&s->mutex);
-                       first_slot_contents = *s;
-                       SpinLockRelease(&s->mutex);
-                       src = s;
-                       break;
-               }
+       if (src != NULL)
+       {
+               /* Copy the slot contents while holding spinlock */
+               SpinLockAcquire(&src->mutex);
+               first_slot_contents = *src;
+               SpinLockRelease(&src->mutex);
        }
 
        LWLockRelease(ReplicationSlotControlLock);
-- 
2.53.0

Reply via email to