postgresql/src/backend/executor
Tom Lane 185279da3f Fix crash when logical decoding is invoked from a PL function.
The logical decoding functions do BeginInternalSubTransaction and
RollbackAndReleaseCurrentSubTransaction to clean up after themselves.
It turns out that AtEOSubXact_SPI has an unrecognized assumption that
we always need to cancel the active SPI operation in the SPI context
that surrounds the subtransaction (if there is one).  That's true
when the RollbackAndReleaseCurrentSubTransaction call is coming from
the SPI-using function itself, but not when it's happening inside
some unrelated function invoked by a SPI query.  In practice the
affected callers are the various PLs.

To fix, record the current subtransaction ID when we begin a SPI
operation, and clean up only if that ID is the subtransaction being
canceled.

Also, remove AtEOSubXact_SPI's assertion that it must have cleaned
up the surrounding SPI context's active tuptable.  That's proven
wrong by the same test case.

Also clarify (or, if you prefer, reinterpret) the calling conventions
for _SPI_begin_call and _SPI_end_call.  The memory context cleanup
in the latter means that these have always had the flavor of a matched
resource-management pair, but they weren't documented that way before.

Per report from Ben Chobot.

Back-patch to 9.4 where logical decoding came in.  In principle,
the SPI changes should go all the way back, since the problem dates
back to commit 7ec1c5a86.  But given the lack of field complaints
it seems few people are using internal subtransactions in this way.
So I don't feel a need to take any risks in 9.2/9.3.

Discussion: https://postgr.es/m/73FBA179-C68C-4540-9473-71E865408B15@silentmedia.com
2017-10-06 19:18:58 -04:00
..
execAmi.c Add SQL-accessible functions for inspecting index AM properties. 2016-08-13 18:31:14 -04:00
execCurrent.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
execGrouping.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
execIndexing.c Fix failure to handle conflicts in non-arbiter exclusion constraints. 2016-07-04 16:09:11 -04:00
execJunk.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
execMain.c Fix traversal of half-frozen update chains 2017-10-06 17:14:42 +02:00
execParallel.c Avoid passing function pointers across process boundaries. 2017-04-15 16:23:27 -04:00
execProcnode.c Fix typo in comment 2016-04-26 10:38:32 +02:00
execQual.c Fix handling of expanded objects in CoerceToDomain and CASE execution. 2016-12-22 15:01:38 -05:00
execScan.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
execTuples.c Stop the executor if no more tuples can be sent from worker to leader. 2016-06-06 14:52:58 -04:00
execUtils.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
functions.c Throw suitable error for COPY TO STDOUT/FROM STDIN in a SQL function. 2017-01-14 13:27:47 -05:00
instrument.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
Makefile Add a Gather executor node. 2015-09-30 19:23:36 -04:00
nodeAgg.c Spelling fixes 2017-03-14 13:45:54 -04:00
nodeAppend.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeBitmapAnd.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeBitmapHeapscan.c Revert no-op changes to BufferGetPage() 2016-04-20 08:31:19 -05:00
nodeBitmapIndexscan.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeBitmapOr.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeCtescan.c Be sure to rewind the tuplestore read pointer in non-leader CTEScan nodes. 2016-09-22 11:34:44 -04:00
nodeCustom.c Allow parallel custom and foreign scans. 2016-02-03 12:49:46 -05:00
nodeForeignscan.c pgindent run for 9.6 2016-06-09 18:02:36 -04:00
nodeFunctionscan.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
nodeGather.c Ensure gatherstate->nextreader is properly initialized. 2016-12-05 15:59:02 -05:00
nodeGroup.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeHash.c Make sure that hash join's bulk-tuple-transfer loops are interruptible. 2017-02-15 16:40:05 -05:00
nodeHashjoin.c Make sure that hash join's bulk-tuple-transfer loops are interruptible. 2017-02-15 16:40:05 -05:00
nodeIndexonlyscan.c Change the format of the VM fork to add a second bit per page. 2016-03-01 21:49:41 -05:00
nodeIndexscan.c Properly initialize SortSupport for ORDER BY rechecks in nodeIndexscan.c. 2016-06-05 11:53:06 -04:00
nodeLimit.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeLockRows.c Fix EvalPlanQual bug when query contains both locked and not-locked rels. 2016-03-22 17:56:20 -04:00
nodeMaterial.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeMergeAppend.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeMergejoin.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeModifyTable.c Avoid testing tuple visibility without buffer lock. 2016-10-23 19:14:32 -04:00
nodeNestloop.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeRecursiveunion.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
nodeResult.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeSamplescan.c Revert no-op changes to BufferGetPage() 2016-04-20 08:31:19 -05:00
nodeSeqscan.c pgindent run for 9.6 2016-06-09 18:02:36 -04:00
nodeSetOp.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
nodeSort.c Reuse abbreviated keys in ordered [set] aggregates. 2016-02-17 15:40:00 +05:30
nodeSubplan.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
nodeSubqueryscan.c Mark read/write expanded values as read-only in ExecProject(). 2016-06-03 15:14:50 -04:00
nodeTidscan.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
nodeUnique.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
nodeValuesscan.c Mark read/write expanded values as read-only in ValuesNext(), too. 2016-06-03 18:07:14 -04:00
nodeWindowAgg.c Fix typos in comments. 2017-02-06 11:34:15 +02:00
nodeWorktablescan.c Update copyright for 2016 2016-01-02 13:33:40 -05:00
README Refactor the executor's API to support data-modifying CTEs better. 2011-02-27 13:44:12 -05:00
spi.c Fix crash when logical decoding is invoked from a PL function. 2017-10-06 19:18:58 -04:00
tqueue.c Add macros to make AllocSetContextCreate() calls simpler and safer. 2016-08-27 17:50:38 -04:00
tstoreReceiver.c Stop the executor if no more tuples can be sent from worker to leader. 2016-06-06 14:52:58 -04:00

