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-2001, PostgreSQL Global Development Group
34 * Portions Copyright (c) 1994, Regents of the University of California
37 * $Header: /cvsroot/pgsql/src/backend/executor/nodeResult.c,v 1.18 2001/03/22 03:59:29 momjian 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;
73 * initialize the result node's state
76 resstate = node->resstate;
77 econtext = resstate->cstate.cs_ExprContext;
80 * check constant qualifications like (2 > 1), if not already done
83 if (resstate->rs_checkqual)
85 bool qualResult = ExecQual((List *) node->resconstantqual,
89 resstate->rs_checkqual = false;
90 if (qualResult == false)
92 resstate->rs_done = true;
98 * Check to see if we're still projecting out tuples from a previous
99 * scan tuple (because there is a function-returning-set in the
100 * projection expressions). If so, try to project another one.
103 if (resstate->cstate.cs_TupFromTlist)
105 resultSlot = ExecProject(resstate->cstate.cs_ProjInfo, &isDone);
106 if (isDone == ExprMultipleResult)
108 /* Done with that source tuple... */
109 resstate->cstate.cs_TupFromTlist = false;
113 * Reset per-tuple memory context to free any expression evaluation
114 * storage allocated in the previous tuple cycle. Note this can't
115 * happen until we're done projecting out tuples from a scan tuple.
118 ResetExprContext(econtext);
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 while (!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
163 * --- unless the projection produces an empty set, in which case
164 * we must loop back to see if there are more outerPlan tuples.
167 resultSlot = ExecProject(resstate->cstate.cs_ProjInfo, &isDone);
169 if (isDone != ExprEndResult)
171 resstate->cstate.cs_TupFromTlist = (isDone == ExprMultipleResult);
179 /* ----------------------------------------------------------------
182 * Creates the run-time state information for the result node
183 * produced by the planner and initailizes outer relations
185 * ----------------------------------------------------------------
188 ExecInitResult(Result *node, EState *estate, Plan *parent)
190 ResultState *resstate;
193 * assign execution state to node
196 node->plan.state = estate;
199 * create new ResultState for node
202 resstate = makeNode(ResultState);
203 resstate->rs_done = false;
204 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
205 node->resstate = resstate;
208 * Miscellaneous initialization
210 * + create expression context for node
213 ExecAssignExprContext(estate, &resstate->cstate);
215 #define RESULT_NSLOTS 1
217 * tuple table initialization
220 ExecInitResultTupleSlot(estate, &resstate->cstate);
223 * then initialize children
226 ExecInitNode(outerPlan(node), estate, (Plan *) node);
229 * we don't use inner plan
231 Assert(innerPlan(node) == NULL);
234 * initialize tuple type and projection info
237 ExecAssignResultTypeFromTL((Plan *) node, &resstate->cstate);
238 ExecAssignProjectionInfo((Plan *) node, &resstate->cstate);
244 ExecCountSlotsResult(Result *node)
246 return ExecCountSlotsNode(outerPlan(node)) + RESULT_NSLOTS;
249 /* ----------------------------------------------------------------
252 * frees up storage allocated through C routines
253 * ----------------------------------------------------------------
256 ExecEndResult(Result *node)
258 ResultState *resstate;
260 resstate = node->resstate;
263 * Free the projection info
265 * Note: we don't ExecFreeResultType(resstate)
266 * because the rule manager depends on the tupType
267 * returned by ExecMain(). So for now, this
268 * is freed at end-transaction time. -cim 6/2/91
271 ExecFreeProjectionInfo(&resstate->cstate);
272 ExecFreeExprContext(&resstate->cstate);
278 ExecEndNode(outerPlan(node), (Plan *) node);
281 * clean out the tuple table
284 ExecClearTuple(resstate->cstate.cs_ResultTupleSlot);
286 node->resstate = NULL; /* XXX - new for us - er1p */
290 ExecReScanResult(Result *node, ExprContext *exprCtxt, Plan *parent)
292 ResultState *resstate = node->resstate;
294 resstate->rs_done = false;
295 resstate->cstate.cs_TupFromTlist = false;
296 resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
299 * if chgParam of subnode is not null then plan will be re-scanned by
300 * first ExecProcNode.
302 if (((Plan *) node)->lefttree &&
303 ((Plan *) node)->lefttree->chgParam == NULL)
304 ExecReScan(((Plan *) node)->lefttree, exprCtxt, (Plan *) node);