From 2cbbcf3e293cebe529be0e5d33af3f657f23dbac Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Fri, 4 Dec 2026 20:15:49 +0800 Subject: [PATCH v1 1/5] Fix DROP SUBSCRIPTION deadlock with subscription owner changes. Commit 6a0a66b3e7 changed AlterSubscriptionOwner_internal() to lock the subscription's conflict log table before updating the pg_subscription tuple, whereas DropSubscription() deletes the pg_subscription tuple before locking and dropping the conflict log table. As a result, concurrent DROP SUBSCRIPTION and ownership changes can lock the pg_subscription tuple and the conflict log table in opposite orders, causing a deadlock. Fix this by delaying CatalogTupleDelete() in DropSubscription() until after drop_sub_conflict_log_table(), keeping the lock acquisition order consistent with ALTER SUBSCRIPTION and ALTER SUBSCRIPTION ... OWNER TO. Also lock the subscription via get_object_address() before fetching the pg_subscription tuple and recheck ownership afterwards, so that concurrent changes made while waiting for the lock (such as renaming the subscription, changing its owner, enabling two_phase, or dropping the conflict log table) are properly handled before operating on the conflict log table. Reported-by: Kirill Reshke Discussion: https://postgr.es/m/CALdSSPhJJsrpVFroxSNLYCN=Pxj6Son6RzSLzxwYzmQwxWh4Rw@mail.gmail.com --- src/backend/commands/subscriptioncmds.c | 83 ++++++++++++++----------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index abea2ecd276..a676b9f63e5 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -2772,53 +2772,63 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) */ rel = table_open(SubscriptionRelationId, RowExclusiveLock); - tup = SearchSysCache2(SUBSCRIPTIONNAME, ObjectIdGetDatum(MyDatabaseId), - CStringGetDatum(stmt->subname)); - - if (!HeapTupleIsValid(tup)) + subid = get_subscription_oid(stmt->subname, stmt->missing_ok); + if (!OidIsValid(subid)) { table_close(rel, NoLock); + ereport(NOTICE, + (errmsg("subscription \"%s\" does not exist, skipping", + stmt->subname))); + return; + } - if (!stmt->missing_ok) - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("subscription \"%s\" does not exist", - stmt->subname))); - else - ereport(NOTICE, - (errmsg("subscription \"%s\" does not exist, skipping", - stmt->subname))); + /* must be owner */ + if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) + aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, + stmt->subname); + /* + * Lock the subscription so nobody else can do anything with it. Use + * get_object_address() in case the subscription was dropped or renamed + * while waiting for the lock. + */ + myself = get_object_address(OBJECT_SUBSCRIPTION, + (Node *) makeString(stmt->subname), + NULL, AccessExclusiveLock, stmt->missing_ok); + if (!OidIsValid(myself.objectId)) + { + table_close(rel, NoLock); + ereport(NOTICE, + (errmsg("subscription \"%s\" does not exist, skipping", + stmt->subname))); return; } + subid = myself.objectId; + + tup = SearchSysCache1(SUBSCRIPTIONOID, ObjectIdGetDatum(subid)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for subscription %u", subid); + + form = (Form_pg_subscription) GETSTRUCT(tup); + + if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) + aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, + NameStr(form->subname)); + + /* DROP hook for the subscription being removed */ + InvokeObjectDropHook(SubscriptionRelationId, subid, 0); datum = SysCacheGetAttr(SUBSCRIPTIONOID, tup, Anum_pg_subscription_subconninfo, &isnull); if (!isnull) subconninfo = TextDatumGetCString(datum); - form = (Form_pg_subscription) GETSTRUCT(tup); - subid = form->oid; subowner = form->subowner; subserver = form->subserver; subconflictlogrelid = form->subconflictlogrelid; subtwophasestate = form->subtwophasestate; must_use_password = !superuser_arg(subowner) && form->subpasswordrequired; - /* must be owner */ - if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) - aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_SUBSCRIPTION, - stmt->subname); - - /* DROP hook for the subscription being removed */ - InvokeObjectDropHook(SubscriptionRelationId, subid, 0); - - /* - * Lock the subscription so nobody else can do anything with it (including - * the replication workers). - */ - LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock); - /* Get subname */ datum = SysCacheGetAttrNotNull(SUBSCRIPTIONOID, tup, Anum_pg_subscription_subname); @@ -2846,14 +2856,8 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) if (slotname) PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION"); - 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 +2912,15 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) drop_sub_conflict_log_table(subid, subname, subconflictlogrelid, stmt->behavior, true); + /* + * Delete the pg_subscription tuple after dropping the conflict log table, + * keeping the lock acquisition order consistent with ALTER SUBSCRIPTION + * and ALTER SUBSCRIPTION ... OWNER TO. + */ + CatalogTupleDelete(rel, &tup->t_self); + + ReleaseSysCache(tup); + /* * Remove the no-longer-useful entry in the launcher's table of apply * worker start times. -- 2.54.0 (Apple Git-157)