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

Commit54eff53

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 parent4380362 commit54eff53

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
@@ -856,11 +856,14 @@ DefineIndex(Oid relationId,
856856
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
857857
* they must wait for. But first, save the snapshot's xmin to use as
858858
* limitXmin for GetCurrentVirtualXIDs().
859+
*
860+
* Our catalog snapshot could have the same effect, so drop that one too.
859861
*/
860862
limitXmin=snapshot->xmin;
861863

862864
PopActiveSnapshot();
863865
UnregisterSnapshot(snapshot);
866+
InvalidateCatalogSnapshot();
864867

865868
/*
866869
* 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
@@ -55,6 +55,7 @@ test: skip-locked-2
5555
test: skip-locked-3
5656
test: skip-locked-4
5757
test: drop-index-concurrently-1
58+
test: multiple-cic
5859
test: alter-table-1
5960
test: alter-table-2
6061
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