]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeAppend.c
Don't scan partitioned tables.
[postgresql] / src / backend / executor / nodeAppend.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeAppend.c
4  *        routines to handle append nodes.
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/executor/nodeAppend.c
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         int                     whichplan;
78
79         /*
80          * get information from the append node
81          */
82         whichplan = appendstate->as_whichplan;
83
84         if (whichplan < 0)
85         {
86                 /*
87                  * if scanning in reverse, we start at the last scan in the list and
88                  * then proceed back to the first.. in any case we inform ExecAppend
89                  * that we are at the end of the line by returning FALSE
90                  */
91                 appendstate->as_whichplan = 0;
92                 return FALSE;
93         }
94         else if (whichplan >= appendstate->as_nplans)
95         {
96                 /*
97                  * as above, end the scan if we go beyond the last scan in our list..
98                  */
99                 appendstate->as_whichplan = appendstate->as_nplans - 1;
100                 return FALSE;
101         }
102         else
103         {
104                 return TRUE;
105         }
106 }
107
108 /* ----------------------------------------------------------------
109  *              ExecInitAppend
110  *
111  *              Begin all of the subscans of the append node.
112  *
113  *         (This is potentially wasteful, since the entire result of the
114  *              append node may not be scanned, but this way all of the
115  *              structures get allocated in the executor's top level memory
116  *              block instead of that of the call to ExecAppend.)
117  * ----------------------------------------------------------------
118  */
119 AppendState *
120 ExecInitAppend(Append *node, EState *estate, int eflags)
121 {
122         AppendState *appendstate = makeNode(AppendState);
123         PlanState **appendplanstates;
124         int                     nplans;
125         int                     i;
126         ListCell   *lc;
127
128         /* check for unsupported flags */
129         Assert(!(eflags & EXEC_FLAG_MARK));
130
131         /*
132          * Lock the non-leaf tables in the partition tree controlled by this
133          * node. It's a no-op for non-partitioned parent tables.
134          */
135         ExecLockNonLeafAppendTables(node->partitioned_rels, estate);
136
137         /*
138          * Set up empty vector of subplan states
139          */
140         nplans = list_length(node->appendplans);
141
142         appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
143
144         /*
145          * create new AppendState for our append node
146          */
147         appendstate->ps.plan = (Plan *) node;
148         appendstate->ps.state = estate;
149         appendstate->appendplans = appendplanstates;
150         appendstate->as_nplans = nplans;
151
152         /*
153          * Miscellaneous initialization
154          *
155          * Append plans don't have expression contexts because they never call
156          * ExecQual or ExecProject.
157          */
158
159         /*
160          * append nodes still have Result slots, which hold pointers to tuples, so
161          * we have to initialize them.
162          */
163         ExecInitResultTupleSlot(estate, &appendstate->ps);
164
165         /*
166          * call ExecInitNode on each of the plans to be executed and save the
167          * results into the array "appendplans".
168          */
169         i = 0;
170         foreach(lc, node->appendplans)
171         {
172                 Plan       *initNode = (Plan *) lfirst(lc);
173
174                 appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
175                 i++;
176         }
177
178         /*
179          * initialize output tuple type
180          */
181         ExecAssignResultTypeFromTL(&appendstate->ps);
182         appendstate->ps.ps_ProjInfo = NULL;
183
184         /*
185          * initialize to scan first subplan
186          */
187         appendstate->as_whichplan = 0;
188         exec_append_initialize_next(appendstate);
189
190         return appendstate;
191 }
192
193 /* ----------------------------------------------------------------
194  *         ExecAppend
195  *
196  *              Handles iteration over multiple subplans.
197  * ----------------------------------------------------------------
198  */
199 TupleTableSlot *
200 ExecAppend(AppendState *node)
201 {
202         for (;;)
203         {
204                 PlanState  *subnode;
205                 TupleTableSlot *result;
206
207                 /*
208                  * figure out which subplan we are currently processing
209                  */
210                 subnode = node->appendplans[node->as_whichplan];
211
212                 /*
213                  * get a tuple from the subplan
214                  */
215                 result = ExecProcNode(subnode);
216
217                 if (!TupIsNull(result))
218                 {
219                         /*
220                          * If the subplan gave us something then return it as-is. We do
221                          * NOT make use of the result slot that was set up in
222                          * ExecInitAppend; there's no need for it.
223                          */
224                         return result;
225                 }
226
227                 /*
228                  * Go on to the "next" subplan in the appropriate direction. If no
229                  * more subplans, return the empty slot set up for us by
230                  * ExecInitAppend.
231                  */
232                 if (ScanDirectionIsForward(node->ps.state->es_direction))
233                         node->as_whichplan++;
234                 else
235                         node->as_whichplan--;
236                 if (!exec_append_initialize_next(node))
237                         return ExecClearTuple(node->ps.ps_ResultTupleSlot);
238
239                 /* Else loop back and try to get a tuple from the new subplan */
240         }
241 }
242
243 /* ----------------------------------------------------------------
244  *              ExecEndAppend
245  *
246  *              Shuts down the subscans of the append node.
247  *
248  *              Returns nothing of interest.
249  * ----------------------------------------------------------------
250  */
251 void
252 ExecEndAppend(AppendState *node)
253 {
254         PlanState **appendplans;
255         int                     nplans;
256         int                     i;
257
258         /*
259          * get information from the node
260          */
261         appendplans = node->appendplans;
262         nplans = node->as_nplans;
263
264         /*
265          * shut down each of the subscans
266          */
267         for (i = 0; i < nplans; i++)
268                 ExecEndNode(appendplans[i]);
269 }
270
271 void
272 ExecReScanAppend(AppendState *node)
273 {
274         int                     i;
275
276         for (i = 0; i < node->as_nplans; i++)
277         {
278                 PlanState  *subnode = node->appendplans[i];
279
280                 /*
281                  * ExecReScan doesn't know about my subplans, so I have to do
282                  * changed-parameter signaling myself.
283                  */
284                 if (node->ps.chgParam != NULL)
285                         UpdateChangedParamSet(subnode, node->ps.chgParam);
286
287                 /*
288                  * If chgParam of subnode is not null then plan will be re-scanned by
289                  * first ExecProcNode.
290                  */
291                 if (subnode->chgParam == NULL)
292                         ExecReScan(subnode);
293         }
294         node->as_whichplan = 0;
295         exec_append_initialize_next(node);
296 }