From 36eb55908687fb3bc64c7bdb2e955755966751b1 Mon Sep 17 00:00:00 2001 From: Tristan Su Date: Tue, 1 Dec 2026 20:24:03 +0800 Subject: [PATCH v2] 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 | 32 +++++++++++------------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index fdeb6a23d7b..76f16fad7f4 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); @@ -639,7 +639,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) { Name src_name = PG_GETARG_NAME(0); Name dst_name = PG_GETARG_NAME(1); - ReplicationSlot *src = NULL; + ReplicationSlot *src; ReplicationSlot first_slot_contents; ReplicationSlot second_slot_contents; XLogRecPtr src_restart_lsn; @@ -675,28 +675,20 @@ 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]; - - 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; - } - } - - LWLockRelease(ReplicationSlotControlLock); + src = SearchNamedReplicationSlot(NameStr(*src_name), false); if (src == NULL) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("replication slot \"%s\" does not exist", NameStr(*src_name)))); + /* Copy the slot contents while holding spinlock */ + SpinLockAcquire(&src->mutex); + first_slot_contents = *src; + SpinLockRelease(&src->mutex); + + LWLockRelease(ReplicationSlotControlLock); + src_islogical = SlotIsLogical(&first_slot_contents); src_restart_lsn = first_slot_contents.data.restart_lsn; temporary = (first_slot_contents.data.persistency == RS_TEMPORARY); -- 2.53.0