| From: | Zhijie Hou <houzhijie22(at)gmail(dot)com> |
|---|---|
| To: | Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>, 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-05 10:19:06 |
| Message-ID: | 980b7f87-72d7-4769-a525-c842d18d0176@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
On Fri, Dec 4, 2026 at 02:38 PM Shlok Kyal wrote:
> On Thu, 3 Dec 2026 at 17:43, Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:
> >
> > On Thu, 3 Dec 2026 at 17:00, Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com> wrote:
> > > 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?
I have been thinking through the reported deadlock and other potential
deadlocks involving conflict log table and subscription commands, and I
think moving CatalogTupleDelete() alone is not sufficient because it
introduces an undetected 2PC self-deadlock:
Previously, CatalogTupleDelete() ran before
drop_sub_conflict_log_table(), so if another session updated
pg_subscription before DropSubscription() acquired AccessExclusiveLock
on the subscription, CatalogTupleDelete() immediately aborted with
"tuple concurrently updated".
Once CatalogTupleDelete() is moved after drop_sub_conflict_log_table(),
DropSubscription() checks subtwophasestate using the tuple read before
LockSharedObject():
- Suppose sub1 is initially disabled with conflict_log_destination =
'table' and two_phase = false (subtwophasestate = 'd').
- S1 starts DROP SUBSCRIPTION sub1, reads tup (subtwophasestate = 'd'),
and waits before LockSharedObject(sub1, AccessExclusiveLock).
- S2 enables two_phase and enables sub1; the apply worker sets
subtwophasestate = 'e' and prepares a transaction that logs a conflict
(holding RowExclusiveLock on the conflict log table).
- S1 acquires AccessExclusiveLock(sub1), stops the apply worker, skips
LookupGXactBySubid() because its stale subtwophasestate is still 'd',
and blocks on drop_sub_conflict_log_table(). Because S1 holds
AccessExclusiveLock(sub1), no apply worker can start to resolve the
prepared transaction, causing an undetected permanent hang.
So I think a complete fix for DROP SUBSCRIPTION is to fetch the
pg_subscription tuple after acquiring AccessExclusiveLock in
DropSubscription(), and we can also delay CatalogTupleDelete() until
after dropping the CLT to keep the lock order consistent with ALTER
SUBSCRIPTION. Fetching the tuple after locking the subscription is also
a general improvement, as it avoids "tuple concurrently updated" (and
"cache lookup failed") errors from concurrent changes before the lock
was acquired. See 0001 for this change.
Note that AlterSubscription() also has the same pre-lock tuple issue on
HEAD, as it reads the pg_subscription tuple and Subscription metadata
before acquiring AccessExclusiveLock, while dropping the CLT for SET
(conflict_log_destination = 'log') runs before CatalogTupleUpdate().
Thus the same 2PC hang can also happen there. 0002 fixes
AlterSubscription() to lock the subscription before fetching the tuple
as well.
In addition, even with DropSubscription() and AlterSubscription() fixed,
AlterSubscriptionOwner_internal() still does not lock the subscription
object itself, so a transaction that runs ALTER SUBSCRIPTION before
dropping or accessing the conflict log table still deadlocks with a
concurrent ownership change:
-- Setup
CREATE ROLE role_b;
GRANT CREATE ON DATABASE postgres TO role_b;
CREATE SUBSCRIPTION sub CONNECTION 'dbname=unused' PUBLICATION pub
WITH (connect = false, slot_name = NONE, enabled = false,
conflict_log_destination = 'table');
-- S1: acquires AccessExclusiveLock(sub) and XID lock on pg_subscription
tuple
BEGIN;
ALTER SUBSCRIPTION sub DISABLE;
-- S2: locks the conflict log table first, then blocks on S1's XID lock
-- on the pg_subscription tuple
ALTER SUBSCRIPTION sub OWNER TO role_b;
-- S1: tries to lock the conflict log table -> ERROR: deadlock detected
-- (or ALTER SUBSCRIPTION sub SET (conflict_log_destination = 'log'), or
-- SELECT * FROM pg_conflict.pg_conflict_log_<subid>)
DROP SUBSCRIPTION sub;
To fix this deadlock, we also need to lock the subscription during
subscription owner changes, and fetch the tuple to be updated after
locking the subscription (for the same reason as in 0001). This is done
in 0004.
However, if we only applied 0004, there would be another deadlock with
REASSIGN OWNED BY. Currently, REASSIGN OWNED BY visits the conflict log
table's own SHARED_DEPENDENCY_OWNER entry in pg_shdepend (locking the
conflict log table first via ATExecChangeOwner()) before visiting the
subscription (which then locks the subscription). That inverts the lock
order against DROP SUBSCRIPTION, ALTER SUBSCRIPTION, and ALTER
SUBSCRIPTION ... OWNER TO, causing a deadlock.
So 0003 addresses the REASSIGN OWNED BY case first by not recording an
owner dependency for the CLT (following the pre-existing behavior for
TOAST relations). I think this makes sense because the CLT's owner is
always updated along with the subscription itself, similar to a TOAST table.
0003 can actually also fix an independent issue apart from the mentioned
deadlock: DROP OWNED BY currently fails whenever the target role owns a
subscription with a conflict log table. Subscriptions are stored in a
shared catalog and recorded in pg_shdepend with dbid = InvalidOid, so
shdepDropOwned() skips them (just like databases and tablespaces).
However, heap_create_with_catalog() records a database-local
SHARED_DEPENDENCY_OWNER entry (dbid = MyDatabaseId) for the conflict log
table. As a result, DROP OWNED BY (even with CASCADE) ignores the
subscription itself and tries to drop the conflict log table directly,
failing with:
ERROR: cannot drop table pg_conflict.pg_conflict_log_<oid> because
subscription <subname> requires it
Here is a brief summary of the attached patch set.
- v1-0001: Delay CatalogTupleDelete() in DropSubscription() until after
drop_sub_conflict_log_table(), and fetch the pg_subscription tuple after
acquiring AccessExclusiveLock on the subscription. Fixes the reported
DROP SUBSCRIPTION deadlock and avoids the 2PC hang above.
- v1-0002: Acquire AccessExclusiveLock on the subscription before
fetching the pg_subscription tuple and Subscription metadata in
AlterSubscription(), avoiding the same stale-subtwophasestate 2PC hang
and concurrent-update errors when dropping the conflict log table.
- v1-0003: Skip initial dependency creation and ignore default
privileges for conflict log tables in heap_create_with_catalog() (while
removing any SHARED_DEPENDENCY_ACL entries for the new owner in
ATExecChangeOwner()), so REASSIGN OWNED BY updates conflict log table
ownership only through the subscription. Prepares for v1-0004 to avoid
the REASSIGN OWNED BY lock-order inversion, and fixes DROP OWNED BY and
binary-upgrade relacl consistency.
- v1-0004: Acquire AccessExclusiveLock on the subscription and re-fetch
the tuple in OWNER change as well. Because holding AccessExclusiveLock
prevents an apply worker from starting to resolve prepared transactions,
follow DropSubscription() when changing the conflict log table's owner
by stopping the apply workers and disallowing the owner change while
prepared transactions exist. Fixes the deadlock between multi-statement
ALTER SUBSCRIPTION transactions and ownership changes.
0005 is a new isolation test covering these lock changes. I've kept it
separate for now, until we agree whether to merge it into the main patches.
--
Best Regards,
Zhijie Hou
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Fix-DROP-SUBSCRIPTION-deadlock-with-subscription-.patch | text/plain | 5.8 KB |
| v1-0002-Lock-the-subscription-before-fetching-its-tuple-i.patch | text/plain | 4.0 KB |
| v1-0003-Track-conflict-log-table-ownership-only-through-i.patch | text/plain | 10.3 KB |
| v1-0004-Lock-the-subscription-during-ownership-changes.patch | text/plain | 12.9 KB |
| v1-0005-Add-isolation-tests-for-concurrent-subscription-D.patch | text/plain | 9.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Álvaro Herrera | 2026-12-05 10:31:39 | Re: Avoid logging dropped TOAST values with REPLICA IDENTITY FULL |
| Previous Message | Richard Guo | 2026-12-05 08:40:33 | FULL JOIN planning failure with virtual generated columns |