]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
Make further use of new bitmapset code: executor's chgParam, extParam,
[postgresql] / src / backend / executor / nodeAppend.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAppend.c
4  *        routines to handle append nodes.
5  *
6  * Portions Copyright (c) 1996-2002, 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.52 2003/02/09 00:30:39 tgl 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(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                  * ExecProcAppend 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 ExecProcAppend.)
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 = 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; ExecAssignResultTypeFromTL 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 *) nth(i, node->appendplans);
219                 appendplanstates[i] = ExecInitNode(initNode, estate);
220         }
221
222         /*
223          * initialize tuple type
224          */
225         ExecAssignResultTypeFromTL(&appendstate->ps);
226         appendstate->ps.ps_ProjInfo = NULL;
227
228         /*
229          * return the result from the first subplan's initialization
230          */
231         appendstate->as_whichplan = appendstate->as_firstplan;
232         exec_append_initialize_next(appendstate);
233
234         return appendstate;
235 }
236
237 int
238 ExecCountSlotsAppend(Append *node)
239 {
240         List       *plan;
241         int                     nSlots = 0;
242
243         foreach(plan, node->appendplans)
244                 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
245         return nSlots + APPEND_NSLOTS;
246 }
247
248 /* ----------------------------------------------------------------
249  *         ExecProcAppend
250  *
251  *              Handles the iteration over the multiple scans.
252  *
253  *         NOTE: Can't call this ExecAppend, that name is used in execMain.
254  * ----------------------------------------------------------------
255  */
256 TupleTableSlot *
257 ExecProcAppend(AppendState *node)
258 {
259         EState     *estate;
260         int                     whichplan;
261         PlanState  *subnode;
262         TupleTableSlot *result;
263         TupleTableSlot *result_slot;
264         ScanDirection direction;
265
266         /*
267          * get information from the node
268          */
269         estate = node->ps.state;
270         direction = estate->es_direction;
271         whichplan = node->as_whichplan;
272         result_slot = node->ps.ps_ResultTupleSlot;
273
274         /*
275          * figure out which subplan we are currently processing
276          */
277         subnode = node->appendplans[whichplan];
278
279         /*
280          * get a tuple from the subplan
281          */
282         result = ExecProcNode(subnode);
283
284         if (!TupIsNull(result))
285         {
286                 /*
287                  * if the subplan gave us something then place a copy of whatever
288                  * we get into our result slot and return it.
289                  *
290                  * Note we rely on the subplan to retain ownership of the tuple for
291                  * as long as we need it --- we don't copy it.
292                  */
293                 return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
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.
309                  */
310                 if (exec_append_initialize_next(node))
311                 {
312                         ExecSetSlotDescriptorIsNew(result_slot, true);
313                         return ExecProcAppend(node);
314                 }
315                 else
316                         return ExecClearTuple(result_slot);
317         }
318 }
319
320 /* ----------------------------------------------------------------
321  *              ExecEndAppend
322  *
323  *              Shuts down the subscans of the append node.
324  *
325  *              Returns nothing of interest.
326  * ----------------------------------------------------------------
327  */
328 void
329 ExecEndAppend(AppendState *node)
330 {
331         PlanState **appendplans;
332         int                     nplans;
333         int                     i;
334
335         /*
336          * get information from the node
337          */
338         appendplans = node->appendplans;
339         nplans = node->as_nplans;
340
341         /*
342          * shut down each of the subscans (that we've initialized)
343          */
344         for (i = 0; i < nplans; i++)
345         {
346                 if (appendplans[i])
347                         ExecEndNode(appendplans[i]);
348         }
349 }
350
351 void
352 ExecReScanAppend(AppendState *node, ExprContext *exprCtxt)
353 {
354         int                     i;
355
356         for (i = node->as_firstplan; i <= node->as_lastplan; i++)
357         {
358                 PlanState *subnode = node->appendplans[i];
359
360                 /*
361                  * ExecReScan doesn't know about my subplans, so I have to do
362                  * changed-parameter signaling myself.
363                  */
364                 if (node->ps.chgParam != NULL)
365                         UpdateChangedParamSet(subnode, node->ps.chgParam);
366
367                 /*
368                  * if chgParam of subnode is not null then plan will be re-scanned
369                  * by first ExecProcNode.
370                  */
371                 if (subnode->chgParam == NULL)
372                 {
373                         /* make sure estate is correct for this subnode (needed??) */
374                         node->as_whichplan = i;
375                         exec_append_initialize_next(node);
376                         ExecReScan(subnode, exprCtxt);
377                 }
378         }
379         node->as_whichplan = node->as_firstplan;
380         exec_append_initialize_next(node);
381 }