1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Portions Copyright (c) 1996-2005, 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.64 2005/05/22 22:30:19 tgl 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 "access/heapam.h"
61 #include "executor/execdebug.h"
62 #include "executor/nodeAppend.h"
63 #include "parser/parsetree.h"
65 static bool exec_append_initialize_next(AppendState *appendstate);
68 /* ----------------------------------------------------------------
69 * exec_append_initialize_next
71 * Sets up the append state node for the "next" scan.
73 * Returns t iff there is a "next" scan to process.
74 * ----------------------------------------------------------------
77 exec_append_initialize_next(AppendState *appendstate)
83 * get information from the append node
85 estate = appendstate->ps.state;
86 whichplan = appendstate->as_whichplan;
88 if (whichplan < appendstate->as_firstplan)
91 * if scanning in reverse, we start at the last scan in the list
92 * and then proceed back to the first.. in any case we inform
93 * ExecAppend that we are at the end of the line by returning
96 appendstate->as_whichplan = appendstate->as_firstplan;
99 else if (whichplan > appendstate->as_lastplan)
102 * as above, end the scan if we go beyond the last scan in our
105 appendstate->as_whichplan = appendstate->as_lastplan;
111 * initialize the scan
113 * If we are controlling the target relation, select the proper
114 * active ResultRelInfo and junk filter for this target.
116 if (((Append *) appendstate->ps.plan)->isTarget)
118 Assert(whichplan < estate->es_num_result_relations);
119 estate->es_result_relation_info =
120 estate->es_result_relations + whichplan;
121 estate->es_junkFilter =
122 estate->es_result_relation_info->ri_junkFilter;
129 /* ----------------------------------------------------------------
132 * Begin all of the subscans of the append node.
134 * (This is potentially wasteful, since the entire result of the
135 * append node may not be scanned, but this way all of the
136 * structures get allocated in the executor's top level memory
137 * block instead of that of the call to ExecAppend.)
139 * Special case: during an EvalPlanQual recheck query of an inherited
140 * target relation, we only want to initialize and scan the single
141 * subplan that corresponds to the target relation being checked.
142 * ----------------------------------------------------------------
145 ExecInitAppend(Append *node, EState *estate)
147 AppendState *appendstate = makeNode(AppendState);
148 PlanState **appendplanstates;
153 CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
156 * Set up empty vector of subplan states
158 nplans = list_length(node->appendplans);
160 appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
163 * create new AppendState for our append node
165 appendstate->ps.plan = (Plan *) node;
166 appendstate->ps.state = estate;
167 appendstate->appendplans = appendplanstates;
168 appendstate->as_nplans = nplans;
171 * Do we want to scan just one subplan? (Special case for
172 * EvalPlanQual) XXX pretty dirty way of determining that this case
175 if (node->isTarget && estate->es_evTuple != NULL)
179 tplan = estate->es_result_relation_info - estate->es_result_relations;
180 Assert(tplan >= 0 && tplan < nplans);
182 appendstate->as_firstplan = tplan;
183 appendstate->as_lastplan = tplan;
187 /* normal case, scan all subplans */
188 appendstate->as_firstplan = 0;
189 appendstate->as_lastplan = nplans - 1;
193 * Miscellaneous initialization
195 * Append plans don't have expression contexts because they never call
196 * ExecQual or ExecProject.
199 #define APPEND_NSLOTS 1
202 * append nodes still have Result slots, which hold pointers to
203 * tuples, so we have to initialize them.
205 ExecInitResultTupleSlot(estate, &appendstate->ps);
208 * call ExecInitNode on each of the plans to be executed and save the
209 * results into the array "appendplans". Note we *must* set
210 * estate->es_result_relation_info correctly while we initialize each
211 * sub-plan; ExecContextForcesOids depends on that!
213 for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
215 appendstate->as_whichplan = i;
216 exec_append_initialize_next(appendstate);
218 initNode = (Plan *) list_nth(node->appendplans, i);
219 appendplanstates[i] = ExecInitNode(initNode, estate);
223 * Initialize tuple type. (Note: in an inherited UPDATE situation,
224 * the tuple type computed here corresponds to the parent table, which
225 * is really a lie since tuples returned from child subplans will not
226 * all look the same.)
228 ExecAssignResultTypeFromTL(&appendstate->ps);
229 appendstate->ps.ps_ProjInfo = NULL;
232 * return the result from the first subplan's initialization
234 appendstate->as_whichplan = appendstate->as_firstplan;
235 exec_append_initialize_next(appendstate);
241 ExecCountSlotsAppend(Append *node)
246 foreach(plan, node->appendplans)
247 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
248 return nSlots + APPEND_NSLOTS;
251 /* ----------------------------------------------------------------
254 * Handles iteration over multiple subplans.
255 * ----------------------------------------------------------------
258 ExecAppend(AppendState *node)
263 TupleTableSlot *result;
266 * figure out which subplan we are currently processing
268 subnode = node->appendplans[node->as_whichplan];
271 * get a tuple from the subplan
273 result = ExecProcNode(subnode);
275 if (!TupIsNull(result))
278 * If the subplan gave us something then return it as-is.
279 * We do NOT make use of the result slot that was set up in
280 * ExecInitAppend, first because there's no reason to and
281 * second because it may have the wrong tuple descriptor in
282 * inherited-UPDATE cases.
288 * Go on to the "next" subplan in the appropriate direction.
289 * If no more subplans, return the empty slot set up for us
292 if (ScanDirectionIsForward(node->ps.state->es_direction))
293 node->as_whichplan++;
295 node->as_whichplan--;
296 if (!exec_append_initialize_next(node))
297 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
299 /* Else loop back and try to get a tuple from the new subplan */
303 /* ----------------------------------------------------------------
306 * Shuts down the subscans of the append node.
308 * Returns nothing of interest.
309 * ----------------------------------------------------------------
312 ExecEndAppend(AppendState *node)
314 PlanState **appendplans;
319 * get information from the node
321 appendplans = node->appendplans;
322 nplans = node->as_nplans;
325 * shut down each of the subscans (that we've initialized)
327 for (i = 0; i < nplans; i++)
330 ExecEndNode(appendplans[i]);
335 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
339 for (i = node->as_firstplan; i <= node->as_lastplan; i++)
341 PlanState *subnode = node->appendplans[i];
344 * ExecReScan doesn't know about my subplans, so I have to do
345 * changed-parameter signaling myself.
347 if (node->ps.chgParam != NULL)
348 UpdateChangedParamSet(subnode, node->ps.chgParam);
351 * if chgParam of subnode is not null then plan will be re-scanned
352 * by first ExecProcNode.
354 if (subnode->chgParam == NULL)
356 /* make sure estate is correct for this subnode (needed??) */
357 node->as_whichplan = i;
358 exec_append_initialize_next(node);
359 ExecReScan(subnode, exprCtxt);
362 node->as_whichplan = node->as_firstplan;
363 exec_append_initialize_next(node);