From f13bb9a32a4a81e117dacbcc479b9dfc21a8971d Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Tue, 1 Dec 2026 17:44:01 +0800 Subject: [PATCH v2] 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 (and, with wal_level = replica, persisting a logical slot across a concurrent deactivation of logical decoding). 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 ++++-- src/test/modules/injection_points/Makefile | 1 + .../expected/slot_copy_drop.out | 33 +++++++++ src/test/modules/injection_points/meson.build | 1 + .../specs/slot_copy_drop.spec | 74 +++++++++++++++++++ 7 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 src/test/modules/injection_points/expected/slot_copy_drop.out create mode 100644 src/test/modules/injection_points/specs/slot_copy_drop.spec diff --git a/contrib/test_decoding/expected/slot.out b/contrib/test_decoding/expected/slot.out index cf0136455b0..965c71a9f7d 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 50a950341fc..c2d1e35b8dd 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 fdeb6a23d7b..2215b63b9b2 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -662,6 +662,13 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot) else CheckSlotRequirements(false); + /* 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); /* @@ -802,15 +809,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, diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile index a7a16d5016a..e30740ab124 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -33,6 +33,7 @@ ISOLATION = basic \ repack_toast \ ri_fastpath_reindex \ ri_fastpath_snapshot \ + slot_copy_drop \ syscache-update-pruned \ $(WAIT_CLEANUP_TST) \ heap_lock_update \ diff --git a/src/test/modules/injection_points/expected/slot_copy_drop.out b/src/test/modules/injection_points/expected/slot_copy_drop.out new file mode 100644 index 00000000000..543aa0dbe52 --- /dev/null +++ b/src/test/modules/injection_points/expected/slot_copy_drop.out @@ -0,0 +1,33 @@ +Parsed test spec with 2 sessions + +starting permutation: s1_copy s2_drop_both s2_checkpoint s2_switch_wal s2_checkpoint s2_wakeup s1_check_slots +step s1_copy: + SELECT slot_name FROM pg_copy_logical_replication_slot('src_slot', 'dst_slot'); + +step s2_drop_both: + SELECT FROM pg_drop_replication_slot('slot_copy_drop_0'); + SELECT FROM pg_drop_replication_slot('src_slot'); + SELECT FROM pg_switch_wal(); + +step s2_checkpoint: + CHECKPOINT; + +step s2_switch_wal: + SELECT FROM pg_switch_wal(); + +step s2_checkpoint: + CHECKPOINT; + +step s2_wakeup: + SELECT FROM injection_points_wakeup('replication-slot-create-begin'); + +step s1_copy: <... completed> +ERROR: could not copy replication slot "src_slot" +step s1_check_slots: + SELECT slot_name FROM pg_replication_slots + WHERE slot_name IN ('src_slot', 'dst_slot'); + +slot_name +--------- +(0 rows) + diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index cf9cb219548..a6a296030f4 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -41,6 +41,7 @@ injection_points_isolation = [ 'repack_toast', 'ri_fastpath_reindex', 'ri_fastpath_snapshot', + 'slot_copy_drop', 'syscache-update-pruned', 'heap_lock_update', 'on_conflict_probe_window', diff --git a/src/test/modules/injection_points/specs/slot_copy_drop.spec b/src/test/modules/injection_points/specs/slot_copy_drop.spec new file mode 100644 index 00000000000..b8ba5740780 --- /dev/null +++ b/src/test/modules/injection_points/specs/slot_copy_drop.spec @@ -0,0 +1,74 @@ +# Test that copying a replication slot fails cleanly if the source slot is +# concurrently dropped during the copy and the destination slot is allocated in +# another free slot array entry. + +setup +{ + CREATE EXTENSION injection_points; +} +# Occupy array entry 0 first so that src_slot is placed at entry 1; once both +# are dropped, dst_slot takes entry 0 rather than reusing src_slot's entry. +setup +{ + SELECT FROM pg_create_physical_replication_slot('slot_copy_drop_0', false); + SELECT FROM pg_create_logical_replication_slot('src_slot', 'pgoutput'); +} + +teardown +{ + SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots + WHERE slot_name IN ('slot_copy_drop_0', 'src_slot', 'dst_slot'); + DROP EXTENSION injection_points; +} + +session s1 +setup +{ + SELECT FROM injection_points_set_local(); + SELECT FROM injection_points_attach('replication-slot-create-begin', 'wait'); +} +step s1_copy +{ + SELECT slot_name FROM pg_copy_logical_replication_slot('src_slot', 'dst_slot'); +} +step s1_check_slots +{ + SELECT slot_name FROM pg_replication_slots + WHERE slot_name IN ('src_slot', 'dst_slot'); +} +teardown +{ + SELECT FROM injection_points_detach('replication-slot-create-begin'); +} + +session s2 +step s2_drop_both +{ + SELECT FROM pg_drop_replication_slot('slot_copy_drop_0'); + SELECT FROM pg_drop_replication_slot('src_slot'); + SELECT FROM pg_switch_wal(); +} +step s2_checkpoint +{ + CHECKPOINT; +} +step s2_switch_wal +{ + SELECT FROM pg_switch_wal(); +} +step s2_wakeup +{ + SELECT FROM injection_points_wakeup('replication-slot-create-begin'); +} + +# Verify that dropping the source slot and recycling its WAL while +# pg_copy_logical_replication_slot() is in progress causes the copy to fail +# cleanly without leaving dst_slot behind when an earlier array entry is free. +permutation + s1_copy + s2_drop_both + s2_checkpoint + s2_switch_wal + s2_checkpoint + s2_wakeup + s1_check_slots -- 2.54.0 (Apple Git-157)