Re: 024_add_drop_pub.pl might fail due to deadlock - Mailing list pgsql-hackers

From Amit Kapila
Subject Re: 024_add_drop_pub.pl might fail due to deadlock
Date
Msg-id CAA4eK1KPa1dJrcd=XfOWx-r37eZudKQRqct0tY1R7vnUw0OabQ@mail.gmail.com
Whole thread Raw
In response to Re: 024_add_drop_pub.pl might fail due to deadlock  (Ajin Cherian <itsajin@gmail.com>)
List pgsql-hackers
On Wed, Jul 16, 2025 at 8:38 AM Ajin Cherian <itsajin@gmail.com> wrote:
>
> Thanks for the test and confirming the fix. Fixed the comments.
>

         * origin. So passing missing_ok = true.
+         *
+         * Also lock SubscriptionRelationId with AccessShareLock to
+         * prevent any deadlocks with the user concurrently performing
+         * refresh on the subscription.
         */
+        LockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid,
+                 0, AccessShareLock);

It seems the patch assumes that the above lock is sufficient because
AlterSubscription will take an AcessExclusiveLock on the same
subscription. So, with this proposal, if both of those become
serialized then the other locking order may not matter. Am I correct
or is there any other theory you have in mind?

If that is the theory then I think we are missing cases where the
apply worker and Alter subscription operates on different
subscriptions.

Consider AlterSubscription_refresh() takes first AccessExclusiveLock
on SubscriptionRelRelationId and then ExclusiveLock on
ReplicationOriginRelationId via replorigin_drop_by_name() . The apply
worker first takes ExclusiveLock on ReplicationOriginRelationId via
replorigin_drop_by_name() and then RowExclusiveLock on
SubscriptionRelRelationId via UpdateSubscriptionRelState(). Won't such
a scenario taking conflicting locks in reverse order can lead to
deadlock at least in PG15?

--
With Regards,
Amit Kapila.



pgsql-hackers by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: Fix PQport to never return NULL if the connection is valid
Next
From: Laurenz Albe
Date:
Subject: Re: Collation and primary keys