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

Commitc3d09b3

Browse files
committed
Avoid deadlock between concurrent CREATE INDEX CONCURRENTLY commands.
There was a high probability of two or more concurrent C.I.C. commandsdeadlocking just before completion, because each would wait for the othersto release their reference snapshots. Fix by releasing the snapshotbefore waiting for other snapshots to go away.Per report from Paul Hinze. Back-patch to all active branches.
1 parent447b317 commitc3d09b3

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

‎src/backend/commands/indexcmds.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ DefineIndex(IndexStmt *stmt,
320320
int16*coloptions;
321321
IndexInfo*indexInfo;
322322
intnumberOfAttributes;
323+
TransactionIdlimitXmin;
323324
VirtualTransactionId*old_lockholders;
324325
VirtualTransactionId*old_snapshots;
325326
intn_old_snapshots;
@@ -769,6 +770,18 @@ DefineIndex(IndexStmt *stmt,
769770
*/
770771
validate_index(relationId,indexRelationId,snapshot);
771772

773+
/*
774+
* Drop the reference snapshot. We must do this before waiting out other
775+
* snapshot holders, else we will deadlock against other processes also
776+
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
777+
* they must wait for. But first, save the snapshot's xmin to use as
778+
* limitXmin for GetCurrentVirtualXIDs().
779+
*/
780+
limitXmin=snapshot->xmin;
781+
782+
PopActiveSnapshot();
783+
UnregisterSnapshot(snapshot);
784+
772785
/*
773786
* The index is now valid in the sense that it contains all currently
774787
* interesting tuples.But since it might not contain tuples deleted just
@@ -801,7 +814,7 @@ DefineIndex(IndexStmt *stmt,
801814
* GetCurrentVirtualXIDs. If, during any iteration, a particular vxid
802815
* doesn't show up in the output, we know we can forget about it.
803816
*/
804-
old_snapshots=GetCurrentVirtualXIDs(snapshot->xmin, true, false,
817+
old_snapshots=GetCurrentVirtualXIDs(limitXmin, true, false,
805818
PROC_IS_AUTOVACUUM |PROC_IN_VACUUM,
806819
&n_old_snapshots);
807820

@@ -818,7 +831,7 @@ DefineIndex(IndexStmt *stmt,
818831
intj;
819832
intk;
820833

821-
newer_snapshots=GetCurrentVirtualXIDs(snapshot->xmin,
834+
newer_snapshots=GetCurrentVirtualXIDs(limitXmin,
822835
true, false,
823836
PROC_IS_AUTOVACUUM |PROC_IN_VACUUM,
824837
&n_newer_snapshots);
@@ -857,12 +870,6 @@ DefineIndex(IndexStmt *stmt,
857870
*/
858871
CacheInvalidateRelcacheByRelid(heaprelid.relId);
859872

860-
/* we can now do away with our active snapshot */
861-
PopActiveSnapshot();
862-
863-
/* And we can remove the validating snapshot too */
864-
UnregisterSnapshot(snapshot);
865-
866873
/*
867874
* Last thing to do is release the session-level lock on the parent table.
868875
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp