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 * Result nodes are also used to optimise queries with constant
14 * qualifications (ie, quals that do not depend on the scanned data),
17 * select * from emp where 2 > 1
19 * In this case, the plan generated is
21 * Result (with 2 > 1 qual)
25 * At runtime, the Result node evaluates the constant qual once,
26 * which is shown by EXPLAIN as a One-Time Filter. If it's
27 * false, we can return an empty result set without running the
28 * controlled plan at all. If it's true, we run the controlled
29 * plan normally and pass back the results.
32 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
33 * Portions Copyright (c) 1994, Regents of the University of California
36 * $PostgreSQL: pgsql/src/backend/executor/nodeResult.c,v 1.30 2005/04/24 11:46:21 neilc Exp $
38 *-------------------------------------------------------------------------
43 #include "executor/executor.h"
44 #include "executor/nodeResult.h"
45 #include "utils/memutils.h"
48 /* ----------------------------------------------------------------
51 * returns the tuples from the outer plan which satisfy the
52 * qualification clause. Since result nodes with right
53 * subtrees are never planned, we ignore the right subtree
54 * entirely (for now).. -cim 10/7/89
56 * The qualification containing only constant clauses are
57 * checked first before any processing is done. It always returns
58 * 'nil' if the constant qualification is not satisfied.
59 * ----------------------------------------------------------------
62 ExecResult(ResultState *node)
64 TupleTableSlot *outerTupleSlot;
65 TupleTableSlot *resultSlot;
67 ExprContext *econtext;
70 econtext = node->ps.ps_ExprContext;
73 * check constant qualifications like (2 > 1), if not already done
75 if (node->rs_checkqual)
77 bool qualResult = ExecQual((List *) node->resconstantqual,
81 node->rs_checkqual = false;
90 * Check to see if we're still projecting out tuples from a previous
91 * scan tuple (because there is a function-returning-set in the
92 * projection expressions). If so, try to project another one.
94 if (node->ps.ps_TupFromTlist)
96 resultSlot = ExecProject(node->ps.ps_ProjInfo, &isDone);
97 if (isDone == ExprMultipleResult)
99 /* Done with that source tuple... */
100 node->ps.ps_TupFromTlist = false;
104 * Reset per-tuple memory context to free any expression evaluation
105 * storage allocated in the previous tuple cycle. Note this can't
106 * happen until we're done projecting out tuples from a scan tuple.
108 ResetExprContext(econtext);
111 * if rs_done is true then it means that we were asked to return a
112 * constant tuple and we already did the last time ExecResult() was
113 * called, OR that we failed the constant qual check. Either way, now
116 while (!node->rs_done)
118 outerPlan = outerPlanState(node);
120 if (outerPlan != NULL)
123 * retrieve tuples from the outer plan until there are no
126 outerTupleSlot = ExecProcNode(outerPlan);
128 if (TupIsNull(outerTupleSlot))
131 node->ps.ps_OuterTupleSlot = outerTupleSlot;
134 * XXX gross hack. use outer tuple as scan tuple for
137 econtext->ecxt_outertuple = outerTupleSlot;
138 econtext->ecxt_scantuple = outerTupleSlot;
143 * if we don't have an outer plan, then we are just generating
144 * the results from a constant target list. Do it only once.
146 node->rs_done = true;
150 * form the result tuple using ExecProject(), and return it ---
151 * unless the projection produces an empty set, in which case we
152 * must loop back to see if there are more outerPlan tuples.
154 resultSlot = ExecProject(node->ps.ps_ProjInfo, &isDone);
156 if (isDone != ExprEndResult)
158 node->ps.ps_TupFromTlist = (isDone == ExprMultipleResult);
166 /* ----------------------------------------------------------------
169 * Creates the run-time state information for the result node
170 * produced by the planner and initailizes outer relations
172 * ----------------------------------------------------------------
175 ExecInitResult(Result *node, EState *estate)
177 ResultState *resstate;
180 * create state structure
182 resstate = makeNode(ResultState);
183 resstate->ps.plan = (Plan *) node;
184 resstate->ps.state = estate;
186 resstate->rs_done = false;
187 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
190 * Miscellaneous initialization
192 * create expression context for node
194 ExecAssignExprContext(estate, &resstate->ps);
196 #define RESULT_NSLOTS 1
199 * tuple table initialization
201 ExecInitResultTupleSlot(estate, &resstate->ps);
204 * initialize child expressions
206 resstate->ps.targetlist = (List *)
207 ExecInitExpr((Expr *) node->plan.targetlist,
208 (PlanState *) resstate);
209 resstate->ps.qual = (List *)
210 ExecInitExpr((Expr *) node->plan.qual,
211 (PlanState *) resstate);
212 resstate->resconstantqual = ExecInitExpr((Expr *) node->resconstantqual,
213 (PlanState *) resstate);
216 * initialize child nodes
218 outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate);
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);
235 ExecCountSlotsResult(Result *node)
237 return ExecCountSlotsNode(outerPlan(node)) + RESULT_NSLOTS;
240 /* ----------------------------------------------------------------
243 * frees up storage allocated through C routines
244 * ----------------------------------------------------------------
247 ExecEndResult(ResultState *node)
250 * Free the exprcontext
252 ExecFreeExprContext(&node->ps);
255 * clean out the tuple table
257 ExecClearTuple(node->ps.ps_ResultTupleSlot);
262 ExecEndNode(outerPlanState(node));
266 ExecReScanResult(ResultState *node, ExprContext *exprCtxt)
268 node->rs_done = false;
269 node->ps.ps_TupFromTlist = false;
270 node->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
273 * if chgParam of subnode is not null then plan will be re-scanned by
274 * first ExecProcNode.
276 if (((PlanState *) node)->lefttree &&
277 ((PlanState *) node)->lefttree->chgParam == NULL)
278 ExecReScan(((PlanState *) node)->lefttree, exprCtxt);