Skip to content

Commit c567535

Browse files
Fix orphan on cancel of drop index concurrently.
Canceling DROP INDEX CONCURRENTLY during wait could allow an orphaned index to be left behind which could not be dropped. Backpatch to 9.2 Andres Freund, tested by Abhijit Menon-Sen
1 parent f61013a commit c567535

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/backend/catalog/dependency.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,7 @@ performMultipleDeletions(const ObjectAddresses *objects,
351351
/* And clean up */
352352
free_object_addresses(targetObjects);
353353

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);
360355
}
361356

362357
/*
@@ -996,6 +991,7 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
996991
int nkeys;
997992
SysScanDesc scan;
998993
HeapTuple tup;
994+
Oid depRelOid = depRel->rd_id;
999995

1000996
/* DROP hook of the objects being removed */
1001997
if (object_access_hook)
@@ -1008,7 +1004,33 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
10081004
}
10091005

10101006
/*
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
10121034
* others. (Any records linking to this object should be gone already.)
10131035
*
10141036
* When dropping a whole object (subId = 0), remove all pg_depend records
@@ -1050,17 +1072,6 @@ deleteOneObject(const ObjectAddress *object, Relation depRel, int flags)
10501072
deleteSharedDependencyRecordsFor(object->classId, object->objectId,
10511073
object->objectSubId);
10521074

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);
10641075

10651076
/*
10661077
* Delete any comments or security labels associated with this object.
@@ -1239,7 +1250,7 @@ AcquireDeletionLock(const ObjectAddress *object, int flags)
12391250
{
12401251
if (object->classId == RelationRelationId)
12411252
{
1242-
if ((flags & PERFORM_DELETION_CONCURRENTLY) == PERFORM_DELETION_CONCURRENTLY)
1253+
if (flags & PERFORM_DELETION_CONCURRENTLY)
12431254
LockRelationOid(object->objectId, ShareUpdateExclusiveLock);
12441255
else
12451256
LockRelationOid(object->objectId, AccessExclusiveLock);

0 commit comments

Comments
 (0)