1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-17 06:41:09 +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:
Tom Lane
2011-02-27 13:43:29 -05:00
parent 67a5e727c8
commit a874fe7b4c
21 changed files with 350 additions and 222 deletions

View File

@ -1753,12 +1753,10 @@ CommitTransaction(void)
Assert(s->parent == NULL);
/*
* Do pre-commit processing (most of this stuff requires database access,
* and in fact could still cause an error...)
*
* It is possible for CommitHoldablePortals to invoke functions that queue
* deferred triggers, and it's also possible that triggers create holdable
* cursors. So we have to loop until there's nothing left to do.
* Do pre-commit processing that involves calling user-defined code, such
* as triggers. Since closing cursors could queue trigger actions,
* triggers could open cursors, etc, we have to keep looping until there's
* nothing left to do.
*/
for (;;)
{
@ -1768,19 +1766,23 @@ CommitTransaction(void)
AfterTriggerFireDeferred();
/*
* Convert any open holdable cursors into static portals. If there
* weren't any, we are done ... otherwise loop back to check if they
* queued deferred triggers. Lather, rinse, repeat.
* Close open portals (converting holdable ones into static portals).
* If there weren't any, we are done ... otherwise loop back to check
* if they queued deferred triggers. Lather, rinse, repeat.
*/
if (!CommitHoldablePortals())
if (!PreCommit_Portals(false))
break;
}
/* Now we can shut down the deferred-trigger manager */
AfterTriggerEndXact(true);
/*
* The remaining actions cannot call any user-defined code, so it's
* safe to start shutting down within-transaction services. But note
* that most of this stuff could still throw an error, which would
* switch us into the transaction-abort path.
*/
/* Close any open regular cursors */
AtCommit_Portals();
/* Shut down the deferred-trigger manager */
AfterTriggerEndXact(true);
/*
* Let ON COMMIT management do its thing (must happen after closing
@ -1954,12 +1956,10 @@ PrepareTransaction(void)
Assert(s->parent == NULL);
/*
* Do pre-commit processing (most of this stuff requires database access,
* and in fact could still cause an error...)
*
* It is possible for PrepareHoldablePortals to invoke functions that
* queue deferred triggers, and it's also possible that triggers create
* holdable cursors. So we have to loop until there's nothing left to do.
* Do pre-commit processing that involves calling user-defined code, such
* as triggers. Since closing cursors could queue trigger actions,
* triggers could open cursors, etc, we have to keep looping until there's
* nothing left to do.
*/
for (;;)
{
@ -1969,19 +1969,23 @@ PrepareTransaction(void)
AfterTriggerFireDeferred();
/*
* Convert any open holdable cursors into static portals. If there
* weren't any, we are done ... otherwise loop back to check if they
* queued deferred triggers. Lather, rinse, repeat.
* Close open portals (converting holdable ones into static portals).
* If there weren't any, we are done ... otherwise loop back to check
* if they queued deferred triggers. Lather, rinse, repeat.
*/
if (!PrepareHoldablePortals())
if (!PreCommit_Portals(true))
break;
}
/* Now we can shut down the deferred-trigger manager */
AfterTriggerEndXact(true);
/*
* The remaining actions cannot call any user-defined code, so it's
* safe to start shutting down within-transaction services. But note
* that most of this stuff could still throw an error, which would
* switch us into the transaction-abort path.
*/
/* Close any open regular cursors */
AtCommit_Portals();
/* Shut down the deferred-trigger manager */
AfterTriggerEndXact(true);
/*
* Let ON COMMIT management do its thing (must happen after closing