]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
Teach the planner to remove SubqueryScan nodes from the plan if they
[postgresql] / src / backend / executor / nodeAppend.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAppend.c
4  *        routines to handle append nodes.
5  *
6  * Portions Copyright (c) 1996-2005, 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.64 2005/05/22 22:30:19 tgl 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 "access/heapam.h"
61 #include "executor/execdebug.h"
62 #include "executor/nodeAppend.h"
63 #include "parser/parsetree.h"
64
65 static bool exec_append_initialize_next(AppendState *appendstate);
66
67
68 /* ----------------------------------------------------------------
69  *              exec_append_initialize_next
70  *
71  *              Sets up the append state node for the "next" scan.
72  *
73  *              Returns t iff there is a "next" scan to process.
74  * ----------------------------------------------------------------
75  */
76 static bool
77 exec_append_initialize_next(AppendState *appendstate)
78 {
79         EState     *estate;
80         int                     whichplan;
81
82         /*
83          * get information from the append node
84          */
85         estate = appendstate->ps.state;
86         whichplan = appendstate->as_whichplan;
87
88         if (whichplan < appendstate->as_firstplan)
89         {
90                 /*
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
94                  * FALSE
95                  */
96                 appendstate->as_whichplan = appendstate->as_firstplan;
97                 return FALSE;
98         }
99         else if (whichplan > appendstate->as_lastplan)
100         {
101                 /*
102                  * as above, end the scan if we go beyond the last scan in our
103                  * list..
104                  */
105                 appendstate->as_whichplan = appendstate->as_lastplan;
106                 return FALSE;
107         }
108         else
109         {
110                 /*
111                  * initialize the scan
112                  *
113                  * If we are controlling the target relation, select the proper
114                  * active ResultRelInfo and junk filter for this target.
115                  */
116                 if (((Append *) appendstate->ps.plan)->isTarget)
117                 {
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;
123                 }
124
125                 return TRUE;
126         }
127 }
128
129 /* ----------------------------------------------------------------
130  *              ExecInitAppend
131  *
132  *              Begin all of the subscans of the append node.
133  *
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.)
138  *
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  * ----------------------------------------------------------------
143  */
144 AppendState *
145 ExecInitAppend(Append *node, EState *estate)
146 {
147         AppendState *appendstate = makeNode(AppendState);
148         PlanState **appendplanstates;
149         int                     nplans;
150         int                     i;
151         Plan       *initNode;
152
153         CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
154
155         /*
156          * Set up empty vector of subplan states
157          */
158         nplans = list_length(node->appendplans);
159
160         appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
161
162         /*
163          * create new AppendState for our append node
164          */
165         appendstate->ps.plan = (Plan *) node;
166         appendstate->ps.state = estate;
167         appendstate->appendplans = appendplanstates;
168         appendstate->as_nplans = nplans;
169
170         /*
171          * Do we want to scan just one subplan?  (Special case for
172          * EvalPlanQual) XXX pretty dirty way of determining that this case
173          * applies ...
174          */
175         if (node->isTarget && estate->es_evTuple != NULL)
176         {
177                 int                     tplan;
178
179                 tplan = estate->es_result_relation_info - estate->es_result_relations;
180                 Assert(tplan >= 0 && tplan < nplans);
181
182                 appendstate->as_firstplan = tplan;
183                 appendstate->as_lastplan = tplan;
184         }
185         else
186         {
187                 /* normal case, scan all subplans */
188                 appendstate->as_firstplan = 0;
189                 appendstate->as_lastplan = nplans - 1;
190         }
191
192         /*
193          * Miscellaneous initialization
194          *
195          * Append plans don't have expression contexts because they never call
196          * ExecQual or ExecProject.
197          */
198
199 #define APPEND_NSLOTS 1
200
201         /*
202          * append nodes still have Result slots, which hold pointers to
203          * tuples, so we have to initialize them.
204          */
205         ExecInitResultTupleSlot(estate, &appendstate->ps);
206
207         /*
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!
212          */
213         for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
214         {
215                 appendstate->as_whichplan = i;
216                 exec_append_initialize_next(appendstate);
217
218                 initNode = (Plan *) list_nth(node->appendplans, i);
219                 appendplanstates[i] = ExecInitNode(initNode, estate);
220         }
221
222         /*
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.)
227          */
228         ExecAssignResultTypeFromTL(&appendstate->ps);
229         appendstate->ps.ps_ProjInfo = NULL;
230
231         /*
232          * return the result from the first subplan's initialization
233          */
234         appendstate->as_whichplan = appendstate->as_firstplan;
235         exec_append_initialize_next(appendstate);
236
237         return appendstate;
238 }
239
240 int
241 ExecCountSlotsAppend(Append *node)
242 {
243         ListCell   *plan;
244         int                     nSlots = 0;
245
246         foreach(plan, node->appendplans)
247                 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
248         return nSlots + APPEND_NSLOTS;
249 }
250
251 /* ----------------------------------------------------------------
252  *         ExecAppend
253  *
254  *              Handles iteration over multiple subplans.
255  * ----------------------------------------------------------------
256  */
257 TupleTableSlot *
258 ExecAppend(AppendState *node)
259 {
260         for (;;)
261         {
262                 PlanState  *subnode;
263                 TupleTableSlot *result;
264
265                 /*
266                  * figure out which subplan we are currently processing
267                  */
268                 subnode = node->appendplans[node->as_whichplan];
269
270                 /*
271                  * get a tuple from the subplan
272                  */
273                 result = ExecProcNode(subnode);
274
275                 if (!TupIsNull(result))
276                 {
277                         /*
278                          * If the subplan gave us something then return it as-is.
279                          * We do NOT make use of the result slot that was set up in
280                          * ExecInitAppend, first because there's no reason to and
281                          * second because it may have the wrong tuple descriptor in
282                          * inherited-UPDATE cases.
283                          */
284                         return result;
285                 }
286
287                 /*
288                  * Go on to the "next" subplan in the appropriate direction.
289                  * If no more subplans, return the empty slot set up for us
290                  * by ExecInitAppend.
291                  */
292                 if (ScanDirectionIsForward(node->ps.state->es_direction))
293                         node->as_whichplan++;
294                 else
295                         node->as_whichplan--;
296                 if (!exec_append_initialize_next(node))
297                         return ExecClearTuple(node->ps.ps_ResultTupleSlot);
298
299                 /* Else loop back and try to get a tuple from the new subplan */
300         }
301 }
302
303 /* ----------------------------------------------------------------
304  *              ExecEndAppend
305  *
306  *              Shuts down the subscans of the append node.
307  *
308  *              Returns nothing of interest.
309  * ----------------------------------------------------------------
310  */
311 void
312 ExecEndAppend(AppendState *node)
313 {
314         PlanState **appendplans;
315         int                     nplans;
316         int                     i;
317
318         /*
319          * get information from the node
320          */
321         appendplans = node->appendplans;
322         nplans = node->as_nplans;
323
324         /*
325          * shut down each of the subscans (that we've initialized)
326          */
327         for (i = 0; i < nplans; i++)
328         {
329                 if (appendplans[i])
330                         ExecEndNode(appendplans[i]);
331         }
332 }
333
334 void
335 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
336 {
337         int                     i;
338
339         for (i = node->as_firstplan; i <= node->as_lastplan; i++)
340         {
341                 PlanState  *subnode = node->appendplans[i];
342
343                 /*
344                  * ExecReScan doesn't know about my subplans, so I have to do
345                  * changed-parameter signaling myself.
346                  */
347                 if (node->ps.chgParam != NULL)
348                         UpdateChangedParamSet(subnode, node->ps.chgParam);
349
350                 /*
351                  * if chgParam of subnode is not null then plan will be re-scanned
352                  * by first ExecProcNode.
353                  */
354                 if (subnode->chgParam == NULL)
355                 {
356                         /* make sure estate is correct for this subnode (needed??) */
357                         node->as_whichplan = i;
358                         exec_append_initialize_next(node);
359                         ExecReScan(subnode, exprCtxt);
360                 }
361         }
362         node->as_whichplan = node->as_firstplan;
363         exec_append_initialize_next(node);
364 }