mirror of
https://github.com/postgres/postgres.git
synced 2025-10-25 13:17:41 +03:00
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.
This commit is contained in:
@@ -1463,6 +1463,7 @@ EndCopyTo(CopyState cstate)
|
||||
if (cstate->queryDesc != NULL)
|
||||
{
|
||||
/* Close down the query and free resources. */
|
||||
ExecutorFinish(cstate->queryDesc);
|
||||
ExecutorEnd(cstate->queryDesc);
|
||||
FreeQueryDesc(cstate->queryDesc);
|
||||
PopActiveSnapshot();
|
||||
|
||||
@@ -61,10 +61,11 @@ DiscardAll(bool isTopLevel)
|
||||
*/
|
||||
PreventTransactionChain(isTopLevel, "DISCARD ALL");
|
||||
|
||||
/* Closing portals might run user-defined code, so do that first. */
|
||||
PortalHashTableDeleteAll();
|
||||
SetPGVariable("session_authorization", NIL, false);
|
||||
ResetAllOptions();
|
||||
DropAllPreparedStatements();
|
||||
PortalHashTableDeleteAll();
|
||||
Async_UnlistenAll();
|
||||
LockReleaseAll(USER_LOCKMETHOD, true);
|
||||
ResetPlanCache();
|
||||
|
||||
@@ -360,6 +360,8 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
|
||||
if (es->buffers)
|
||||
instrument_option |= INSTRUMENT_BUFFERS;
|
||||
|
||||
INSTR_TIME_SET_CURRENT(starttime);
|
||||
|
||||
/*
|
||||
* Use a snapshot with an updated command ID to ensure this query sees
|
||||
* results of any previously executed queries.
|
||||
@@ -371,12 +373,6 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
|
||||
GetActiveSnapshot(), InvalidSnapshot,
|
||||
None_Receiver, params, instrument_option);
|
||||
|
||||
INSTR_TIME_SET_CURRENT(starttime);
|
||||
|
||||
/* If analyzing, we need to cope with queued triggers */
|
||||
if (es->analyze)
|
||||
AfterTriggerBeginQuery();
|
||||
|
||||
/* Select execution options */
|
||||
if (es->analyze)
|
||||
eflags = 0; /* default run-to-completion flags */
|
||||
@@ -392,7 +388,10 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
|
||||
/* run the plan */
|
||||
ExecutorRun(queryDesc, ForwardScanDirection, 0L);
|
||||
|
||||
/* We can't clean up 'till we're done printing the stats... */
|
||||
/* run cleanup too */
|
||||
ExecutorFinish(queryDesc);
|
||||
|
||||
/* We can't run ExecutorEnd 'till we're done printing the stats... */
|
||||
totaltime += elapsed_time(&starttime);
|
||||
}
|
||||
|
||||
@@ -401,18 +400,6 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
|
||||
/* Create textual dump of plan tree */
|
||||
ExplainPrintPlan(es, queryDesc);
|
||||
|
||||
/*
|
||||
* If we ran the command, run any AFTER triggers it queued. (Note this
|
||||
* will not include DEFERRED triggers; since those don't run until end of
|
||||
* transaction, we can't measure them.) Include into total runtime.
|
||||
*/
|
||||
if (es->analyze)
|
||||
{
|
||||
INSTR_TIME_SET_CURRENT(starttime);
|
||||
AfterTriggerEndQuery(queryDesc->estate);
|
||||
totaltime += elapsed_time(&starttime);
|
||||
}
|
||||
|
||||
/* Print info about runtime of triggers */
|
||||
if (es->analyze)
|
||||
{
|
||||
|
||||
@@ -726,10 +726,9 @@ execute_sql_string(const char *sql, const char *filename)
|
||||
GetActiveSnapshot(), NULL,
|
||||
dest, NULL, 0);
|
||||
|
||||
AfterTriggerBeginQuery();
|
||||
ExecutorStart(qdesc, 0);
|
||||
ExecutorRun(qdesc, ForwardScanDirection, 0);
|
||||
AfterTriggerEndQuery(qdesc->estate);
|
||||
ExecutorFinish(qdesc);
|
||||
ExecutorEnd(qdesc);
|
||||
|
||||
FreeQueryDesc(qdesc);
|
||||
|
||||
@@ -254,7 +254,14 @@ PortalCleanup(Portal portal)
|
||||
queryDesc = PortalGetQueryDesc(portal);
|
||||
if (queryDesc)
|
||||
{
|
||||
/*
|
||||
* Reset the queryDesc before anything else. This prevents us
|
||||
* from trying to shut down the executor twice, in case of an
|
||||
* error below. The transaction abort mechanisms will take care
|
||||
* of resource cleanup in such a case.
|
||||
*/
|
||||
portal->queryDesc = NULL;
|
||||
|
||||
if (portal->status != PORTAL_FAILED)
|
||||
{
|
||||
ResourceOwner saveResourceOwner;
|
||||
@@ -264,7 +271,7 @@ PortalCleanup(Portal portal)
|
||||
PG_TRY();
|
||||
{
|
||||
CurrentResourceOwner = portal->resowner;
|
||||
/* we do not need AfterTriggerEndQuery() here */
|
||||
ExecutorFinish(queryDesc);
|
||||
ExecutorEnd(queryDesc);
|
||||
FreeQueryDesc(queryDesc);
|
||||
}
|
||||
@@ -371,7 +378,7 @@ PersistHoldablePortal(Portal portal)
|
||||
* Now shut down the inner executor.
|
||||
*/
|
||||
portal->queryDesc = NULL; /* prevent double shutdown */
|
||||
/* we do not need AfterTriggerEndQuery() here */
|
||||
ExecutorFinish(queryDesc);
|
||||
ExecutorEnd(queryDesc);
|
||||
FreeQueryDesc(queryDesc);
|
||||
|
||||
|
||||
@@ -3676,9 +3676,9 @@ AfterTriggerBeginQuery(void)
|
||||
* we invoke all AFTER IMMEDIATE trigger events queued by the query, and
|
||||
* transfer deferred trigger events to the global deferred-trigger list.
|
||||
*
|
||||
* Note that this should be called just BEFORE closing down the executor
|
||||
* Note that this must be called BEFORE closing down the executor
|
||||
* with ExecutorEnd, because we make use of the EState's info about
|
||||
* target relations.
|
||||
* target relations. Normally it is called from ExecutorFinish.
|
||||
* ----------
|
||||
*/
|
||||
void
|
||||
|
||||
Reference in New Issue
Block a user