1 /*-------------------------------------------------------------------------
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
10 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
17 *-------------------------------------------------------------------------
21 * ExecCountSlotsNode - count tuple slots needed by plan tree
22 * ExecInitNode - initialize a plan node and its subplans
23 * ExecProcNode - get a tuple by executing the plan node
24 * ExecEndNode - shut down a plan node and its subplans
27 * This used to be three files. It is now all combined into
28 * one file so that it is easier to keep ExecInitNode, ExecProcNode,
29 * and ExecEndNode in sync when new nodes are added.
32 * Suppose we want the age of the manager of the shoe department and
33 * the number of employees in that department. So we have the query:
35 * select DEPT.no_emps, EMP.age
36 * where EMP.name = DEPT.mgr and
39 * Suppose the planner gives us the following plan:
41 * Nest Loop (DEPT.mgr = EMP.name)
48 * ExecutorStart() is called first.
49 * It calls InitPlan() which calls ExecInitNode() on
50 * the root of the plan -- the nest loop node.
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.
59 * * Then when ExecRun() 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.
68 * * Eventually ExecSeqScan() stops returning tuples and the nest
69 * loop join ends. Lastly, ExecEnd() calls ExecEndNode() which
70 * calls ExecEndNestLoop() which in turn calls ExecEndNode() on
71 * its subplans which result in ExecEndSeqScan().
73 * This should show how the executor works by having
74 * ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
75 * their work to the appopriate node support routines which may
76 * in turn call these routines themselves on their subplans.
80 #include "executor/executor.h"
81 #include "executor/instrument.h"
82 #include "executor/nodeAgg.h"
83 #include "executor/nodeAppend.h"
84 #include "executor/nodeBitmapAnd.h"
85 #include "executor/nodeBitmapHeapscan.h"
86 #include "executor/nodeBitmapIndexscan.h"
87 #include "executor/nodeBitmapOr.h"
88 #include "executor/nodeFunctionscan.h"
89 #include "executor/nodeGroup.h"
90 #include "executor/nodeHash.h"
91 #include "executor/nodeHashjoin.h"
92 #include "executor/nodeIndexscan.h"
93 #include "executor/nodeLimit.h"
94 #include "executor/nodeMaterial.h"
95 #include "executor/nodeMergejoin.h"
96 #include "executor/nodeNestloop.h"
97 #include "executor/nodeRecursiveunion.h"
98 #include "executor/nodeResult.h"
99 #include "executor/nodeSeqscan.h"
100 #include "executor/nodeSetOp.h"
101 #include "executor/nodeSort.h"
102 #include "executor/nodeSubplan.h"
103 #include "executor/nodeSubqueryscan.h"
104 #include "executor/nodeTidscan.h"
105 #include "executor/nodeUnique.h"
106 #include "executor/nodeValuesscan.h"
107 #include "executor/nodeCtescan.h"
108 #include "executor/nodeWorktablescan.h"
109 #include "miscadmin.h"
112 /* ------------------------------------------------------------------------
115 * Recursively initializes all the nodes in the plan tree rooted
119 * 'node' is the current node of the plan produced by the query planner
120 * 'estate' is the shared execution state for the plan tree
121 * 'eflags' is a bitwise OR of flag bits described in executor.h
123 * Returns a PlanState node corresponding to the given Plan node.
124 * ------------------------------------------------------------------------
127 ExecInitNode(Plan
*node
, EState
*estate
, int eflags
)
134 * do nothing when we get to the end of a leaf on tree.
139 switch (nodeTag(node
))
145 result
= (PlanState
*) ExecInitResult((Result
*) node
,
150 result
= (PlanState
*) ExecInitAppend((Append
*) node
,
154 case T_RecursiveUnion
:
155 result
= (PlanState
*) ExecInitRecursiveUnion((RecursiveUnion
*) node
,
160 result
= (PlanState
*) ExecInitBitmapAnd((BitmapAnd
*) node
,
165 result
= (PlanState
*) ExecInitBitmapOr((BitmapOr
*) node
,
173 result
= (PlanState
*) ExecInitSeqScan((SeqScan
*) node
,
178 result
= (PlanState
*) ExecInitIndexScan((IndexScan
*) node
,
182 case T_BitmapIndexScan
:
183 result
= (PlanState
*) ExecInitBitmapIndexScan((BitmapIndexScan
*) node
,
187 case T_BitmapHeapScan
:
188 result
= (PlanState
*) ExecInitBitmapHeapScan((BitmapHeapScan
*) node
,
193 result
= (PlanState
*) ExecInitTidScan((TidScan
*) node
,
198 result
= (PlanState
*) ExecInitSubqueryScan((SubqueryScan
*) node
,
203 result
= (PlanState
*) ExecInitFunctionScan((FunctionScan
*) node
,
208 result
= (PlanState
*) ExecInitValuesScan((ValuesScan
*) node
,
213 result
= (PlanState
*) ExecInitCteScan((CteScan
*) node
,
217 case T_WorkTableScan
:
218 result
= (PlanState
*) ExecInitWorkTableScan((WorkTableScan
*) node
,
226 result
= (PlanState
*) ExecInitNestLoop((NestLoop
*) node
,
231 result
= (PlanState
*) ExecInitMergeJoin((MergeJoin
*) node
,
236 result
= (PlanState
*) ExecInitHashJoin((HashJoin
*) node
,
241 * materialization nodes
244 result
= (PlanState
*) ExecInitMaterial((Material
*) node
,
249 result
= (PlanState
*) ExecInitSort((Sort
*) node
,
254 result
= (PlanState
*) ExecInitGroup((Group
*) node
,
259 result
= (PlanState
*) ExecInitAgg((Agg
*) node
,
264 result
= (PlanState
*) ExecInitUnique((Unique
*) node
,
269 result
= (PlanState
*) ExecInitHash((Hash
*) node
,
274 result
= (PlanState
*) ExecInitSetOp((SetOp
*) node
,
279 result
= (PlanState
*) ExecInitLimit((Limit
*) node
,
284 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));
285 result
= NULL
; /* keep compiler quiet */
290 * Initialize any initPlans present in this node. The planner put them in
291 * a separate list for us.
294 foreach(l
, node
->initPlan
)
296 SubPlan
*subplan
= (SubPlan
*) lfirst(l
);
297 SubPlanState
*sstate
;
299 Assert(IsA(subplan
, SubPlan
));
300 sstate
= ExecInitSubPlan(subplan
, result
);
301 subps
= lappend(subps
, sstate
);
303 result
->initPlan
= subps
;
305 /* Set up instrumentation for this node if requested */
306 if (estate
->es_instrument
)
307 result
->instrument
= InstrAlloc(1);
313 /* ----------------------------------------------------------------
316 * Execute the given node to return a(nother) tuple.
317 * ----------------------------------------------------------------
320 ExecProcNode(PlanState
*node
)
322 TupleTableSlot
*result
;
324 CHECK_FOR_INTERRUPTS();
326 if (node
->chgParam
!= NULL
) /* something changed */
327 ExecReScan(node
, NULL
); /* let ReScan handle this */
329 if (node
->instrument
)
330 InstrStartNode(node
->instrument
);
332 switch (nodeTag(node
))
338 result
= ExecResult((ResultState
*) node
);
342 result
= ExecAppend((AppendState
*) node
);
345 case T_RecursiveUnionState
:
346 result
= ExecRecursiveUnion((RecursiveUnionState
*) node
);
349 /* BitmapAndState does not yield tuples */
351 /* BitmapOrState does not yield tuples */
357 result
= ExecSeqScan((SeqScanState
*) node
);
360 case T_IndexScanState
:
361 result
= ExecIndexScan((IndexScanState
*) node
);
364 /* BitmapIndexScanState does not yield tuples */
366 case T_BitmapHeapScanState
:
367 result
= ExecBitmapHeapScan((BitmapHeapScanState
*) node
);
371 result
= ExecTidScan((TidScanState
*) node
);
374 case T_SubqueryScanState
:
375 result
= ExecSubqueryScan((SubqueryScanState
*) node
);
378 case T_FunctionScanState
:
379 result
= ExecFunctionScan((FunctionScanState
*) node
);
382 case T_ValuesScanState
:
383 result
= ExecValuesScan((ValuesScanState
*) node
);
387 result
= ExecCteScan((CteScanState
*) node
);
390 case T_WorkTableScanState
:
391 result
= ExecWorkTableScan((WorkTableScanState
*) node
);
397 case T_NestLoopState
:
398 result
= ExecNestLoop((NestLoopState
*) node
);
401 case T_MergeJoinState
:
402 result
= ExecMergeJoin((MergeJoinState
*) node
);
405 case T_HashJoinState
:
406 result
= ExecHashJoin((HashJoinState
*) node
);
410 * materialization nodes
412 case T_MaterialState
:
413 result
= ExecMaterial((MaterialState
*) node
);
417 result
= ExecSort((SortState
*) node
);
421 result
= ExecGroup((GroupState
*) node
);
425 result
= ExecAgg((AggState
*) node
);
429 result
= ExecUnique((UniqueState
*) node
);
433 result
= ExecHash((HashState
*) node
);
437 result
= ExecSetOp((SetOpState
*) node
);
441 result
= ExecLimit((LimitState
*) node
);
445 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));
450 if (node
->instrument
)
451 InstrStopNode(node
->instrument
, TupIsNull(result
) ? 0.0 : 1.0);
457 /* ----------------------------------------------------------------
460 * Execute a node that doesn't return individual tuples
461 * (it might return a hashtable, bitmap, etc). Caller should
462 * check it got back the expected kind of Node.
464 * This has essentially the same responsibilities as ExecProcNode,
465 * but it does not do InstrStartNode/InstrStopNode (mainly because
466 * it can't tell how many returned tuples to count). Each per-node
467 * function must provide its own instrumentation support.
468 * ----------------------------------------------------------------
471 MultiExecProcNode(PlanState
*node
)
475 CHECK_FOR_INTERRUPTS();
477 if (node
->chgParam
!= NULL
) /* something changed */
478 ExecReScan(node
, NULL
); /* let ReScan handle this */
480 switch (nodeTag(node
))
483 * Only node types that actually support multiexec will be listed
487 result
= MultiExecHash((HashState
*) node
);
490 case T_BitmapIndexScanState
:
491 result
= MultiExecBitmapIndexScan((BitmapIndexScanState
*) node
);
494 case T_BitmapAndState
:
495 result
= MultiExecBitmapAnd((BitmapAndState
*) node
);
498 case T_BitmapOrState
:
499 result
= MultiExecBitmapOr((BitmapOrState
*) node
);
503 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));
513 * ExecCountSlotsNode - count up the number of tuple table slots needed
515 * Note that this scans a Plan tree, not a PlanState tree, because we
516 * haven't built the PlanState tree yet ...
519 ExecCountSlotsNode(Plan
*node
)
524 switch (nodeTag(node
))
530 return ExecCountSlotsResult((Result
*) node
);
533 return ExecCountSlotsAppend((Append
*) node
);
535 case T_RecursiveUnion
:
536 return ExecCountSlotsRecursiveUnion((RecursiveUnion
*) node
);
539 return ExecCountSlotsBitmapAnd((BitmapAnd
*) node
);
542 return ExecCountSlotsBitmapOr((BitmapOr
*) node
);
548 return ExecCountSlotsSeqScan((SeqScan
*) node
);
551 return ExecCountSlotsIndexScan((IndexScan
*) node
);
553 case T_BitmapIndexScan
:
554 return ExecCountSlotsBitmapIndexScan((BitmapIndexScan
*) node
);
556 case T_BitmapHeapScan
:
557 return ExecCountSlotsBitmapHeapScan((BitmapHeapScan
*) node
);
560 return ExecCountSlotsTidScan((TidScan
*) node
);
563 return ExecCountSlotsSubqueryScan((SubqueryScan
*) node
);
566 return ExecCountSlotsFunctionScan((FunctionScan
*) node
);
569 return ExecCountSlotsValuesScan((ValuesScan
*) node
);
572 return ExecCountSlotsCteScan((CteScan
*) node
);
574 case T_WorkTableScan
:
575 return ExecCountSlotsWorkTableScan((WorkTableScan
*) node
);
581 return ExecCountSlotsNestLoop((NestLoop
*) node
);
584 return ExecCountSlotsMergeJoin((MergeJoin
*) node
);
587 return ExecCountSlotsHashJoin((HashJoin
*) node
);
590 * materialization nodes
593 return ExecCountSlotsMaterial((Material
*) node
);
596 return ExecCountSlotsSort((Sort
*) node
);
599 return ExecCountSlotsGroup((Group
*) node
);
602 return ExecCountSlotsAgg((Agg
*) node
);
605 return ExecCountSlotsUnique((Unique
*) node
);
608 return ExecCountSlotsHash((Hash
*) node
);
611 return ExecCountSlotsSetOp((SetOp
*) node
);
614 return ExecCountSlotsLimit((Limit
*) node
);
617 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));
624 /* ----------------------------------------------------------------
627 * Recursively cleans up all the nodes in the plan rooted
630 * After this operation, the query plan will not be able to
631 * processed any further. This should be called only after
632 * the query plan has been fully executed.
633 * ----------------------------------------------------------------
636 ExecEndNode(PlanState
*node
)
639 * do nothing when we get to the end of a leaf on tree.
644 if (node
->chgParam
!= NULL
)
646 bms_free(node
->chgParam
);
647 node
->chgParam
= NULL
;
650 switch (nodeTag(node
))
656 ExecEndResult((ResultState
*) node
);
660 ExecEndAppend((AppendState
*) node
);
663 case T_RecursiveUnionState
:
664 ExecEndRecursiveUnion((RecursiveUnionState
*) node
);
667 case T_BitmapAndState
:
668 ExecEndBitmapAnd((BitmapAndState
*) node
);
671 case T_BitmapOrState
:
672 ExecEndBitmapOr((BitmapOrState
*) node
);
679 ExecEndSeqScan((SeqScanState
*) node
);
682 case T_IndexScanState
:
683 ExecEndIndexScan((IndexScanState
*) node
);
686 case T_BitmapIndexScanState
:
687 ExecEndBitmapIndexScan((BitmapIndexScanState
*) node
);
690 case T_BitmapHeapScanState
:
691 ExecEndBitmapHeapScan((BitmapHeapScanState
*) node
);
695 ExecEndTidScan((TidScanState
*) node
);
698 case T_SubqueryScanState
:
699 ExecEndSubqueryScan((SubqueryScanState
*) node
);
702 case T_FunctionScanState
:
703 ExecEndFunctionScan((FunctionScanState
*) node
);
706 case T_ValuesScanState
:
707 ExecEndValuesScan((ValuesScanState
*) node
);
711 ExecEndCteScan((CteScanState
*) node
);
714 case T_WorkTableScanState
:
715 ExecEndWorkTableScan((WorkTableScanState
*) node
);
721 case T_NestLoopState
:
722 ExecEndNestLoop((NestLoopState
*) node
);
725 case T_MergeJoinState
:
726 ExecEndMergeJoin((MergeJoinState
*) node
);
729 case T_HashJoinState
:
730 ExecEndHashJoin((HashJoinState
*) node
);
734 * materialization nodes
736 case T_MaterialState
:
737 ExecEndMaterial((MaterialState
*) node
);
741 ExecEndSort((SortState
*) node
);
745 ExecEndGroup((GroupState
*) node
);
749 ExecEndAgg((AggState
*) node
);
753 ExecEndUnique((UniqueState
*) node
);
757 ExecEndHash((HashState
*) node
);
761 ExecEndSetOp((SetOpState
*) node
);
765 ExecEndLimit((LimitState
*) node
);
769 elog(ERROR
, "unrecognized node type: %d", (int) nodeTag(node
));