Skip to content

Commit 05ccf97

Browse files
committed
Fix risk of deadlock failure while dropping a partitioned index.
DROP INDEX needs to lock the index's table before the index itself, else it will deadlock against ordinary queries that acquire the relation locks in that order. This is correctly mechanized for plain indexes by RangeVarCallbackForDropRelation; but in the case of a partitioned index, we neglected to lock the child tables in advance of locking the child indexes. We can fix that by traversing the inheritance tree and acquiring the needed locks in RemoveRelations, after we have acquired our locks on the parent partitioned table and index. While at it, do some refactoring to eliminate confusion between the actual and expected relkind in RangeVarCallbackForDropRelation. We can save a couple of syscache lookups too, by having that function pass back info that RemoveRelations will need. Back-patch to v11 where partitioned indexes were added. Jimmy Yih, Gaurab Dey, Tom Lane Discussion: https://postgr.es/m/BYAPR05MB645402330042E17D91A70C12BD5F9@BYAPR05MB6454.namprd05.prod.outlook.com
1 parent a1efc8f commit 05ccf97

File tree

4 files changed

+191
-18
lines changed

4 files changed

+191
-18
lines changed

src/backend/commands/tablecmds.c

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,18 @@ static const struct dropmsgstrings dropmsgstringarray[] = {
293293
{'\0', 0, NULL, NULL, NULL, NULL}
294294
};
295295

296+
/* communication between RemoveRelations and RangeVarCallbackForDropRelation */
296297
struct DropRelationCallbackState
297298
{
298-
char relkind;
299+
/* These fields are set by RemoveRelations: */
300+
char expected_relkind;
301+
LOCKMODE heap_lockmode;
302+
/* These fields are state to track which subsidiary locks are held: */
299303
Oid heapOid;
300304
Oid partParentOid;
301-
bool concurrent;
305+
/* These fields are passed back by RangeVarCallbackForDropRelation: */
306+
char actual_relkind;
307+
char actual_relpersistence;
302308
};
303309

304310
/* Alter table target-type flags for ATSimplePermissions */
@@ -1394,10 +1400,13 @@ RemoveRelations(DropStmt *drop)
13941400
AcceptInvalidationMessages();
13951401

13961402
/* Look up the appropriate relation using namespace search. */
1397-
state.relkind = relkind;
1403+
state.expected_relkind = relkind;
1404+
state.heap_lockmode = drop->concurrent ?
1405+
ShareUpdateExclusiveLock : AccessExclusiveLock;
1406+
/* We must initialize these fields to show that no locks are held: */
13981407
state.heapOid = InvalidOid;
13991408
state.partParentOid = InvalidOid;
1400-
state.concurrent = drop->concurrent;
1409+
14011410
relOid = RangeVarGetRelidExtended(rel, lockmode, RVR_MISSING_OK,
14021411
RangeVarCallbackForDropRelation,
14031412
(void *) &state);
@@ -1411,10 +1420,10 @@ RemoveRelations(DropStmt *drop)
14111420

14121421
/*
14131422
* Decide if concurrent mode needs to be used here or not. The
1414-
* relation persistence cannot be known without its OID.
1423+
* callback retrieved the rel's persistence for us.
14151424
*/
14161425
if (drop->concurrent &&
1417-
get_rel_persistence(relOid) != RELPERSISTENCE_TEMP)
1426+
state.actual_relpersistence != RELPERSISTENCE_TEMP)
14181427
{
14191428
Assert(list_length(drop->objects) == 1 &&
14201429
drop->removeType == OBJECT_INDEX);
@@ -1426,12 +1435,24 @@ RemoveRelations(DropStmt *drop)
14261435
* either.
14271436
*/
14281437
if ((flags & PERFORM_DELETION_CONCURRENTLY) != 0 &&
1429-
get_rel_relkind(relOid) == RELKIND_PARTITIONED_INDEX)
1438+
state.actual_relkind == RELKIND_PARTITIONED_INDEX)
14301439
ereport(ERROR,
14311440
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
14321441
errmsg("cannot drop partitioned index \"%s\" concurrently",
14331442
rel->relname)));
14341443

