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

Commitfb7b439

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 parentf9c1c60 commitfb7b439

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
@@ -824,11 +824,14 @@ DefineIndex(Oid relationId,
824824
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
825825
* they must wait for. But first, save the snapshot's xmin to use as
826826
* limitXmin for GetCurrentVirtualXIDs().
827+
*
828+
* Our catalog snapshot could have the same effect, so drop that one too.
827829
*/
828830
limitXmin=snapshot->xmin;
829831

830832
PopActiveSnapshot();
831833
UnregisterSnapshot(snapshot);
834+
InvalidateCatalogSnapshot();
832835

833836
/*
834837
* 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
@@ -52,6 +52,7 @@ test: skip-locked-2
5252
test: skip-locked-3
5353
test: skip-locked-4
5454
test: drop-index-concurrently-1
55+
test: multiple-cic
5556
test: alter-table-1
5657
test: alter-table-2
5758
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