diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c index 0b941ae12ca..edc4a82b02f 100644 --- a/src/backend/access/nbtree/nbtsort.c +++ b/src/backend/access/nbtree/nbtsort.c @@ -1353,14 +1353,6 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) pcxt = CreateParallelContext("postgres", "_bt_parallel_build_main", request); - /* If no DSM segment was available, back out (do serial build) */ - if (pcxt->seg == NULL) - { - DestroyParallelContext(pcxt); - ExitParallelMode(); - return; - } - scantuplesortstates = leaderparticipates ? request + 1 : request; /* @@ -1404,6 +1396,16 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) /* Everyone's had a chance to ask for space, so now create the DSM */ InitializeParallelDSM(pcxt); + /* If no DSM segment was available, back out (do serial build) */ + if (pcxt->seg == NULL) + { + if (IsMVCCSnapshot(snapshot)) + UnregisterSnapshot(snapshot); + DestroyParallelContext(pcxt); + ExitParallelMode(); + return; + } + /* Store shared build state, for which we reserved space */ btshared = (BTShared *) shm_toc_allocate(pcxt->toc, estbtshared); /* Initialize immutable state */