1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* nodeResult.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* support for constant nodes needing special code.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* DESCRIPTION
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-07-12 04:37:39 +02:00
|
|
|
* Result nodes are used in queries where no relations are scanned.
|
|
|
|
* Examples of such queries are:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-04-24 13:46:21 +02:00
|
|
|
* select 1 * 2
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-04-24 17:32:07 +02:00
|
|
|
* insert into emp values ('mike', 15000)
|
|
|
|
*
|
|
|
|
* (Remember that in an INSERT or UPDATE, we need a plan tree that
|
|
|
|
* generates the new rows.)
|
|
|
|
*
|
2000-07-12 04:37:39 +02:00
|
|
|
* Result nodes are also used to optimise queries with constant
|
|
|
|
* qualifications (ie, quals that do not depend on the scanned data),
|
|
|
|
* such as:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-04-24 13:46:21 +02:00
|
|
|
* select * from emp where 2 > 1
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* In this case, the plan generated is
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Result (with 2 > 1 qual)
|
|
|
|
* /
|
2005-04-24 17:32:07 +02:00
|
|
|
* SeqScan (emp.*)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-04-24 13:46:21 +02:00
|
|
|
* At runtime, the Result node evaluates the constant qual once,
|
|
|
|
* which is shown by EXPLAIN as a One-Time Filter. If it's
|
|
|
|
* false, we can return an empty result set without running the
|
|
|
|
* controlled plan at all. If it's true, we run the controlled
|
2000-07-12 04:37:39 +02:00
|
|
|
* plan normally and pass back the results.
|
|
|
|
*
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
|
2000-07-12 04:37:39 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/nodeResult.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-10-31 11:12:26 +01:00
|
|
|
|
2000-07-12 04:37:39 +02:00
|
|
|
#include "postgres.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeResult.h"
|
2017-07-26 02:37:17 +02:00
|
|
|
#include "miscadmin.h"
|
2000-07-17 05:05:41 +02:00
|
|
|
#include "utils/memutils.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecResult(node)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-07-12 04:37:39 +02:00
|
|
|
* returns the tuples from the outer plan which satisfy the
|
1997-09-07 07:04:48 +02:00
|
|
|
* qualification clause. Since result nodes with right
|
|
|
|
* subtrees are never planned, we ignore the right subtree
|
|
|
|
* entirely (for now).. -cim 10/7/89
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* The qualification containing only constant clauses are
|
|
|
|
* checked first before any processing is done. It always returns
|
|
|
|
* 'nil' if the constant qualification is not satisfied.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot *
|
|
|
|
ExecResult(PlanState *pstate)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
ResultState *node = castNode(ResultState, pstate);
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleTableSlot *outerTupleSlot;
|
2003-08-04 02:43:34 +02:00
|
|
|
PlanState *outerPlan;
|
1997-09-08 04:41:22 +02:00
|
|
|
ExprContext *econtext;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2017-07-26 02:37:17 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
econtext = node->ps.ps_ExprContext;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* check constant qualifications like (2 > 1), if not already done
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
if (node->rs_checkqual)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
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 23:45:36 +01:00
|
|
|
bool qualResult = ExecQual(node->resconstantqual, econtext);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
node->rs_checkqual = false;
|
|
|
|
if (!qualResult)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->rs_done = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
return NULL;
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Reset per-tuple memory context to free any expression evaluation
|
2017-01-19 23:12:38 +01:00
|
|
|
* storage allocated in the previous tuple cycle.
|
2000-08-24 05:29:15 +02:00
|
|
|
*/
|
|
|
|
ResetExprContext(econtext);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* if rs_done is true then it means that we were asked to return a
|
|
|
|
* constant tuple and we already did the last time ExecResult() was
|
2005-10-15 04:49:52 +02:00
|
|
|
* called, OR that we failed the constant qual check. Either way, now we
|
|
|
|
* are through.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
while (!node->rs_done)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
outerPlan = outerPlanState(node);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (outerPlan != NULL)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* retrieve tuples from the outer plan until there are no more.
|
2000-07-12 04:37:39 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
outerTupleSlot = ExecProcNode(outerPlan);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (TupIsNull(outerTupleSlot))
|
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* prepare to compute projection expressions, which will expect to
|
|
|
|
* access the input tuples as varno OUTER.
|
2000-07-12 04:37:39 +02:00
|
|
|
*/
|
|
|
|
econtext->ecxt_outertuple = outerTupleSlot;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* if we don't have an outer plan, then we are just generating the
|
|
|
|
* results from a constant target list. Do it only once.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
node->rs_done = true;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 23:12:38 +01:00
|
|
|
/* form the result tuple using ExecProject(), and return it */
|
|
|
|
return ExecProject(node->ps.ps_ProjInfo);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2007-02-15 04:07:13 +01:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecResultMarkPos
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecResultMarkPos(ResultState *node)
|
|
|
|
{
|
|
|
|
PlanState *outerPlan = outerPlanState(node);
|
|
|
|
|
|
|
|
if (outerPlan != NULL)
|
|
|
|
ExecMarkPos(outerPlan);
|
|
|
|
else
|
|
|
|
elog(DEBUG2, "Result nodes do not support mark/restore");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecResultRestrPos
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecResultRestrPos(ResultState *node)
|
|
|
|
{
|
|
|
|
PlanState *outerPlan = outerPlanState(node);
|
|
|
|
|
|
|
|
if (outerPlan != NULL)
|
|
|
|
ExecRestrPos(outerPlan);
|
|
|
|
else
|
|
|
|
elog(ERROR, "Result nodes do not support mark/restore");
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitResult
|
|
|
|
*
|
|
|
|
* Creates the run-time state information for the result node
|
2006-02-28 05:10:28 +01:00
|
|
|
* produced by the planner and initializes outer relations
|
1997-09-07 07:04:48 +02:00
|
|
|
* (child nodes).
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ResultState *
|
2006-02-28 05:10:28 +01:00
|
|
|
ExecInitResult(Result *node, EState *estate, int eflags)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
ResultState *resstate;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-02-28 05:10:28 +01:00
|
|
|
/* check for unsupported flags */
|
2007-02-15 04:07:13 +01:00
|
|
|
Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD)) ||
|
|
|
|
outerPlan(node) != NULL);
|
2006-02-28 05:10:28 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* create state structure
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
resstate = makeNode(ResultState);
|
2002-12-05 16:50:39 +01:00
|
|
|
resstate->ps.plan = (Plan *) node;
|
|
|
|
resstate->ps.state = estate;
|
2017-07-17 09:33:49 +02:00
|
|
|
resstate->ps.ExecProcNode = ExecResult;
|
2002-12-05 16:50:39 +01:00
|
|
|
|
1998-02-13 04:26:53 +01:00
|
|
|
resstate->rs_done = false;
|
|
|
|
resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Miscellaneous initialization
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2001-03-22 07:16:21 +01:00
|
|
|
* create expression context for node
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecAssignExprContext(estate, &resstate->ps);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tuple table initialization
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecInitResultTupleSlot(estate, &resstate->ps);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* initialize child expressions
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
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 23:45:36 +01:00
|
|
|
resstate->ps.qual =
|
|
|
|
ExecInitQual(node->plan.qual, (PlanState *) resstate);
|
|
|
|
resstate->resconstantqual =
|
|
|
|
ExecInitQual((List *) node->resconstantqual, (PlanState *) resstate);
|
2002-12-05 16:50:39 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize child nodes
|
|
|
|
*/
|
2006-02-28 05:10:28 +01:00
|
|
|
outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate, eflags);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we don't use inner plan
|
|
|
|
*/
|
|
|
|
Assert(innerPlan(node) == NULL);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* initialize tuple type and projection info
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecAssignResultTypeFromTL(&resstate->ps);
|
2007-02-02 01:07:03 +01:00
|
|
|
ExecAssignProjectionInfo(&resstate->ps, NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
return resstate;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecEndResult
|
|
|
|
*
|
2000-07-12 04:37:39 +02:00
|
|
|
* frees up storage allocated through C routines
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndResult(ResultState *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-15 17:17:59 +01:00
|
|
|
* Free the exprcontext
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecFreeExprContext(&node->ps);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* clean out the tuple table
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecClearTuple(node->ps.ps_ResultTupleSlot);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* shut down subplans
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndNode(outerPlanState(node));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-02-13 04:26:53 +01:00
|
|
|
|
|
|
|
void
|
2010-07-12 19:01:06 +02:00
|
|
|
ExecReScanResult(ResultState *node)
|
1998-02-13 04:26:53 +01:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
node->rs_done = false;
|
|
|
|
node->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
/*
|
2007-02-16 04:49:04 +01:00
|
|
|
* If chgParam of subnode is not null then plan will be re-scanned by
|
2010-07-12 19:01:06 +02:00
|
|
|
* first ExecProcNode.
|
1998-02-13 04:26:53 +01:00
|
|
|
*/
|
2007-02-16 04:49:04 +01:00
|
|
|
if (node->ps.lefttree &&
|
2010-07-12 19:01:06 +02:00
|
|
|
node->ps.lefttree->chgParam == NULL)
|
|
|
|
ExecReScan(node->ps.lefttree);
|
1998-02-13 04:26:53 +01:00
|
|
|
}
|