1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/nodeAppend.c,v 1.70 2006/07/14 14:52:19 momjian Exp $
13 *-------------------------------------------------------------------------
16 * ExecInitAppend - initialize the append node
17 * ExecAppend - retrieve the next tuple from the node
18 * ExecEndAppend - shut down the append node
19 * ExecReScanAppend - rescan the append node
22 * Each append node contains a list of one or more subplans which
23 * must be iteratively processed (forwards or backwards).
24 * Tuples are retrieved by executing the 'whichplan'th subplan
25 * until the subplan stops returning tuples, at which point that
26 * plan is shut down and the next started up.
28 * Append nodes don't make use of their left and right
29 * subtrees, rather they maintain a list of subplans so
30 * a typical append node looks like this in the plan tree:
34 * Append -------+------+------+--- nil
39 * Append nodes are currently used for unions, and to support
40 * inheritance queries, where several relations need to be scanned.
41 * For example, in our standard person/student/employee/student-emp
42 * example, where student and employee inherit from person
43 * and student-emp inherits from student and employee, the
46 * select name from person
51 * Append -------+-------+--------+--------+
53 * nil nil Scan Scan Scan Scan
55 * person employee student student-emp
60 #include "executor/execdebug.h"
61 #include "executor/nodeAppend.h"
63 static bool exec_append_initialize_next(AppendState *appendstate);
66 /* ----------------------------------------------------------------
67 * exec_append_initialize_next
69 * Sets up the append state node for the "next" scan.
71 * Returns t iff there is a "next" scan to process.
72 * ----------------------------------------------------------------
75 exec_append_initialize_next(AppendState *appendstate)
81 * get information from the append node
83 estate = appendstate->ps.state;
84 whichplan = appendstate->as_whichplan;
86 if (whichplan < appendstate->as_firstplan)
89 * if scanning in reverse, we start at the last scan in the list and
90 * then proceed back to the first.. in any case we inform ExecAppend
91 * that we are at the end of the line by returning FALSE
93 appendstate->as_whichplan = appendstate->as_firstplan;
96 else if (whichplan > appendstate->as_lastplan)
99 * as above, end the scan if we go beyond the last scan in our list..
101 appendstate->as_whichplan = appendstate->as_lastplan;
107 * initialize the scan
109 * If we are controlling the target relation, select the proper active
110 * ResultRelInfo and junk filter for this target.
112 if (((Append *) appendstate->ps.plan)->isTarget)
114 Assert(whichplan < estate->es_num_result_relations);
115 estate->es_result_relation_info =
116 estate->es_result_relations + whichplan;
117 estate->es_junkFilter =
118 estate->es_result_relation_info->ri_junkFilter;
125 /* ----------------------------------------------------------------
128 * Begin all of the subscans of the append node.
130 * (This is potentially wasteful, since the entire result of the
131 * append node may not be scanned, but this way all of the
132 * structures get allocated in the executor's top level memory
133 * block instead of that of the call to ExecAppend.)
135 * Special case: during an EvalPlanQual recheck query of an inherited
136 * target relation, we only want to initialize and scan the single
137 * subplan that corresponds to the target relation being checked.
138 * ----------------------------------------------------------------
141 ExecInitAppend(Append *node, EState *estate, int eflags)
143 AppendState *appendstate = makeNode(AppendState);
144 PlanState **appendplanstates;
149 /* check for unsupported flags */
150 Assert(!(eflags & EXEC_FLAG_MARK));
153 * Set up empty vector of subplan states
155 nplans = list_length(node->appendplans);
157 appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
160 * create new AppendState for our append node
162 appendstate->ps.plan = (Plan *) node;
163 appendstate->ps.state = estate;
164 appendstate->appendplans = appendplanstates;
165 appendstate->as_nplans = nplans;
168 * Do we want to scan just one subplan? (Special case for EvalPlanQual)
169 * XXX pretty dirty way of determining that this case applies ...
171 if (node->isTarget && estate->es_evTuple != NULL)
175 tplan = estate->es_result_relation_info - estate->es_result_relations;
176 Assert(tplan >= 0 && tplan < nplans);
178 appendstate->as_firstplan = tplan;
179 appendstate->as_lastplan = tplan;
183 /* normal case, scan all subplans */
184 appendstate->as_firstplan = 0;
185 appendstate->as_lastplan = nplans - 1;
189 * Miscellaneous initialization
191 * Append plans don't have expression contexts because they never call
192 * ExecQual or ExecProject.
195 #define APPEND_NSLOTS 1
198 * append nodes still have Result slots, which hold pointers to tuples, so
199 * we have to initialize them.
201 ExecInitResultTupleSlot(estate, &appendstate->ps);
204 * call ExecInitNode on each of the plans to be executed and save the
205 * results into the array "appendplans". Note we *must* set
206 * estate->es_result_relation_info correctly while we initialize each
207 * sub-plan; ExecContextForcesOids depends on that!
209 for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
211 appendstate->as_whichplan = i;
212 exec_append_initialize_next(appendstate);
214 initNode = (Plan *) list_nth(node->appendplans, i);
215 appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
219 * Initialize tuple type. (Note: in an inherited UPDATE situation, the
220 * tuple type computed here corresponds to the parent table, which is
221 * really a lie since tuples returned from child subplans will not all
224 ExecAssignResultTypeFromTL(&appendstate->ps);
225 appendstate->ps.ps_ProjInfo = NULL;
228 * return the result from the first subplan's initialization
230 appendstate->as_whichplan = appendstate->as_firstplan;
231 exec_append_initialize_next(appendstate);
237 ExecCountSlotsAppend(Append *node)
242 foreach(plan, node->appendplans)
243 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
244 return nSlots + APPEND_NSLOTS;
247 /* ----------------------------------------------------------------
250 * Handles iteration over multiple subplans.
251 * ----------------------------------------------------------------
254 ExecAppend(AppendState *node)
259 TupleTableSlot *result;
262 * figure out which subplan we are currently processing
264 subnode = node->appendplans[node->as_whichplan];
267 * get a tuple from the subplan
269 result = ExecProcNode(subnode);
271 if (!TupIsNull(result))
274 * If the subplan gave us something then return it as-is. We do
275 * NOT make use of the result slot that was set up in
276 * ExecInitAppend, first because there's no reason to and second
277 * because it may have the wrong tuple descriptor in
278 * inherited-UPDATE cases.
284 * Go on to the "next" subplan in the appropriate direction. If no
285 * more subplans, return the empty slot set up for us by
288 if (ScanDirectionIsForward(node->ps.state->es_direction))
289 node->as_whichplan++;
291 node->as_whichplan--;
292 if (!exec_append_initialize_next(node))
293 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
295 /* Else loop back and try to get a tuple from the new subplan */
299 /* ----------------------------------------------------------------
302 * Shuts down the subscans of the append node.
304 * Returns nothing of interest.
305 * ----------------------------------------------------------------
308 ExecEndAppend(AppendState *node)
310 PlanState **appendplans;
315 * get information from the node
317 appendplans = node->appendplans;
318 nplans = node->as_nplans;
321 * shut down each of the subscans (that we've initialized)
323 for (i = 0; i < nplans; i++)
326 ExecEndNode(appendplans[i]);
331 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
335 for (i = node->as_firstplan; i <= node->as_lastplan; i++)
337 PlanState *subnode = node->appendplans[i];
340 * ExecReScan doesn't know about my subplans, so I have to do
341 * changed-parameter signaling myself.
343 if (node->ps.chgParam != NULL)
344 UpdateChangedParamSet(subnode, node->ps.chgParam);
347 * If chgParam of subnode is not null then plan will be re-scanned by
348 * first ExecProcNode. However, if caller is passing us an exprCtxt
349 * then forcibly rescan all the subnodes now, so that we can pass
350 * the exprCtxt down to the subnodes (needed for appendrel indexscan).
352 if (subnode->chgParam == NULL || exprCtxt != NULL)
354 /* make sure estate is correct for this subnode (needed??) */
355 node->as_whichplan = i;
356 exec_append_initialize_next(node);
357 ExecReScan(subnode, exprCtxt);
360 node->as_whichplan = node->as_firstplan;
361 exec_append_initialize_next(node);