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-2016, 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 * INTERFACE ROUTINES
21 * ExecInitNode - initialize a plan node and its subplans
22 * ExecProcNode - get a tuple by executing the plan node
23 * ExecEndNode - shut down a plan node and its subplans
24 *
25 * NOTES
26 * This used to be three files. It is now all combined into
27 * one file so that it is easier to keep ExecInitNode, ExecProcNode,
28 * and ExecEndNode in sync when new nodes are added.
29 *
30 * EXAMPLE
31 * Suppose we want the age of the manager of the shoe department and
32 * the number of employees in that department. So we have the query:
33 *
34 * select DEPT.no_emps, EMP.age
35 * from DEPT, EMP
36 * where EMP.name = DEPT.mgr and
37 * DEPT.name = "shoe"
38 *
39 * Suppose the planner gives us the following plan:
40 *
41 * Nest Loop (DEPT.mgr = EMP.name)
42 * / \
43 * / \
44 * Seq Scan Seq Scan
45 * DEPT EMP
46 * (name = "shoe")
47 *
48 * ExecutorStart() is called first.
49 * It calls InitPlan() which calls ExecInitNode() on
50 * the root of the plan -- the nest loop node.
51 *
52 * * ExecInitNode() notices that it is looking at a nest loop and
53 * as the code below demonstrates, it calls ExecInitNestLoop().
54 * Eventually this calls ExecInitNode() on the right and left subplans
55 * and so forth until the entire plan is initialized. The result
56 * of ExecInitNode() is a plan state tree built with the same structure
57 * as the underlying plan tree.
58 *
59 * * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
60 * ExecProcNode() repeatedly on the top node of the plan state tree.
61 * Each time this happens, ExecProcNode() will end up calling
62 * ExecNestLoop(), which calls ExecProcNode() on its subplans.
63 * Each of these subplans is a sequential scan so ExecSeqScan() is
64 * called. The slots returned by ExecSeqScan() may contain
65 * tuples which contain the attributes ExecNestLoop() uses to
66 * form the tuples it returns.
67 *
68 * * Eventually ExecSeqScan() stops returning tuples and the nest
69 * loop join ends. Lastly, ExecutorEnd() calls ExecEndNode() which
70 * calls ExecEndNestLoop() which in turn calls ExecEndNode() on
71 * its subplans which result in ExecEndSeqScan().
72 *
73 * This should show how the executor works by having
74 * ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
75 * their work to the appropriate node support routines which may
76 * in turn call these routines themselves on their subplans.
77 */
78 #include "postgres.h"
79
80 #include "executor/executor.h"
81 #include "executor/nodeAgg.h"
82 #include "executor/nodeAppend.h"
83 #include "executor/nodeBitmapAnd.h"
84 #include "executor/nodeBitmapHeapscan.h"
85 #include "executor/nodeBitmapIndexscan.h"
86 #include "executor/nodeBitmapOr.h"
87 #include "executor/nodeCtescan.h"
88 #include "executor/nodeCustom.h"
89 #include "executor/nodeForeignscan.h"
90 #include "executor/nodeFunctionscan.h"
91 #include "executor/nodeGroup.h"
92 #include "executor/nodeHash.h"
93 #include "executor/nodeHashjoin.h"
94 #include "executor/nodeIndexonlyscan.h"
95 #include "executor/nodeIndexscan.h"
96 #include "executor/nodeLimit.h"
97 #include "executor/nodeLockRows.h"
98 #include "executor/nodeMaterial.h"
99 #include "executor/nodeMergeAppend.h"
100 #include "executor/nodeMergejoin.h"
101 #include "executor/nodeModifyTable.h"
102 #include "executor/nodeNestloop.h"
103 #include "executor/nodeGather.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/nodeTidscan.h"
113 #include "executor/nodeUnique.h"
114 #include "executor/nodeValuesscan.h"
115 #include "executor/nodeWindowAgg.h"
116 #include "executor/nodeWorktablescan.h"
117 #include "nodes/nodeFuncs.h"
118 #include "miscadmin.h"
119
120
121 /* ------------------------------------------------------------------------
122 * ExecInitNode
123 *
124 * Recursively initializes all the nodes in the plan tree rooted
125 * at 'node'.
126 *
127 * Inputs:
128 * 'node' is the current node of the plan produced by the query planner
129 * 'estate' is the shared execution state for the plan tree
130 * 'eflags' is a bitwise OR of flag bits described in executor.h
131 *
132 * Returns a PlanState node corresponding to the given Plan node.
133 * ------------------------------------------------------------------------
134 */
135 PlanState *
ExecInitNode(Plan * node,EState * estate,int eflags)136 ExecInitNode(Plan *node, EState *estate, int eflags)
137 {
138 PlanState *result;
139 List *subps;
140 ListCell *l;
141
142 /*
143 * do nothing when we get to the end of a leaf on tree.
144 */
145 if (node == NULL)
146 return NULL;
147
148 switch (nodeTag(node))
149 {
150 /*
151 * control nodes
152 */
153 case T_Result:
154 result = (PlanState *) ExecInitResult((Result *) node,
155 estate, eflags);
156 break;
157
158 case T_ModifyTable:
159 result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
160 estate, eflags);
161 break;
162
163 case T_Append:
164 result = (PlanState *) ExecInitAppend((Append *) node,
165 estate, eflags);
166 break;
167
168 case T_MergeAppend:
169 result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
170 estate, eflags);
171 break;
172
173 case T_RecursiveUnion:
174 result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
175 estate, eflags);
176 break;
177
178 case T_BitmapAnd:
179 result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
180 estate, eflags);
181 break;
182
183 case T_BitmapOr:
184 result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
185 estate, eflags);
186 break;
187
188 /*
189 * scan nodes
190 */
191 case T_SeqScan:
192 result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
193 estate, eflags);
194 break;
195
196 case T_SampleScan:
197 result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
198 estate, eflags);
199 break;
200
201 case T_IndexScan:
202 result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
203 estate, eflags);
204 break;
205
206 case T_IndexOnlyScan:
207 result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
208 estate, eflags);
209 break;
210
211 case T_BitmapIndexScan:
212 result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
213 estate, eflags);
214 break;
215
216 case T_BitmapHeapScan:
217 result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
218 estate, eflags);
219 break;
220
221 case T_TidScan:
222 result = (PlanState *) ExecInitTidScan((TidScan *) node,
223 estate, eflags);
224 break;
225
226 case T_SubqueryScan:
227 result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
228 estate, eflags);
229 break;
230
231 case T_FunctionScan:
232 result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
233 estate, eflags);
234 break;
235
236 case T_ValuesScan:
237 result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
238 estate, eflags);
239 break;
240
241 case T_CteScan:
242 result = (PlanState *) ExecInitCteScan((CteScan *) node,
243 estate, eflags);
244 break;
245
246 case T_WorkTableScan:
247 result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
248 estate, eflags);
249 break;
250
251 case T_ForeignScan:
252 result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
253 estate, eflags);
254 break;
255
256 case T_CustomScan:
257 result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
258 estate, eflags);
259 break;
260
261 /*
262 * join nodes
263 */
264 case T_NestLoop:
265 result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
266 estate, eflags);
267 break;
268
269 case T_MergeJoin:
270 result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
271 estate, eflags);
272 break;
273
274 case T_HashJoin:
275 result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
276 estate, eflags);
277 break;
278
279 /*
280 * materialization nodes
281 */
282 case T_Material:
283 result = (PlanState *) ExecInitMaterial((Material *) node,
284 estate, eflags);
285 break;
286
287 case T_Sort:
288 result = (PlanState *) ExecInitSort((Sort *) node,
289 estate, eflags);
290 break;
291
292 case T_Group:
293 result = (PlanState *) ExecInitGroup((Group *) node,
294 estate, eflags);
295 break;
296
297 case T_Agg:
298 result = (PlanState *) ExecInitAgg((Agg *) node,
299 estate, eflags);
300 break;
301
302 case T_WindowAgg:
303 result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
304 estate, eflags);
305 break;
306
307 case T_Unique:
308 result = (PlanState *) ExecInitUnique((Unique *) node,
309 estate, eflags);
310 break;
311
312 case T_Gather:
313 result = (PlanState *) ExecInitGather((Gather *) node,
314 estate, eflags);
315 break;
316
317 case T_Hash:
318 result = (PlanState *) ExecInitHash((Hash *) node,
319 estate, eflags);
320 break;
321
322 case T_SetOp:
323 result = (PlanState *) ExecInitSetOp((SetOp *) node,
324 estate, eflags);
325 break;
326
327 case T_LockRows:
328 result = (PlanState *) ExecInitLockRows((LockRows *) node,
329 estate, eflags);
330 break;
331
332 case T_Limit:
333 result = (PlanState *) ExecInitLimit((Limit *) node,
334 estate, eflags);
335 break;
336
337 default:
338 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
339 result = NULL; /* keep compiler quiet */
340 break;
341 }
342
343 /*
344 * Initialize any initPlans present in this node. The planner put them in
345 * a separate list for us.
346 */
347 subps = NIL;
348 foreach(l, node->initPlan)
349 {
350 SubPlan *subplan = (SubPlan *) lfirst(l);
351 SubPlanState *sstate;
352
353 Assert(IsA(subplan, SubPlan));
354 sstate = ExecInitSubPlan(subplan, result);
355 subps = lappend(subps, sstate);
356 }
357 result->initPlan = subps;
358
359 /* Set up instrumentation for this node if requested */
360 if (estate->es_instrument)
361 result->instrument = InstrAlloc(1, estate->es_instrument);
362
363 return result;
364 }
365
366
367 /* ----------------------------------------------------------------
368 * ExecProcNode
369 *
370 * Execute the given node to return a(nother) tuple.
371 * ----------------------------------------------------------------
372 */
373 TupleTableSlot *
ExecProcNode(PlanState * node)374 ExecProcNode(PlanState *node)
375 {
376 TupleTableSlot *result;
377
378 CHECK_FOR_INTERRUPTS();
379
380 if (node->chgParam != NULL) /* something changed */
381 ExecReScan(node); /* let ReScan handle this */
382
383 if (node->instrument)
384 InstrStartNode(node->instrument);
385
386 switch (nodeTag(node))
387 {
388 /*
389 * control nodes
390 */
391 case T_ResultState:
392 result = ExecResult((ResultState *) node);
393 break;
394
395 case T_ModifyTableState:
396 result = ExecModifyTable((ModifyTableState *) node);
397 break;
398
399 case T_AppendState:
400 result = ExecAppend((AppendState *) node);
401 break;
402
403 case T_MergeAppendState:
404 result = ExecMergeAppend((MergeAppendState *) node);
405 break;
406
407 case T_RecursiveUnionState:
408 result = ExecRecursiveUnion((RecursiveUnionState *) node);
409 break;
410
411 /* BitmapAndState does not yield tuples */
412
413 /* BitmapOrState does not yield tuples */
414
415 /*
416 * scan nodes
417 */
418 case T_SeqScanState:
419 result = ExecSeqScan((SeqScanState *) node);
420 break;
421
422 case T_SampleScanState:
423 result = ExecSampleScan((SampleScanState *) node);
424 break;
425
426 case T_IndexScanState:
427 result = ExecIndexScan((IndexScanState *) node);
428 break;
429
430 case T_IndexOnlyScanState:
431 result = ExecIndexOnlyScan((IndexOnlyScanState *) node);
432 break;
433
434 /* BitmapIndexScanState does not yield tuples */
435
436 case T_BitmapHeapScanState:
437 result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
438 break;
439
440 case T_TidScanState:
441 result = ExecTidScan((TidScanState *) node);
442 break;
443
444 case T_SubqueryScanState:
445 result = ExecSubqueryScan((SubqueryScanState *) node);
446 break;
447
448 case T_FunctionScanState:
449 result = ExecFunctionScan((FunctionScanState *) node);
450 break;
451
452 case T_ValuesScanState:
453 result = ExecValuesScan((ValuesScanState *) node);
454 break;
455
456 case T_CteScanState:
457 result = ExecCteScan((CteScanState *) node);
458 break;
459
460 case T_WorkTableScanState:
461 result = ExecWorkTableScan((WorkTableScanState *) node);
462 break;
463
464 case T_ForeignScanState:
465 result = ExecForeignScan((ForeignScanState *) node);
466 break;
467
468 case T_CustomScanState:
469 result = ExecCustomScan((CustomScanState *) node);
470 break;
471
472 /*
473 * join nodes
474 */
475 case T_NestLoopState:
476 result = ExecNestLoop((NestLoopState *) node);
477 break;
478
479 case T_MergeJoinState:
480 result = ExecMergeJoin((MergeJoinState *) node);
481 break;
482
483 case T_HashJoinState:
484 result = ExecHashJoin((HashJoinState *) node);
485 break;
486
487 /*
488 * materialization nodes
489 */
490 case T_MaterialState:
491 result = ExecMaterial((MaterialState *) node);
492 break;
493
494 case T_SortState:
495 result = ExecSort((SortState *) node);
496 break;
497
498 case T_GroupState:
499 result = ExecGroup((GroupState *) node);
500 break;
501
502 case T_AggState:
503 result = ExecAgg((AggState *) node);
504 break;
505
506 case T_WindowAggState:
507 result = ExecWindowAgg((WindowAggState *) node);
508 break;
509
510 case T_UniqueState:
511 result = ExecUnique((UniqueState *) node);
512 break;
513
514 case T_GatherState:
515 result = ExecGather((GatherState *) node);
516 break;
517
518 case T_HashState:
519 result = ExecHash((HashState *) node);
520 break;
521
522 case T_SetOpState:
523 result = ExecSetOp((SetOpState *) node);
524 break;
525
526 case T_LockRowsState:
527 result = ExecLockRows((LockRowsState *) node);
528 break;
529
530 case T_LimitState:
531 result = ExecLimit((LimitState *) node);
532 break;
533
534 default:
535 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
536 result = NULL;
537 break;
538 }
539
540 if (node->instrument)
541 InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
542
543 return result;
544 }
545
546
547 /* ----------------------------------------------------------------
548 * MultiExecProcNode
549 *
550 * Execute a node that doesn't return individual tuples
551 * (it might return a hashtable, bitmap, etc). Caller should
552 * check it got back the expected kind of Node.
553 *
554 * This has essentially the same responsibilities as ExecProcNode,
555 * but it does not do InstrStartNode/InstrStopNode (mainly because
556 * it can't tell how many returned tuples to count). Each per-node
557 * function must provide its own instrumentation support.
558 * ----------------------------------------------------------------
559 */
560 Node *
MultiExecProcNode(PlanState * node)561 MultiExecProcNode(PlanState *node)
562 {
563 Node *result;
564
565 CHECK_FOR_INTERRUPTS();
566
567 if (node->chgParam != NULL) /* something changed */
568 ExecReScan(node); /* let ReScan handle this */
569
570 switch (nodeTag(node))
571 {
572 /*
573 * Only node types that actually support multiexec will be listed
574 */
575
576 case T_HashState:
577 result = MultiExecHash((HashState *) node);
578 break;
579
580 case T_BitmapIndexScanState:
581 result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
582 break;
583
584 case T_BitmapAndState:
585 result = MultiExecBitmapAnd((BitmapAndState *) node);
586 break;
587
588 case T_BitmapOrState:
589 result = MultiExecBitmapOr((BitmapOrState *) node);
590 break;
591
592 default:
593 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
594 result = NULL;
595 break;
596 }
597
598 return result;
599 }
600
601
602 /* ----------------------------------------------------------------
603 * ExecEndNode
604 *
605 * Recursively cleans up all the nodes in the plan rooted
606 * at 'node'.
607 *
608 * After this operation, the query plan will not be able to be
609 * processed any further. This should be called only after
610 * the query plan has been fully executed.
611 * ----------------------------------------------------------------
612 */
613 void
ExecEndNode(PlanState * node)614 ExecEndNode(PlanState *node)
615 {
616 /*
617 * do nothing when we get to the end of a leaf on tree.
618 */
619 if (node == NULL)
620 return;
621
622 if (node->chgParam != NULL)
623 {
624 bms_free(node->chgParam);
625 node->chgParam = NULL;
626 }
627
628 switch (nodeTag(node))
629 {
630 /*
631 * control nodes
632 */
633 case T_ResultState:
634 ExecEndResult((ResultState *) node);
635 break;
636
637 case T_ModifyTableState:
638 ExecEndModifyTable((ModifyTableState *) node);
639 break;
640
641 case T_AppendState:
642 ExecEndAppend((AppendState *) node);
643 break;
644
645 case T_MergeAppendState:
646 ExecEndMergeAppend((MergeAppendState *) node);
647 break;
648
649 case T_RecursiveUnionState:
650 ExecEndRecursiveUnion((RecursiveUnionState *) node);
651 break;
652
653 case T_BitmapAndState:
654 ExecEndBitmapAnd((BitmapAndState *) node);
655 break;
656
657 case T_BitmapOrState:
658 ExecEndBitmapOr((BitmapOrState *) node);
659 break;
660
661 /*
662 * scan nodes
663 */
664 case T_SeqScanState:
665 ExecEndSeqScan((SeqScanState *) node);
666 break;
667
668 case T_SampleScanState:
669 ExecEndSampleScan((SampleScanState *) node);
670 break;
671
672 case T_GatherState:
673 ExecEndGather((GatherState *) node);
674 break;
675
676 case T_IndexScanState:
677 ExecEndIndexScan((IndexScanState *) node);
678 break;
679
680 case T_IndexOnlyScanState:
681 ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
682 break;
683
684 case T_BitmapIndexScanState:
685 ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
686 break;
687
688 case T_BitmapHeapScanState:
689 ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
690 break;
691
692 case T_TidScanState:
693 ExecEndTidScan((TidScanState *) node);
694 break;
695
696 case T_SubqueryScanState:
697 ExecEndSubqueryScan((SubqueryScanState *) node);
698 break;
699
700 case T_FunctionScanState:
701 ExecEndFunctionScan((FunctionScanState *) node);
702 break;
703
704 case T_ValuesScanState:
705 ExecEndValuesScan((ValuesScanState *) node);
706 break;
707
708 case T_CteScanState:
709 ExecEndCteScan((CteScanState *) node);
710 break;
711
712 case T_WorkTableScanState:
713 ExecEndWorkTableScan((WorkTableScanState *) node);
714 break;
715
716 case T_ForeignScanState:
717 ExecEndForeignScan((ForeignScanState *) node);
718 break;
719
720 case T_CustomScanState:
721 ExecEndCustomScan((CustomScanState *) node);
722 break;
723
724 /*
725 * join nodes
726 */
727 case T_NestLoopState:
728 ExecEndNestLoop((NestLoopState *) node);
729 break;
730
731 case T_MergeJoinState:
732 ExecEndMergeJoin((MergeJoinState *) node);
733 break;
734
735 case T_HashJoinState:
736 ExecEndHashJoin((HashJoinState *) node);
737 break;
738
739 /*
740 * materialization nodes
741 */
742 case T_MaterialState:
743 ExecEndMaterial((MaterialState *) node);
744 break;
745
746 case T_SortState:
747 ExecEndSort((SortState *) node);
748 break;
749
750 case T_GroupState:
751 ExecEndGroup((GroupState *) node);
752 break;
753
754 case T_AggState:
755 ExecEndAgg((AggState *) node);
756 break;
757
758 case T_WindowAggState:
759 ExecEndWindowAgg((WindowAggState *) node);
760 break;
761
762 case T_UniqueState:
763 ExecEndUnique((UniqueState *) node);
764 break;
765
766 case T_HashState:
767 ExecEndHash((HashState *) node);
768 break;
769
770 case T_SetOpState:
771 ExecEndSetOp((SetOpState *) node);
772 break;
773
774 case T_LockRowsState:
775 ExecEndLockRows((LockRowsState *) node);
776 break;
777
778 case T_LimitState:
779 ExecEndLimit((LimitState *) node);
780 break;
781
782 default:
783 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
784 break;
785 }
786 }
787
788 /*
789 * ExecShutdownNode
790 *
791 * Give execution nodes a chance to stop asynchronous resource consumption
792 * and release any resources still held. Currently, this is only used for
793 * parallel query, but we might want to extend it to other cases also (e.g.
794 * FDW). We might also want to call it sooner, as soon as it's evident that
795 * no more rows will be needed (e.g. when a Limit is filled) rather than only
796 * at the end of ExecutorRun.
797 */
798 bool
ExecShutdownNode(PlanState * node)799 ExecShutdownNode(PlanState *node)
800 {
801 if (node == NULL)
802 return false;
803
804 /*
805 * Treat the node as running while we shut it down, but only if it's run
806 * at least once already. We don't expect much CPU consumption during
807 * node shutdown, but in the case of Gather or Gather Merge, we may shut
808 * down workers at this stage. If so, their buffer usage will get
809 * propagated into pgBufferUsage at this point, and we want to make sure
810 * that it gets associated with the Gather node. We skip this if the node
811 * has never been executed, so as to avoid incorrectly making it appear
812 * that it has.
813 */
814 if (node->instrument && node->instrument->running)
815 InstrStartNode(node->instrument);
816
817 switch (nodeTag(node))
818 {
819 case T_GatherState:
820 ExecShutdownGather((GatherState *) node);
821 break;
822 default:
823 break;
824 }
825
826 /* Stop the node if we started it above, reporting 0 tuples. */
827 if (node->instrument && node->instrument->running)
828 InstrStopNode(node->instrument, 0);
829
830 return planstate_tree_walker(node, ExecShutdownNode, NULL);
831 }
832