2008-10-04 17:56:55 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* nodeWorktablescan.c
|
|
|
|
|
* routines to handle WorkTableScan nodes.
|
|
|
|
|
*
|
2017-01-03 13:48:53 -05:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2008-10-04 17:56:55 -04:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/backend/executor/nodeWorktablescan.c
|
2008-10-04 17:56:55 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
|
|
#include "executor/execdebug.h"
|
|
|
|
|
#include "executor/nodeWorktablescan.h"
|
|
|
|
|
|
|
|
|
|
static TupleTableSlot *WorkTableScanNext(WorkTableScanState *node);
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* WorkTableScanNext
|
|
|
|
|
*
|
|
|
|
|
* This is a workhorse for ExecWorkTableScan
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
static TupleTableSlot *
|
|
|
|
|
WorkTableScanNext(WorkTableScanState *node)
|
|
|
|
|
{
|
|
|
|
|
TupleTableSlot *slot;
|
|
|
|
|
Tuplestorestate *tuplestorestate;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* get information from the estate and scan state
|
2008-10-28 13:13:51 -04:00
|
|
|
*
|
|
|
|
|
* Note: we intentionally do not support backward scan. Although it would
|
|
|
|
|
* take only a couple more lines here, it would force nodeRecursiveunion.c
|
|
|
|
|
* to create the tuplestore with backward scan enabled, which has a
|
|
|
|
|
* performance cost. In practice backward scan is never useful for a
|
|
|
|
|
* worktable plan node, since it cannot appear high enough in the plan
|
|
|
|
|
* tree of a scrollable cursor to be exposed to a backward-scan
|
|
|
|
|
* requirement. So it's not worth expending effort to support it.
|
2009-03-27 14:30:21 -04:00
|
|
|
*
|
|
|
|
|
* Note: we are also assuming that this node is the only reader of the
|
|
|
|
|
* worktable. Therefore, we don't need a private read pointer for the
|
|
|
|
|
* tuplestore, nor do we need to tell tuplestore_gettupleslot to copy.
|
2008-10-04 17:56:55 -04:00
|
|
|
*/
|
2012-03-21 17:30:14 -04:00
|
|
|
Assert(ScanDirectionIsForward(node->ss.ps.state->es_direction));
|
2008-10-04 17:56:55 -04:00
|
|
|
|
|
|
|
|
tuplestorestate = node->rustate->working_table;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get the next tuple from tuplestore. Return NULL if no more tuples.
|
|
|
|
|
*/
|
|
|
|
|
slot = node->ss.ss_ScanTupleSlot;
|
2009-03-27 14:30:21 -04:00
|
|
|
(void) tuplestore_gettupleslot(tuplestorestate, true, false, slot);
|
2008-10-04 17:56:55 -04:00
|
|
|
return slot;
|
|
|
|
|
}
|
|
|
|
|
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-25 22:26:45 -04:00
|
|
|
/*
|
|
|
|
|
* WorkTableScanRecheck -- access method routine to recheck a tuple in EvalPlanQual
|
|
|
|
|
*/
|
|
|
|
|
static bool
|
|
|
|
|
WorkTableScanRecheck(WorkTableScanState *node, TupleTableSlot *slot)
|
|
|
|
|
{
|
|
|
|
|
/* nothing to check */
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-04 17:56:55 -04:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecWorkTableScan(node)
|
|
|
|
|
*
|
|
|
|
|
* Scans the worktable sequentially and returns the next qualifying tuple.
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-25 22:26:45 -04:00
|
|
|
* We call the ExecScan() routine and pass it the appropriate
|
|
|
|
|
* access method functions.
|
2008-10-04 17:56:55 -04:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
2017-07-17 03:33:49 -04:00
|
|
|
static TupleTableSlot *
|
|
|
|
|
ExecWorkTableScan(PlanState *pstate)
|
2008-10-04 17:56:55 -04:00
|
|
|
{
|
2017-07-17 03:33:49 -04:00
|
|
|
WorkTableScanState *node = castNode(WorkTableScanState, pstate);
|
|
|
|
|
|
2008-10-12 20:41:41 -04:00
|
|
|
/*
|
2009-06-11 10:49:15 -04:00
|
|
|
* On the first call, find the ancestor RecursiveUnion's state via the
|
2014-05-06 12:12:18 -04:00
|
|
|
* Param slot reserved for it. (We can't do this during node init because
|
2009-06-11 10:49:15 -04:00
|
|
|
* there are corner cases where we'll get the init call before the
|
|
|
|
|
* RecursiveUnion does.)
|
2008-10-12 20:41:41 -04:00
|
|
|
*/
|
|
|
|
|
if (node->rustate == NULL)
|
|
|
|
|
{
|
|
|
|
|
WorkTableScan *plan = (WorkTableScan *) node->ss.ps.plan;
|
|
|
|
|
EState *estate = node->ss.ps.state;
|
|
|
|
|
ParamExecData *param;
|
|
|
|
|
|
|
|
|
|
param = &(estate->es_param_exec_vals[plan->wtParam]);
|
|
|
|
|
Assert(param->execPlan == NULL);
|
|
|
|
|
Assert(!param->isnull);
|
2017-01-26 19:47:03 -05:00
|
|
|
node->rustate = castNode(RecursiveUnionState, DatumGetPointer(param->value));
|
|
|
|
|
Assert(node->rustate);
|
2008-10-12 20:41:41 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The scan tuple type (ie, the rowtype we expect to find in the work
|
|
|
|
|
* table) is the same as the result rowtype of the ancestor
|
|
|
|
|
* RecursiveUnion node. Note this depends on the assumption that
|
|
|
|
|
* RecursiveUnion doesn't allow projection.
|
|
|
|
|
*/
|
|
|
|
|
ExecAssignScanType(&node->ss,
|
|
|
|
|
ExecGetResultType(&node->rustate->ps));
|
|
|
|
|
|
|
|
|
|
/*
|
2009-06-11 10:49:15 -04:00
|
|
|
* Now we can initialize the projection info. This must be completed
|
|
|
|
|
* before we can call ExecScan().
|
2008-10-12 20:41:41 -04:00
|
|
|
*/
|
|
|
|
|
ExecAssignScanProjectionInfo(&node->ss);
|
|
|
|
|
}
|
|
|
|
|
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-25 22:26:45 -04:00
|
|
|
return ExecScan(&node->ss,
|
|
|
|
|
(ExecScanAccessMtd) WorkTableScanNext,
|
|
|
|
|
(ExecScanRecheckMtd) WorkTableScanRecheck);
|
2008-10-04 17:56:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecInitWorkTableScan
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
WorkTableScanState *
|
|
|
|
|
ExecInitWorkTableScan(WorkTableScan *node, EState *estate, int eflags)
|
|
|
|
|
{
|
|
|
|
|
WorkTableScanState *scanstate;
|
|
|
|
|
|
|
|
|
|
/* check for unsupported flags */
|
2008-10-28 13:13:51 -04:00
|
|
|
Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
|
2008-10-04 17:56:55 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* WorkTableScan should not have any children.
|
|
|
|
|
*/
|
|
|
|
|
Assert(outerPlan(node) == NULL);
|
|
|
|
|
Assert(innerPlan(node) == NULL);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* create new WorkTableScanState for node
|
|
|
|
|
*/
|
|
|
|
|
scanstate = makeNode(WorkTableScanState);
|
|
|
|
|
scanstate->ss.ps.plan = (Plan *) node;
|
|
|
|
|
scanstate->ss.ps.state = estate;
|
2017-07-17 03:33:49 -04:00
|
|
|
scanstate->ss.ps.ExecProcNode = ExecWorkTableScan;
|
2008-10-12 20:41:41 -04:00
|
|
|
scanstate->rustate = NULL; /* we'll set this later */
|
2008-10-04 17:56:55 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Miscellaneous initialization
|
|
|
|
|
*
|
|
|
|
|
* create expression context for node
|
|
|
|
|
*/
|
|
|
|
|
ExecAssignExprContext(estate, &scanstate->ss.ps);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* initialize child expressions
|
|
|
|
|
*/
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 18:45:36 -04:00
|
|
|
scanstate->ss.ps.qual =
|
|
|
|
|
ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
|
2008-10-04 17:56:55 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* tuple table initialization
|
|
|
|
|
*/
|
|
|
|
|
ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
|
|
|
|
|
ExecInitScanTupleSlot(estate, &scanstate->ss);
|
|
|
|
|
|
|
|
|
|
/*
|
2008-10-12 20:41:41 -04:00
|
|
|
* Initialize result tuple type, but not yet projection info.
|
2008-10-04 17:56:55 -04:00
|
|
|
*/
|
|
|
|
|
ExecAssignResultTypeFromTL(&scanstate->ss.ps);
|
|
|
|
|
|
|
|
|
|
return scanstate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
|
* ExecEndWorkTableScan
|
|
|
|
|
*
|
|
|
|
|
* frees any storage allocated through C routines.
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
ExecEndWorkTableScan(WorkTableScanState *node)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Free exprcontext
|
|
|
|
|
*/
|
|
|
|
|
ExecFreeExprContext(&node->ss.ps);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* clean out the tuple table
|
|
|
|
|
*/
|
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
|
|
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2010-07-12 13:01:06 -04:00
|
|
|
* ExecReScanWorkTableScan
|
2008-10-04 17:56:55 -04:00
|
|
|
*
|
|
|
|
|
* Rescans the relation.
|
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
2010-07-12 13:01:06 -04:00
|
|
|
ExecReScanWorkTableScan(WorkTableScanState *node)
|
2008-10-04 17:56:55 -04:00
|
|
|
{
|
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
Re-implement EvalPlanQual processing to improve its performance and eliminate
a lot of strange behaviors that occurred in join cases. We now identify the
"current" row for every joined relation in UPDATE, DELETE, and SELECT FOR
UPDATE/SHARE queries. If an EvalPlanQual recheck is necessary, we jam the
appropriate row into each scan node in the rechecking plan, forcing it to emit
only that one row. The former behavior could rescan the whole of each joined
relation for each recheck, which was terrible for performance, and what's much
worse could result in duplicated output tuples.
Also, the original implementation of EvalPlanQual could not re-use the recheck
execution tree --- it had to go through a full executor init and shutdown for
every row to be tested. To avoid this overhead, I've associated a special
runtime Param with each LockRows or ModifyTable plan node, and arranged to
make every scan node below such a node depend on that Param. Thus, by
signaling a change in that Param, the EPQ machinery can just rescan the
already-built test plan.
This patch also adds a prohibition on set-returning functions in the
targetlist of SELECT FOR UPDATE/SHARE. This is needed to avoid the
duplicate-output-tuple problem. It seems fairly reasonable since the
other restrictions on SELECT FOR UPDATE are meant to ensure that there
is a unique correspondence between source tuples and result tuples,
which an output SRF destroys as much as anything else does.
2009-10-25 22:26:45 -04:00
|
|
|
|
|
|
|
|
ExecScanReScan(&node->ss);
|
2008-10-23 11:29:23 -04:00
|
|
|
|
2008-10-12 20:41:41 -04:00
|
|
|
/* No need (or way) to rescan if ExecWorkTableScan not called yet */
|
|
|
|
|
if (node->rustate)
|
|
|
|
|
tuplestore_rescan(node->rustate->working_table);
|
2008-10-04 17:56:55 -04:00
|
|
|
}
|