On Thu, 3 Dec 2026 at 17:43, Kirill Reshke <[email protected]> wrote: > > On Thu, 3 Dec 2026 at 17:00, Shlok Kyal <[email protected]> wrote: > > > > > > 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. > > Yes, I described this in my report too in less detail. The deadlock is > reachable with sequence . > > > > 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? > > Is this fix the same as mine in the first message on the thread? I wrote > Yes, the idea is the same. But I found that the patch shared by you in [1] can retain the Conflict log table even if the corresponding subscription is dropped. I have shared the issue details in [2].
I think it happens because the original tuple is released and we refetch the tuple: ``` - /* Remove the tuple from catalog. */ - CatalogTupleDelete(rel, &tup->t_self); - ReleaseSysCache(tup); . . + /* + * 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. The tuple may have + * changed while we waited, so fetch its current version before deleting it. + */ + tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for subscription %u", subid); ``` I attempted to fix it with the patch in [3] by removing this refetching and instead using the original tuple. I have verified that it fixes the original issue and also the issue I reported in [2]. Is it ok to remove the refetching tuple? Thoughts? > > . I didn't find any core call sites who expect that tuple deletion should > > go first, so done this way. > > So I think fixing this way is OK [1]: https://www.postgresql.org/message-id/CALdSSPhJJsrpVFroxSNLYCN%3DPxj6Son6RzSLzxwYzmQwxWh4Rw%40mail.gmail.com [2]: https://www.postgresql.org/message-id/CANhcyEXKn6gH%2B5TZNvho%3D%2BQNJyGs8Vs%2BhJcabR9Kn%3D5RyAdrkg%40mail.gmail.com [3]: https://www.postgresql.org/message-id/CANhcyEV2xhWLoHzCBj6SfXjXZmpxDpMusniB5hYq1A3yTpxe0g%40mail.gmail.com Thanks, Shlok Kyal
