PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeResult.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeResult.c
4  * support for constant nodes needing special code.
5  *
6  * DESCRIPTION
7  *
8  * Result nodes are used in queries where no relations are scanned.
9  * Examples of such queries are:
10  *
11  * select 1 * 2
12  *
13  * insert into emp values ('mike', 15000)
14  *
15  * (Remember that in an INSERT or UPDATE, we need a plan tree that
16  * generates the new rows.)
17  *
18  * Result nodes are also used to optimise queries with constant
19  * qualifications (ie, quals that do not depend on the scanned data),
20  * such as:
21  *
22  * select * from emp where 2 > 1
23  *
24  * In this case, the plan generated is
25  *
26  * Result (with 2 > 1 qual)
27  * /
28  * SeqScan (emp.*)
29  *
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.
35  *
36  *
37  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
38  * Portions Copyright (c) 1994, Regents of the University of California
39  *
40  * IDENTIFICATION
41  * src/backend/executor/nodeResult.c
42  *
43  *-------------------------------------------------------------------------
44  */
45 
46 #include "postgres.h"
47 
48 #include "executor/executor.h"
49 #include "executor/nodeResult.h"
50 #include "utils/memutils.h"
51 
52 
53 /* ----------------------------------------------------------------
54  * ExecResult(node)
55  *
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
60  *
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  * ----------------------------------------------------------------
65  */
68 {
69  TupleTableSlot *outerTupleSlot;
71  ExprContext *econtext;
72 
73  econtext = node->ps.ps_ExprContext;
74 
75  /*
76  * check constant qualifications like (2 > 1), if not already done
77  */
78  if (node->rs_checkqual)
79  {
80  bool qualResult = ExecQual((List *) node->resconstantqual,
81  econtext,
82  false);
83 
84  node->rs_checkqual = false;
85  if (!qualResult)
86  {
87  node->rs_done = true;
88  return NULL;
89  }
90  }
91 
92  /*
93  * Reset per-tuple memory context to free any expression evaluation
94  * storage allocated in the previous tuple cycle.
95  */
96  ResetExprContext(econtext);
97 
98  /*
99  * if rs_done is true then it means that we were asked to return a
100  * constant tuple and we already did the last time ExecResult() was
101  * called, OR that we failed the constant qual check. Either way, now we
102  * are through.
103  */
104  while (!node->rs_done)
105  {
106  outerPlan = outerPlanState(node);
107 
108  if (outerPlan != NULL)
109  {
110  /*
111  * retrieve tuples from the outer plan until there are no more.
112  */
113  outerTupleSlot = ExecProcNode(outerPlan);
114 
115  if (TupIsNull(outerTupleSlot))
116  return NULL;
117 
118  /*
119  * prepare to compute projection expressions, which will expect to
120  * access the input tuples as varno OUTER.
121  */
122  econtext->ecxt_outertuple = outerTupleSlot;
123  }
124  else
125  {
126  /*
127  * if we don't have an outer plan, then we are just generating the
128  * results from a constant target list. Do it only once.
129  */
130  node->rs_done = true;
131  }
132 
133  /* form the result tuple using ExecProject(), and return it */
134  return ExecProject(node->ps.ps_ProjInfo);
135  }
136 
137  return NULL;
138 }
139 
140 /* ----------------------------------------------------------------
141  * ExecResultMarkPos
142  * ----------------------------------------------------------------
143  */
144 void
146 {
148 
149  if (outerPlan != NULL)
150  ExecMarkPos(outerPlan);
151  else
152  elog(DEBUG2, "Result nodes do not support mark/restore");
153 }
154 
155 /* ----------------------------------------------------------------
156  * ExecResultRestrPos
157  * ----------------------------------------------------------------
158  */
159 void
161 {
163 
164  if (outerPlan != NULL)
165  ExecRestrPos(outerPlan);
166  else
167  elog(ERROR, "Result nodes do not support mark/restore");
168 }
169 
170 /* ----------------------------------------------------------------
171  * ExecInitResult
172  *
173  * Creates the run-time state information for the result node
174  * produced by the planner and initializes outer relations
175  * (child nodes).
176  * ----------------------------------------------------------------
177  */
178 ResultState *
179 ExecInitResult(Result *node, EState *estate, int eflags)
180 {
181  ResultState *resstate;
182 
183  /* check for unsupported flags */
184  Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD)) ||
185  outerPlan(node) != NULL);
186 
187  /*
188  * create state structure
189  */
190  resstate = makeNode(ResultState);
191  resstate->ps.plan = (Plan *) node;
192  resstate->ps.state = estate;
193 
194  resstate->rs_done = false;
195  resstate->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
196 
197  /*
198  * Miscellaneous initialization
199  *
200  * create expression context for node
201  */
202  ExecAssignExprContext(estate, &resstate->ps);
203 
204  /*
205  * tuple table initialization
206  */
207  ExecInitResultTupleSlot(estate, &resstate->ps);
208 
209  /*
210  * initialize child expressions
211  */
212  resstate->ps.targetlist = (List *)
213  ExecInitExpr((Expr *) node->plan.targetlist,
214  (PlanState *) resstate);
215  resstate->ps.qual = (List *)
216  ExecInitExpr((Expr *) node->plan.qual,
217  (PlanState *) resstate);
218  resstate->resconstantqual = ExecInitExpr((Expr *) node->resconstantqual,
219  (PlanState *) resstate);
220 
221  /*
222  * initialize child nodes
223  */
224  outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate, eflags);
225 
226  /*
227  * we don't use inner plan
228  */
229  Assert(innerPlan(node) == NULL);
230 
231  /*
232  * initialize tuple type and projection info
233  */
234  ExecAssignResultTypeFromTL(&resstate->ps);
235  ExecAssignProjectionInfo(&resstate->ps, NULL);
236 
237  return resstate;
238 }
239 
240 /* ----------------------------------------------------------------
241  * ExecEndResult
242  *
243  * frees up storage allocated through C routines
244  * ----------------------------------------------------------------
245  */
246 void
248 {
249  /*
250  * Free the exprcontext
251  */
252  ExecFreeExprContext(&node->ps);
253 
254  /*
255  * clean out the tuple table
256  */
258 
259  /*
260  * shut down subplans
261  */
263 }
264 
265 void
267 {
268  node->rs_done = false;
269  node->rs_checkqual = (node->resconstantqual == NULL) ? false : true;
270 
271  /*
272  * If chgParam of subnode is not null then plan will be re-scanned by
273  * first ExecProcNode.
274  */
275  if (node->ps.lefttree &&
276  node->ps.lefttree->chgParam == NULL)
277  ExecReScan(node->ps.lefttree);
278 }
Plan plan
Definition: plannodes.h:175
List * qual
Definition: plannodes.h:130
bool rs_done
Definition: execnodes.h:1128
TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: execQual.c:5214
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:380
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1079
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
ExprContext * ps_ExprContext
Definition: execnodes.h:1078
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
List * qual
Definition: execnodes.h:1062
TupleTableSlot * ExecResult(ResultState *node)
Definition: nodeResult.c:67
List * targetlist
Definition: execnodes.h:1061
EState * state
Definition: execnodes.h:1049
void ExecReScanResult(ResultState *node)
Definition: nodeResult.c:266
void ExecResultRestrPos(ResultState *node)
Definition: nodeResult.c:160
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:685
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:430
struct PlanState * lefttree
Definition: execnodes.h:1063
Node * resconstantqual
Definition: plannodes.h:176
void ExecRestrPos(PlanState *node)
Definition: execAmi.c:348
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1077
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4266
void ExecEndResult(ResultState *node)
Definition: nodeResult.c:247
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
ExprState * resconstantqual
Definition: execnodes.h:1127
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1090
#define innerPlan(node)
Definition: plannodes.h:158
#define DEBUG2
Definition: elog.h:24
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:657
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5055
#define TupIsNull(slot)
Definition: tuptable.h:138
Bitmapset * chgParam
Definition: execnodes.h:1072
#define outerPlan(node)
Definition: plannodes.h:159
bool rs_checkqual
Definition: execnodes.h:1129
Plan * plan
Definition: execnodes.h:1047
void ExecMarkPos(PlanState *node)
Definition: execAmi.c:299
#define makeNode(_type_)
Definition: nodes.h:556
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:130
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define EXEC_FLAG_MARK
Definition: executor.h:61
ResultState * ExecInitResult(Result *node, EState *estate, int eflags)
Definition: nodeResult.c:179
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:408
void ExecResultMarkPos(ResultState *node)
Definition: nodeResult.c:145
List * targetlist
Definition: plannodes.h:129
#define elog
Definition: elog.h:219
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137
Definition: pg_list.h:45
#define ResetExprContext(econtext)
Definition: executor.h:332
PlanState ps
Definition: execnodes.h:1126