On Thu, 3 Dec 2026 at 02:39, Kirill Reshke <[email protected]> wrote:
>
> Hi!
> After the CLT feature committed [0] server nowl can deadlock if the
> subscription has a conflict-log table and DROP SUBSCRIPTION races with
> ALTER SUBSCRIPTION OWNER TO.
>
>
>
>
> 2026-12-02 16:04:43.070 UTC [588797] LOG: process 588797 detected
> deadlock while waiting for ShareLock on transaction 786 after 1000.094
> ms
> 2026-12-02 16:04:43.070 UTC [588797] DETAIL: Process holding the
> lock: 589381. Wait queue: .
> 2026-12-02 16:04:43.070 UTC [588797] CONTEXT: waiting for ShareLock
> on transaction 786
> while updating tuple (0,12) in relation "pg_subscription"
> 2026-12-02 16:04:43.070 UTC [588797] STATEMENT: ALTER SUBSCRIPTION
> clt_drop_race_1202 OWNER TO clt_drop_b_1202
> 2026-12-02 16:04:43.070 UTC [588797] ERROR: deadlock detected
> 2026-12-02 16:04:43.070 UTC [588797] DETAIL: Process 588797 waits for
> ShareLock on transaction 786; blocked by process 589381.
> Process 589381 waits for AccessExclusiveLock on relation 16506 of
> database 5; blocked by process 588797.
> Process 588797: ALTER SUBSCRIPTION clt_drop_race_1202 OWNER TO
clt_drop_b_1202
> Process 589381: DROP SUBSCRIPTION clt_drop_race_1202
> 2026-12-02 16:04:43.070 UTC [588797] HINT: See server log for query
details.
> 2026-12-02 16:04:43.070 UTC [588797] CONTEXT: while updating tuple
> (0,12) in relation "pg_subscription"
> 2026-12-02 16:04:43.070 UTC [588797] STATEMENT: ALTER SUBSCRIPTION
> clt_drop_race_1202 OWNER TO clt_drop_b_1202
> 2026-12-02 16:04:43.071 UTC [589381] LOG: process 589381 acquired
> AccessExclusiveLock on relation 16506 of database 5 after 16199.061 ms
>
>
> ALTER OWNER locks the CLT before updating the pg_subscription tuple,
> but DROP delete the subscription tuple before CLT lock. Attached
> regression test covers the deadlock.
>
> For the fix, I merely moved pg_subscription tuple deletion in
> DropSubscription. I didn't find any core call sites who expect that
> tuple deletion should go first, so done this way.
>
>
> Without fix, test ends with
>
> step s1_drop_subscription: <... completed>
> step s2_alter_owner: <... completed>
> ERROR: deadlock detected
>
> Test runner summary
> -------------------
> # ok 19 - multixact-aborted-updater
> # diff -U3 expected/subscription_conflict_log_owner_drop.out
> output_iso/results/subscription_conflict_log_owner_drop.out
> # not ok 20 - subscription_conflict_log_owner_drop 435 ms
> 1..20
> make: *** [check] Error 1
>
I debugged it further:
The deadlock error was like:
postgres=# DROP SUBSCRIPTION sub1;
ERROR: deadlock detected
DETAIL: Process 1197528 waits for AccessExclusiveLock on relation 16401 of
database 5; blocked by process 1197547.
Process 1197547 waits for ShareLock on transaction 689; blocked by process
1197528.
It happens because:
1. DROP SUBSCRIPTION holds ExclusiveLock on its own XID (689).
CatalogTupleDelete() marks the subscription row deleted by that XID.
2. ALTER SUBSCRIPTION ... OWNER TO acquires AccessExclusiveLock on the
conflict log table (16401). When it tries to update the same subscription
row, it sees the uncommitted deletion and requests ShareLock on XID 689 to
wait for the outcome.
AlterSubscriptionOwner_internal()
-> CatalogTupleUpdate()
-> simple_heap_update()
-> heap_update(wait = true)
-> XactLockTableWait(xwait = 689)
-> LockAcquire(&tag, ShareLock, false, false)
3. DROP then requests AccessExclusiveLock on the conflict log table. ALTER
OWNER still holds that lock while waiting for DROP.
Hence a deadlock is reported.
To fix the deadlock can we move the 'CatalogTupleDelete(rel,
&tup->t_self);' after 'drop_sub_conflict_log_table'?
That would make DROP SUBSCRIPTION and ALTER SUBSCRIPTION ... OWNER TO
acquire the conflict log table lock before modifying the pg_subscription
row. If ALTER OWNER acquires the table lock first, DROP waits without
marking the row deleted, so ALTER OWNER can update the row and commit. If
DROP acquires the lock first, ALTER OWNER waits for the table lock before
attempting to update the row.
Does it make sense or am I missing something?
Thanks,
Shlok Kyal
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index abea2ecd276756dd803d7d72bbf76c107ff5a907..c294ca1517e847d6a610906209e627a9ba6f42d5 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -2849,11 +2849,6 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
ObjectAddressSet(myself, SubscriptionRelationId, subid);
EventTriggerSQLDropAddObject(&myself, true, true);
- /* Remove the tuple from catalog. */
- CatalogTupleDelete(rel, &tup->t_self);
-
- ReleaseSysCache(tup);
-
/*
* Stop all the subscription workers immediately.
*
@@ -2908,6 +2903,15 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
drop_sub_conflict_log_table(subid, subname, subconflictlogrelid,
stmt->behavior, true);
+ /*
+ * Delete the catalog tuple only after dropping the conflict log table.
+ * This keeps the lock order consistent with ALTER SUBSCRIPTION ... OWNER,
+ * which locks the table before updating the tuple. Use the original tuple
+ * so a concurrent update invalidating our cleanup decisions causes an error.
+ */
+ CatalogTupleDelete(rel, &tup->t_self);
+ ReleaseSysCache(tup);
+
/*
* Remove the no-longer-useful entry in the launcher's table of apply
* worker start times.
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index c294ca1517e847d6a610906209e627a9ba6f42d5..c09a8eec876e0881cb9f0b31ec1d74463ed058e9 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -2900,6 +2900,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
* table, so do it before the non-transactional cleanup of origins and the
* slot below.
*/
+ INJECTION_POINT("subscription-conflict-log-drop-before-table-lock", NULL);
drop_sub_conflict_log_table(subid, subname, subconflictlogrelid,
stmt->behavior, true);
diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile
index e30740ab124b1c75ff6915432d9697364a5ee2a8..cc40038af8e6424a2f30886e7dea71f6d47e145c 100644
--- a/src/test/modules/injection_points/Makefile
+++ b/src/test/modules/injection_points/Makefile
@@ -38,7 +38,8 @@ ISOLATION = basic \
$(WAIT_CLEANUP_TST) \
heap_lock_update \
on_conflict_probe_window \
- multixact-aborted-updater
+ multixact-aborted-updater \
+ subscription_conflict_log_owner_drop
# some isolation tests require wal_level=replica
ISOLATION_OPTS = --temp-config $(top_srcdir)/src/test/modules/injection_points/extra.conf
diff --git a/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out b/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out
new file mode 100644
index 0000000000000000000000000000000000000000..90b9e51fc7cc07d3af0a9952d490d3c646649ae3
--- /dev/null
+++ b/src/test/modules/injection_points/expected/subscription_conflict_log_owner_drop.out
@@ -0,0 +1,21 @@
+Parsed test spec with 3 sessions
+
+starting permutation: s1_deadlock_timeout s1_quiet_notices s1_drop_subscription s2_deadlock_timeout s2_alter_owner s3_wakeup
+injection_points_attach
+-----------------------
+
+(1 row)
+
+step s1_deadlock_timeout: SET deadlock_timeout = '100ms';
+step s1_quiet_notices: SET client_min_messages = warning;
+step s1_drop_subscription: DROP SUBSCRIPTION regress_clt_drop_sub; <waiting ...>
+step s2_deadlock_timeout: SET deadlock_timeout = '100ms';
+step s2_alter_owner: ALTER SUBSCRIPTION regress_clt_drop_sub OWNER TO regress_clt_drop_new_owner;
+step s3_wakeup: SELECT injection_points_wakeup('subscription-conflict-log-drop-before-table-lock');
+injection_points_wakeup
+-----------------------
+
+(1 row)
+
+step s1_drop_subscription: <... completed>
+ERROR: tuple concurrently updated
diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build
index a6a296030f42071326b61cff81f2a79ce56ea488..0233891c50837947b01fe081a663fb36da69bb0a 100644
--- a/src/test/modules/injection_points/meson.build
+++ b/src/test/modules/injection_points/meson.build
@@ -46,6 +46,7 @@ injection_points_isolation = [
'heap_lock_update',
'on_conflict_probe_window',
'multixact-aborted-updater',
+ 'subscription_conflict_log_owner_drop',
]
# wait_cleanup terminates another backend, whose FATAL message can be lost
diff --git a/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec b/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec
new file mode 100644
index 0000000000000000000000000000000000000000..3d0003ec48bff050e252d39d61c8c95169bba62a
--- /dev/null
+++ b/src/test/modules/injection_points/specs/subscription_conflict_log_owner_drop.spec
@@ -0,0 +1,54 @@
+# Exercise the conflict-log table and pg_subscription lock order with the two
+# DDL commands from the reported deadlock. A third session wakes DROP after
+# ALTER has attempted the catalog update.
+
+setup
+{
+ CREATE EXTENSION injection_points;
+ CREATE ROLE regress_clt_drop_new_owner;
+ DO $$
+ BEGIN
+ EXECUTE format('GRANT CREATE ON DATABASE %I TO regress_clt_drop_new_owner', current_database());
+ END;
+ $$;
+
+ CREATE SUBSCRIPTION regress_clt_drop_sub
+ CONNECTION 'dbname=unused'
+ PUBLICATION unused
+ WITH (connect = false, slot_name = NONE, enabled = false,
+ conflict_log_destination = 'table');
+
+ SELECT injection_points_attach('subscription-conflict-log-drop-before-table-lock', 'wait');
+}
+
+teardown
+{
+ SELECT injection_points_detach('subscription-conflict-log-drop-before-table-lock');
+ DROP SUBSCRIPTION IF EXISTS regress_clt_drop_sub;
+ DO $$
+ BEGIN
+ EXECUTE format('REVOKE CREATE ON DATABASE %I FROM regress_clt_drop_new_owner', current_database());
+ END;
+ $$;
+ DROP ROLE regress_clt_drop_new_owner;
+ DROP EXTENSION injection_points;
+}
+
+session "s1"
+step "s1_deadlock_timeout" { SET deadlock_timeout = '100ms'; }
+step "s1_quiet_notices" { SET client_min_messages = warning; }
+step "s1_drop_subscription" { DROP SUBSCRIPTION regress_clt_drop_sub; }
+
+session "s2"
+step "s2_deadlock_timeout" { SET deadlock_timeout = '100ms'; }
+step "s2_alter_owner" { ALTER SUBSCRIPTION regress_clt_drop_sub OWNER TO regress_clt_drop_new_owner; }
+
+session "s3"
+step "s3_wakeup" { SELECT injection_points_wakeup('subscription-conflict-log-drop-before-table-lock'); }
+
+# DROP reaches the injection point first. With the original tuple-delete
+# order it has already deleted the catalog row; ALTER then locks the table and
+# waits for that row. Waking DROP makes it wait for ALTER's table lock and
+# exposes the deadlock. With the fix, ALTER updates the row and completes
+# before DROP resumes. DROP then fails because its original tuple was updated.
+permutation "s1_deadlock_timeout" "s1_quiet_notices" "s1_drop_subscription" "s2_deadlock_timeout" "s2_alter_owner" "s3_wakeup"