From ad4ad1dac5cf443dee8b069c16b6e29ae31a8a6e Mon Sep 17 00:00:00 2001 From: Dilip Kumar Date: Wed, 6 Aug 2025 05:45:14 +0000 Subject: [PATCH v2] Fix DROP SUBSCRIPTION deadlock with new database creation DROP SUBSCRIPTION previously acquired an AccessExclusiveLock on the pg_subscription catalog to prevent the replication launcher from starting a new worker. However, this caused a deadlock. New database creation also need to acquire an AccessShareLock on this same catalog during their initialization phase. This created a lock conflict where DROP SUBSCRIPTION would block this, while simultaneously waiting for a connection to complete for dropping the replication slot. This commit resolves the deadlock by having DROP SUBSCRIPTION acquire a less restrictive AccessShareLock on the catalog instead. To address the original concern of orphaned workers, a new check is implemented. The replication worker now takes a shared object lock on the subscription itself. If a worker starts for a subscription that no longer exists, it immediately detects this condition and exits. This ensures that no orphan workers are created without the need for an overly broad AccessExclusiveLock on the system catalog. --- src/backend/commands/subscriptioncmds.c | 6 +----- src/backend/replication/logical/worker.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 9467f58a23d..05478e53530 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1571,11 +1571,7 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) List *rstates; bool must_use_password; - /* - * Lock pg_subscription with AccessExclusiveLock to ensure that the - * launcher doesn't restart new worker during dropping the subscription - */ - rel = table_open(SubscriptionRelationId, AccessExclusiveLock); + rel = table_open(SubscriptionRelationId, AccessShareLock); tup = SearchSysCache2(SUBSCRIPTIONNAME, MyDatabaseId, CStringGetDatum(stmt->subname)); diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 1bff6c92dda..136982ba211 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -4613,6 +4613,12 @@ InitializeLogRepWorker(void) StartTransactionCommand(); oldctx = MemoryContextSwitchTo(ApplyContext); + /* + * Lock the subscription to prevent it from being concurrently dropped, + * then re-verify its existence. + */ + LockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid, 0, + AccessShareLock); MySubscription = GetSubscription(MyLogicalRepWorker->subid, true); if (!MySubscription) { @@ -4624,9 +4630,15 @@ InitializeLogRepWorker(void) if (am_leader_apply_worker()) ApplyLauncherForgetWorkerStartTime(MyLogicalRepWorker->subid); + UnlockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid, + 0, AccessShareLock); + proc_exit(0); } + UnlockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid, 0, + AccessShareLock); + MySubscriptionValid = true; MemoryContextSwitchTo(oldctx); -- 2.50.1.565.gc32cd1483b-goog