1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/nodeAppend.c
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)
80 * get information from the append node
82 whichplan = appendstate->as_whichplan;
87 * if scanning in reverse, we start at the last scan in the list and
88 * then proceed back to the first.. in any case we inform ExecAppend
89 * that we are at the end of the line by returning FALSE
91 appendstate->as_whichplan = 0;
94 else if (whichplan >= appendstate->as_nplans)
97 * as above, end the scan if we go beyond the last scan in our list..
99 appendstate->as_whichplan = appendstate->as_nplans - 1;
108 /* ----------------------------------------------------------------
111 * Begin all of the subscans of the append node.
113 * (This is potentially wasteful, since the entire result of the
114 * append node may not be scanned, but this way all of the
115 * structures get allocated in the executor's top level memory
116 * block instead of that of the call to ExecAppend.)
117 * ----------------------------------------------------------------
120 ExecInitAppend(Append *node, EState *estate, int eflags)
122 AppendState *appendstate = makeNode(AppendState);
123 PlanState **appendplanstates;
128 /* check for unsupported flags */
129 Assert(!(eflags & EXEC_FLAG_MARK));
132 * Set up empty vector of subplan states
134 nplans = list_length(node->appendplans);
136 appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
139 * create new AppendState for our append node
141 appendstate->ps.plan = (Plan *) node;
142 appendstate->ps.state = estate;
143 appendstate->appendplans = appendplanstates;
144 appendstate->as_nplans = nplans;
147 * Miscellaneous initialization
149 * Append plans don't have expression contexts because they never call
150 * ExecQual or ExecProject.
154 * append nodes still have Result slots, which hold pointers to tuples, so
155 * we have to initialize them.
157 ExecInitResultTupleSlot(estate, &appendstate->ps);
160 * call ExecInitNode on each of the plans to be executed and save the
161 * results into the array "appendplans".
164 foreach(lc, node->appendplans)
166 Plan *initNode = (Plan *) lfirst(lc);
168 appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
173 * initialize output tuple type
175 ExecAssignResultTypeFromTL(&appendstate->ps);
176 appendstate->ps.ps_ProjInfo = NULL;
179 * initialize to scan first subplan
181 appendstate->as_whichplan = 0;
182 exec_append_initialize_next(appendstate);
187 /* ----------------------------------------------------------------
190 * Handles iteration over multiple subplans.
191 * ----------------------------------------------------------------
194 ExecAppend(AppendState *node)
199 TupleTableSlot *result;
202 * figure out which subplan we are currently processing
204 subnode = node->appendplans[node->as_whichplan];
207 * get a tuple from the subplan
209 result = ExecProcNode(subnode);
211 if (!TupIsNull(result))
214 * If the subplan gave us something then return it as-is. We do
215 * NOT make use of the result slot that was set up in
216 * ExecInitAppend; there's no need for it.
222 * Go on to the "next" subplan in the appropriate direction. If no
223 * more subplans, return the empty slot set up for us by
226 if (ScanDirectionIsForward(node->ps.state->es_direction))
227 node->as_whichplan++;
229 node->as_whichplan--;
230 if (!exec_append_initialize_next(node))
231 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
233 /* Else loop back and try to get a tuple from the new subplan */
237 /* ----------------------------------------------------------------
240 * Shuts down the subscans of the append node.
242 * Returns nothing of interest.
243 * ----------------------------------------------------------------
246 ExecEndAppend(AppendState *node)
248 PlanState **appendplans;
253 * get information from the node
255 appendplans = node->appendplans;
256 nplans = node->as_nplans;
259 * shut down each of the subscans
261 for (i = 0; i < nplans; i++)
262 ExecEndNode(appendplans[i]);
266 ExecReScanAppend(AppendState *node)
270 for (i = 0; i < node->as_nplans; i++)
272 PlanState *subnode = node->appendplans[i];
275 * ExecReScan doesn't know about my subplans, so I have to do
276 * changed-parameter signaling myself.
278 if (node->ps.chgParam != NULL)
279 UpdateChangedParamSet(subnode, node->ps.chgParam);
282 * If chgParam of subnode is not null then plan will be re-scanned by
283 * first ExecProcNode.
285 if (subnode->chgParam == NULL)
288 node->as_whichplan = 0;
289 exec_append_initialize_next(node);