Heed lock protocol in DROP OWNED BY
authorAlvaro Herrera <[email protected]>
Wed, 6 May 2020 16:29:41 +0000 (12:29 -0400)
committerAlvaro Herrera <[email protected]>
Wed, 6 May 2020 16:29:41 +0000 (12:29 -0400)
We were acquiring object locks then deleting objects one by one, instead
of acquiring all object locks first, ignoring those that did not exist,
and then deleting all objects together.   The latter is the correct
protocol to use, and what this commits changes to code to do.  Failing
to follow that leads to "cache lookup failed for relation XYZ" error
reports when DROP OWNED runs concurrently with other DDL -- for example,
a session termination that removes some temp tables.

Author: Álvaro Herrera
Reported-by: Mithun Chicklore Yogendra (Mithun CY)
Reviewed-by: Ahsan Hadi, Tom Lane
Discussion: https://postgr.es/m/CADq3xVZTbzK4ZLKq+dn_vB4QafXXbmMgDP3trY-GuLnib2Ai1w@mail.gmail.com

src/backend/catalog/dependency.c
src/backend/catalog/pg_shdepend.c
src/backend/commands/subscriptioncmds.c
src/include/catalog/dependency.h

index d42951e293f034be285906f0bfe445120aef0068..4b80e713a1445e976fffb7220bf27b073baa4ded 100644 (file)
@@ -191,8 +191,6 @@ static void reportDependentObjects(const ObjectAddresses *targetObjects,
 static void deleteOneObject(const ObjectAddress *object,
                Relation *depRel, int32 flags);
 static void doDeletion(const ObjectAddress *object, int flags);
-static void AcquireDeletionLock(const ObjectAddress *object, int flags);
-static void ReleaseDeletionLock(const ObjectAddress *object);
 static bool find_expr_references_walker(Node *node,
                            find_expr_references_context *context);
 static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
@@ -1317,11 +1315,14 @@ doDeletion(const ObjectAddress *object, int flags)
 /*
  * AcquireDeletionLock - acquire a suitable lock for deleting an object
  *
+ * Accepts the same flags as performDeletion (though currently only
+ * PERFORM_DELETION_CONCURRENTLY does anything).
+ *
  * We use LockRelation for relations, LockDatabaseObject for everything
- * else.  Note that dependency.c is not concerned with deleting any kind of
- * shared-across-databases object, so we have no need for LockSharedObject.
+ * else.  Shared-across-databases objects are not currently supported
+ * because no caller cares, but could be modified to use LockSharedObject.
  */
-static void
+void
 AcquireDeletionLock(const ObjectAddress *object, int flags)
 {
    if (object->classId == RelationRelationId)
@@ -1347,8 +1348,10 @@ AcquireDeletionLock(const ObjectAddress *object, int flags)
 
 /*
  * ReleaseDeletionLock - release an object deletion lock
+ *
+ * Companion to AcquireDeletionLock.
  */
-static void
+void
 ReleaseDeletionLock(const ObjectAddress *object)
 {
    if (object->classId == RelationRelationId)
index 31b09a1da515241d6e352baf5d493ddf757e0ccd..a6286666978746729122897b0c8b31ba101e97db 100644 (file)
@@ -1240,7 +1240,10 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
                                            sdepForm->objid);
                    break;
                case SHARED_DEPENDENCY_POLICY:
-                   /* If unable to remove role from policy, remove policy. */
+                   /*
+                    * Try to remove role from policy; if unable to, remove
+                    * policy.
+                    */
                    if (!RemoveRoleFromObjectPolicy(roleid,
                                                    sdepForm->classid,
                                                    sdepForm->objid))
@@ -1248,6 +1251,18 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
                        obj.classId = sdepForm->classid;
                        obj.objectId = sdepForm->objid;
                        obj.objectSubId = sdepForm->objsubid;
+                       /*
+                        * Acquire lock on object, then verify this dependency
+                        * is still relevant.  If not, the object might have
+                        * been dropped or the policy modified.  Ignore the
+                        * object in that case.
+                        */
+                       AcquireDeletionLock(&obj, 0);
+                       if (!systable_recheck_tuple(scan, tuple))
+                       {
+                           ReleaseDeletionLock(&obj);
+                           break;
+                       }
                        add_exact_object_address(&obj, deleteobjs);
                    }
                    break;
@@ -1258,6 +1273,13 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
                        obj.classId = sdepForm->classid;
                        obj.objectId = sdepForm->objid;
                        obj.objectSubId = sdepForm->objsubid;
+                       /* as above */
+                       AcquireDeletionLock(&obj, 0);
+                       if (!systable_recheck_tuple(scan, tuple))
+                       {
+                           ReleaseDeletionLock(&obj);
+                           break;
+                       }
                        add_exact_object_address(&obj, deleteobjs);
                    }
                    break;
index 2960f9da0a272c0404aa22fb3b3fca38dbdd29b5..e927b048d6eb465b2cfbde82797f384dac8641a3 100644 (file)
@@ -899,7 +899,6 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
    if (slotname)
        PreventTransactionChain(isTopLevel, "DROP SUBSCRIPTION");
 
-
    ObjectAddressSet(myself, SubscriptionRelationId, subid);
    EventTriggerSQLDropAddObject(&myself, true, true);
 
index f9b5d29674d18769f5f5a6a25587852710d9f3bf..c732f62ed7fbd3a686533efb5e30ab77ecd3040f 100644 (file)
@@ -180,6 +180,10 @@ typedef enum ObjectClass
 
 /* in dependency.c */
 
+extern void AcquireDeletionLock(const ObjectAddress *object, int flags);
+
+extern void ReleaseDeletionLock(const ObjectAddress *object);
+
 extern void performDeletion(const ObjectAddress *object,
                DropBehavior behavior, int flags);