1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Portions Copyright (c) 1996-2017, 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"
62 #include "miscadmin.h"
64 static bool exec_append_initialize_next(AppendState *appendstate);
67 /* ----------------------------------------------------------------
68 * exec_append_initialize_next
70 * Sets up the append state node for the "next" scan.
72 * Returns t iff there is a "next" scan to process.
73 * ----------------------------------------------------------------
76 exec_append_initialize_next(AppendState *appendstate)
81 * get information from the append node
83 whichplan = appendstate->as_whichplan;
88 * if scanning in reverse, we start at the last scan in the list and
89 * then proceed back to the first.. in any case we inform ExecAppend
90 * that we are at the end of the line by returning FALSE
92 appendstate->as_whichplan = 0;
95 else if (whichplan >= appendstate->as_nplans)
98 * as above, end the scan if we go beyond the last scan in our list..
100 appendstate->as_whichplan = appendstate->as_nplans - 1;
109 /* ----------------------------------------------------------------
112 * Begin all of the subscans of the append node.
114 * (This is potentially wasteful, since the entire result of the
115 * append node may not be scanned, but this way all of the
116 * structures get allocated in the executor's top level memory
117 * block instead of that of the call to ExecAppend.)
118 * ----------------------------------------------------------------
121 ExecInitAppend(Append *node, EState *estate, int eflags)
123 AppendState *appendstate = makeNode(AppendState);
124 PlanState **appendplanstates;
129 /* check for unsupported flags */
130 Assert(!(eflags & EXEC_FLAG_MARK));
133 * Lock the non-leaf tables in the partition tree controlled by this node.
134 * It's a no-op for non-partitioned parent tables.
136 ExecLockNonLeafAppendTables(node->partitioned_rels, estate);
139 * Set up empty vector of subplan states
141 nplans = list_length(node->appendplans);
143 appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
146 * create new AppendState for our append node
148 appendstate->ps.plan = (Plan *) node;
149 appendstate->ps.state = estate;
150 appendstate->appendplans = appendplanstates;
151 appendstate->as_nplans = nplans;
154 * Miscellaneous initialization
156 * Append plans don't have expression contexts because they never call
157 * ExecQual or ExecProject.
161 * append nodes still have Result slots, which hold pointers to tuples, so
162 * we have to initialize them.
164 ExecInitResultTupleSlot(estate, &appendstate->ps);
167 * call ExecInitNode on each of the plans to be executed and save the
168 * results into the array "appendplans".
171 foreach(lc, node->appendplans)
173 Plan *initNode = (Plan *) lfirst(lc);
175 appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
180 * initialize output tuple type
182 ExecAssignResultTypeFromTL(&appendstate->ps);
183 appendstate->ps.ps_ProjInfo = NULL;
186 * initialize to scan first subplan
188 appendstate->as_whichplan = 0;
189 exec_append_initialize_next(appendstate);
194 /* ----------------------------------------------------------------
197 * Handles iteration over multiple subplans.
198 * ----------------------------------------------------------------
201 ExecAppend(AppendState *node)
206 TupleTableSlot *result;
208 CHECK_FOR_INTERRUPTS();
211 * figure out which subplan we are currently processing
213 subnode = node->appendplans[node->as_whichplan];
216 * get a tuple from the subplan
218 result = ExecProcNode(subnode);
220 if (!TupIsNull(result))
223 * If the subplan gave us something then return it as-is. We do
224 * NOT make use of the result slot that was set up in
225 * ExecInitAppend; there's no need for it.
231 * Go on to the "next" subplan in the appropriate direction. If no
232 * more subplans, return the empty slot set up for us by
235 if (ScanDirectionIsForward(node->ps.state->es_direction))
236 node->as_whichplan++;
238 node->as_whichplan--;
239 if (!exec_append_initialize_next(node))
240 return ExecClearTuple(node->ps.ps_ResultTupleSlot);
242 /* Else loop back and try to get a tuple from the new subplan */
246 /* ----------------------------------------------------------------
249 * Shuts down the subscans of the append node.
251 * Returns nothing of interest.
252 * ----------------------------------------------------------------
255 ExecEndAppend(AppendState *node)
257 PlanState **appendplans;
262 * get information from the node
264 appendplans = node->appendplans;
265 nplans = node->as_nplans;
268 * shut down each of the subscans
270 for (i = 0; i < nplans; i++)
271 ExecEndNode(appendplans[i]);
275 ExecReScanAppend(AppendState *node)
279 for (i = 0; i < node->as_nplans; i++)
281 PlanState *subnode = node->appendplans[i];
284 * ExecReScan doesn't know about my subplans, so I have to do
285 * changed-parameter signaling myself.
287 if (node->ps.chgParam != NULL)
288 UpdateChangedParamSet(subnode, node->ps.chgParam);
291 * If chgParam of subnode is not null then plan will be re-scanned by
292 * first ExecProcNode.
294 if (subnode->chgParam == NULL)
297 node->as_whichplan = 0;
298 exec_append_initialize_next(node);