src/backend/executor/README

The Postgres Executor
=====================

The executor processes a tree of "plan nodes".  The plan tree is essentially
a demand-pull pipeline of tuple processing operations.  Each node, when
called, will produce the next tuple in its output sequence, or NULL if no
more tuples are available.  If the node is not a primitive relation-scanning
node, it will have child node(s) that it calls in turn to obtain input
tuples.

Refinements on this basic model include:

* Choice of scan direction (forwards or backwards).  Caution: this is not
currently well-supported.  It works for primitive scan nodes, but not very
well for joins, aggregates, etc.

* Rescan command to reset a node and make it generate its output sequence
over again.

* Parameters that can alter a node's results.  After adjusting a parameter,
the rescan command must be applied to that node and all nodes above it.
There is a moderately intelligent scheme to avoid rescanning nodes
unnecessarily (for example, Sort does not rescan its input if no parameters
of the input have changed, since it can just reread its stored sorted data).

For a SELECT, it is only necessary to deliver the top-level result tuples
to the client.  For INSERT/UPDATE/DELETE, the actual table modification
operations happen in a top-level ModifyTable plan node.  If the query
includes a RETURNING clause, the ModifyTable node delivers the computed
RETURNING rows as output, otherwise it returns nothing.  Handling INSERT
is pretty straightforward: the tuples returned from the plan tree below
ModifyTable are inserted into the correct result relation.  For UPDATE,
the plan tree returns the computed tuples to be updated, plus a "junk"
(hidden) CTID column identifying which table row is to be replaced by each
one.  For DELETE, the plan tree need only deliver a CTID column, and the
ModifyTable node visits each of those rows and marks the row deleted.

XXX a great deal more documentation needs to be written here...


Plan Trees and State Trees
--------------------------

The plan tree delivered by the planner contains a tree of Plan nodes (struct
types derived from struct Plan).  Each Plan node may have expression trees
associated with it, to represent its target list, qualification conditions,
etc.  During executor startup we build a parallel tree of identical structure
containing executor state nodes --- every plan and expression node type has
a corresponding executor state node type.  Each node in the state tree has a
pointer to its corresponding node in the plan tree, plus executor state data
as needed to implement that node type.  This arrangement allows the plan
tree to be completely read-only as far as the executor is concerned: all data
that is modified during execution is in the state tree.  Read-only plan trees
make life much simpler for plan caching and reuse.

Altogether there are four classes of nodes used in these trees: Plan nodes,
their corresponding PlanState nodes, Expr nodes, and their corresponding
ExprState nodes.  (Actually, there are also List nodes, which are used as
"glue" in all four kinds of tree.)


Memory Management
-----------------

A "per query" memory context is created during CreateExecutorState();
all storage allocated during an executor invocation is allocated in that
context or a child context.  This allows easy reclamation of storage
during executor shutdown --- rather than messing with retail pfree's and
probable storage leaks, we just destroy the memory context.

In particular, the plan state trees and expression state trees described
in the previous section are allocated in the per-query memory context.

To avoid intra-query memory leaks, most processing while a query runs
is done in "per tuple" memory contexts, which are so-called because they
are typically reset to empty once per tuple.  Per-tuple contexts are usually
associated with ExprContexts, and commonly each PlanState node has its own
ExprContext to evaluate its qual and targetlist expressions in.


Query Processing Control Flow
-----------------------------

