@@ -351,12 +351,7 @@ performMultipleDeletions(const ObjectAddresses *objects,
351
351
/* And clean up */
352
352
free_object_addresses (targetObjects );
353
353
354
- /*
355
- * We closed depRel earlier in deleteOneObject if doing a drop
356
- * concurrently
357
- */
358
- if ((flags & PERFORM_DELETION_CONCURRENTLY ) != PERFORM_DELETION_CONCURRENTLY )
359
- heap_close (depRel , RowExclusiveLock );
354
+ heap_close (depRel , RowExclusiveLock );
360
355
}
361
356
362
357
/*
@@ -996,6 +991,7 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
996
991
int nkeys ;
997
992
SysScanDesc scan ;
998
993
HeapTuple tup ;
994
+ Oid depRelOid = depRel -> rd_id ;
999
995
1000
996
/* DROP hook of the objects being removed */
1001
997
if (object_access_hook )
@@ -1008,7 +1004,33 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
1008
1004
}
1009
1005
1010
1006
/*
1011
- * First remove any pg_depend records that link from this object to
1007
+ * Close depRel if we are doing a drop concurrently. The individual
1008
+ * deletion has to commit the transaction and we don't want dangling
1009
+ * references.
1010
+ */
1011
+ if (flags & PERFORM_DELETION_CONCURRENTLY )
1012
+ heap_close (depRel , RowExclusiveLock );
1013
+
1014
+ /*
1015
+ * Delete the object itself, in an object-type-dependent way.
1016
+ *
1017
+ * Do this before removing outgoing dependencies as deletions can be
1018
+ * happening in concurrent mode. That will only happen for a single object
1019
+ * at once and if so the object will be invalidated inside a separate
1020
+ * transaction and only dropped inside a transaction thats in-progress when
1021
+ * doDeletion returns. This way no observer can see dangling dependency
1022
+ * entries.
1023
+ */
1024
+ doDeletion (object , flags );
1025
+
1026
+ /*
1027
+ * Reopen depRel if we closed it before
1028
+ */
1029
+ if (flags & PERFORM_DELETION_CONCURRENTLY )
1030
+ depRel = heap_open (depRelOid , RowExclusiveLock );
1031
+
1032
+ /*
1033
+ * Then remove any pg_depend records that link from this object to
1012
1034
* others. (Any records linking to this object should be gone already.)
1013
1035
*
1014
1036
* When dropping a whole object (subId = 0), remove all pg_depend records
@@ -1050,17 +1072,6 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
1050
1072
deleteSharedDependencyRecordsFor (object -> classId , object -> objectId ,
1051
1073
object -> objectSubId );
1052
1074
1053
- /*
1054
- * Close depRel if we are doing a drop concurrently because it commits the
1055
- * transaction, so we don't want dangling references.
1056
- */
1057
- if ((flags & PERFORM_DELETION_CONCURRENTLY ) == PERFORM_DELETION_CONCURRENTLY )
1058
- heap_close (depRel , RowExclusiveLock );
1059
-
1060
- /*
1061
- * Now delete the object itself, in an object-type-dependent way.
1062
- */
1063
- doDeletion (object , flags );
1064
1075
1065
1076
/*
1066
1077
* Delete any comments or security labels associated with this object.
@@ -1239,7 +1250,7 @@ AcquireDeletionLock(const ObjectAddress *object, int flags)
1239
1250
{
1240
1251
if (object -> classId == RelationRelationId )
1241
1252
{
1242
- if (( flags & PERFORM_DELETION_CONCURRENTLY ) == PERFORM_DELETION_CONCURRENTLY )
1253
+ if (flags & PERFORM_DELETION_CONCURRENTLY )
1243
1254
LockRelationOid (object -> objectId , ShareUpdateExclusiveLock );
1244
1255
else
1245
1256
LockRelationOid (object -> objectId , AccessExclusiveLock );
0 commit comments