]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
Update some comments to use SQL examples rather than QUEL. From Simon
[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.63 2005/04/24 11:46:20 neilc 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 the iteration over the multiple scans.
255  * ----------------------------------------------------------------
256  */
257 TupleTableSlot *
258 ExecAppend(AppendState *node)
259 {
260         EState     *estate;
261         int                     whichplan;
262         PlanState  *subnode;
263         TupleTableSlot *result;
264         TupleTableSlot *result_slot;
265         ScanDirection direction;
266
267         /*
268          * get information from the node
269          */
270         estate = node->ps.state;
271         direction = estate->es_direction;
272         whichplan = node->as_whichplan;
273         result_slot = node->ps.ps_ResultTupleSlot;
274
275         /*
276          * figure out which subplan we are currently processing
277          */
278         subnode = node->appendplans[whichplan];
279
280         /*
281          * get a tuple from the subplan
282          */
283         result = ExecProcNode(subnode);
284
285         if (!TupIsNull(result))
286         {
287                 /*
288                  * if the subplan gave us something then return it as-is.  We do
289                  * NOT make use of the result slot that was set up in ExecInitAppend,
290                  * first because there's no reason to and second because it may have
291                  * the wrong tuple descriptor in inherited-UPDATE cases.
292                  */
293                 return result;
294         }
295         else
296         {
297                 /*
298                  * .. go on to the "next" subplan in the appropriate direction and
299                  * try processing again (recursively)
300                  */
301                 if (ScanDirectionIsForward(direction))
302                         node->as_whichplan++;
303                 else
304                         node->as_whichplan--;
305
306                 /*
307                  * return something from next node or an empty slot if all of our
308                  * subplans have been exhausted.  The empty slot is the one set up
309                  * by ExecInitAppend.
310                  */
311                 if (exec_append_initialize_next(node))
312                         return ExecAppend(node);
313                 else
314                         return ExecClearTuple(result_slot);
315         }
316 }
317
318 /* ----------------------------------------------------------------
319  *              ExecEndAppend
320  *
321  *              Shuts down the subscans of the append node.
322  *
323  *              Returns nothing of interest.
324  * ----------------------------------------------------------------
325  */
326 void
327 ExecEndAppend(AppendState *node)
328 {
329         PlanState **appendplans;
330         int                     nplans;
331         int                     i;
332
333         /*
334          * get information from the node
335          */
336         appendplans = node->appendplans;
337         nplans = node->as_nplans;
338
339         /*
340          * shut down each of the subscans (that we've initialized)
341          */
342         for (i = 0; i < nplans; i++)
343         {
344                 if (appendplans[i])
345                         ExecEndNode(appendplans[i]);
346         }
347 }
348
349 void
350 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
351 {
352         int                     i;
353
354         for (i = node->as_firstplan; i <= node->as_lastplan; i++)
355         {
356                 PlanState  *subnode = node->appendplans[i];
357
358                 /*
359                  * ExecReScan doesn't know about my subplans, so I have to do
360                  * changed-parameter signaling myself.
361                  */
362                 if (node->ps.chgParam != NULL)
363                         UpdateChangedParamSet(subnode, node->ps.chgParam);
364
365                 /*
366                  * if chgParam of subnode is not null then plan will be re-scanned
367                  * by first ExecProcNode.
368                  */
369                 if (subnode->chgParam == NULL)
370                 {
371                         /* make sure estate is correct for this subnode (needed??) */
372                         node->as_whichplan = i;
373                         exec_append_initialize_next(node);
374                         ExecReScan(subnode, exprCtxt);
375                 }
376         }
377         node->as_whichplan = node->as_firstplan;
378         exec_append_initialize_next(node);
379 }