Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd8d378d

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 therelation locks in that order. This is correctly mechanized forplain indexes by RangeVarCallbackForDropRelation; but in the case ofa partitioned index, we neglected to lock the child tables in advanceof locking the child indexes. We can fix that by traversing theinheritance tree and acquiring the needed locks in RemoveRelations,after we have acquired our locks on the parent partitioned table andindex.While at it, do some refactoring to eliminate confusion betweenthe actual and expected relkind in RangeVarCallbackForDropRelation.We can save a couple of syscache lookups too, by having that functionpass back info that RemoveRelations will need.Back-patch to v11 where partitioned indexes were added.Jimmy Yih, Gaurab Dey, Tom LaneDiscussion:https://postgr.es/m/BYAPR05MB645402330042E17D91A70C12BD5F9@BYAPR05MB6454.namprd05.prod.outlook.com
1 parent88418aa commitd8d378d

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
@@ -278,12 +278,18 @@ static const struct dropmsgstrings dropmsgstringarray[] = {
278278
{'\0', 0, NULL, NULL, NULL, NULL}
279279
};
280280

281+
/* communication between RemoveRelations and RangeVarCallbackForDropRelation */
281282
struct DropRelationCallbackState
282283
{
283-
charrelkind;
284+
/* These fields are set by RemoveRelations: */
285+
charexpected_relkind;
286+
LOCKMODEheap_lockmode;
287+
/* These fields are state to track which subsidiary locks are held: */
284288
OidheapOid;
285289
OidpartParentOid;
286-
boolconcurrent;
290+
/* These fields are passed back by RangeVarCallbackForDropRelation: */
291+
charactual_relkind;
292+
charactual_relpersistence;
287293
};
288294

289295
/* Alter table target-type flags for ATSimplePermissions */
@@ -1323,10 +1329,13 @@ RemoveRelations(DropStmt *drop)
13231329
AcceptInvalidationMessages();
13241330

13251331
/* Look up the appropriate relation using namespace search. */
1326-
state.relkind = relkind;
1332+
state.expected_relkind = relkind;
1333+
state.heap_lockmode = drop->concurrent ?
1334+
ShareUpdateExclusiveLock : AccessExclusiveLock;
1335+
/* We must initialize these fields to show that no locks are held: */
13271336
state.heapOid = InvalidOid;
13281337
state.partParentOid = InvalidOid;
1329-
state.concurrent = drop->concurrent;
1338+
13301339
relOid = RangeVarGetRelidExtended(rel, lockmode, RVR_MISSING_OK,
13311340
RangeVarCallbackForDropRelation,
13321341
(void *) &state);
@@ -1340,10 +1349,10 @@ RemoveRelations(DropStmt *drop)
13401349

13411350
/*
13421351
* Decide if concurrent mode needs to be used here or not. The
1343-
*relation persistence cannot be known without its OID.
1352+
*callback retrieved the rel's persistence for us.
13441353
*/
13451354
if (drop->concurrent &&
1346-
get_rel_persistence(relOid) != RELPERSISTENCE_TEMP)
1355+
state.actual_relpersistence != RELPERSISTENCE_TEMP)
13471356
{
13481357
Assert(list_length(drop->objects) == 1 &&
13491358
drop->removeType == OBJECT_INDEX);
@@ -1355,12 +1364,24 @@ RemoveRelations(DropStmt *drop)
13551364
* either.
13561365
*/
13571366
if ((flags & PERFORM_DELETION_CONCURRENTLY) != 0 &&
1358-
get_rel_relkind(relOid) == RELKIND_PARTITIONED_INDEX)
1367+
state.actual_relkind == RELKIND_PARTITIONED_INDEX)
13591368
ereport(ERROR,
13601369
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
13611370
errmsg("cannot drop partitioned index \"%s\" concurrently",
13621371
rel->relname)));
13631372

1373+
/*
1374+
* If we're told to drop a partitioned index, we must acquire lock on
1375+
* all the children of its parent partitioned table before proceeding.
1376+
* Otherwise we'd try to lock the child index partitions before their
1377+
* tables, leading to potential deadlock against other sessions that
1378+
* will lock those objects in the other order.
1379+
*/
1380+
if (state.actual_relkind == RELKIND_PARTITIONED_INDEX)
1381+
(void) find_all_inheritors(state.heapOid,
1382+
state.heap_lockmode,
1383+
NULL);
1384+
13641385
/* OK, we're ready to delete this one */
13651386
obj.classId = RelationRelationId;
13661387
obj.objectId = relOid;
@@ -1386,17 +1407,14 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
13861407
{
13871408
HeapTupletuple;
13881409
struct DropRelationCallbackState *state;
1389-
charrelkind;
13901410
charexpected_relkind;
13911411
boolis_partition;
13921412
Form_pg_class classform;
13931413
LOCKMODEheap_lockmode;
13941414
boolinvalid_system_index = false;
13951415

13961416
state = (struct DropRelationCallbackState *) arg;
1397-
relkind = state->relkind;
1398-
heap_lockmode = state->concurrent ?
1399-
ShareUpdateExclusiveLock : AccessExclusiveLock;
1417+
heap_lockmode = state->heap_lockmode;
14001418

14011419
/*
14021420
* If we previously locked some other index's heap, and the name we're
@@ -1430,6 +1448,10 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
14301448
classform = (Form_pg_class) GETSTRUCT(tuple);
14311449
is_partition = classform->relispartition;
14321450

1451+
/* Pass back some data to save lookups in RemoveRelations */
1452+
state->actual_relkind = classform->relkind;
1453+
state->actual_relpersistence = classform->relpersistence;
1454+
14331455
/*
14341456
* Both RELKIND_RELATION and RELKIND_PARTITIONED_TABLE are OBJECT_TABLE,
14351457
* but RemoveRelations() can only pass one relkind for a given relation.
@@ -1445,13 +1467,15 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
14451467
else
14461468
expected_relkind = classform->relkind;
14471469

1448-
if (relkind != expected_relkind)
1449-
DropErrorMsgWrongType(rel->relname, classform->relkind, relkind);
1470+
if (state->expected_relkind != expected_relkind)
1471+
DropErrorMsgWrongType(rel->relname, classform->relkind,
1472+
state->expected_relkind);
14501473

14511474
/* Allow DROP to either table owner or schema owner */
14521475
if (!pg_class_ownercheck(relOid, GetUserId()) &&
14531476
!pg_namespace_ownercheck(classform->relnamespace, GetUserId()))
1454-
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relOid)),
1477+
aclcheck_error(ACLCHECK_NOT_OWNER,
1478+
get_relkind_objtype(classform->relkind),
14551479
rel->relname);
14561480

