]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
Remove 576 references of include files that were not needed.
[postgresql] / src / backend / executor / nodeAppend.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAppend.c
4  *        routines to handle append nodes.
5  *
6  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/executor/nodeAppend.c,v 1.70 2006/07/14 14:52:19 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /* INTERFACE ROUTINES
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
20  *
21  *       NOTES
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.
27  *
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:
31  *
32  *                                 ...
33  *                                 /
34  *                              Append -------+------+------+--- nil
35  *                              /       \                 |              |              |
36  *                        nil   nil              ...    ...    ...
37  *                                                               subplans
38  *
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
44  *              query:
45  *
46  *                              select name from person
47  *
48  *              generates the plan:
49  *
50  *                                |
51  *                              Append -------+-------+--------+--------+
52  *                              /       \                 |               |                |            |
53  *                        nil   nil              Scan    Scan     Scan     Scan
54  *                                                        |               |                |            |
55  *                                                      person employee student student-emp
56  */
57
58 #include "postgres.h"
59
60 #include "executor/execdebug.h"
61 #include "executor/nodeAppend.h"
62
63 static bool exec_append_initialize_next(AppendState *appendstate);
64
65
66 /* ----------------------------------------------------------------
67  *              exec_append_initialize_next
68  *
69  *              Sets up the append state node for the "next" scan.
70  *
71  *              Returns t iff there is a "next" scan to process.
72  * ----------------------------------------------------------------
73  */
74 static bool
75 exec_append_initialize_next(AppendState *appendstate)
76 {
77         EState     *estate;
78         int                     whichplan;
79
80         /*
81          * get information from the append node
82          */
83         estate = appendstate->ps.state;
84         whichplan = appendstate->as_whichplan;
85
86         if (whichplan < appendstate->as_firstplan)
87         {
88                 /*
89                  * if scanning in reverse, we start at the last scan in the list and
90                  * then proceed back to the first.. in any case we inform ExecAppend
91                  * that we are at the end of the line by returning FALSE
92                  */
93                 appendstate->as_whichplan = appendstate->as_firstplan;
94                 return FALSE;
95         }
96         else if (whichplan > appendstate->as_lastplan)
97         {
98                 /*
99                  * as above, end the scan if we go beyond the last scan in our list..
100                  */
101                 appendstate->as_whichplan = appendstate->as_lastplan;
102                 return FALSE;
103         }
104         else
105         {
106                 /*
107                  * initialize the scan
108                  *
109                  * If we are controlling the target relation, select the proper active
110                  * ResultRelInfo and junk filter for this target.
111                  */
112                 if (((Append *) appendstate->ps.plan)->isTarget)
113                 {
114                         Assert(whichplan < estate->es_num_result_relations);
115                         estate->es_result_relation_info =
116                                 estate->es_result_relations + whichplan;
117                         estate->es_junkFilter =
118                                 estate->es_result_relation_info->ri_junkFilter;
119                 }
120
121                 return TRUE;
122         }
123 }
124
125 /* ----------------------------------------------------------------
126  *              ExecInitAppend
127  *
128  *              Begin all of the subscans of the append node.
129  *
130  *         (This is potentially wasteful, since the entire result of the
131  *              append node may not be scanned, but this way all of the
132  *              structures get allocated in the executor's top level memory
133  *              block instead of that of the call to ExecAppend.)
134  *
135  *              Special case: during an EvalPlanQual recheck query of an inherited
136  *              target relation, we only want to initialize and scan the single
137  *              subplan that corresponds to the target relation being checked.
138  * ----------------------------------------------------------------
139  */
140 AppendState *
141 ExecInitAppend(Append *node, EState *estate, int eflags)
142 {
143         AppendState *appendstate = makeNode(AppendState);
144         PlanState **appendplanstates;
145         int                     nplans;
146         int                     i;
147         Plan       *initNode;
148
149         /* check for unsupported flags */
150         Assert(!(eflags & EXEC_FLAG_MARK));
151
152         /*
153          * Set up empty vector of subplan states
154          */
155         nplans = list_length(node->appendplans);
156
157         appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
158
159         /*
160          * create new AppendState for our append node
161          */
162         appendstate->ps.plan = (Plan *) node;
163         appendstate->ps.state = estate;
164         appendstate->appendplans = appendplanstates;
165         appendstate->as_nplans = nplans;
166
167         /*
168          * Do we want to scan just one subplan?  (Special case for EvalPlanQual)
169          * XXX pretty dirty way of determining that this case applies ...
170          */
171         if (node->isTarget && estate->es_evTuple != NULL)
172         {
173                 int                     tplan;
174
175                 tplan = estate->es_result_relation_info - estate->es_result_relations;
176                 Assert(tplan >= 0 && tplan < nplans);
177
178                 appendstate->as_firstplan = tplan;
179                 appendstate->as_lastplan = tplan;
180         }
181         else
182         {
183                 /* normal case, scan all subplans */
184                 appendstate->as_firstplan = 0;
185                 appendstate->as_lastplan = nplans - 1;
186         }
187
188         /*
189          * Miscellaneous initialization
190          *
191          * Append plans don't have expression contexts because they never call
192          * ExecQual or ExecProject.
193          */
194
195 #define APPEND_NSLOTS 1
196
197         /*
198          * append nodes still have Result slots, which hold pointers to tuples, so
199          * we have to initialize them.
200          */
201         ExecInitResultTupleSlot(estate, &appendstate->ps);
202
203         /*
204          * call ExecInitNode on each of the plans to be executed and save the
205          * results into the array "appendplans".  Note we *must* set
206          * estate->es_result_relation_info correctly while we initialize each
207          * sub-plan; ExecContextForcesOids depends on that!
208          */
209         for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
210         {
211                 appendstate->as_whichplan = i;
212                 exec_append_initialize_next(appendstate);
213
214                 initNode = (Plan *) list_nth(node->appendplans, i);
215                 appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
216         }
217
218         /*
219          * Initialize tuple type.  (Note: in an inherited UPDATE situation, the
220          * tuple type computed here corresponds to the parent table, which is
221          * really a lie since tuples returned from child subplans will not all
222          * look the same.)
223          */
224         ExecAssignResultTypeFromTL(&appendstate->ps);
225         appendstate->ps.ps_ProjInfo = NULL;
226
227         /*
228          * return the result from the first subplan's initialization
229          */
230         appendstate->as_whichplan = appendstate->as_firstplan;
231         exec_append_initialize_next(appendstate);
232
233         return appendstate;
234 }
235
236 int
237 ExecCountSlotsAppend(Append *node)
238 {
239         ListCell   *plan;
240         int                     nSlots = 0;
241
242         foreach(plan, node->appendplans)
243                 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
244         return nSlots + APPEND_NSLOTS;
245 }
246
247 /* ----------------------------------------------------------------
248  *         ExecAppend
249  *
250  *              Handles iteration over multiple subplans.
251  * ----------------------------------------------------------------
252  */
253 TupleTableSlot *
254 ExecAppend(AppendState *node)
255 {
256         for (;;)
257         {
258                 PlanState  *subnode;
259                 TupleTableSlot *result;
260
261                 /*
262                  * figure out which subplan we are currently processing
263                  */
264                 subnode = node->appendplans[node->as_whichplan];
265
266                 /*
267                  * get a tuple from the subplan
268                  */
269                 result = ExecProcNode(subnode);
270
271                 if (!TupIsNull(result))
272                 {
273                         /*
274                          * If the subplan gave us something then return it as-is. We do
275                          * NOT make use of the result slot that was set up in
276                          * ExecInitAppend, first because there's no reason to and second
277                          * because it may have the wrong tuple descriptor in
278                          * inherited-UPDATE cases.
279                          */
280                         return result;
281                 }
282
283                 /*
284                  * Go on to the "next" subplan in the appropriate direction. If no
285                  * more subplans, return the empty slot set up for us by
286                  * ExecInitAppend.
287                  */
288                 if (ScanDirectionIsForward(node->ps.state->es_direction))
289                         node->as_whichplan++;
290                 else
291                         node->as_whichplan--;
292                 if (!exec_append_initialize_next(node))
293                         return ExecClearTuple(node->ps.ps_ResultTupleSlot);
294
295                 /* Else loop back and try to get a tuple from the new subplan */
296         }
297 }
298
299 /* ----------------------------------------------------------------
300  *              ExecEndAppend
301  *
302  *              Shuts down the subscans of the append node.
303  *
304  *              Returns nothing of interest.
305  * ----------------------------------------------------------------
306  */
307 void
308 ExecEndAppend(AppendState *node)
309 {
310         PlanState **appendplans;
311         int                     nplans;
312         int                     i;
313
314         /*
315          * get information from the node
316          */
317         appendplans = node->appendplans;
318         nplans = node->as_nplans;
319
320         /*
321          * shut down each of the subscans (that we've initialized)
322          */
323         for (i = 0; i < nplans; i++)
324         {
325                 if (appendplans[i])
326                         ExecEndNode(appendplans[i]);
327         }
328 }
329
330 void
331 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
332 {
333         int                     i;
334
335         for (i = node->as_firstplan; i <= node->as_lastplan; i++)
336         {
337                 PlanState  *subnode = node->appendplans[i];
338
339                 /*
340                  * ExecReScan doesn't know about my subplans, so I have to do
341                  * changed-parameter signaling myself.
342                  */
343                 if (node->ps.chgParam != NULL)
344                         UpdateChangedParamSet(subnode, node->ps.chgParam);
345
346                 /*
347                  * If chgParam of subnode is not null then plan will be re-scanned by
348                  * first ExecProcNode.  However, if caller is passing us an exprCtxt
349                  * then forcibly rescan all the subnodes now, so that we can pass
350                  * the exprCtxt down to the subnodes (needed for appendrel indexscan).
351                  */
352                 if (subnode->chgParam == NULL || exprCtxt != NULL)
353                 {
354                         /* make sure estate is correct for this subnode (needed??) */
355                         node->as_whichplan = i;
356                         exec_append_initialize_next(node);
357                         ExecReScan(subnode, exprCtxt);
358                 }
359         }
360         node->as_whichplan = node->as_firstplan;
361         exec_append_initialize_next(node);
362 }