From 41350e61f9d0a5060a7a76fc1de91ba944f4328c Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Tue, 16 Jun 2026 18:15:35 +0800 Subject: [PATCH v2_PG18] Avoid stale slot access after dropping obsolete synced slots drop_local_obsolete_slots() kept using local_slot after calling ReplicationSlotDropAcquired(). Once the drop completes, the slot array entry can be reused by another backend, so later reads of local_slot->data could refer to a different slot. Copy the slot name and database OID before dropping the slot, and use those saved values for unlocking and logging after the drop. Author: Xuneng Zhou Reviewed-by: Zhijie Hou Reviewed-by: Amit Kapila --- src/backend/replication/logical/slotsync.c | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index bc42d74fec2..c4dda8aa5f1 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -463,6 +463,7 @@ drop_local_obsolete_slots(List *remote_slot_list) /* Drop the local slot if it is not required to be retained. */ if (!local_sync_slot_required(local_slot, remote_slot_list)) { + Oid slot_database = local_slot->data.database; bool synced_slot; /* @@ -470,8 +471,8 @@ drop_local_obsolete_slots(List *remote_slot_list) * ReplicationSlotsDropDBSlots(), trying to drop the same slot * during a drop-database operation. */ - LockSharedObject(DatabaseRelationId, local_slot->data.database, - 0, AccessShareLock); + LockSharedObject(DatabaseRelationId, slot_database, 0, + AccessShareLock); /* * In the small window between getting the slot to drop and @@ -488,17 +489,19 @@ drop_local_obsolete_slots(List *remote_slot_list) if (synced_slot) { - ReplicationSlotAcquire(NameStr(local_slot->data.name), true, false); + NameData slot_name = local_slot->data.name; + + ReplicationSlotAcquire(NameStr(slot_name), true, false); ReplicationSlotDropAcquired(); - } - UnlockSharedObject(DatabaseRelationId, local_slot->data.database, - 0, AccessShareLock); + ereport(LOG, + errmsg("dropped replication slot \"%s\" of database with OID %u", + NameStr(slot_name), + slot_database)); + } - ereport(LOG, - errmsg("dropped replication slot \"%s\" of database with OID %u", - NameStr(local_slot->data.name), - local_slot->data.database)); + UnlockSharedObject(DatabaseRelationId, slot_database, 0, + AccessShareLock); } } } -- 2.43.0