diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-02-27 13:43:29 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-02-27 13:44:12 -0500 |
commit | a874fe7b4c890d1fe3455215a83ca777867beadd (patch) | |
tree | 04b7870100a76bb79470abaf0f971550043590d7 /src/backend/executor/functions.c | |
parent | 67a5e727c8655496013b007d2fb6137fcc244b18 (diff) | |
download | postgresql-a874fe7b4c890d1fe3455215a83ca777867beadd.tar.gz postgresql-a874fe7b4c890d1fe3455215a83ca777867beadd.zip |
Refactor the executor's API to support data-modifying CTEs better.
The originally committed patch for modifying CTEs didn't interact well
with EXPLAIN, as noted by myself, and also had corner-case problems with
triggers, as noted by Dean Rasheed. Those problems show it is really not
practical for ExecutorEnd to call any user-defined code; so split the
cleanup duties out into a new function ExecutorFinish, which must be called
between the last ExecutorRun call and ExecutorEnd. Some Asserts have been
added to these functions to help verify correct usage.
It is no longer necessary for callers of the executor to call
AfterTriggerBeginQuery/AfterTriggerEndQuery for themselves, as this is now
done by ExecutorStart/ExecutorFinish respectively. If you really need to
suppress that and do it for yourself, pass EXEC_FLAG_SKIP_TRIGGERS to
ExecutorStart.
Also, refactor portal commit processing to allow for the possibility that
PortalDrop will invoke user-defined code. I think this is not actually
necessary just yet, since the portal-execution-strategy logic forces any
non-pure-SELECT query to be run to completion before we will consider
committing. But it seems like good future-proofing.
Diffstat (limited to 'src/backend/executor/functions.c')
-rw-r--r-- | src/backend/executor/functions.c | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c index 79bbe6bc762..d5385438e3b 100644 --- a/src/backend/executor/functions.c +++ b/src/backend/executor/functions.c @@ -205,7 +205,8 @@ init_execution_state(List *queryTree_list, if (ps->commandType == CMD_SELECT && ps->utilityStmt == NULL && - ps->intoClause == NULL) + ps->intoClause == NULL && + !ps->hasModifyingCTE) fcache->lazyEval = lasttages->lazyEval = true; } } @@ -431,14 +432,19 @@ postquel_start(execution_state *es, SQLFunctionCachePtr fcache) if (es->qd->utilitystmt == NULL) { /* - * Only set up to collect queued triggers if it's not a SELECT. This - * isn't just an optimization, but is necessary in case a SELECT - * returns multiple rows to caller --- we mustn't exit from the - * function execution with a stacked AfterTrigger level still active. + * In lazyEval mode, do not let the executor set up an AfterTrigger + * context. This is necessary not just an optimization, because we + * mustn't exit from the function execution with a stacked + * AfterTrigger level still active. We are careful not to select + * lazyEval mode for any statement that could possibly queue triggers. */ - if (es->qd->operation != CMD_SELECT) - AfterTriggerBeginQuery(); - ExecutorStart(es->qd, 0); + int eflags; + + if (es->lazyEval) + eflags = EXEC_FLAG_SKIP_TRIGGERS; + else + eflags = 0; /* default run-to-completion flags */ + ExecutorStart(es->qd, eflags); } es->status = F_EXEC_RUN; @@ -499,8 +505,7 @@ postquel_end(execution_state *es) /* Make our snapshot the active one for any called functions */ PushActiveSnapshot(es->qd->snapshot); - if (es->qd->operation != CMD_SELECT) - AfterTriggerEndQuery(es->qd->estate); + ExecutorFinish(es->qd); ExecutorEnd(es->qd); PopActiveSnapshot(); |