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 * append emp (name = "mike", salary = 15000)
15 * Result nodes are also used to optimise queries with constant
16 * qualifications (ie, quals that do not depend on the scanned data),
19 * retrieve (emp.all) where 2 > 1
21 * In this case, the plan generated is
23 * Result (with 2 > 1 qual)
27 * At runtime, the Result node evaluates the constant qual once.
28 * If it's false, we can return an empty result set without running
29 * the controlled plan at all. If it's true, we run the controlled
30 * plan normally and pass back the results.
33 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
34 * Portions Copyright (c) 1994, Regents of the University of California
37 * $Header: /cvsroot/pgsql/src/backend/executor/nodeResult.c,v 1.15 2000/07/17 03:04:53 tgl Exp $
39 *-------------------------------------------------------------------------
44 #include "executor/executor.h"
45 #include "executor/nodeResult.h"
46 #include "utils/memutils.h"
49 /* ----------------------------------------------------------------
52 * returns the tuples from the outer plan which satisfy the
53 * qualification clause. Since result nodes with right
54 * subtrees are never planned, we ignore the right subtree
55 * entirely (for now).. -cim 10/7/89
57 * The qualification containing only constant clauses are
58 * checked first before any processing is done. It always returns
59 * 'nil' if the constant qualification is not satisfied.
60 * ----------------------------------------------------------------
63 ExecResult(Result *node)
65 ResultState *resstate;
66 TupleTableSlot *outerTupleSlot;
67 TupleTableSlot *resultSlot;
69 ExprContext *econtext;
71 ProjectionInfo *projInfo;
74 * initialize the result node's state
77 resstate = node->resstate;
78 econtext = resstate->cstate.cs_ExprContext;
81 * Reset per-tuple memory context to free any expression evaluation
82 * storage allocated in the previous tuple cycle.
85 ResetExprContext(econtext);
88 * check constant qualifications like (2 > 1), if not already done
91 if (resstate->rs_checkqual)
93 bool qualResult = ExecQual((List *) node->resconstantqual,
97 resstate->rs_checkqual = false;
98 if (qualResult == false)
100 resstate->rs_done = true;
106 * Check to see if we're still projecting out tuples from a previous
107 * scan tuple (because there is a function-returning-set in the
108 * projection expressions). If so, try to project another one.
111 if (resstate->cstate.cs_TupFromTlist)
113 resultSlot = ExecProject(resstate->cstate.cs_ProjInfo, &isDone);
116 /* Done with that source tuple... */
117 resstate->cstate.cs_TupFromTlist = false;
121 * if rs_done is true then it means that we were asked to return
122 * a constant tuple and we already did the last time ExecResult()
123 * was called, OR that we failed the constant qual check.
124 * Either way, now we are through.
127 if (!resstate->rs_done)
129 outerPlan = outerPlan(node);
131 if (outerPlan != NULL)
134 * retrieve tuples from the outer plan until there are no more.
137 outerTupleSlot = ExecProcNode(outerPlan, (Plan *) node);
139 if (TupIsNull(outerTupleSlot))
142 resstate->cstate.cs_OuterTupleSlot = outerTupleSlot;
145 * XXX gross hack. use outer tuple as scan tuple for projection
148 econtext->ecxt_outertuple = outerTupleSlot;
149 econtext->ecxt_scantuple = outerTupleSlot;
154 * if we don't have an outer plan, then we are just generating
155 * the results from a constant target list. Do it only once.
158 resstate->rs_done = true;
162 * form the result tuple using ExecProject(), and return it.
165 projInfo = resstate->cstate.cs_ProjInfo;
166 resultSlot = ExecProject(projInfo, &isDone);
167 resstate->cstate.cs_TupFromTlist = !isDone;
174 /* ----------------------------------------------------------------
177 * Creates the run-time state information for the result node
178 * produced by the planner and initailizes outer relations
180 * ----------------------------------------------------------------
183 ExecInitResult(Result *node, EState *estate, Plan *parent)
185 ResultState *resstate;
188 * assign execution state to node
191 node->plan.state = estate;
194 * create new ResultState for node
197 resstate = makeNode(ResultState);
198 resstate->rs_done = false;
199 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
200 node->resstate = resstate;
203 * Miscellaneous initialization
205 * + create expression context for node
208 ExecAssignExprContext(estate, &resstate->cstate);
210 #define RESULT_NSLOTS 1
212 * tuple table initialization
215 ExecInitResultTupleSlot(estate, &resstate->cstate);
218 * then initialize children
221 ExecInitNode(outerPlan(node), estate, (Plan *) node);
224 * we don't use inner plan
226 Assert(innerPlan(node) == NULL);
229 * initialize tuple type and projection info
232 ExecAssignResultTypeFromTL((Plan *) node, &resstate->cstate);
233 ExecAssignProjectionInfo((Plan *) node, &resstate->cstate);
239 ExecCountSlotsResult(Result *node)
241 return ExecCountSlotsNode(outerPlan(node)) + RESULT_NSLOTS;
244 /* ----------------------------------------------------------------
247 * frees up storage allocated through C routines
248 * ----------------------------------------------------------------
251 ExecEndResult(Result *node)
253 ResultState *resstate;
255 resstate = node->resstate;
258 * Free the projection info
260 * Note: we don't ExecFreeResultType(resstate)
261 * because the rule manager depends on the tupType
262 * returned by ExecMain(). So for now, this
263 * is freed at end-transaction time. -cim 6/2/91
266 ExecFreeProjectionInfo(&resstate->cstate);
267 ExecFreeExprContext(&resstate->cstate);
273 ExecEndNode(outerPlan(node), (Plan *) node);
276 * clean out the tuple table
279 ExecClearTuple(resstate->cstate.cs_ResultTupleSlot);
281 node->resstate = NULL; /* XXX - new for us - er1p */
285 ExecReScanResult(Result *node, ExprContext *exprCtxt, Plan *parent)
287 ResultState *resstate = node->resstate;
289 resstate->rs_done = false;
290 resstate->cstate.cs_TupFromTlist = false;
291 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
294 * if chgParam of subnode is not null then plan will be re-scanned by
295 * first ExecProcNode.
297 if (((Plan *) node)->lefttree &&
298 ((Plan *) node)->lefttree->chgParam == NULL)
299 ExecReScan(((Plan *) node)->lefttree, exprCtxt, (Plan *) node);