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

Commit82f1c3b

Browse files
committed
Fix deadlock hazard in CREATE INDEX CONCURRENTLY
Multiple sessions doing CREATE INDEX CONCURRENTLY simultaneously aresupposed to be able to work in parallel, as evidenced by fixes in commitc3d09b3 specifically to support this case. In reality, one of thesessions would be aborted by a misterious "deadlock detected" error.Jeff Janes diagnosed that this is because of leftover snapshots used forsystem catalog scans -- this was broken by8aa3e47 keeping track of(registering) the catalog snapshot. To fix the deadlocks, it's enoughto de-register that snapshot prior to waiting.Backpatch to 9.4, which introduced MVCC catalog scans.Include an isolationtester spec that 8 out of 10 times reproduces thedeadlock with the unpatched code for me (Álvaro).Author: Jeff JanesDiagnosed-by: Jeff JanesReported-by: Jeremy FinzelDiscussion:https://postgr.es/m/CAMa1XUhHjCv8Qkx0WOr1Mpm_R4qxN26EibwCrj0Oor2YBUFUTg%40mail.gmail.com
1 parenta84e90b commit82f1c3b

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

‎src/backend/commands/indexcmds.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,14 @@ DefineIndex(Oid relationId,
779779
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
780780
* they must wait for. But first, save the snapshot's xmin to use as
781781
* limitXmin for GetCurrentVirtualXIDs().
782+
*
783+
* Our catalog snapshot could have the same effect, so drop that one too.
782784
*/
783785
limitXmin=snapshot->xmin;
784786

785787
PopActiveSnapshot();
786788
UnregisterSnapshot(snapshot);
789+
InvalidateCatalogSnapshot();
787790

788791
/*
789792
* The index is now valid in the sense that it contains all currently
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: s2l s1i s2i
4+
step s2l: SELECT pg_advisory_lock(281457);
5+
pg_advisory_lock
6+
7+
8+
step s1i:
9+
CREATE INDEX CONCURRENTLY mcic_one_pkey ON mcic_one (id)
10+
WHERE lck_shr(281457);
11+
<waiting ...>
12+
step s2i:
13+
CREATE INDEX CONCURRENTLY mcic_two_pkey ON mcic_two (id)
14+
WHERE unlck();
15+
16+
step s1i: <... completed>
17+
s1
18+
19+

‎src/test/isolation/isolation_schedule‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ test: skip-locked-2
4646
test: skip-locked-3
4747
test: skip-locked-4
4848
test: drop-index-concurrently-1
49+
test: multiple-cic
4950
test: alter-table-1
5051
test: alter-table-2
5152
test: alter-table-3
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Test multiple CREATE INDEX CONCURRENTLY working simultaneously
2+
3+
setup
4+
{
5+
CREATETABLEmcic_one (
6+
idint
7+
);
8+
CREATETABLEmcic_two (
9+
idint
10+
);
11+
CREATEFUNCTIONlck_shr(bigint)RETURNSboolIMMUTABLELANGUAGEplpgsqlAS $$
12+
BEGINPERFORMpg_advisory_lock_shared($1);RETURNtrue;END;
13+
$$;
14+
CREATEFUNCTIONunlck()RETURNSboolIMMUTABLELANGUAGEplpgsqlAS $$
15+
BEGINPERFORMpg_advisory_unlock_all();RETURNtrue;END;
16+
$$;
17+
}
18+
teardown
19+
{
20+
DROPTABLEmcic_one,mcic_two;
21+
DROPFUNCTIONlck_shr(bigint);
22+
DROPFUNCTIONunlck();
23+
}
24+
25+
session"s1"
26+
step"s1i"{
27+
CREATEINDEXCONCURRENTLYmcic_one_pkeyONmcic_one (id)
28+
WHERElck_shr(281457);
29+
}
30+
teardown{SELECTpg_advisory_unlock_all()AS"s1"; }
31+
32+
33+
session"s2"
34+
step"s2l" {SELECTpg_advisory_lock(281457); }
35+
step"s2i"{
36+
CREATEINDEXCONCURRENTLYmcic_two_pkeyONmcic_two (id)
37+
WHEREunlck();
38+
}
39+
40+
permutation"s2l""s1i""s2i"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp