]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
pgindent run on all C files. Java run to follow. initdb/regression
[postgresql] / src / backend / executor / nodeAppend.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAppend.c
4  *        routines to handle append nodes.
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.43 2001/10/25 05:49:28 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /* INTERFACE ROUTINES
16  *              ExecInitAppend  - initialize the append node
17  *              ExecProcAppend  - 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  *                              retrieve (e.name) from e in 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(Append *node);
66
67
68 /* ----------------------------------------------------------------
69  *              exec_append_initialize_next
70  *
71  *              Sets up the append node state (i.e. the append state node)
72  *              for the "next" scan.
73  *
74  *              Returns t iff there is a "next" scan to process.
75  * ----------------------------------------------------------------
76  */
77 static bool
78 exec_append_initialize_next(Append *node)
79 {
80         EState     *estate;
81         AppendState *appendstate;
82         int                     whichplan;
83
84         /*
85          * get information from the append node
86          */
87         estate = node->plan.state;
88         appendstate = node->appendstate;
89         whichplan = appendstate->as_whichplan;
90
91         if (whichplan < appendstate->as_firstplan)
92         {
93                 /*
94                  * if scanning in reverse, we start at the last scan in the list
95                  * and then proceed back to the first.. in any case we inform
96                  * ExecProcAppend that we are at the end of the line by returning
97                  * FALSE
98                  */
99                 appendstate->as_whichplan = appendstate->as_firstplan;
100                 return FALSE;
101         }
102         else if (whichplan > appendstate->as_lastplan)
103         {
104                 /*
105                  * as above, end the scan if we go beyond the last scan in our
106                  * list..
107                  */
108                 appendstate->as_whichplan = appendstate->as_lastplan;
109                 return FALSE;
110         }
111         else
112         {
113                 /*
114                  * initialize the scan
115                  *
116                  * If we are controlling the target relation, select the proper
117                  * active ResultRelInfo and junk filter for this target.
118                  */
119                 if (node->isTarget)
120                 {
121                         Assert(whichplan < estate->es_num_result_relations);
122                         estate->es_result_relation_info =
123                                 estate->es_result_relations + whichplan;
124                         estate->es_junkFilter =
125                                 estate->es_result_relation_info->ri_junkFilter;
126                 }
127
128                 return TRUE;
129         }
130 }
131
132 /* ----------------------------------------------------------------
133  *              ExecInitAppend
134  *
135  *              Begins all of the subscans of the append node, storing the
136  *              scan structures in the 'initialized' vector of the append-state
137  *              structure.
138  *
139  *         (This is potentially wasteful, since the entire result of the
140  *              append node may not be scanned, but this way all of the
141  *              structures get allocated in the executor's top level memory
142  *              block instead of that of the call to ExecProcAppend.)
143  *
144  *              Special case: during an EvalPlanQual recheck query of an inherited
145  *              target relation, we only want to initialize and scan the single
146  *              subplan that corresponds to the target relation being checked.
147  * ----------------------------------------------------------------
148  */
149 bool
150 ExecInitAppend(Append *node, EState *estate, Plan *parent)
151 {
152         AppendState *appendstate;
153         int                     nplans;
154         List       *appendplans;
155         bool       *initialized;
156         int                     i;
157         Plan       *initNode;
158
159         CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
160
161         /*
162          * assign execution state to node and get information for append state
163          */
164         node->plan.state = estate;
165
166         appendplans = node->appendplans;
167         nplans = length(appendplans);
168
169         initialized = (bool *) palloc(nplans * sizeof(bool));
170         MemSet(initialized, 0, nplans * sizeof(bool));
171
172         /*
173          * create new AppendState for our append node
174          */
175         appendstate = makeNode(AppendState);
176         appendstate->as_nplans = nplans;
177         appendstate->as_initialized = initialized;
178
179         node->appendstate = appendstate;
180
181         /*
182          * Do we want to scan just one subplan?  (Special case for
183          * EvalPlanQual) XXX pretty dirty way of determining that this case
184          * applies ...
185          */
186         if (node->isTarget && estate->es_evTuple != NULL)
187         {
188                 int                     tplan;
189
190                 tplan = estate->es_result_relation_info - estate->es_result_relations;
191                 Assert(tplan >= 0 && tplan < nplans);
192
193                 appendstate->as_firstplan = tplan;
194                 appendstate->as_lastplan = tplan;
195         }
196         else
197         {
198                 /* normal case, scan all subplans */
199                 appendstate->as_firstplan = 0;
200                 appendstate->as_lastplan = nplans - 1;
201         }
202
203         /*
204          * Miscellaneous initialization
205          *
206          * Append plans don't have expression contexts because they never call
207          * ExecQual or ExecProject.
208          */
209
210 #define APPEND_NSLOTS 1
211
212         /*
213          * append nodes still have Result slots, which hold pointers to
214          * tuples, so we have to initialize them.
215          */
216         ExecInitResultTupleSlot(estate, &appendstate->cstate);
217
218         /*
219          * call ExecInitNode on each of the plans to be executed and save the
220          * results into the array "initialized"
221          */
222         for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
223         {
224                 appendstate->as_whichplan = i;
225                 exec_append_initialize_next(node);
226
227                 initNode = (Plan *) nth(i, appendplans);
228                 initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
229         }
230
231         /*
232          * initialize tuple type
233          */
234         ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
235         appendstate->cstate.cs_ProjInfo = NULL;
236
237         /*
238          * return the result from the first subplan's initialization
239          */
240         appendstate->as_whichplan = appendstate->as_firstplan;
241         exec_append_initialize_next(node);
242
243         return TRUE;
244 }
245
246 int
247 ExecCountSlotsAppend(Append *node)
248 {
249         List       *plan;
250         int                     nSlots = 0;
251
252         foreach(plan, node->appendplans)
253                 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
254         return nSlots + APPEND_NSLOTS;
255 }
256
257 /* ----------------------------------------------------------------
258  *         ExecProcAppend
259  *
260  *              Handles the iteration over the multiple scans.
261  *
262  *         NOTE: Can't call this ExecAppend, that name is used in execMain.
263  * ----------------------------------------------------------------
264  */
265 TupleTableSlot *
266 ExecProcAppend(Append *node)
267 {
268         EState     *estate;
269         AppendState *appendstate;
270         int                     whichplan;
271         List       *appendplans;
272         Plan       *subnode;
273         TupleTableSlot *result;
274         TupleTableSlot *result_slot;
275         ScanDirection direction;
276
277         /*
278          * get information from the node
279          */
280         appendstate = node->appendstate;
281         estate = node->plan.state;
282         direction = estate->es_direction;
283         appendplans = node->appendplans;
284         whichplan = appendstate->as_whichplan;
285         result_slot = appendstate->cstate.cs_ResultTupleSlot;
286
287         /*
288          * figure out which subplan we are currently processing
289          */
290         subnode = (Plan *) nth(whichplan, appendplans);
291
292         if (subnode == NULL)
293                 elog(DEBUG, "ExecProcAppend: subnode is NULL");
294
295         /*
296          * get a tuple from the subplan
297          */
298         result = ExecProcNode(subnode, (Plan *) node);
299
300         if (!TupIsNull(result))
301         {
302                 /*
303                  * if the subplan gave us something then place a copy of whatever
304                  * we get into our result slot and return it.
305                  *
306                  * Note we rely on the subplan to retain ownership of the tuple for
307                  * as long as we need it --- we don't copy it.
308                  */
309                 return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
310         }
311         else
312         {
313                 /*
314                  * .. go on to the "next" subplan in the appropriate direction and
315                  * try processing again (recursively)
316                  */
317                 if (ScanDirectionIsForward(direction))
318                         appendstate->as_whichplan++;
319                 else
320                         appendstate->as_whichplan--;
321
322                 /*
323                  * return something from next node or an empty slot if all of our
324                  * subplans have been exhausted.
325                  */
326                 if (exec_append_initialize_next(node))
327                 {
328                         ExecSetSlotDescriptorIsNew(result_slot, true);
329                         return ExecProcAppend(node);
330                 }
331                 else
332                         return ExecClearTuple(result_slot);
333         }
334 }
335
336 /* ----------------------------------------------------------------
337  *              ExecEndAppend
338  *
339  *              Shuts down the subscans of the append node.
340  *
341  *              Returns nothing of interest.
342  * ----------------------------------------------------------------
343  */
344 void
345 ExecEndAppend(Append *node)
346 {
347         EState     *estate;
348         AppendState *appendstate;
349         int                     nplans;
350         List       *appendplans;
351         bool       *initialized;
352         int                     i;
353
354         /*
355          * get information from the node
356          */
357         appendstate = node->appendstate;
358         estate = node->plan.state;
359         appendplans = node->appendplans;
360         nplans = appendstate->as_nplans;
361         initialized = appendstate->as_initialized;
362
363         /*
364          * shut down each of the subscans
365          */
366         for (i = 0; i < nplans; i++)
367         {
368                 if (initialized[i])
369                         ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
370         }
371 }
372
373 void
374 ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
375 {
376         AppendState *appendstate = node->appendstate;
377         int                     i;
378
379         for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
380         {
381                 Plan       *subnode;
382
383                 subnode = (Plan *) nth(i, node->appendplans);
384
385                 /*
386                  * ExecReScan doesn't know about my subplans, so I have to do
387                  * changed-parameter signaling myself.
388                  */
389                 if (node->plan.chgParam != NULL)
390                         SetChangedParamList(subnode, node->plan.chgParam);
391
392                 /*
393                  * if chgParam of subnode is not null then plan will be re-scanned
394                  * by first ExecProcNode.
395                  */
396                 if (subnode->chgParam == NULL)
397                 {
398                         /* make sure estate is correct for this subnode (needed??) */
399                         appendstate->as_whichplan = i;
400                         exec_append_initialize_next(node);
401                         ExecReScan(subnode, exprCtxt, (Plan *) node);
402                 }
403         }
404         appendstate->as_whichplan = appendstate->as_firstplan;
405         exec_append_initialize_next(node);
406 }