1444+
/*
1445+
* If we're told to drop a partitioned index, we must acquire lock on
1446+
* all the children of its parent partitioned table before proceeding.
1447+
* Otherwise we'd try to lock the child index partitions before their
1448+
* tables, leading to potential deadlock against other sessions that
1449+
* will lock those objects in the other order.
1450+
*/
1451+
if (state.actual_relkind == RELKIND_PARTITIONED_INDEX)
1452+
(void) find_all_inheritors(state.heapOid,
1453+
state.heap_lockmode,
1454+
NULL);
1455+
14351456
/* OK, we're ready to delete this one */
14361457
obj.classId = RelationRelationId;
14371458
obj.objectId = relOid;
@@ -1457,17 +1478,14 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
14571478
{
14581479
HeapTuple tuple;
14591480
struct DropRelationCallbackState *state;
1460-
char relkind;
14611481
char expected_relkind;
14621482
bool is_partition;
14631483
Form_pg_class classform;
14641484
LOCKMODE heap_lockmode;
14651485
bool invalid_system_index = false;
14661486

14671487
state = (struct DropRelationCallbackState *) arg;
1468-
relkind = state->relkind;
1469-
heap_lockmode = state->concurrent ?
1470-
ShareUpdateExclusiveLock : AccessExclusiveLock;
1488+
heap_lockmode = state->heap_lockmode;
14711489

14721490
/*
14731491
* If we previously locked some other index's heap, and the name we're
@@ -1501,6 +1519,10 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15011519
classform = (Form_pg_class) GETSTRUCT(tuple);
15021520
is_partition = classform->relispartition;
15031521

1522+
/* Pass back some data to save lookups in RemoveRelations */
1523+
state->actual_relkind = classform->relkind;
1524+
state->actual_relpersistence = classform->relpersistence;
1525+
15041526
/*
15051527
* Both RELKIND_RELATION and RELKIND_PARTITIONED_TABLE are OBJECT_TABLE,
15061528
* but RemoveRelations() can only pass one relkind for a given relation.
@@ -1516,13 +1538,15 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15161538
else
15171539
expected_relkind = classform->relkind;
15181540

1519-
if (relkind != expected_relkind)
1520-
DropErrorMsgWrongType(rel->relname, classform->relkind, relkind);
1541+
if (state->expected_relkind != expected_relkind)
1542+
DropErrorMsgWrongType(rel->relname, classform->relkind,
1543+
state->expected_relkind);
15211544

15221545
/* Allow DROP to either table owner or schema owner */
15231546
if (!pg_class_ownercheck(relOid, GetUserId()) &&
15241547
!pg_namespace_ownercheck(classform->relnamespace, GetUserId()))
1525-
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relOid)),
1548+
aclcheck_error(ACLCHECK_NOT_OWNER,
1549+
get_relkind_objtype(classform->relkind),
15261550
rel->relname);
15271551

