1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.50 2002/11/13 00:39:47 momjian Exp $
13 *-------------------------------------------------------------------------
16 * ExecInitAppend - initialize the append node
17 * ExecProcAppend - 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 * retrieve (e.name) from e in 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(Append *node);
68 /* ----------------------------------------------------------------
69 * exec_append_initialize_next
71 * Sets up the append node state (i.e. the append state node)
72 * for the "next" scan.
74 * Returns t iff there is a "next" scan to process.
75 * ----------------------------------------------------------------
78 exec_append_initialize_next(Append *node)
81 AppendState *appendstate;
85 * get information from the append node
87 estate = node->plan.state;
88 appendstate = node->appendstate;
89 whichplan = appendstate->as_whichplan;
91 if (whichplan < appendstate->as_firstplan)
94 * if scanning in reverse, we start at the last scan in the list
95 * and then proceed back to the first.. in any case we inform
96 * ExecProcAppend that we are at the end of the line by returning
99 appendstate->as_whichplan = appendstate->as_firstplan;
102 else if (whichplan > appendstate->as_lastplan)
105 * as above, end the scan if we go beyond the last scan in our
108 appendstate->as_whichplan = appendstate->as_lastplan;
114 * initialize the scan
116 * If we are controlling the target relation, select the proper
117 * active ResultRelInfo and junk filter for this target.
121 Assert(whichplan < estate->es_num_result_relations);
122 estate->es_result_relation_info =
123 estate->es_result_relations + whichplan;
124 estate->es_junkFilter =
125 estate->es_result_relation_info->ri_junkFilter;
132 /* ----------------------------------------------------------------
135 * Begins all of the subscans of the append node, storing the
136 * scan structures in the 'initialized' vector of the append-state
139 * (This is potentially wasteful, since the entire result of the
140 * append node may not be scanned, but this way all of the
141 * structures get allocated in the executor's top level memory
142 * block instead of that of the call to ExecProcAppend.)
144 * Special case: during an EvalPlanQual recheck query of an inherited
145 * target relation, we only want to initialize and scan the single
146 * subplan that corresponds to the target relation being checked.
147 * ----------------------------------------------------------------
150 ExecInitAppend(Append *node, EState *estate, Plan *parent)
152 AppendState *appendstate;
159 CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
162 * assign execution state to node and get information for append state
164 node->plan.state = estate;
166 appendplans = node->appendplans;
167 nplans = length(appendplans);
169 initialized = (bool *) palloc0(nplans * sizeof(bool));
172 * create new AppendState for our append node
174 appendstate = makeNode(AppendState);
175 appendstate->as_nplans = nplans;
176 appendstate->as_initialized = initialized;
178 node->appendstate = appendstate;
181 * Do we want to scan just one subplan? (Special case for
182 * EvalPlanQual) XXX pretty dirty way of determining that this case
185 if (node->isTarget && estate->es_evTuple != NULL)
189 tplan = estate->es_result_relation_info - estate->es_result_relations;
190 Assert(tplan >= 0 && tplan < nplans);
192 appendstate->as_firstplan = tplan;
193 appendstate->as_lastplan = tplan;
197 /* normal case, scan all subplans */
198 appendstate->as_firstplan = 0;
199 appendstate->as_lastplan = nplans - 1;
203 * Miscellaneous initialization
205 * Append plans don't have expression contexts because they never call
206 * ExecQual or ExecProject.
209 #define APPEND_NSLOTS 1
212 * append nodes still have Result slots, which hold pointers to
213 * tuples, so we have to initialize them.
215 ExecInitResultTupleSlot(estate, &appendstate->cstate);
218 * call ExecInitNode on each of the plans to be executed and save the
219 * results into the array "initialized". Note we *must* set
220 * estate->es_result_relation_info correctly while we initialize each
221 * sub-plan; ExecAssignResultTypeFromTL depends on that!
223 for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
225 appendstate->as_whichplan = i;
226 exec_append_initialize_next(node);
228 initNode = (Plan *) nth(i, appendplans);
229 initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
233 * initialize tuple type
235 ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
236 appendstate->cstate.cs_ProjInfo = NULL;
239 * return the result from the first subplan's initialization
241 appendstate->as_whichplan = appendstate->as_firstplan;
242 exec_append_initialize_next(node);
248 ExecCountSlotsAppend(Append *node)
253 foreach(plan, node->appendplans)
254 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
255 return nSlots + APPEND_NSLOTS;
258 /* ----------------------------------------------------------------
261 * Handles the iteration over the multiple scans.
263 * NOTE: Can't call this ExecAppend, that name is used in execMain.
264 * ----------------------------------------------------------------
267 ExecProcAppend(Append *node)
270 AppendState *appendstate;
274 TupleTableSlot *result;
275 TupleTableSlot *result_slot;
276 ScanDirection direction;
279 * get information from the node
281 appendstate = node->appendstate;
282 estate = node->plan.state;
283 direction = estate->es_direction;
284 appendplans = node->appendplans;
285 whichplan = appendstate->as_whichplan;
286 result_slot = appendstate->cstate.cs_ResultTupleSlot;
289 * figure out which subplan we are currently processing
291 subnode = (Plan *) nth(whichplan, appendplans);
294 elog(DEBUG1, "ExecProcAppend: subnode is NULL");
297 * get a tuple from the subplan
299 result = ExecProcNode(subnode, (Plan *) node);
301 if (!TupIsNull(result))
304 * if the subplan gave us something then place a copy of whatever
305 * we get into our result slot and return it.
307 * Note we rely on the subplan to retain ownership of the tuple for
308 * as long as we need it --- we don't copy it.
310 return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
315 * .. go on to the "next" subplan in the appropriate direction and
316 * try processing again (recursively)
318 if (ScanDirectionIsForward(direction))
319 appendstate->as_whichplan++;
321 appendstate->as_whichplan--;
324 * return something from next node or an empty slot if all of our
325 * subplans have been exhausted.
327 if (exec_append_initialize_next(node))
329 ExecSetSlotDescriptorIsNew(result_slot, true);
330 return ExecProcAppend(node);
333 return ExecClearTuple(result_slot);
337 /* ----------------------------------------------------------------
340 * Shuts down the subscans of the append node.
342 * Returns nothing of interest.
343 * ----------------------------------------------------------------
346 ExecEndAppend(Append *node)
349 AppendState *appendstate;
356 * get information from the node
358 appendstate = node->appendstate;
359 estate = node->plan.state;
360 appendplans = node->appendplans;
361 nplans = appendstate->as_nplans;
362 initialized = appendstate->as_initialized;
365 * shut down each of the subscans
367 for (i = 0; i < nplans; i++)
370 ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
375 ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
377 AppendState *appendstate = node->appendstate;
380 for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
384 subnode = (Plan *) nth(i, node->appendplans);
387 * ExecReScan doesn't know about my subplans, so I have to do
388 * changed-parameter signaling myself.
390 if (node->plan.chgParam != NULL)
391 SetChangedParamList(subnode, node->plan.chgParam);
394 * if chgParam of subnode is not null then plan will be re-scanned
395 * by first ExecProcNode.
397 if (subnode->chgParam == NULL)
399 /* make sure estate is correct for this subnode (needed??) */
400 appendstate->as_whichplan = i;
401 exec_append_initialize_next(node);
402 ExecReScan(subnode, exprCtxt, (Plan *) node);
405 appendstate->as_whichplan = appendstate->as_firstplan;
406 exec_append_initialize_next(node);