From b2518b620a539b2af2958b79bf8d7ca45f18d09a Mon Sep 17 00:00:00 2001 From: Greg Nancarrow Date: Wed, 18 Aug 2021 22:44:26 +1000 Subject: [PATCH] Fix parallel worker failed assertion and coredump. Currently, InitializeParallelDSM() calls GetTransactionSnapshot() and serializes both the active_snapshot and transaction_snapshot, for parallel workers to restore and use. This appears to be correct in the IsolationUsesXactSnapshot() case, but in the !IsolationUsesXactSnapshot() case the transaction_snapshot is potentially a later snapshot than the active_snapshot, resulting in setting TransactionXmin greater than the xmin of the snapshot used in the query execution by the workers. For the !IsolationUsesXactSnapshot() case, this patch removes the extraneous call to GetTransactionSnapshot() and modifies the parallel DSM code to just pass the active_snapshot, and restore it in the parallel workers as the transaction snapshot and set it as the active snapshot. This resolves an assertion failure in SubTransGetTopmostTransaction() that can occur in a parallel scan, after snapshot suboverflow has occurred. Discussion: https://postgr.es/m/002f01d748ac$eaa781a0$bff684e0$@tju.edu.cn --- src/backend/access/transam/parallel.c | 55 ++++++++++++++++++++------- src/backend/utils/time/snapmgr.c | 15 ++++++++ src/include/utils/snapmgr.h | 1 + 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/backend/access/transam/parallel.c b/src/backend/access/transam/parallel.c index 5f690393fc..4f7599dbe3 100644 --- a/src/backend/access/transam/parallel.c +++ b/src/backend/access/transam/parallel.c @@ -254,8 +254,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 (IsolationUsesXactSnapshot()) + { + 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 +369,19 @@ 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 the transaction snapshot if the transaction + * isolation-level uses a transaction snapshot. + */ + if (IsolationUsesXactSnapshot()) + { + tsnapspace = shm_toc_allocate(pcxt->toc, tsnaplen); + SerializeSnapshot(transaction_snapshot, tsnapspace); + shm_toc_insert(pcxt->toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, + tsnapspace); + } + + /* Serialize the active snapshot. */ asnapspace = shm_toc_allocate(pcxt->toc, asnaplen); SerializeSnapshot(active_snapshot, asnapspace); shm_toc_insert(pcxt->toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, asnapspace); @@ -1408,14 +1419,30 @@ ParallelWorkerMain(Datum main_arg) shm_toc_lookup(toc, PARALLEL_KEY_SESSION_DSM, false); 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); - - /* Restore active snapshot. */ + /* + * If the transaction snapshot was serialized, restore it and restore the + * active snapshot too. Otherwise, the active snapshot is also installed as + * the transaction snapshot. + */ asnapspace = shm_toc_lookup(toc, PARALLEL_KEY_ACTIVE_SNAPSHOT, false); - PushActiveSnapshot(RestoreSnapshot(asnapspace)); + tsnapspace = shm_toc_lookup(toc, PARALLEL_KEY_TRANSACTION_SNAPSHOT, true); + if (tsnapspace) + { + RestoreTransactionSnapshot(RestoreSnapshot(tsnapspace), + fps->parallel_leader_pgproc); + + /* Restore active snapshot. */ + PushActiveSnapshot(RestoreSnapshot(asnapspace)); + } + else + { + /* + * Restore the snapshot, install it as the transaction snapshot and + * set it as the active snapshot. + */ + RestoreTxnSnapshotAndSetAsActive(RestoreSnapshot(asnapspace), + fps->parallel_leader_pgproc); + } /* * We've changed which tuples we can see, and must therefore invalidate diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c index 2968c7f7b7..fe32731316 100644 --- a/src/backend/utils/time/snapmgr.c +++ b/src/backend/utils/time/snapmgr.c @@ -2228,6 +2228,21 @@ RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc) SetTransactionSnapshot(snapshot, NULL, InvalidPid, source_pgproc); } +/* + * Install a restored snapshot as the transaction snapshot, and set it as + * the active snapshot. + * + * The second argument is of type void * so that snapmgr.h need not include + * the declaration for PGPROC. + */ +void +RestoreTxnSnapshotAndSetAsActive(Snapshot snapshot, void *source_pgproc) +{ + RestoreTransactionSnapshot(snapshot, source_pgproc); + PushActiveSnapshot(CurrentSnapshot); +} + + /* * XidInMVCCSnapshot * Is the given XID still-in-progress according to the snapshot? diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h index 44539fe15a..df31e28887 100644 --- a/src/include/utils/snapmgr.h +++ b/src/include/utils/snapmgr.h @@ -173,5 +173,6 @@ extern Size EstimateSnapshotSpace(Snapshot snapshot); extern void SerializeSnapshot(Snapshot snapshot, char *start_address); extern Snapshot RestoreSnapshot(char *start_address); extern void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc); +extern void RestoreTxnSnapshotAndSetAsActive(Snapshot snapshot, void *source_pgproc); #endif /* SNAPMGR_H */ -- 2.27.0