From eb547c2e49d2e19e00bc8a9ca808011c599308fa Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Tue, 24 Nov 2026 17:52:56 +0800 Subject: [PATCH v1 1/3] 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 checking that second_slot_contents.in_use is still true and that MyReplicationSlot did not reuse src when revalidating the source slot. Author: Zhijie Hou Backpatch-through: 15 --- src/backend/replication/slotfuncs.c | 13 ++- src/test/modules/injection_points/Makefile | 1 + .../expected/slot_copy_drop.out | 64 +++++++++++++ src/test/modules/injection_points/meson.build | 1 + .../specs/slot_copy_drop.spec | 93 +++++++++++++++++++ 5 files changed, 167 insertions(+), 5 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/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index fdeb6a23d7b..80da61cf5cb 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -802,15 +802,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 (either still unused or reused + * by the destination slot itself), 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. */ - if (copy_restart_lsn < src_restart_lsn || + if (!second_slot_contents.in_use || + src == MyReplicationSlot || + 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..4cf292515e8 --- /dev/null +++ b/src/test/modules/injection_points/expected/slot_copy_drop.out @@ -0,0 +1,64 @@ +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) + + +starting permutation: s1_copy_same_name s2_drop_src s2_checkpoint s2_switch_wal s2_checkpoint s2_wakeup s1_check_slots +step s1_copy_same_name: + SELECT slot_name FROM pg_copy_logical_replication_slot('src_slot', 'src_slot'); + +step s2_drop_src: + 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_same_name: <... 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..040b10800f3 --- /dev/null +++ b/src/test/modules/injection_points/specs/slot_copy_drop.spec @@ -0,0 +1,93 @@ +# Test that copying a replication slot fails cleanly if the source slot is +# concurrently dropped during the copy, both when an earlier slot array entry +# is free for the destination slot and when copying to the same slot name +# reuses the source slot's array entry. + +setup +{ + CREATE EXTENSION injection_points; +} +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_copy_same_name +{ + SELECT slot_name FROM pg_copy_logical_replication_slot('src_slot', 'src_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_drop_src +{ + 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 + +# Same race when copying to the same slot name so that the destination slot +# reuses the freed source slot's array entry. +permutation + s1_copy_same_name + s2_drop_src + s2_checkpoint + s2_switch_wal + s2_checkpoint + s2_wakeup + s1_check_slots -- 2.54.0 (Apple Git-157)