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-2025, 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 "miscadmin.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 * ----------------------------------------------------------------
66 static TupleTableSlot
*
67 ExecResult(PlanState
*pstate
)
69 ResultState
*node
= castNode(ResultState
, pstate
);
70 TupleTableSlot
*outerTupleSlot
;
72 ExprContext
*econtext
;
74 CHECK_FOR_INTERRUPTS();
76 econtext
= node
->ps
.ps_ExprContext
;
79 * check constant qualifications like (2 > 1), if not already done
81 if (node
->rs_checkqual
)
83 bool qualResult
= ExecQual(node
->resconstantqual
, econtext
);
85 node
->rs_checkqual
= false;
94 * Reset per-tuple memory context to free any expression evaluation
95 * storage allocated in the previous tuple cycle.
97 ResetExprContext(econtext
);
100 * if rs_done is true then it means that we were asked to return a
101 * constant tuple and we already did the last time ExecResult() was
102 * called, OR that we failed the constant qual check. Either way, now we
107 outerPlan
= outerPlanState(node
);
109 if (outerPlan
!= NULL
)
112 * retrieve tuples from the outer plan until there are no more.
114 outerTupleSlot
= ExecProcNode(outerPlan
);
116 if (TupIsNull(outerTupleSlot
))
120 * prepare to compute projection expressions, which will expect to
121 * access the input tuples as varno OUTER.
123 econtext
->ecxt_outertuple
= outerTupleSlot
;
128 * if we don't have an outer plan, then we are just generating the
129 * results from a constant target list. Do it only once.
131 node
->rs_done
= true;
134 /* form the result tuple using ExecProject(), and return it */
135 return ExecProject(node
->ps
.ps_ProjInfo
);
141 /* ----------------------------------------------------------------
143 * ----------------------------------------------------------------
146 ExecResultMarkPos(ResultState
*node
)
148 PlanState
*outerPlan
= outerPlanState(node
);
150 if (outerPlan
!= NULL
)
151 ExecMarkPos(outerPlan
);
153 elog(DEBUG2
, "Result nodes do not support mark/restore");
156 /* ----------------------------------------------------------------
158 * ----------------------------------------------------------------
161 ExecResultRestrPos(ResultState
*node
)
163 PlanState
*outerPlan
= outerPlanState(node
);
165 if (outerPlan
!= NULL
)
166 ExecRestrPos(outerPlan
);
168 elog(ERROR
, "Result nodes do not support mark/restore");
171 /* ----------------------------------------------------------------
174 * Creates the run-time state information for the result node
175 * produced by the planner and initializes outer relations
177 * ----------------------------------------------------------------
180 ExecInitResult(Result
*node
, EState
*estate
, int eflags
)
182 ResultState
*resstate
;
184 /* check for unsupported flags */
185 Assert(!(eflags
& (EXEC_FLAG_MARK
| EXEC_FLAG_BACKWARD
)) ||
186 outerPlan(node
) != NULL
);
189 * create state structure
191 resstate
= makeNode(ResultState
);
192 resstate
->ps
.plan
= (Plan
*) node
;
193 resstate
->ps
.state
= estate
;
194 resstate
->ps
.ExecProcNode
= ExecResult
;
196 resstate
->rs_done
= false;
197 resstate
->rs_checkqual
= (node
->resconstantqual
!= NULL
);
200 * Miscellaneous initialization
202 * create expression context for node
204 ExecAssignExprContext(estate
, &resstate
->ps
);
207 * initialize child nodes
209 outerPlanState(resstate
) = ExecInitNode(outerPlan(node
), estate
, eflags
);
212 * we don't use inner plan
214 Assert(innerPlan(node
) == NULL
);
217 * Initialize result slot, type and projection.
219 ExecInitResultTupleSlotTL(&resstate
->ps
, &TTSOpsVirtual
);
220 ExecAssignProjectionInfo(&resstate
->ps
, NULL
);
223 * initialize child expressions
226 ExecInitQual(node
->plan
.qual
, (PlanState
*) resstate
);
227 resstate
->resconstantqual
=
228 ExecInitQual((List
*) node
->resconstantqual
, (PlanState
*) resstate
);
233 /* ----------------------------------------------------------------
236 * frees up storage allocated through C routines
237 * ----------------------------------------------------------------
240 ExecEndResult(ResultState
*node
)
245 ExecEndNode(outerPlanState(node
));
249 ExecReScanResult(ResultState
*node
)
251 PlanState
*outerPlan
= outerPlanState(node
);
253 node
->rs_done
= false;
254 node
->rs_checkqual
= (node
->resconstantqual
!= NULL
);
257 * If chgParam of subnode is not null then plan will be re-scanned by
258 * first ExecProcNode.
260 if (outerPlan
&& outerPlan
->chgParam
== NULL
)
261 ExecReScan(outerPlan
);