Skip to content

Commit 4c9d090

Browse files
committed
Correct predicate locking for DROP INDEX CONCURRENTLY.
For the non-concurrent case there is an AccessExclusiveLock lock on both the index and the heap at a time during which no other process is using either, before which the index is maintained and used for scans, and after which the index is no longer used or maintained. Predicate locks can safely be moved from the index to the related heap relation under the protection of these locks. This was done prior to the introductin of DROP INDEX CONCURRENTLY and continues to be done for non-concurrent index drops. For concurrent index drops, the predicate locks must be moved when there are no index scans in progress on that index and no more can subsequently start, and before heap inserts stop maintaining the index. As long as these conditions are guaranteed when the TransferPredicateLocksToHeapRelation() function is called, stronger locks are not needed for correctness. Kevin Grittner based on questions by Tom Lane in reviewing the DROP INDEX CONCURRENTLY patch and in cooperation with Andres Freund and Simon Riggs.
1 parent edef20f commit 4c9d090

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/backend/catalog/index.c

+22-6
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,18 @@ index_drop(Oid indexId, bool concurrent)
13201320
* In the concurrent case we make sure that nobody can be looking at the
13211321
* indexes by dropping the index in multiple steps, so we don't need a full
13221322
* AccessExclusiveLock yet.
1323+
*
1324+
* All predicate locks on the index are about to be made invalid. Promote
1325+
* them to relation locks on the heap. For correctness the index must not
1326+
* be seen with indisvalid = true during query planning after the move
1327+
* starts, so that the index will not be used for a scan after the
1328+
* predicate lock move, as this could create new predicate locks on the
1329+
* index which would not ensure a heap relation lock. Also, the index must
1330+
* not be seen during execution of a heap tuple insert with indisready =
1331+
* false before the move is complete, since the conflict with the
1332+
* predicate lock on the index gap could be missed before the lock on the
1333+
* heap relation is in place to detect a conflict based on the heap tuple
1334+
* insert.
13231335
*/
13241336
heapId = IndexGetRelation(indexId, false);
13251337
if (concurrent)
@@ -1444,6 +1456,14 @@ index_drop(Oid indexId, bool concurrent)
14441456
old_lockholders++;
14451457
}
14461458

1459+
/*
1460+
* No more predicate locks will be acquired on this index, and we're
1461+
* about to stop doing inserts into the index which could show
1462+
* conflicts with existing predicate locks, so now is the time to move
1463+
* them to the heap relation.
1464+
*/
1465+
TransferPredicateLocksToHeapRelation(userIndexRelation);
1466+
14471467
/*
14481468
* Now we are sure that nobody uses the index for queries, they just
14491469
* might have it opened for updating it. So now we can unset
@@ -1514,12 +1534,8 @@ index_drop(Oid indexId, bool concurrent)
15141534
userHeapRelation = heap_open(heapId, ShareUpdateExclusiveLock);
15151535
userIndexRelation = index_open(indexId, AccessExclusiveLock);
15161536
}
1517-
1518-
/*
1519-
* All predicate locks on the index are about to be made invalid. Promote
1520-
* them to relation locks on the heap.
1521-
*/
1522-
TransferPredicateLocksToHeapRelation(userIndexRelation);
1537+
else
1538+
TransferPredicateLocksToHeapRelation(userIndexRelation);
15231539

15241540
/*
15251541
* Schedule physical removal of the files

0 commit comments

Comments
 (0)