15281552
/*
@@ -1531,7 +1555,7 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15311555
* only concerns indexes of toast relations that became invalid during a
15321556
* REINDEX CONCURRENTLY process.
15331557
*/
1534-
if (IsSystemClass(relOid, classform) && relkind == RELKIND_INDEX)
1558+
if (IsSystemClass(relOid, classform) && classform->relkind == RELKIND_INDEX)
15351559
{
15361560
HeapTuple locTuple;
15371561
Form_pg_index indexform;
@@ -1567,9 +1591,10 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15671591
* locking the index. index_drop() will need this anyway, and since
15681592
* regular queries lock tables before their indexes, we risk deadlock if
15691593
* we do it the other way around. No error if we don't find a pg_index
1570-
* entry, though --- the relation may have been dropped.
1594+
* entry, though --- the relation may have been dropped. Note that this
1595+
* code will execute for either plain or partitioned indexes.
15711596
*/
1572-
if ((relkind == RELKIND_INDEX || relkind == RELKIND_PARTITIONED_INDEX) &&
1597+
if (expected_relkind == RELKIND_INDEX &&
15731598
relOid != oldRelOid)
15741599
{
15751600
state->heapOid = IndexGetRelation(relOid, true);
@@ -1580,7 +1605,7 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15801605
/*
15811606
* Similarly, if the relation is a partition, we must acquire lock on its
15821607
* parent before locking the partition. That's because queries lock the
1583-
* parent before its partitions, so we risk deadlock it we do it the other
1608+
* parent before its partitions, so we risk deadlock if we do it the other
15841609
* way around.
15851610
*/
15861611
if (is_partition && relOid != oldRelOid)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Parsed test spec with 3 sessions
2+
3+
starting permutation: s1begin s1lock s2begin s2drop s1select s3getlocks s1commit s3getlocks s2commit
4+
step s1begin: BEGIN;
5+
step s1lock: LOCK TABLE part_drop_index_locking_subpart_child IN ACCESS SHARE MODE;
6+
step s2begin: BEGIN;
7+
step s2drop: DROP INDEX part_drop_index_locking_idx; <waiting ...>
8+
step s1select: SELECT * FROM part_drop_index_locking_subpart_child;
9+
id
10+
--
11+
(0 rows)
12+
13+
step s3getlocks:
14+
SELECT s.query, c.relname, l.mode, l.granted
15+
FROM pg_locks l
16+
JOIN pg_class c ON l.relation = c.oid
17+
JOIN pg_stat_activity s ON l.pid = s.pid
18+
WHERE c.relname LIKE 'part_drop_index_locking%'
19+
ORDER BY s.query, c.relname, l.mode, l.granted;
20+
21+
query |relname |mode |granted
22+
----------------------------------------------------+---------------------------------------------+-------------------+-------
23+
DROP INDEX part_drop_index_locking_idx; |part_drop_index_locking |AccessExclusiveLock|t
24+
DROP INDEX part_drop_index_locking_idx; |part_drop_index_locking_idx |AccessExclusiveLock|t
25+
DROP INDEX part_drop_index_locking_idx; |part_drop_index_locking_subpart |AccessExclusiveLock|t
26+
DROP INDEX part_drop_index_locking_idx; |part_drop_index_locking_subpart_child |AccessExclusiveLock|f
27+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child |AccessShareLock |t
28+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child_id_idx |AccessShareLock |t
29+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child_id_idx1|AccessShareLock |t
30+
(7 rows)
31+
32+
step s1commit: COMMIT;
33+
step s2drop: <... completed>
34+
step s3getlocks:
35+
SELECT s.query, c.relname, l.mode, l.granted
36+
FROM pg_locks l
37+
JOIN pg_class c ON l.relation = c.oid
38+
JOIN pg_stat_activity s ON l.pid = s.pid
39+
WHERE c.relname LIKE 'part_drop_index_locking%'
40+
ORDER BY s.query, c.relname, l.mode, l.granted;
41+
42+
query |relname |mode |granted
43+
---------------------------------------+--------------------------------------------+-------------------+-------
44+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking |AccessExclusiveLock|t
45+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking_idx |AccessExclusiveLock|t
46+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking_subpart |AccessExclusiveLock|t
47+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking_subpart_child |AccessExclusiveLock|t
48+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking_subpart_child_id_idx|AccessExclusiveLock|t
49+
DROP INDEX part_drop_index_locking_idx;|part_drop_index_locking_subpart_id_idx |AccessExclusiveLock|t
50+
(6 rows)
51+
52+
step s2commit: COMMIT;
53+
54+
starting permutation: s1begin s1lock s2begin s2dropsub s1select s3getlocks s1commit s3getlocks s2commit
55+
step s1begin: BEGIN;
56+
step s1lock: LOCK TABLE part_drop_index_locking_subpart_child IN ACCESS SHARE MODE;
57+
step s2begin: BEGIN;
58+
step s2dropsub: DROP INDEX part_drop_index_locking_subpart_idx; <waiting ...>
59+
step s1select: SELECT * FROM part_drop_index_locking_subpart_child;
60+
id
61+
--
62+
(0 rows)
63+
64+
step s3getlocks:
65+
SELECT s.query, c.relname, l.mode, l.granted
66+
FROM pg_locks l
67+
JOIN pg_class c ON l.relation = c.oid
68+
JOIN pg_stat_activity s ON l.pid = s.pid
69+
WHERE c.relname LIKE 'part_drop_index_locking%'
70+
ORDER BY s.query, c.relname, l.mode, l.granted;
71+
72+
query |relname |mode |granted
73+
----------------------------------------------------+---------------------------------------------+-------------------+-------
74+
DROP INDEX part_drop_index_locking_subpart_idx; |part_drop_index_locking_subpart |AccessExclusiveLock|t
75+
DROP INDEX part_drop_index_locking_subpart_idx; |part_drop_index_locking_subpart_child |AccessExclusiveLock|f
76+
DROP INDEX part_drop_index_locking_subpart_idx; |part_drop_index_locking_subpart_idx |AccessExclusiveLock|t
77+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child |AccessShareLock |t
78+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child_id_idx |AccessShareLock |t
79+
SELECT * FROM part_drop_index_locking_subpart_child;|part_drop_index_locking_subpart_child_id_idx1|AccessShareLock |t
80+
(6 rows)
81+
82+
step s1commit: COMMIT;
83+
step s2dropsub: <... completed>
84+
step s3getlocks:
85+
SELECT s.query, c.relname, l.mode, l.granted
86+
FROM pg_locks l
87+
JOIN pg_class c ON l.relation = c.oid
88+
JOIN pg_stat_activity s ON l.pid = s.pid
89+
WHERE c.relname LIKE 'part_drop_index_locking%'
90+
ORDER BY s.query, c.relname, l.mode, l.granted;
91+
92+
query |relname |mode |granted
93+
-----------------------------------------------+---------------------------------------------+-------------------+-------
94+
DROP INDEX part_drop_index_locking_subpart_idx;|part_drop_index_locking_subpart |AccessExclusiveLock|t
95+
DROP INDEX part_drop_index_locking_subpart_idx;|part_drop_index_locking_subpart_child |AccessExclusiveLock|t
96+
DROP INDEX part_drop_index_locking_subpart_idx;|part_drop_index_locking_subpart_child_id_idx1|AccessExclusiveLock|t
97+
DROP INDEX part_drop_index_locking_subpart_idx;|part_drop_index_locking_subpart_idx |AccessExclusiveLock|t
98+
(4 rows)
99+
100+
step s2commit: COMMIT;

src/test/isolation/isolation_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ test: predicate-hash
9090
test: predicate-gist
9191
test: predicate-gin
9292
test: partition-concurrent-attach
93+
test: partition-drop-index-locking
9394
test: partition-key-update-1
9495
test: partition-key-update-2
9596
test: partition-key-update-3
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Verify that DROP INDEX properly locks all downward sub-partitions
2+
# and partitions before locking the indexes.
3+
4+
setup
5+
{
6+
CREATE TABLE part_drop_index_locking (id int) PARTITION BY RANGE(id);
7+
CREATE TABLE part_drop_index_locking_subpart PARTITION OF part_drop_index_locking FOR VALUES FROM (1) TO (100) PARTITION BY RANGE(id);
8+
CREATE TABLE part_drop_index_locking_subpart_child PARTITION OF part_drop_index_locking_subpart FOR VALUES FROM (1) TO (100);
9+
CREATE INDEX part_drop_index_locking_idx ON part_drop_index_locking(id);
10+
CREATE INDEX part_drop_index_locking_subpart_idx ON part_drop_index_locking_subpart(id);
11+
}
12+
13+
teardown
14+
{
15+
DROP TABLE part_drop_index_locking;
16+
}
17+
18+
# SELECT will take AccessShare lock first on the table and then on its index.
19+
# We can simulate the case where DROP INDEX starts between those steps
20+
# by manually taking the table lock beforehand.
21+
session s1
22+
step s1begin { BEGIN; }
23+
step s1lock { LOCK TABLE part_drop_index_locking_subpart_child IN ACCESS SHARE MODE; }
24+
step s1select { SELECT * FROM part_drop_index_locking_subpart_child; }
25+
step s1commit { COMMIT; }
26+
27+
session s2
28+
step s2begin { BEGIN; }
29+
step s2drop { DROP INDEX part_drop_index_locking_idx; }
30+
step s2dropsub { DROP INDEX part_drop_index_locking_subpart_idx; }
31+
step s2commit { COMMIT; }
32+
33+
session s3
34+
step s3getlocks {
35+
SELECT s.query, c.relname, l.mode, l.granted
36+
FROM pg_locks l
37+
JOIN pg_class c ON l.relation = c.oid
38+
JOIN pg_stat_activity s ON l.pid = s.pid
39+
WHERE c.relname LIKE 'part_drop_index_locking%'
40+
ORDER BY s.query, c.relname, l.mode, l.granted;
41+
}
42+
43+
# Run DROP INDEX on top partitioned table
44+
permutation s1begin s1lock s2begin s2drop(s1commit) s1select s3getlocks s1commit s3getlocks s2commit
45+
46+
# Run DROP INDEX on top sub-partition table
47+
permutation s1begin s1lock s2begin s2dropsub(s1commit) s1select s3getlocks s1commit s3getlocks s2commit

0 commit comments

Comments
 (0)