Skip to content

Commit 1dec820

Browse files
committed
Better fix for deadlock hazard in CREATE INDEX CONCURRENTLY.
Commit 54eff53 did not account for the possibility that we'd have a transaction snapshot due to default_transaction_isolation being set high enough to require one. The transaction snapshot is enough to hold back our advertised xmin and thus risk deadlock anyway. The only way to get rid of that snap is to start a new transaction, so let's do that instead. Also throw in an assert checking that we really have gotten to a state where no xmin is being advertised. Back-patch to 9.4, like the previous commit. Discussion: https://postgr.es/m/CAMkU=1ztk3TpQdcUNbxq93pc80FrXUjpDWLGMeVBDx71GHNwZQ@mail.gmail.com
1 parent fe7fc52 commit 1dec820

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/backend/commands/indexcmds.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,14 +1197,25 @@ DefineIndex(Oid relationId,
11971197
* doing CREATE INDEX CONCURRENTLY, which would see our snapshot as one
11981198
* they must wait for. But first, save the snapshot's xmin to use as
11991199
* limitXmin for GetCurrentVirtualXIDs().
1200-
*
1201-
* Our catalog snapshot could have the same effect, so drop that one too.
12021200
*/
12031201
limitXmin = snapshot->xmin;
12041202

12051203
PopActiveSnapshot();
12061204
UnregisterSnapshot(snapshot);
1207-
InvalidateCatalogSnapshot();
1205+
1206+
/*
1207+
* The snapshot subsystem could still contain registered snapshots that
1208+
* are holding back our process's advertised xmin; in particular, if
1209+
* default_transaction_isolation = serializable, there is a transaction
1210+
* snapshot that is still active. The CatalogSnapshot is likewise a
1211+
* hazard. To ensure no deadlocks, we must commit and start yet another
1212+
* transaction, and do our wait before any snapshot has been taken in it.
1213+
*/
1214+
CommitTransactionCommand();
1215+
StartTransactionCommand();
1216+
1217+
/* We should now definitely not be advertising any xmin. */
1218+
Assert(MyPgXact->xmin == InvalidTransactionId);
12081219

12091220
/*
12101221
* The index is now valid in the sense that it contains all currently

0 commit comments

Comments
 (0)