From 543de33f32d0fdf6ab45f2bf726a78b9b920e7c8 Mon Sep 17 00:00:00 2001 From: Pavel Borisov Date: Wed, 4 Aug 2021 13:45:26 +0400 Subject: [PATCH v7] Correct usage of transaction snapshot in a parallel scan --- src/backend/access/transam/parallel.c | 30 +++++++++++++++++---------- src/backend/utils/time/snapmgr.c | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 3550ef13ba..289dcc8ec0 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -216,7 +216,8 @@ InitializeParallelDSM(ParallelContext *pcxt) int i; FixedParallelState *fps; dsm_handle session_dsm_handle = DSM_HANDLE_INVALID; - Snapshot transaction_snapshot = GetTransactionSnapshot(); + Snapshot transaction_snapshot = IsolationUsesXactSnapshot() ? + GetTransactionSnapshot() : NULL; Snapshot active_snapshot = GetActiveSnapshot(); /* We might be running in a very short-lived memory context. */ @@ -254,8 +255,11 @@ InitializeParallelDSM(ParallelContext *pcxt) shm_toc_estimate_chunk(&pcxt->estimator, guc_len); combocidlen = EstimateComboCIDStateSpace(); shm_toc_estimate_chunk(&pcxt->estimator, combocidlen); - tsnaplen = EstimateSnapshotSpace(transaction_snapshot); - shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen); + if (transaction_snapshot) + { + tsnaplen = EstimateSnapshotSpace(transaction_snapshot); + shm_toc_estimate_chunk(&pcxt->estimator, tsnaplen); + } asnaplen = EstimateSnapshotSpace(active_snapshot); shm_toc_estimate_chunk(&pcxt->estimator, asnaplen); tstatelen = EstimateTransactionStateSpace(); @@ -366,11 +370,14 @@ InitializeParallelDSM(ParallelContext *pcxt) SerializeComboCIDState(combocidlen, combocidspace); shm_toc_insert(pcxt->toc, PARALLEL_KEY_COMBO_CID, combocidspace); - /* Serialize transaction snapshot and active snapshot. */ - tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen); - SerializeSnapshot(transaction_snapshot, tsnapspace); - shm_toc_insert(pcxt->toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, - tsnapspace); + /* Serialize transaction snapshot (if needed) and active snapshot. */ + if (transaction_snapshot) + { + tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen); + SerializeSnapshot(transaction_snapshot, tsnapspace); + shm_toc_insert(pcxt->toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, + tsnapspace); + } asnapspace = shm_toc_allocate(pcxt->toc, asnaplen); SerializeSnapshot(active_snapshot, asnapspace); shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace); @@ -1409,9 +1416,10 @@ ParallelWorkerMain(Datum main_arg) AttachSession(*(dsm_handle *) session_dsm_handle_space); /* Restore transaction snapshot. */ - tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, false); - RestoreTransactionSnapshot(RestoreSnapshot(tsnapspace), - fps->parallel_leader_pgproc); + tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, true); + if (tsnapspace) + RestoreTransactionSnapshot(RestoreSnapshot(tsnapspace), + fps->parallel_leader_pgproc); /* Restore active snapshot. */ asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false); diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c index 2968c7f7b7..9900de640e 100644 --- a/src/backend/utils/time/snapmgr.c +++ b/src/backend/utils/time/snapmgr.c @@ -262,7 +262,7 @@ GetTransactionSnapshot(void) } /* First call in transaction? */ - if (!FirstSnapshotSet) + if (!FirstSnapshotSet && IsolationUsesXactSnapshot()) { /* * Don't allow catalog snapshot to be older than xact snapshot. Must -- 2.24.3 (Apple Git-128)