From a9cb713b8d4e83a5e6259aef8b4b0a4fd03001e2 Mon Sep 17 00:00:00 2001 From: Tristan Su 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