This is a sketch of control flow for full query processing:

	CreateQueryDesc

	ExecutorStart
		CreateExecutorState
			creates per-query context
		switch to per-query context to run ExecInitNode
		ExecInitNode --- recursively scans plan tree
			CreateExprContext
				creates per-tuple context
			ExecInitExpr
		AfterTriggerBeginQuery

	ExecutorRun
		ExecProcNode --- recursively called in per-query context
			ExecEvalExpr --- called in per-tuple context
			ResetExprContext --- to free memory

	ExecutorFinish
		ExecPostprocessPlan --- run any unfinished ModifyTable nodes
		AfterTriggerEndQuery

	ExecutorEnd
		ExecEndNode --- recursively releases resources
		FreeExecutorState
			frees per-query context and child contexts

	FreeQueryDesc

Per above comments, it's not really critical for ExecEndNode to free any
memory; it'll all go away in FreeExecutorState anyway.  However, we do need to
be careful to close relations, drop buffer pins, etc, so we do need to scan
the plan state tree to find these sorts of resources.


The executor can also be used to evaluate simple expressions without any Plan
tree ("simple" meaning "no aggregates and no sub-selects", though such might
be hidden inside function calls).  This case has a flow of control like

	CreateExecutorState
		creates per-query context

	CreateExprContext	-- or use GetPerTupleExprContext(estate)
		creates per-tuple context

	ExecPrepareExpr
		temporarily switch to per-query context
		run the expression through expression_planner
		ExecInitExpr

	Repeatedly do:
		ExecEvalExprSwitchContext
			ExecEvalExpr --- called in per-tuple context
		ResetExprContext --- to free memory

	FreeExecutorState
		frees per-query context, as well as ExprContext
		(a separate FreeExprContext call is not necessary)


EvalPlanQual (READ COMMITTED Update Checking)
---------------------------------------------

For simple SELECTs, the executor need only pay attention to tuples that are
valid according to the snapshot seen by the current transaction (ie, they
were inserted by a previously committed transaction, and not deleted by any
previously committed transaction).  However, for UPDATE and DELETE it is not
cool to modify or delete a tuple that's been modified by an open or
concurrently-committed transaction.  If we are running in SERIALIZABLE
isolation level then we just raise an error when this condition is seen to
occur.  In READ COMMITTED isolation level, we must work a lot harder.

The basic idea in READ COMMITTED mode is to take the modified tuple
committed by the concurrent transaction (after waiting for it to commit,
if need be) and re-evaluate the query qualifications to see if it would
still meet the quals.  If so, we regenerate the updated tuple (if we are
doing an UPDATE) from the modified tuple, and finally update/delete the
modified tuple.  SELECT FOR UPDATE/SHARE behaves similarly, except that its
action is just to lock the modified tuple and return results based on that
version of the tuple.

To implement this checking, we actually re-run the query from scratch for
each modified tuple (or set of tuples, for SELECT FOR UPDATE), with the
relation scan nodes tweaked to return only the current tuples --- either
the original ones, or the updated (and now locked) versions of the modified
tuple(s).  If this query returns a tuple, then the modified tuple(s) pass
the quals (and the query output is the suitably modified update tuple, if
we're doing UPDATE).  If no tuple is returned, then the modified tuple(s)
fail the quals, so we ignore the current result tuple and continue the
original query.

In UPDATE/DELETE, only the target relation needs to be handled this way.
In SELECT FOR UPDATE, there may be multiple relations flagged FOR UPDATE,
so we obtain lock on the current tuple version in each such relation before
executing the recheck.

It is also possible that there are relations in the query that are not
to be locked (they are neither the UPDATE/DELETE target nor specified to
be locked in SELECT FOR UPDATE/SHARE).  When re-running the test query
we want to use the same rows from these relations that were joined to
the locked rows.  For ordinary relations this can be implemented relatively
cheaply by including the row TID in the join outputs and re-fetching that
TID.  (The re-fetch is expensive, but we're trying to optimize the normal
case where no re-test is needed.)  We have also to consider non-table
relations, such as a ValuesScan or FunctionScan.  For these, since there
is no equivalent of TID, the only practical solution seems to be to include
the entire row value in the join output row.

We disallow set-returning functions in the targetlist of SELECT FOR UPDATE,
so as to ensure that at most one tuple can be returned for any particular
set of scan tuples.  Otherwise we'd get duplicates due to the original
query returning the same set of scan tuples multiple times.  (Note: there
is no explicit prohibition on SRFs in UPDATE, but the net effect will be
that only the first result row of an SRF counts, because all subsequent
rows will result in attempts to re-update an already updated target row.
This is historical behavior and seems not worth changing.)