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.63 2005/04/24 11:46:20 neilc 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 the iteration over the multiple scans.
255 * ----------------------------------------------------------------
258 ExecAppend(AppendState *node)
263 TupleTableSlot *result;
264 TupleTableSlot *result_slot;
265 ScanDirection direction;
268 * get information from the node
270 estate = node->ps.state;
271 direction = estate->es_direction;
272 whichplan = node->as_whichplan;
273 result_slot = node->ps.ps_ResultTupleSlot;
276 * figure out which subplan we are currently processing
278 subnode = node->appendplans[whichplan];
281 * get a tuple from the subplan
283 result = ExecProcNode(subnode);
285 if (!TupIsNull(result))
288 * if the subplan gave us something then return it as-is. We do
289 * NOT make use of the result slot that was set up in ExecInitAppend,
290 * first because there's no reason to and second because it may have
291 * the wrong tuple descriptor in inherited-UPDATE cases.
298 * .. go on to the "next" subplan in the appropriate direction and
299 * try processing again (recursively)
301 if (ScanDirectionIsForward(direction))
302 node->as_whichplan++;
304 node->as_whichplan--;
307 * return something from next node or an empty slot if all of our
308 * subplans have been exhausted. The empty slot is the one set up
311 if (exec_append_initialize_next(node))
312 return ExecAppend(node);
314 return ExecClearTuple(result_slot);
318 /* ----------------------------------------------------------------
321 * Shuts down the subscans of the append node.
323 * Returns nothing of interest.
324 * ----------------------------------------------------------------
327 ExecEndAppend(AppendState *node)
329 PlanState **appendplans;
334 * get information from the node
336 appendplans = node->appendplans;
337 nplans = node->as_nplans;
340 * shut down each of the subscans (that we've initialized)
342 for (i = 0; i < nplans; i++)
345 ExecEndNode(appendplans[i]);
350 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
354 for (i = node->as_firstplan; i <= node->as_lastplan; i++)
356 PlanState *subnode = node->appendplans[i];
359 * ExecReScan doesn't know about my subplans, so I have to do
360 * changed-parameter signaling myself.
362 if (node->ps.chgParam != NULL)
363 UpdateChangedParamSet(subnode, node->ps.chgParam);
366 * if chgParam of subnode is not null then plan will be re-scanned
367 * by first ExecProcNode.
369 if (subnode->chgParam == NULL)
371 /* make sure estate is correct for this subnode (needed??) */
372 node->as_whichplan = i;
373 exec_append_initialize_next(node);
374 ExecReScan(subnode, exprCtxt);
377 node->as_whichplan = node->as_firstplan;
378 exec_append_initialize_next(node);