1 /*-------------------------------------------------------------------------
4 * support for constant nodes needing special code.
8 * Result nodes are used in queries where no relations are scanned.
9 * Examples of such queries are:
13 * insert into emp values ('mike', 15000)
15 * (Remember that in an INSERT or UPDATE, we need a plan tree that
16 * generates the new rows.)
18 * Result nodes are also used to optimise queries with constant
19 * qualifications (ie, quals that do not depend on the scanned data),
22 * select * from emp where 2 > 1
24 * In this case, the plan generated is
26 * Result (with 2 > 1 qual)
30 * At runtime, the Result node evaluates the constant qual once,
31 * which is shown by EXPLAIN as a One-Time Filter. If it's
32 * false, we can return an empty result set without running the
33 * controlled plan at all. If it's true, we run the controlled
34 * plan normally and pass back the results.
37 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
38 * Portions Copyright (c) 1994, Regents of the University of California
41 * src/backend/executor/nodeResult.c
43 *-------------------------------------------------------------------------
48 #include "executor/executor.h"
49 #include "executor/nodeResult.h"
50 #include "utils/memutils.h"
53 /* ----------------------------------------------------------------
56 * returns the tuples from the outer plan which satisfy the
57 * qualification clause. Since result nodes with right
58 * subtrees are never planned, we ignore the right subtree
59 * entirely (for now).. -cim 10/7/89
61 * The qualification containing only constant clauses are
62 * checked first before any processing is done. It always returns
63 * 'nil' if the constant qualification is not satisfied.
64 * ----------------------------------------------------------------
67 ExecResult(ResultState *node)
69 TupleTableSlot *outerTupleSlot;
71 ExprContext *econtext;
73 econtext = node->ps.ps_ExprContext;
76 * check constant qualifications like (2 > 1), if not already done
78 if (node->rs_checkqual)
80 bool qualResult = ExecQual(node->resconstantqual, econtext);
82 node->rs_checkqual = false;
91 * Reset per-tuple memory context to free any expression evaluation
92 * storage allocated in the previous tuple cycle.
94 ResetExprContext(econtext);
97 * if rs_done is true then it means that we were asked to return a
98 * constant tuple and we already did the last time ExecResult() was
99 * called, OR that we failed the constant qual check. Either way, now we
102 while (!node->rs_done)
104 outerPlan = outerPlanState(node);
106 if (outerPlan != NULL)
109 * retrieve tuples from the outer plan until there are no more.
111 outerTupleSlot = ExecProcNode(outerPlan);
113 if (TupIsNull(outerTupleSlot))
117 * prepare to compute projection expressions, which will expect to
118 * access the input tuples as varno OUTER.
120 econtext->ecxt_outertuple = outerTupleSlot;
125 * if we don't have an outer plan, then we are just generating the
126 * results from a constant target list. Do it only once.
128 node->rs_done = true;
131 /* form the result tuple using ExecProject(), and return it */
132 return ExecProject(node->ps.ps_ProjInfo);
138 /* ----------------------------------------------------------------
140 * ----------------------------------------------------------------
143 ExecResultMarkPos(ResultState *node)
145 PlanState *outerPlan = outerPlanState(node);
147 if (outerPlan != NULL)
148 ExecMarkPos(outerPlan);
150 elog(DEBUG2, "Result nodes do not support mark/restore");
153 /* ----------------------------------------------------------------
155 * ----------------------------------------------------------------
158 ExecResultRestrPos(ResultState *node)
160 PlanState *outerPlan = outerPlanState(node);
162 if (outerPlan != NULL)
163 ExecRestrPos(outerPlan);
165 elog(ERROR, "Result nodes do not support mark/restore");
168 /* ----------------------------------------------------------------
171 * Creates the run-time state information for the result node
172 * produced by the planner and initializes outer relations
174 * ----------------------------------------------------------------
177 ExecInitResult(Result *node, EState *estate, int eflags)
179 ResultState *resstate;
181 /* check for unsupported flags */
182 Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD)) ||
183 outerPlan(node) != NULL);
186 * create state structure
188 resstate = makeNode(ResultState);
189 resstate->ps.plan = (Plan *) node;
190 resstate->ps.state = estate;
192 resstate->rs_done = false;
193 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
196 * Miscellaneous initialization
198 * create expression context for node
200 ExecAssignExprContext(estate, &resstate->ps);
203 * tuple table initialization
205 ExecInitResultTupleSlot(estate, &resstate->ps);
208 * initialize child expressions
211 ExecInitQual(node->plan.qual, (PlanState *) resstate);
212 resstate->resconstantqual =
213 ExecInitQual((List *) node->resconstantqual, (PlanState *) resstate);
216 * initialize child nodes
218 outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate, eflags);
221 * we don't use inner plan
223 Assert(innerPlan(node) == NULL);
226 * initialize tuple type and projection info
228 ExecAssignResultTypeFromTL(&resstate->ps);
229 ExecAssignProjectionInfo(&resstate->ps, NULL);
234 /* ----------------------------------------------------------------
237 * frees up storage allocated through C routines
238 * ----------------------------------------------------------------
241 ExecEndResult(ResultState *node)
244 * Free the exprcontext
246 ExecFreeExprContext(&node->ps);
249 * clean out the tuple table
251 ExecClearTuple(node->ps.ps_ResultTupleSlot);
256 ExecEndNode(outerPlanState(node));
260 ExecReScanResult(ResultState *node)
262 node->rs_done = false;
263 node->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
266 * If chgParam of subnode is not null then plan will be re-scanned by
267 * first ExecProcNode.
269 if (node->ps.lefttree &&
270 node->ps.lefttree->chgParam == NULL)
271 ExecReScan(node->ps.lefttree);