From cf451c172d72d84bdfb8828b092cf5774b7930d7 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Wed, 2 Dec 2026 09:43:59 +0800 Subject: [PATCH v3] Fix race between copying and dropping a replication slot. copy_replication_slot() fetches the source slot's initial metadata under ReplicationSlotControlLock, releases the lock while creating the destination slot, and then re-reads the source slot to copy its latest LSN and xmin values and verify that it still exists. However, the existence check missed two cases when the source slot was concurrently dropped before the destination slot reserved its resources. First, it did not check the source slot's in_use flag. Because ReplicationSlotDropPtr() clears in_use without zeroing the slot's persistent data, copy_replication_slot() failed to detect the drop when the destination slot was allocated in a different array entry (for example, an earlier free entry). Second, when the destination slot had the same name as the source slot, the destination slot itself could be allocated in the freed source slot entry with in_use set and the same slot name, also bypassing the check. Once the source slot was dropped, checkpoints or VACUUM could remove WAL segments or catalog tuples still referenced by the stale restart_lsn and xmin values, causing an assertion failure or leaving a corrupted copied slot. Fix this by rejecting attempts to copy a replication slot to itself upfront and checking that second_slot_contents.in_use is still true when revalidating the source slot. With self-copy disallowed, any reuse of the freed source slot entry by the destination slot has a different slot name and is caught by the existing name check. While at it, clarify the nearby comment that erroring out does not drop a persistent physical destination slot. Author: Zhijie Hou Reviewed-by: shveta malik Reviewed-by: Masahiko Sawada Discussion: https://postgr.es/m/3abb64fe-d494-4960-83dc-30e48ac82729@gmail.com Backpatch-through: 15 --- contrib/test_decoding/expected/slot.out | 6 ++++++ contrib/test_decoding/sql/slot.sql | 6 ++++++ src/backend/replication/slotfuncs.c | 24 +++++++++++++++++------- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/contrib/test_decoding/expected/slot.out b/contrib/test_decoding/expected/slot.out index 6faeebfd8ab..3354671e343 100644 --- a/contrib/test_decoding/expected/slot.out +++ b/contrib/test_decoding/expected/slot.out @@ -224,6 +224,9 @@ ORDER BY o.slot_name, c.slot_name; SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error ERROR: all replication slots are in use HINT: Free one or increase max_replication_slots. +-- Cannot copy a logical slot to itself +SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'orig_slot1'); -- error +ERROR: cannot copy replication slot "orig_slot1" to itself -- temporary slots were dropped automatically SELECT pg_drop_replication_slot('orig_slot1'); pg_drop_replication_slot @@ -338,6 +341,9 @@ ERROR: cannot copy logical replication slot "orig_slot1" as a physical replicat -- Cannot copy a physical slot that doesn't reserve WAL SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error ERROR: cannot copy a replication slot that doesn't reserve WAL +-- Cannot copy a physical slot to itself +SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'orig_slot1'); -- error +ERROR: cannot copy replication slot "orig_slot1" to itself -- temporary slots were dropped automatically SELECT pg_drop_replication_slot('orig_slot1'); pg_drop_replication_slot diff --git a/contrib/test_decoding/sql/slot.sql b/contrib/test_decoding/sql/slot.sql index f4d2b0cd557..7c2e22ee015 100644 --- a/contrib/test_decoding/sql/slot.sql +++ b/contrib/test_decoding/sql/slot.sql @@ -110,6 +110,9 @@ ORDER BY o.slot_name, c.slot_name; -- released even when raise error during creating the target slot. SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error +-- Cannot copy a logical slot to itself +SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'orig_slot1'); -- error + -- temporary slots were dropped automatically SELECT pg_drop_replication_slot('orig_slot1'); SELECT pg_drop_replication_slot('copied_slot1_no_change'); @@ -156,6 +159,9 @@ SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- -- Cannot copy a physical slot that doesn't reserve WAL SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error +-- Cannot copy a physical slot to itself +SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'orig_slot1'); -- error + -- temporary slots were dropped automatically SELECT pg_drop_replication_slot('orig_slot1'); SELECT pg_drop_replication_slot('orig_slot2'); diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index 612bdd99b52..8adf618f74d 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -700,6 +700,13 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) else CheckSlotRequirements(); + /* Cannot copy a replication slot to itself */ + if (strcmp(NameStr(*src_name), NameStr(*dst_name)) == 0) + ereport(ERROR, + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot copy replication slot \"%s\" to itself", + NameStr(*src_name))); + LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); /* @@ -826,15 +833,18 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) /* * Check if the source slot still exists and is valid. We regard it as - * invalid if the type of replication slot or name has been changed, - * or the restart_lsn either is invalid or has gone backward. (The - * restart_lsn could go backwards if the source slot is dropped and - * copied from an older slot during installation.) + * invalid if the slot has been dropped, the type of replication slot + * or name has been changed, or the restart_lsn either is invalid or + * has gone backward. (The restart_lsn could go backwards if the + * source slot is dropped and copied from an older slot during + * installation.) * - * Since erroring out will release and drop the destination slot we - * don't need to release it here. + * Since erroring out will release the destination slot (and drop it + * unless it is a persistent physical slot), we don't need to release + * it here. */ - if (copy_restart_lsn < src_restart_lsn || + if (!second_slot_contents.in_use || + copy_restart_lsn < src_restart_lsn || src_islogical != copy_islogical || strcmp(copy_name, NameStr(*src_name)) != 0) ereport(ERROR, -- 2.54.0 (Apple Git-157)