From 43d6ab7a9b3a7fe368ce6fad3ac5a688bfd4abb6 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Wed, 2 Dec 2026 09:43:58 +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, add the "replication-slot-create-begin" injection point for the new isolation test and 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/slot.c | 3 + src/backend/replication/slotfuncs.c | 24 ++++--- src/test/modules/injection_points/Makefile | 4 ++ .../expected/slot_copy_drop.out | 23 +++++++ src/test/modules/injection_points/extra.conf | 1 + src/test/modules/injection_points/meson.build | 5 ++ .../specs/slot_copy_drop.spec | 62 +++++++++++++++++++ 9 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 src/test/modules/injection_points/expected/slot_copy_drop.out create mode 100644 src/test/modules/injection_points/extra.conf 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/slot.c b/src/backend/replication/slot.c index 1f3bb7f7f87..60e7fe4832e 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -56,6 +56,7 @@ #include "storage/procarray.h" #include "utils/builtins.h" #include "utils/guc_hooks.h" +#include "utils/injection_point.h" #include "utils/varlena.h" /* @@ -374,6 +375,8 @@ ReplicationSlotCreate(const char *name, bool db_specific, errmsg("cannot enable failover for a temporary replication slot")); } + INJECTION_POINT("replication-slot-create-begin"); + /* * If some other backend ran this code concurrently with us, we'd likely * both allocate the same slot, and that would be bad. We'd also be at diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c index 01e98bc1cef..0fbff5b5133 100644 --- a/src/backend/replication/slotfuncs.c +++ b/src/backend/replication/slotfuncs.c @@ -625,6 +625,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); /* @@ -765,15 +772,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 9df9bff649f..260871126f9 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -20,12 +20,16 @@ WAIT_CLEANUP_TST = $(if $(filter win32,$(PORTNAME)),,wait_cleanup) ISOLATION = inplace \ reindex_concurrently_deferred \ reindex_concurrently_toast \ + slot_copy_drop \ syscache-update-pruned \ $(WAIT_CLEANUP_TST) \ heap_lock_update \ on_conflict_probe_window \ multixact-aborted-updater +# slot_copy_drop requires wal_level=logical +ISOLATION_OPTS = --temp-config $(top_srcdir)/src/test/modules/injection_points/extra.conf + # The injection points are cluster-wide, so disable installcheck NO_INSTALLCHECK = 1 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..4fa83289477 --- /dev/null +++ b/src/test/modules/injection_points/expected/slot_copy_drop.out @@ -0,0 +1,23 @@ +Parsed test spec with 2 sessions + +starting permutation: s1_copy s2_drop_both 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'); + +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/extra.conf b/src/test/modules/injection_points/extra.conf new file mode 100644 index 00000000000..e3d257315fa --- /dev/null +++ b/src/test/modules/injection_points/extra.conf @@ -0,0 +1 @@ +wal_level = logical diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index f386e20e947..03f32675f91 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -30,6 +30,7 @@ injection_points_isolation = [ 'inplace', 'reindex_concurrently_deferred', 'reindex_concurrently_toast', + 'slot_copy_drop', 'syscache-update-pruned', 'heap_lock_update', 'on_conflict_probe_window', @@ -61,5 +62,9 @@ tests += { 'isolation': { 'specs': injection_points_isolation, 'runningcheck': false, # see syscache-update-pruned + # slot_copy_drop requires wal_level=logical + 'regress_args': [ + '--temp-config', files('extra.conf'), + ], }, } 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..97044c9308d --- /dev/null +++ b/src/test/modules/injection_points/specs/slot_copy_drop.spec @@ -0,0 +1,62 @@ +# 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'); +} +step s2_wakeup +{ + SELECT FROM injection_points_wakeup('replication-slot-create-begin'); +} + +# Verify that dropping the source slot 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_wakeup + s1_check_slots -- 2.54.0 (Apple Git-157)