Re: DROP SUBSCRIPTION deadlock with ALTER SUBSCRIPTION OWNER TO

From: Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: DROP SUBSCRIPTION deadlock with ALTER SUBSCRIPTION OWNER TO
Date: 2026-12-03 11:59:59
Message-ID: CANhcyEV2xhWLoHzCBj6SfXjXZmpxDpMusniB5hYq1A3yTpxe0g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 3 Dec 2026 at 02:39, Kirill Reshke <reshkekirill(at)gmail(dot)com> 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

Attachment Content-Type Size
0001-Fix-subscription-conflict-log-lock-order.patch application/x-patch 1.3 KB
0002-Test-subscription-conflict-log-lock-order.patch application/x-patch 5.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2026-12-03 12:09:49 Re: Use local buffer for packed numerics
Previous Message 'Álvaro Herrera' 2026-12-03 11:58:55 Re: logical decoding: removing change_useless_for_repack()