From 631239e6071fe163d4c593d4dca337afbdf59c16 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Sat, 5 Dec 2026 10:26:46 +0800 Subject: [PATCH v1 2/5] Lock the subscription before fetching its tuple in AlterSubscription(). AlterSubscription() fetched the pg_subscription tuple and Subscription metadata before acquiring AccessExclusiveLock on the subscription, while dropping the conflict log table for conflict_log_destination = 'log' happens before CatalogTupleUpdate(). If another session enabled two_phase and the apply worker prepared a conflict-logging transaction before AlterSubscription() acquired the subscription lock, AlterSubscription() observed the stale disabled subtwophasestate, skipped the prepared-transaction check, and hung waiting for the conflict log table lock while holding AccessExclusiveLock on the subscription. Concurrent changes before acquiring the lock could also cause spurious "cache lookup failed" or "tuple concurrently updated" errors, or target a renamed subscription. Fix this by acquiring AccessExclusiveLock on the subscription via get_object_address() after initial permission and option checks, then fetching the pg_subscription tuple and Subscription metadata and rechecking ownership. Discussion: https://postgr.es/m/CALdSSPhJJsrpVFroxSNLYCN=Pxj6Son6RzSLzxwYzmQwxWh4Rw@mail.gmail.com --- src/backend/commands/subscriptioncmds.c | 38 ++++++++++++++----------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index a676b9f63e5..67eccf45974 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1731,18 +1731,7 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, rel = table_open(SubscriptionRelationId, RowExclusiveLock); - /* Fetch the existing tuple. */ - tup = SearchSysCacheCopy2(SUBSCRIPTIONNAME, ObjectIdGetDatum(MyDatabaseId), - CStringGetDatum(stmt->subname)); - - if (!HeapTupleIsValid(tup)) - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("subscription \"%s\" does not exist", - stmt->subname))); - - form = (Form_pg_subscription) GETSTRUCT(tup); - subid = form->oid; + subid = get_subscription_oid(stmt->subname, false); /* must be owner */ if (!object_ownercheck(SubscriptionRelationId, subid, GetUserId())) @@ -1795,6 +1784,26 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, if (supported_opts > 0) parse_subscription_options(pstate, stmt->options, supported_opts, &opts); + /* + * 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, false); + subid = myself.objectId; + + tup = SearchSysCacheCopy1(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)); + sub = GetSubscription(subid, false); /* @@ -1868,16 +1877,11 @@ AlterSubscription(ParseState *pstate, AlterSubscriptionStmt *stmt, errmsg("password_required=false is superuser-only"), errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser."))); - /* Lock the subscription so nobody else can do anything with it. */ - LockSharedObject(SubscriptionRelationId, subid, 0, AccessExclusiveLock); - /* Form a new tuple. */ memset(values, 0, sizeof(values)); memset(nulls, false, sizeof(nulls)); memset(replaces, false, sizeof(replaces)); - ObjectAddressSet(myself, SubscriptionRelationId, subid); - switch (stmt->kind) { case ALTER_SUBSCRIPTION_OPTIONS: -- 2.54.0 (Apple Git-157)