14571481
/*
@@ -1460,7 +1484,7 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
14601484
* only concerns indexes of toast relations that became invalid during a
14611485
* REINDEX CONCURRENTLY process.
14621486
*/
1463-
if (IsSystemClass(relOid, classform) && relkind == RELKIND_INDEX)
1487+
if (IsSystemClass(relOid, classform) &&classform->relkind == RELKIND_INDEX)
14641488
{
14651489
HeapTuplelocTuple;
14661490
Form_pg_index indexform;
@@ -1496,9 +1520,10 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
14961520
* locking the index. index_drop() will need this anyway, and since
14971521
* regular queries lock tables before their indexes, we risk deadlock if
14981522
* we do it the other way around. No error if we don't find a pg_index
1499-
* entry, though --- the relation may have been dropped.
1523+
* entry, though --- the relation may have been dropped. Note that this
1524+
* code will execute for either plain or partitioned indexes.
15001525
*/
1501-
if ((relkind == RELKIND_INDEX || relkind == RELKIND_PARTITIONED_INDEX) &&
1526+
if (expected_relkind == RELKIND_INDEX &&
15021527
relOid != oldRelOid)
15031528
{
15041529
state->heapOid = IndexGetRelation(relOid, true);
@@ -1509,7 +1534,7 @@ RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid,
15091534
/*
15101535
* Similarly, if the relation is a partition, we must acquire lock on its
15111536
* parent before locking the partition. That's because queries lock the
1512-
* parent before its partitions, so we risk deadlockit we do it the other
1537+
* parent before its partitions, so we risk deadlockif we do it the other
15131538
* way around.
15141539
*/
15151540
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
@@ -83,6 +83,7 @@ test: predicate-hash
8383
test: predicate-gist
8484
test: predicate-gin
8585
test: partition-concurrent-attach
86+
test: partition-drop-index-locking
8687
test: partition-key-update-1
8788
test: partition-key-update-2
8889
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+
CREATETABLEpart_drop_index_locking (idint)PARTITIONBYRANGE(id);
7+
CREATETABLEpart_drop_index_locking_subpartPARTITIONOFpart_drop_index_lockingFORVALUESFROM (1)TO (100)PARTITIONBYRANGE(id);
8+
CREATETABLEpart_drop_index_locking_subpart_childPARTITIONOFpart_drop_index_locking_subpartFORVALUESFROM (1)TO (100);
9+
CREATEINDEXpart_drop_index_locking_idxONpart_drop_index_locking(id);
10+
CREATEINDEXpart_drop_index_locking_subpart_idxONpart_drop_index_locking_subpart(id);
11+
}
12+
13+
teardown
14+
{
15+
DROPTABLEpart_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+
sessions1
22+
steps1begin {BEGIN; }
23+
steps1lock {LOCKTABLEpart_drop_index_locking_subpart_childINACCESSSHAREMODE; }
24+
steps1select {SELECT*FROMpart_drop_index_locking_subpart_child; }
25+
steps1commit {COMMIT; }
26+
27+
sessions2
28+
steps2begin {BEGIN; }
29+
steps2drop {DROPINDEXpart_drop_index_locking_idx; }
30+
steps2dropsub {DROPINDEXpart_drop_index_locking_subpart_idx; }
31+
steps2commit {COMMIT; }
32+
33+
sessions3
34+
steps3getlocks {
35+
SELECTs.query,c.relname,l.mode,l.granted
36+
FROMpg_locksl
37+
JOINpg_classcONl.relation=c.oid
38+
JOINpg_stat_activitysONl.pid=s.pid
39+
WHEREc.relnameLIKE'part_drop_index_locking%'
40+
ORDERBYs.query,c.relname,l.mode,l.granted;
41+
}
42+
43+
# Run DROP INDEX on top partitioned table
44+
permutations1begins1locks2begins2drop(s1commit)s1selects3getlockss1commits3getlockss2commit
45+
46+
# Run DROP INDEX on top sub-partition table
47+
permutations1begins1locks2begins2dropsub(s1commit)s1selects3getlockss1commits3getlockss2commit

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp