diff options
Diffstat (limited to 'src/backend/executor')
-rw-r--r-- | src/backend/executor/spi.c | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c index 9d214f92d1c..47a6b5f5996 100644 --- a/src/backend/executor/spi.c +++ b/src/backend/executor/spi.c @@ -2406,6 +2406,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, uint64 my_processed = 0; SPITupleTable *my_tuptable = NULL; int res = 0; + bool allow_nonatomic; bool pushed_active_snap = false; ResourceOwner plan_owner = options->owner; SPICallbackArg spicallbackarg; @@ -2414,6 +2415,12 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, ListCell *lc1; /* + * We allow nonatomic behavior only if options->allow_nonatomic is set + * *and* the SPI_OPT_NONATOMIC flag was given when connecting. + */ + allow_nonatomic = options->allow_nonatomic && !_SPI_current->atomic; + + /* * Setup error traceback support for ereport() */ spicallbackarg.query = NULL; /* we'll fill this below */ @@ -2432,12 +2439,17 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, * snapshot != InvalidSnapshot, read_only = false: use the given snapshot, * modified by advancing its command ID before each querytree. * - * snapshot == InvalidSnapshot, read_only = true: use the entry-time - * ActiveSnapshot, if any (if there isn't one, we run with no snapshot). + * snapshot == InvalidSnapshot, read_only = true: do nothing for queries + * that require no snapshot. For those that do, ensure that a Portal + * snapshot exists; then use that, or use the entry-time ActiveSnapshot if + * that exists and is different. * - * snapshot == InvalidSnapshot, read_only = false: take a full new - * snapshot for each user command, and advance its command ID before each - * querytree within the command. + * snapshot == InvalidSnapshot, read_only = false: do nothing for queries + * that require no snapshot. For those that do, ensure that a Portal + * snapshot exists; then, in atomic execution (!allow_nonatomic) take a + * full new snapshot for each user command, and advance its command ID + * before each querytree within the command. In allow_nonatomic mode we + * just use the Portal snapshot unmodified. * * In the first two cases, we can just push the snap onto the stack once * for the whole plan list. @@ -2447,6 +2459,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, */ if (snapshot != InvalidSnapshot) { + /* this intentionally tests the options field not the derived value */ Assert(!options->allow_nonatomic); if (options->read_only) { @@ -2592,7 +2605,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, * Skip it when doing non-atomic execution, though (we rely * entirely on the Portal snapshot in that case). */ - if (!options->read_only && !options->allow_nonatomic) + if (!options->read_only && !allow_nonatomic) { if (pushed_active_snap) PopActiveSnapshot(); @@ -2692,14 +2705,13 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, QueryCompletion qc; /* - * If the SPI context is atomic, or we were not told to allow - * nonatomic operations, tell ProcessUtility this is an atomic - * execution context. + * If we're not allowing nonatomic operations, tell + * ProcessUtility this is an atomic execution context. */ - if (_SPI_current->atomic || !options->allow_nonatomic) - context = PROCESS_UTILITY_QUERY; - else + if (allow_nonatomic) context = PROCESS_UTILITY_QUERY_NONATOMIC; + else + context = PROCESS_UTILITY_QUERY; InitializeQueryCompletion(&qc); ProcessUtility(stmt, |