Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * execProcnode.c
4 : * contains dispatch functions which call the appropriate "initialize",
5 : * "get a tuple", and "cleanup" routines for the given node type.
6 : * If the node has children, then it will presumably call ExecInitNode,
7 : * ExecProcNode, or ExecEndNode on its subnodes and do the appropriate
8 : * processing.
9 : *
10 : * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
11 : * Portions Copyright (c) 1994, Regents of the University of California
12 : *
13 : *
14 : * IDENTIFICATION
15 : * src/backend/executor/execProcnode.c
16 : *
17 : *-------------------------------------------------------------------------
18 : */
19 : /*
20 : * NOTES
21 : * This used to be three files. It is now all combined into
22 : * one file so that it is easier to keep the dispatch routines
23 : * in sync when new nodes are added.
24 : *
25 : * EXAMPLE
26 : * Suppose we want the age of the manager of the shoe department and
27 : * the number of employees in that department. So we have the query:
28 : *
29 : * select DEPT.no_emps, EMP.age
30 : * from DEPT, EMP
31 : * where EMP.name = DEPT.mgr and
32 : * DEPT.name = "shoe"
33 : *
34 : * Suppose the planner gives us the following plan:
35 : *
36 : * Nest Loop (DEPT.mgr = EMP.name)
37 : * / \
38 : * / \
39 : * Seq Scan Seq Scan
40 : * DEPT EMP
41 : * (name = "shoe")
42 : *
43 : * ExecutorStart() is called first.
44 : * It calls InitPlan() which calls ExecInitNode() on
45 : * the root of the plan -- the nest loop node.
46 : *
47 : * * ExecInitNode() notices that it is looking at a nest loop and
48 : * as the code below demonstrates, it calls ExecInitNestLoop().
49 : * Eventually this calls ExecInitNode() on the right and left subplans
50 : * and so forth until the entire plan is initialized. The result
51 : * of ExecInitNode() is a plan state tree built with the same structure
52 : * as the underlying plan tree.
53 : *
54 : * * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
55 : * ExecProcNode() repeatedly on the top node of the plan state tree.
56 : * Each time this happens, ExecProcNode() will end up calling
57 : * ExecNestLoop(), which calls ExecProcNode() on its subplans.
58 : * Each of these subplans is a sequential scan so ExecSeqScan() is
59 : * called. The slots returned by ExecSeqScan() may contain
60 : * tuples which contain the attributes ExecNestLoop() uses to
61 : * form the tuples it returns.
62 : *
63 : * * Eventually ExecSeqScan() stops returning tuples and the nest
64 : * loop join ends. Lastly, ExecutorEnd() calls ExecEndNode() which
65 : * calls ExecEndNestLoop() which in turn calls ExecEndNode() on
66 : * its subplans which result in ExecEndSeqScan().
67 : *
68 : * This should show how the executor works by having
69 : * ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
70 : * their work to the appropriate node support routines which may
71 : * in turn call these routines themselves on their subplans.
72 : */
73 : #include "postgres.h"
74 :
75 : #include "executor/executor.h"
76 : #include "executor/nodeAgg.h"
77 : #include "executor/nodeAppend.h"
78 : #include "executor/nodeBitmapAnd.h"
79 : #include "executor/nodeBitmapHeapscan.h"
80 : #include "executor/nodeBitmapIndexscan.h"
81 : #include "executor/nodeBitmapOr.h"
82 : #include "executor/nodeCtescan.h"
83 : #include "executor/nodeCustom.h"
84 : #include "executor/nodeForeignscan.h"
85 : #include "executor/nodeFunctionscan.h"
86 : #include "executor/nodeGather.h"
87 : #include "executor/nodeGatherMerge.h"
88 : #include "executor/nodeGroup.h"
89 : #include "executor/nodeHash.h"
90 : #include "executor/nodeHashjoin.h"
91 : #include "executor/nodeIncrementalSort.h"
92 : #include "executor/nodeIndexonlyscan.h"
93 : #include "executor/nodeIndexscan.h"
94 : #include "executor/nodeLimit.h"
95 : #include "executor/nodeLockRows.h"
96 : #include "executor/nodeMaterial.h"
97 : #include "executor/nodeMemoize.h"
98 : #include "executor/nodeMergeAppend.h"
99 : #include "executor/nodeMergejoin.h"
100 : #include "executor/nodeModifyTable.h"
101 : #include "executor/nodeNamedtuplestorescan.h"
102 : #include "executor/nodeNestloop.h"
103 : #include "executor/nodeProjectSet.h"
104 : #include "executor/nodeRecursiveunion.h"
105 : #include "executor/nodeResult.h"
106 : #include "executor/nodeSamplescan.h"
107 : #include "executor/nodeSeqscan.h"
108 : #include "executor/nodeSetOp.h"
109 : #include "executor/nodeSort.h"
110 : #include "executor/nodeSubplan.h"
111 : #include "executor/nodeSubqueryscan.h"
112 : #include "executor/nodeTableFuncscan.h"
113 : #include "executor/nodeTidrangescan.h"
114 : #include "executor/nodeTidscan.h"
115 : #include "executor/nodeUnique.h"
116 : #include "executor/nodeValuesscan.h"
117 : #include "executor/nodeWindowAgg.h"
118 : #include "executor/nodeWorktablescan.h"
119 : #include "miscadmin.h"
120 : #include "nodes/nodeFuncs.h"
121 :
122 : static TupleTableSlot *ExecProcNodeFirst(PlanState *node);
123 : static TupleTableSlot *ExecProcNodeInstr(PlanState *node);
124 : static bool ExecShutdownNode_walker(PlanState *node, void *context);
125 :
126 :
127 : /* ------------------------------------------------------------------------
128 : * ExecInitNode
129 : *
130 : * Recursively initializes all the nodes in the plan tree rooted
131 : * at 'node'.
132 : *
133 : * Inputs:
134 : * 'node' is the current node of the plan produced by the query planner
135 : * 'estate' is the shared execution state for the plan tree
136 : * 'eflags' is a bitwise OR of flag bits described in executor.h
137 : *
138 : * Returns a PlanState node corresponding to the given Plan node.
139 : * ------------------------------------------------------------------------
140 : */
141 : PlanState *
142 1486524 : ExecInitNode(Plan *node, EState *estate, int eflags)
143 : {
144 : PlanState *result;
145 : List *subps;
146 : ListCell *l;
147 :
148 : /*
149 : * do nothing when we get to the end of a leaf on tree.
150 : */
151 1486524 : if (node == NULL)
152 318196 : return NULL;
153 :
154 : /*
155 : * Make sure there's enough stack available. Need to check here, in
156 : * addition to ExecProcNode() (via ExecProcNodeFirst()), to ensure the
157 : * stack isn't overrun while initializing the node tree.
158 : */
159 1168328 : check_stack_depth();
160 :
161 1168328 : switch (nodeTag(node))
162 : {
163 : /*
164 : * control nodes
165 : */
166 326126 : case T_Result:
167 326126 : result = (PlanState *) ExecInitResult((Result *) node,
168 : estate, eflags);
169 326056 : break;
170 :
171 6578 : case T_ProjectSet:
172 6578 : result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
173 : estate, eflags);
174 6574 : break;
175 :
176 102962 : case T_ModifyTable:
177 102962 : result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
178 : estate, eflags);
179 102694 : break;
180 :
181 12826 : case T_Append:
182 12826 : result = (PlanState *) ExecInitAppend((Append *) node,
183 : estate, eflags);
184 12826 : break;
185 :
186 420 : case T_MergeAppend:
187 420 : result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
188 : estate, eflags);
189 420 : break;
190 :
191 778 : case T_RecursiveUnion:
192 778 : result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
193 : estate, eflags);
194 778 : break;
195 :
196 92 : case T_BitmapAnd:
197 92 : result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
198 : estate, eflags);
199 92 : break;
200 :
201 258 : case T_BitmapOr:
202 258 : result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
203 : estate, eflags);
204 258 : break;
205 :
206 : /*
207 : * scan nodes
208 : */
209 186748 : case T_SeqScan:
210 186748 : result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
211 : estate, eflags);
212 186736 : break;
213 :
214 252 : case T_SampleScan:
215 252 : result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
216 : estate, eflags);
217 252 : break;
218 :
219 137222 : case T_IndexScan:
220 137222 : result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
221 : estate, eflags);
222 137222 : break;
223 :
224 14740 : case T_IndexOnlyScan:
225 14740 : result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
226 : estate, eflags);
227 14740 : break;
228 :
229 17186 : case T_BitmapIndexScan:
230 17186 : result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
231 : estate, eflags);
232 17186 : break;
233 :
234 16782 : case T_BitmapHeapScan:
235 16782 : result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
236 : estate, eflags);
237 16782 : break;
238 :
239 718 : case T_TidScan:
240 718 : result = (PlanState *) ExecInitTidScan((TidScan *) node,
241 : estate, eflags);
242 718 : break;
243 :
244 202 : case T_TidRangeScan:
245 202 : result = (PlanState *) ExecInitTidRangeScan((TidRangeScan *) node,
246 : estate, eflags);
247 202 : break;
248 :
249 9252 : case T_SubqueryScan:
250 9252 : result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
251 : estate, eflags);
252 9252 : break;
253 :
254 59570 : case T_FunctionScan:
255 59570 : result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
256 : estate, eflags);
257 59562 : break;
258 :
259 216 : case T_TableFuncScan:
260 216 : result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
261 : estate, eflags);
262 216 : break;
263 :
264 8134 : case T_ValuesScan:
265 8134 : result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
266 : estate, eflags);
267 8134 : break;
268 :
269 2996 : case T_CteScan:
270 2996 : result = (PlanState *) ExecInitCteScan((CteScan *) node,
271 : estate, eflags);
272 2996 : break;
273 :
274 660 : case T_NamedTuplestoreScan:
275 660 : result = (PlanState *) ExecInitNamedTuplestoreScan((NamedTuplestoreScan *) node,
276 : estate, eflags);
277 660 : break;
278 :
279 778 : case T_WorkTableScan:
280 778 : result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
281 : estate, eflags);
282 778 : break;
283 :
284 1916 : case T_ForeignScan:
285 1916 : result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
286 : estate, eflags);
287 1900 : break;
288 :
289 0 : case T_CustomScan:
290 0 : result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
291 : estate, eflags);
292 0 : break;
293 :
294 : /*
295 : * join nodes
296 : */
297 78544 : case T_NestLoop:
298 78544 : result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
299 : estate, eflags);
300 78544 : break;
301 :
302 5376 : case T_MergeJoin:
303 5376 : result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
304 : estate, eflags);
305 5376 : break;
306 :
307 27666 : case T_HashJoin:
308 27666 : result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
309 : estate, eflags);
310 27666 : break;
311 :
312 : /*
313 : * materialization nodes
314 : */
315 3606 : case T_Material:
316 3606 : result = (PlanState *) ExecInitMaterial((Material *) node,
317 : estate, eflags);
318 3606 : break;
319 :
320 56652 : case T_Sort:
321 56652 : result = (PlanState *) ExecInitSort((Sort *) node,
322 : estate, eflags);
323 56646 : break;
324 :
325 630 : case T_IncrementalSort:
326 630 : result = (PlanState *) ExecInitIncrementalSort((IncrementalSort *) node,
327 : estate, eflags);
328 630 : break;
329 :
330 1082 : case T_Memoize:
331 1082 : result = (PlanState *) ExecInitMemoize((Memoize *) node, estate,
332 : eflags);
333 1082 : break;
334 :
335 222 : case T_Group:
336 222 : result = (PlanState *) ExecInitGroup((Group *) node,
337 : estate, eflags);
338 222 : break;
339 :
340 42176 : case T_Agg:
341 42176 : result = (PlanState *) ExecInitAgg((Agg *) node,
342 : estate, eflags);
343 42170 : break;
344 :
345 2406 : case T_WindowAgg:
346 2406 : result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
347 : estate, eflags);
348 2406 : break;
349 :
350 1742 : case T_Unique:
351 1742 : result = (PlanState *) ExecInitUnique((Unique *) node,
352 : estate, eflags);
353 1742 : break;
354 :
355 950 : case T_Gather:
356 950 : result = (PlanState *) ExecInitGather((Gather *) node,
357 : estate, eflags);
358 950 : break;
359 :
360 276 : case T_GatherMerge:
361 276 : result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
362 : estate, eflags);
363 276 : break;
364 :
365 27666 : case T_Hash:
366 27666 : result = (PlanState *) ExecInitHash((Hash *) node,
367 : estate, eflags);
368 27666 : break;
369 :
370 544 : case T_SetOp:
371 544 : result = (PlanState *) ExecInitSetOp((SetOp *) node,
372 : estate, eflags);
373 544 : break;
374 :
375 7014 : case T_LockRows:
376 7014 : result = (PlanState *) ExecInitLockRows((LockRows *) node,
377 : estate, eflags);
378 7014 : break;
379 :
380 4364 : case T_Limit:
381 4364 : result = (PlanState *) ExecInitLimit((Limit *) node,
382 : estate, eflags);
383 4364 : break;
384 :
385 0 : default:
386 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
387 : result = NULL; /* keep compiler quiet */
388 : break;
389 : }
390 :
391 1167938 : ExecSetExecProcNode(result, result->ExecProcNode);
392 :
393 : /*
394 : * Initialize any initPlans present in this node. The planner put them in
395 : * a separate list for us.
396 : */
397 1167938 : subps = NIL;
398 1179494 : foreach(l, node->initPlan)
399 : {
400 11556 : SubPlan *subplan = (SubPlan *) lfirst(l);
401 : SubPlanState *sstate;
402 :
403 : Assert(IsA(subplan, SubPlan));
404 11556 : sstate = ExecInitSubPlan(subplan, result);
405 11556 : subps = lappend(subps, sstate);
406 : }
407 1167938 : result->initPlan = subps;
408 :
409 : /* Set up instrumentation for this node if requested */
410 1167938 : if (estate->es_instrument)
411 10050 : result->instrument = InstrAlloc(1, estate->es_instrument,
412 10050 : result->async_capable);
413 :
414 1167938 : return result;
415 : }
416 :
417 :
418 : /*
419 : * If a node wants to change its ExecProcNode function after ExecInitNode()
420 : * has finished, it should do so with this function. That way any wrapper
421 : * functions can be reinstalled, without the node having to know how that
422 : * works.
423 : */
424 : void
425 1168366 : ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function)
426 : {
427 : /*
428 : * Add a wrapper around the ExecProcNode callback that checks stack depth
429 : * during the first execution and maybe adds an instrumentation wrapper.
430 : * When the callback is changed after execution has already begun that
431 : * means we'll superfluously execute ExecProcNodeFirst, but that seems ok.
432 : */
433 1168366 : node->ExecProcNodeReal = function;
434 1168366 : node->ExecProcNode = ExecProcNodeFirst;
435 1168366 : }
436 :
437 :
438 : /*
439 : * ExecProcNode wrapper that performs some one-time checks, before calling
440 : * the relevant node method (possibly via an instrumentation wrapper).
441 : */
442 : static TupleTableSlot *
443 1011072 : ExecProcNodeFirst(PlanState *node)
444 : {
445 : /*
446 : * Perform stack depth check during the first execution of the node. We
447 : * only do so the first time round because it turns out to not be cheap on
448 : * some common architectures (eg. x86). This relies on the assumption
449 : * that ExecProcNode calls for a given plan node will always be made at
450 : * roughly the same stack depth.
451 : */
452 1011072 : check_stack_depth();
453 :
454 : /*
455 : * If instrumentation is required, change the wrapper to one that just
456 : * does instrumentation. Otherwise we can dispense with all wrappers and
457 : * have ExecProcNode() directly call the relevant function from now on.
458 : */
459 1011072 : if (node->instrument)
460 7530 : node->ExecProcNode = ExecProcNodeInstr;
461 : else
462 1003542 : node->ExecProcNode = node->ExecProcNodeReal;
463 :
464 1011072 : return node->ExecProcNode(node);
465 : }
466 :
467 :
468 : /*
469 : * ExecProcNode wrapper that performs instrumentation calls. By keeping
470 : * this a separate function, we avoid overhead in the normal case where
471 : * no instrumentation is wanted.
472 : */
473 : static TupleTableSlot *
474 12599914 : ExecProcNodeInstr(PlanState *node)
475 : {
476 : TupleTableSlot *result;
477 :
478 12599914 : InstrStartNode(node->instrument);
479 :
480 12599914 : result = node->ExecProcNodeReal(node);
481 :
482 12599902 : InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
483 :
484 12599902 : return result;
485 : }
486 :
487 :
488 : /* ----------------------------------------------------------------
489 : * MultiExecProcNode
490 : *
491 : * Execute a node that doesn't return individual tuples
492 : * (it might return a hashtable, bitmap, etc). Caller should
493 : * check it got back the expected kind of Node.
494 : *
495 : * This has essentially the same responsibilities as ExecProcNode,
496 : * but it does not do InstrStartNode/InstrStopNode (mainly because
497 : * it can't tell how many returned tuples to count). Each per-node
498 : * function must provide its own instrumentation support.
499 : * ----------------------------------------------------------------
500 : */
501 : Node *
502 35752 : MultiExecProcNode(PlanState *node)
503 : {
504 : Node *result;
505 :
506 35752 : check_stack_depth();
507 :
508 35752 : CHECK_FOR_INTERRUPTS();
509 :
510 35752 : if (node->chgParam != NULL) /* something changed */
511 5374 : ExecReScan(node); /* let ReScan handle this */
512 :
513 35752 : switch (nodeTag(node))
514 : {
515 : /*
516 : * Only node types that actually support multiexec will be listed
517 : */
518 :
519 18904 : case T_HashState:
520 18904 : result = MultiExecHash((HashState *) node);
521 18904 : break;
522 :
523 16580 : case T_BitmapIndexScanState:
524 16580 : result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
525 16580 : break;
526 :
527 68 : case T_BitmapAndState:
528 68 : result = MultiExecBitmapAnd((BitmapAndState *) node);
529 68 : break;
530 :
531 200 : case T_BitmapOrState:
532 200 : result = MultiExecBitmapOr((BitmapOrState *) node);
533 200 : break;
534 :
535 0 : default:
536 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
537 : result = NULL;
538 : break;
539 : }
540 :
541 35752 : return result;
542 : }
543 :
544 :
545 : /* ----------------------------------------------------------------
546 : * ExecEndNode
547 : *
548 : * Recursively cleans up all the nodes in the plan rooted
549 : * at 'node'.
550 : *
551 : * After this operation, the query plan will not be able to be
552 : * processed any further. This should be called only after
553 : * the query plan has been fully executed.
554 : * ----------------------------------------------------------------
555 : */
556 : void
557 1440730 : ExecEndNode(PlanState *node)
558 : {
559 : /*
560 : * do nothing when we get to the end of a leaf on tree.
561 : */
562 1440730 : if (node == NULL)
563 302618 : return;
564 :
565 : /*
566 : * Make sure there's enough stack available. Need to check here, in
567 : * addition to ExecProcNode() (via ExecProcNodeFirst()), because it's not
568 : * guaranteed that ExecProcNode() is reached for all nodes.
569 : */
570 1138112 : check_stack_depth();
571 :
572 1138112 : if (node->chgParam != NULL)
573 : {
574 6122 : bms_free(node->chgParam);
575 6122 : node->chgParam = NULL;
576 : }
577 :
578 1138112 : switch (nodeTag(node))
579 : {
580 : /*
581 : * control nodes
582 : */
583 310582 : case T_ResultState:
584 310582 : ExecEndResult((ResultState *) node);
585 310582 : break;
586 :
587 5930 : case T_ProjectSetState:
588 5930 : ExecEndProjectSet((ProjectSetState *) node);
589 5930 : break;
590 :
591 99028 : case T_ModifyTableState:
592 99028 : ExecEndModifyTable((ModifyTableState *) node);
593 99028 : break;
594 :
595 12566 : case T_AppendState:
596 12566 : ExecEndAppend((AppendState *) node);
597 12566 : break;
598 :
599 420 : case T_MergeAppendState:
600 420 : ExecEndMergeAppend((MergeAppendState *) node);
601 420 : break;
602 :
603 778 : case T_RecursiveUnionState:
604 778 : ExecEndRecursiveUnion((RecursiveUnionState *) node);
605 778 : break;
606 :
607 92 : case T_BitmapAndState:
608 92 : ExecEndBitmapAnd((BitmapAndState *) node);
609 92 : break;
610 :
611 258 : case T_BitmapOrState:
612 258 : ExecEndBitmapOr((BitmapOrState *) node);
613 258 : break;
614 :
615 : /*
616 : * scan nodes
617 : */
618 184502 : case T_SeqScanState:
619 184502 : ExecEndSeqScan((SeqScanState *) node);
620 184502 : break;
621 :
622 212 : case T_SampleScanState:
623 212 : ExecEndSampleScan((SampleScanState *) node);
624 212 : break;
625 :
626 944 : case T_GatherState:
627 944 : ExecEndGather((GatherState *) node);
628 944 : break;
629 :
630 276 : case T_GatherMergeState:
631 276 : ExecEndGatherMerge((GatherMergeState *) node);
632 276 : break;
633 :
634 136530 : case T_IndexScanState:
635 136530 : ExecEndIndexScan((IndexScanState *) node);
636 136530 : break;
637 :
638 14616 : case T_IndexOnlyScanState:
639 14616 : ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
640 14616 : break;
641 :
642 17120 : case T_BitmapIndexScanState:
643 17120 : ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
644 17120 : break;
645 :
646 16716 : case T_BitmapHeapScanState:
647 16716 : ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
648 16716 : break;
649 :
650 598 : case T_TidScanState:
651 598 : ExecEndTidScan((TidScanState *) node);
652 598 : break;
653 :
654 202 : case T_TidRangeScanState:
655 202 : ExecEndTidRangeScan((TidRangeScanState *) node);
656 202 : break;
657 :
658 9252 : case T_SubqueryScanState:
659 9252 : ExecEndSubqueryScan((SubqueryScanState *) node);
660 9252 : break;
661 :
662 54338 : case T_FunctionScanState:
663 54338 : ExecEndFunctionScan((FunctionScanState *) node);
664 54338 : break;
665 :
666 198 : case T_TableFuncScanState:
667 198 : ExecEndTableFuncScan((TableFuncScanState *) node);
668 198 : break;
669 :
670 2970 : case T_CteScanState:
671 2970 : ExecEndCteScan((CteScanState *) node);
672 2970 : break;
673 :
674 1848 : case T_ForeignScanState:
675 1848 : ExecEndForeignScan((ForeignScanState *) node);
676 1848 : break;
677 :
678 0 : case T_CustomScanState:
679 0 : ExecEndCustomScan((CustomScanState *) node);
680 0 : break;
681 :
682 : /*
683 : * join nodes
684 : */
685 78338 : case T_NestLoopState:
686 78338 : ExecEndNestLoop((NestLoopState *) node);
687 78338 : break;
688 :
689 5370 : case T_MergeJoinState:
690 5370 : ExecEndMergeJoin((MergeJoinState *) node);
691 5370 : break;
692 :
693 27582 : case T_HashJoinState:
694 27582 : ExecEndHashJoin((HashJoinState *) node);
695 27582 : break;
696 :
697 : /*
698 : * materialization nodes
699 : */
700 3546 : case T_MaterialState:
701 3546 : ExecEndMaterial((MaterialState *) node);
702 3546 : break;
703 :
704 56508 : case T_SortState:
705 56508 : ExecEndSort((SortState *) node);
706 56508 : break;
707 :
708 630 : case T_IncrementalSortState:
709 630 : ExecEndIncrementalSort((IncrementalSortState *) node);
710 630 : break;
711 :
712 1082 : case T_MemoizeState:
713 1082 : ExecEndMemoize((MemoizeState *) node);
714 1082 : break;
715 :
716 222 : case T_GroupState:
717 222 : ExecEndGroup((GroupState *) node);
718 222 : break;
719 :
720 42064 : case T_AggState:
721 42064 : ExecEndAgg((AggState *) node);
722 42064 : break;
723 :
724 2274 : case T_WindowAggState:
725 2274 : ExecEndWindowAgg((WindowAggState *) node);
726 2274 : break;
727 :
728 1742 : case T_UniqueState:
729 1742 : ExecEndUnique((UniqueState *) node);
730 1742 : break;
731 :
732 27582 : case T_HashState:
733 27582 : ExecEndHash((HashState *) node);
734 27582 : break;
735 :
736 544 : case T_SetOpState:
737 544 : ExecEndSetOp((SetOpState *) node);
738 544 : break;
739 :
740 6928 : case T_LockRowsState:
741 6928 : ExecEndLockRows((LockRowsState *) node);
742 6928 : break;
743 :
744 4304 : case T_LimitState:
745 4304 : ExecEndLimit((LimitState *) node);
746 4304 : break;
747 :
748 : /* No clean up actions for these nodes. */
749 9420 : case T_ValuesScanState:
750 : case T_NamedTuplestoreScanState:
751 : case T_WorkTableScanState:
752 9420 : break;
753 :
754 0 : default:
755 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
756 : break;
757 : }
758 : }
759 :
760 : /*
761 : * ExecShutdownNode
762 : *
763 : * Give execution nodes a chance to stop asynchronous resource consumption
764 : * and release any resources still held.
765 : */
766 : void
767 542094 : ExecShutdownNode(PlanState *node)
768 : {
769 542094 : (void) ExecShutdownNode_walker(node, NULL);
770 542094 : }
771 :
772 : static bool
773 1088282 : ExecShutdownNode_walker(PlanState *node, void *context)
774 : {
775 1088282 : if (node == NULL)
776 0 : return false;
777 :
778 1088282 : check_stack_depth();
779 :
780 : /*
781 : * Treat the node as running while we shut it down, but only if it's run
782 : * at least once already. We don't expect much CPU consumption during
783 : * node shutdown, but in the case of Gather or Gather Merge, we may shut
784 : * down workers at this stage. If so, their buffer usage will get
785 : * propagated into pgBufferUsage at this point, and we want to make sure
786 : * that it gets associated with the Gather node. We skip this if the node
787 : * has never been executed, so as to avoid incorrectly making it appear
788 : * that it has.
789 : */
790 1088282 : if (node->instrument && node->instrument->running)
791 8368 : InstrStartNode(node->instrument);
792 :
793 1088282 : planstate_tree_walker(node, ExecShutdownNode_walker, context);
794 :
795 1088282 : switch (nodeTag(node))
796 : {
797 536 : case T_GatherState:
798 536 : ExecShutdownGather((GatherState *) node);
799 536 : break;
800 1084 : case T_ForeignScanState:
801 1084 : ExecShutdownForeignScan((ForeignScanState *) node);
802 1084 : break;
803 0 : case T_CustomScanState:
804 0 : ExecShutdownCustomScan((CustomScanState *) node);
805 0 : break;
806 120 : case T_GatherMergeState:
807 120 : ExecShutdownGatherMerge((GatherMergeState *) node);
808 120 : break;
809 24716 : case T_HashState:
810 24716 : ExecShutdownHash((HashState *) node);
811 24716 : break;
812 24716 : case T_HashJoinState:
813 24716 : ExecShutdownHashJoin((HashJoinState *) node);
814 24716 : break;
815 1037110 : default:
816 1037110 : break;
817 : }
818 :
819 : /* Stop the node if we started it above, reporting 0 tuples. */
820 1088282 : if (node->instrument && node->instrument->running)
821 8368 : InstrStopNode(node->instrument, 0);
822 :
823 1088282 : return false;
824 : }
825 :
826 : /*
827 : * ExecSetTupleBound
828 : *
829 : * Set a tuple bound for a planstate node. This lets child plan nodes
830 : * optimize based on the knowledge that the maximum number of tuples that
831 : * their parent will demand is limited. The tuple bound for a node may
832 : * only be changed between scans (i.e., after node initialization or just
833 : * before an ExecReScan call).
834 : *
835 : * Any negative tuples_needed value means "no limit", which should be the
836 : * default assumption when this is not called at all for a particular node.
837 : *
838 : * Note: if this is called repeatedly on a plan tree, the exact same set
839 : * of nodes must be updated with the new limit each time; be careful that
840 : * only unchanging conditions are tested here.
841 : */
842 : void
843 7144 : ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
844 : {
845 : /*
846 : * Since this function recurses, in principle we should check stack depth
847 : * here. In practice, it's probably pointless since the earlier node
848 : * initialization tree traversal would surely have consumed more stack.
849 : */
850 :
851 7144 : if (IsA(child_node, SortState))
852 : {
853 : /*
854 : * If it is a Sort node, notify it that it can use bounded sort.
855 : *
856 : * Note: it is the responsibility of nodeSort.c to react properly to
857 : * changes of these parameters. If we ever redesign this, it'd be a
858 : * good idea to integrate this signaling with the parameter-change
859 : * mechanism.
860 : */
861 1196 : SortState *sortState = (SortState *) child_node;
862 :
863 1196 : if (tuples_needed < 0)
864 : {
865 : /* make sure flag gets reset if needed upon rescan */
866 280 : sortState->bounded = false;
867 : }
868 : else
869 : {
870 916 : sortState->bounded = true;
871 916 : sortState->bound = tuples_needed;
872 : }
873 : }
874 5948 : else if (IsA(child_node, IncrementalSortState))
875 : {
876 : /*
877 : * If it is an IncrementalSort node, notify it that it can use bounded
878 : * sort.
879 : *
880 : * Note: it is the responsibility of nodeIncrementalSort.c to react
881 : * properly to changes of these parameters. If we ever redesign this,
882 : * it'd be a good idea to integrate this signaling with the
883 : * parameter-change mechanism.
884 : */
885 146 : IncrementalSortState *sortState = (IncrementalSortState *) child_node;
886 :
887 146 : if (tuples_needed < 0)
888 : {
889 : /* make sure flag gets reset if needed upon rescan */
890 0 : sortState->bounded = false;
891 : }
892 : else
893 : {
894 146 : sortState->bounded = true;
895 146 : sortState->bound = tuples_needed;
896 : }
897 : }
898 5802 : else if (IsA(child_node, AppendState))
899 : {
900 : /*
901 : * If it is an Append, we can apply the bound to any nodes that are
902 : * children of the Append, since the Append surely need read no more
903 : * than that many tuples from any one input.
904 : */
905 160 : AppendState *aState = (AppendState *) child_node;
906 : int i;
907 :
908 508 : for (i = 0; i < aState->as_nplans; i++)
909 348 : ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
910 : }
911 5642 : else if (IsA(child_node, MergeAppendState))
912 : {
913 : /*
914 : * If it is a MergeAppend, we can apply the bound to any nodes that
915 : * are children of the MergeAppend, since the MergeAppend surely need
916 : * read no more than that many tuples from any one input.
917 : */
918 60 : MergeAppendState *maState = (MergeAppendState *) child_node;
919 : int i;
920 :
921 240 : for (i = 0; i < maState->ms_nplans; i++)
922 180 : ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
923 : }
924 5582 : else if (IsA(child_node, ResultState))
925 : {
926 : /*
927 : * Similarly, for a projecting Result, we can apply the bound to its
928 : * child node.
929 : *
930 : * If Result supported qual checking, we'd have to punt on seeing a
931 : * qual. Note that having a resconstantqual is not a showstopper: if
932 : * that condition succeeds it affects nothing, while if it fails, no
933 : * rows will be demanded from the Result child anyway.
934 : */
935 444 : if (outerPlanState(child_node))
936 104 : ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
937 : }
938 5138 : else if (IsA(child_node, SubqueryScanState))
939 : {
940 : /*
941 : * We can also descend through SubqueryScan, but only if it has no
942 : * qual (otherwise it might discard rows).
943 : */
944 94 : SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
945 :
946 94 : if (subqueryState->ss.ps.qual == NULL)
947 72 : ExecSetTupleBound(tuples_needed, subqueryState->subplan);
948 : }
949 5044 : else if (IsA(child_node, GatherState))
950 : {
951 : /*
952 : * A Gather node can propagate the bound to its workers. As with
953 : * MergeAppend, no one worker could possibly need to return more
954 : * tuples than the Gather itself needs to.
955 : *
956 : * Note: As with Sort, the Gather node is responsible for reacting
957 : * properly to changes to this parameter.
958 : */
959 0 : GatherState *gstate = (GatherState *) child_node;
960 :
961 0 : gstate->tuples_needed = tuples_needed;
962 :
963 : /* Also pass down the bound to our own copy of the child plan */
964 0 : ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
965 : }
966 5044 : else if (IsA(child_node, GatherMergeState))
967 : {
968 : /* Same comments as for Gather */
969 30 : GatherMergeState *gstate = (GatherMergeState *) child_node;
970 :
971 30 : gstate->tuples_needed = tuples_needed;
972 :
973 30 : ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
974 : }
975 :
976 : /*
977 : * In principle we could descend through any plan node type that is
978 : * certain not to discard or combine input rows; but on seeing a node that
979 : * can do that, we can't propagate the bound any further. For the moment
980 : * it's unclear that any other cases are worth checking here.
981 : */
982 7144 : }
|