Skip to content

Commit 2eea494

Browse files
committed
Heed lock protocol in DROP OWNED BY
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
1 parent 8c0939d commit 2eea494

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/backend/catalog/dependency.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ static void reportDependentObjects(const ObjectAddresses *targetObjects,
199199
static void deleteOneObject(const ObjectAddress *object,
200200
Relation *depRel, int32 flags);
201201
static void doDeletion(const ObjectAddress *object, int flags);
202-
static void AcquireDeletionLock(const ObjectAddress *object, int flags);
203-
static void ReleaseDeletionLock(const ObjectAddress *object);
204202
static bool find_expr_references_walker(Node *node,
205203
find_expr_references_context *context);
206204
static void eliminate_duplicate_dependencies(ObjectAddresses *addrs);
@@ -1526,11 +1524,14 @@ doDeletion(const ObjectAddress *object, int flags)
15261524
/*
15271525
* AcquireDeletionLock - acquire a suitable lock for deleting an object
15281526
*
1527+
* Accepts the same flags as performDeletion (though currently only
1528+
* PERFORM_DELETION_CONCURRENTLY does anything).
1529+
*
15291530
* We use LockRelation for relations, LockDatabaseObject for everything
1530-
* else. Note that dependency.c is not concerned with deleting any kind of
1531-
* shared-across-databases object, so we have no need for LockSharedObject.
1531+
* else. Shared-across-databases objects are not currently supported
1532+
* because no caller cares, but could be modified to use LockSharedObject.
15321533
*/
1533-
static void
1534+
void
15341535
AcquireDeletionLock(const ObjectAddress *object, int flags)
15351536
{
15361537
if (object->classId == RelationRelationId)
@@ -1556,8 +1557,10 @@ AcquireDeletionLock(const ObjectAddress *object, int flags)
15561557

15571558
/*
15581559
* ReleaseDeletionLock - release an object deletion lock
1560+
*
1561+
* Companion to AcquireDeletionLock.
15591562
*/
1560-
static void
1563+
void
15611564
ReleaseDeletionLock(const ObjectAddress *object)
15621565
{
15631566
if (object->classId == RelationRelationId)

src/backend/catalog/pg_shdepend.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,14 +1325,29 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
13251325
sdepForm->objid);
13261326
break;
13271327
case SHARED_DEPENDENCY_POLICY:
1328-
/* If unable to remove role from policy, remove policy. */
1328+
/*
1329+
* Try to remove role from policy; if unable to, remove
1330+
* policy.
1331+
*/
13291332
if (!RemoveRoleFromObjectPolicy(roleid,
13301333
sdepForm->classid,
13311334
sdepForm->objid))
13321335
{
13331336
obj.classId = sdepForm->classid;
13341337
obj.objectId = sdepForm->objid;
13351338
obj.objectSubId = sdepForm->objsubid;
1339+
/*
1340+
* Acquire lock on object, then verify this dependency
1341+
* is still relevant. If not, the object might have
1342+
* been dropped or the policy modified. Ignore the
1343+
* object in that case.
1344+
*/
1345+
AcquireDeletionLock(&obj, 0);
1346+
if (!systable_recheck_tuple(scan, tuple))
1347+
{
1348+
ReleaseDeletionLock(&obj);
1349+
break;
1350+
}
13361351
add_exact_object_address(&obj, deleteobjs);
13371352
}
13381353
break;
@@ -1343,6 +1358,13 @@ shdepDropOwned(List *roleids, DropBehavior behavior)
13431358
obj.classId = sdepForm->classid;
13441359
obj.objectId = sdepForm->objid;
13451360
obj.objectSubId = sdepForm->objsubid;
1361+
/* as above */
1362+
AcquireDeletionLock(&obj, 0);
1363+
if (!systable_recheck_tuple(scan, tuple))
1364+
{
1365+
ReleaseDeletionLock(&obj);
1366+
break;
1367+
}
13461368
add_exact_object_address(&obj, deleteobjs);
13471369
}
13481370
break;

src/backend/commands/subscriptioncmds.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,6 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel)
928928
if (slotname)
929929
PreventInTransactionBlock(isTopLevel, "DROP SUBSCRIPTION");
930930

931-
932931
ObjectAddressSet(myself, SubscriptionRelationId, subid);
933932
EventTriggerSQLDropAddObject(&myself, true, true);
934933

src/include/catalog/dependency.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ typedef enum ObjectClass
142142

143143
/* in dependency.c */
144144

145+
extern void AcquireDeletionLock(const ObjectAddress *object, int flags);
146+
147+
extern void ReleaseDeletionLock(const ObjectAddress *object);
148+
145149
extern void performDeletion(const ObjectAddress *object,
146150
DropBehavior behavior, int flags);
147151

0 commit comments

Comments
 (0)