diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index abea2ec..3da77cd 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -2849,9 +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); /* @@ -2908,6 +2905,19 @@ 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. 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); + + CatalogTupleDelete(rel, &tup->t_self); + heap_freetuple(tup); + /* * Remove the no-longer-useful entry in the launcher's table of apply * worker start times.