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