Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / backend / executor / execProcnode.c
blob15af71122c0c69638d0d84ab6eae64823ea422db
1 /*-------------------------------------------------------------------------
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.
10 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
14 * IDENTIFICATION
15 * $PostgreSQL$
17 *-------------------------------------------------------------------------
20 * INTERFACE ROUTINES
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
26 * NOTES
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.
31 * EXAMPLE
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
37 * DEPT.name = "shoe"
39 * Suppose the planner gives us the following plan:
41 * Nest Loop (DEPT.mgr = EMP.name)
42 * / \
43 * / \
44 * Seq Scan Seq Scan
45 * DEPT EMP
46 * (name = "shoe")
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.
78 #include "postgres.h"
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/nodeCtescan.h"
89 #include "executor/nodeFunctionscan.h"
90 #include "executor/nodeGroup.h"
91 #include "executor/nodeHash.h"
92 #include "executor/nodeHashjoin.h"
93 #include "executor/nodeIndexscan.h"
94 #include "executor/nodeLimit.h"
95 #include "executor/nodeMaterial.h"
96 #include "executor/nodeMergejoin.h"
97 #include "executor/nodeNestloop.h"
98 #include "executor/nodeRecursiveunion.h"
99 #include "executor/nodeResult.h"
100 #include "executor/nodeSeqscan.h"
101 #include "executor/nodeSetOp.h"
102 #include "executor/nodeSort.h"
103 #include "executor/nodeSubplan.h"
104 #include "executor/nodeSubqueryscan.h"
105 #include "executor/nodeTidscan.h"
106 #include "executor/nodeUnique.h"
107 #include "executor/nodeValuesscan.h"
108 #include "executor/nodeWindowAgg.h"
109 #include "executor/nodeWorktablescan.h"
110 #include "miscadmin.h"
113 /* ------------------------------------------------------------------------
114 * ExecInitNode
116 * Recursively initializes all the nodes in the plan tree rooted
117 * at 'node'.
119 * Inputs:
120 * 'node' is the current node of the plan produced by the query planner
121 * 'estate' is the shared execution state for the plan tree
122 * 'eflags' is a bitwise OR of flag bits described in executor.h
124 * Returns a PlanState node corresponding to the given Plan node.
125 * ------------------------------------------------------------------------
127 PlanState *
128 ExecInitNode(Plan *node, EState *estate, int eflags)
130 PlanState *result;
131 List *subps;
132 ListCell *l;
135 * do nothing when we get to the end of a leaf on tree.
137 if (node == NULL)
138 return NULL;
140 switch (nodeTag(node))
143 * control nodes
145 case T_Result:
146 result = (PlanState *) ExecInitResult((Result *) node,
147 estate, eflags);
148 break;
150 case T_Append:
151 result = (PlanState *) ExecInitAppend((Append *) node,
152 estate, eflags);
153 break;
155 case T_RecursiveUnion:
156 result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
157 estate, eflags);
158 break;
160 case T_BitmapAnd:
161 result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
162 estate, eflags);
163 break;
165 case T_BitmapOr:
166 result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
167 estate, eflags);
168 break;
171 * scan nodes
173 case T_SeqScan:
174 result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
175 estate, eflags);
176 break;
178 case T_IndexScan:
179 result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
180 estate, eflags);
181 break;
183 case T_BitmapIndexScan:
184 result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
185 estate, eflags);
186 break;
188 case T_BitmapHeapScan:
189 result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
190 estate, eflags);
191 break;
193 case T_TidScan:
194 result = (PlanState *) ExecInitTidScan((TidScan *) node,
195 estate, eflags);
196 break;
198 case T_SubqueryScan:
199 result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
200 estate, eflags);
201 break;
203 case T_FunctionScan:
204 result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
205 estate, eflags);
206 break;
208 case T_ValuesScan:
209 result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
210 estate, eflags);
211 break;
213 case T_CteScan:
214 result = (PlanState *) ExecInitCteScan((CteScan *) node,
215 estate, eflags);
216 break;
218 case T_WorkTableScan:
219 result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
220 estate, eflags);
221 break;
224 * join nodes
226 case T_NestLoop:
227 result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
228 estate, eflags);
229 break;
231 case T_MergeJoin:
232 result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
233 estate, eflags);
234 break;
236 case T_HashJoin:
237 result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
238 estate, eflags);
239 break;
242 * materialization nodes
244 case T_Material:
245 result = (PlanState *) ExecInitMaterial((Material *) node,
246 estate, eflags);
247 break;
249 case T_Sort:
250 result = (PlanState *) ExecInitSort((Sort *) node,
251 estate, eflags);
252 break;
254 case T_Group:
255 result = (PlanState *) ExecInitGroup((Group *) node,
256 estate, eflags);
257 break;
259 case T_Agg:
260 result = (PlanState *) ExecInitAgg((Agg *) node,
261 estate, eflags);
262 break;
264 case T_WindowAgg:
265 result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
266 estate, eflags);
267 break;
269 case T_Unique:
270 result = (PlanState *) ExecInitUnique((Unique *) node,
271 estate, eflags);
272 break;
274 case T_Hash:
275 result = (PlanState *) ExecInitHash((Hash *) node,
276 estate, eflags);
277 break;
279 case T_SetOp:
280 result = (PlanState *) ExecInitSetOp((SetOp *) node,
281 estate, eflags);
282 break;
284 case T_Limit:
285 result = (PlanState *) ExecInitLimit((Limit *) node,
286 estate, eflags);
287 break;
289 default:
290 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
291 result = NULL; /* keep compiler quiet */
292 break;
296 * Initialize any initPlans present in this node. The planner put them in
297 * a separate list for us.
299 subps = NIL;
300 foreach(l, node->initPlan)
302 SubPlan *subplan = (SubPlan *) lfirst(l);
303 SubPlanState *sstate;
305 Assert(IsA(subplan, SubPlan));
306 sstate = ExecInitSubPlan(subplan, result);
307 subps = lappend(subps, sstate);
309 result->initPlan = subps;
311 /* Set up instrumentation for this node if requested */
312 if (estate->es_instrument)
313 result->instrument = InstrAlloc(1);
315 return result;
319 /* ----------------------------------------------------------------
320 * ExecProcNode
322 * Execute the given node to return a(nother) tuple.
323 * ----------------------------------------------------------------
325 TupleTableSlot *
326 ExecProcNode(PlanState *node)
328 TupleTableSlot *result;
330 CHECK_FOR_INTERRUPTS();
332 if (node->chgParam != NULL) /* something changed */
333 ExecReScan(node, NULL); /* let ReScan handle this */
335 if (node->instrument)
336 InstrStartNode(node->instrument);
338 switch (nodeTag(node))
341 * control nodes
343 case T_ResultState:
344 result = ExecResult((ResultState *) node);
345 break;
347 case T_AppendState:
348 result = ExecAppend((AppendState *) node);
349 break;
351 case T_RecursiveUnionState:
352 result = ExecRecursiveUnion((RecursiveUnionState *) node);
353 break;
355 /* BitmapAndState does not yield tuples */
357 /* BitmapOrState does not yield tuples */
360 * scan nodes
362 case T_SeqScanState:
363 result = ExecSeqScan((SeqScanState *) node);
364 break;
366 case T_IndexScanState:
367 result = ExecIndexScan((IndexScanState *) node);
368 break;
370 /* BitmapIndexScanState does not yield tuples */
372 case T_BitmapHeapScanState:
373 result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
374 break;
376 case T_TidScanState:
377 result = ExecTidScan((TidScanState *) node);
378 break;
380 case T_SubqueryScanState:
381 result = ExecSubqueryScan((SubqueryScanState *) node);
382 break;
384 case T_FunctionScanState:
385 result = ExecFunctionScan((FunctionScanState *) node);
386 break;
388 case T_ValuesScanState:
389 result = ExecValuesScan((ValuesScanState *) node);
390 break;
392 case T_CteScanState:
393 result = ExecCteScan((CteScanState *) node);
394 break;
396 case T_WorkTableScanState:
397 result = ExecWorkTableScan((WorkTableScanState *) node);
398 break;
401 * join nodes
403 case T_NestLoopState:
404 result = ExecNestLoop((NestLoopState *) node);
405 break;
407 case T_MergeJoinState:
408 result = ExecMergeJoin((MergeJoinState *) node);
409 break;
411 case T_HashJoinState:
412 result = ExecHashJoin((HashJoinState *) node);
413 break;
416 * materialization nodes
418 case T_MaterialState:
419 result = ExecMaterial((MaterialState *) node);
420 break;
422 case T_SortState:
423 result = ExecSort((SortState *) node);
424 break;
426 case T_GroupState:
427 result = ExecGroup((GroupState *) node);
428 break;
430 case T_AggState:
431 result = ExecAgg((AggState *) node);
432 break;
434 case T_WindowAggState:
435 result = ExecWindowAgg((WindowAggState *) node);
436 break;
438 case T_UniqueState:
439 result = ExecUnique((UniqueState *) node);
440 break;
442 case T_HashState:
443 result = ExecHash((HashState *) node);
444 break;
446 case T_SetOpState:
447 result = ExecSetOp((SetOpState *) node);
448 break;
450 case T_LimitState:
451 result = ExecLimit((LimitState *) node);
452 break;
454 default:
455 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
456 result = NULL;
457 break;
460 if (node->instrument)
461 InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
463 return result;
467 /* ----------------------------------------------------------------
468 * MultiExecProcNode
470 * Execute a node that doesn't return individual tuples
471 * (it might return a hashtable, bitmap, etc). Caller should
472 * check it got back the expected kind of Node.
474 * This has essentially the same responsibilities as ExecProcNode,
475 * but it does not do InstrStartNode/InstrStopNode (mainly because
476 * it can't tell how many returned tuples to count). Each per-node
477 * function must provide its own instrumentation support.
478 * ----------------------------------------------------------------
480 Node *
481 MultiExecProcNode(PlanState *node)
483 Node *result;
485 CHECK_FOR_INTERRUPTS();
487 if (node->chgParam != NULL) /* something changed */
488 ExecReScan(node, NULL); /* let ReScan handle this */
490 switch (nodeTag(node))
493 * Only node types that actually support multiexec will be listed
496 case T_HashState:
497 result = MultiExecHash((HashState *) node);
498 break;
500 case T_BitmapIndexScanState:
501 result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
502 break;
504 case T_BitmapAndState:
505 result = MultiExecBitmapAnd((BitmapAndState *) node);
506 break;
508 case T_BitmapOrState:
509 result = MultiExecBitmapOr((BitmapOrState *) node);
510 break;
512 default:
513 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
514 result = NULL;
515 break;
518 return result;
523 * ExecCountSlotsNode - count up the number of tuple table slots needed
525 * Note that this scans a Plan tree, not a PlanState tree, because we
526 * haven't built the PlanState tree yet ...
529 ExecCountSlotsNode(Plan *node)
531 if (node == NULL)
532 return 0;
534 switch (nodeTag(node))
537 * control nodes
539 case T_Result:
540 return ExecCountSlotsResult((Result *) node);
542 case T_Append:
543 return ExecCountSlotsAppend((Append *) node);
545 case T_RecursiveUnion:
546 return ExecCountSlotsRecursiveUnion((RecursiveUnion *) node);
548 case T_BitmapAnd:
549 return ExecCountSlotsBitmapAnd((BitmapAnd *) node);
551 case T_BitmapOr:
552 return ExecCountSlotsBitmapOr((BitmapOr *) node);
555 * scan nodes
557 case T_SeqScan:
558 return ExecCountSlotsSeqScan((SeqScan *) node);
560 case T_IndexScan:
561 return ExecCountSlotsIndexScan((IndexScan *) node);
563 case T_BitmapIndexScan:
564 return ExecCountSlotsBitmapIndexScan((BitmapIndexScan *) node);
566 case T_BitmapHeapScan:
567 return ExecCountSlotsBitmapHeapScan((BitmapHeapScan *) node);
569 case T_TidScan:
570 return ExecCountSlotsTidScan((TidScan *) node);
572 case T_SubqueryScan:
573 return ExecCountSlotsSubqueryScan((SubqueryScan *) node);
575 case T_FunctionScan:
576 return ExecCountSlotsFunctionScan((FunctionScan *) node);
578 case T_ValuesScan:
579 return ExecCountSlotsValuesScan((ValuesScan *) node);
581 case T_CteScan:
582 return ExecCountSlotsCteScan((CteScan *) node);
584 case T_WorkTableScan:
585 return ExecCountSlotsWorkTableScan((WorkTableScan *) node);
588 * join nodes
590 case T_NestLoop:
591 return ExecCountSlotsNestLoop((NestLoop *) node);
593 case T_MergeJoin:
594 return ExecCountSlotsMergeJoin((MergeJoin *) node);
596 case T_HashJoin:
597 return ExecCountSlotsHashJoin((HashJoin *) node);
600 * materialization nodes
602 case T_Material:
603 return ExecCountSlotsMaterial((Material *) node);
605 case T_Sort:
606 return ExecCountSlotsSort((Sort *) node);
608 case T_Group:
609 return ExecCountSlotsGroup((Group *) node);
611 case T_Agg:
612 return ExecCountSlotsAgg((Agg *) node);
614 case T_WindowAgg:
615 return ExecCountSlotsWindowAgg((WindowAgg *) node);
616 break;
618 case T_Unique:
619 return ExecCountSlotsUnique((Unique *) node);
621 case T_Hash:
622 return ExecCountSlotsHash((Hash *) node);
624 case T_SetOp:
625 return ExecCountSlotsSetOp((SetOp *) node);
627 case T_Limit:
628 return ExecCountSlotsLimit((Limit *) node);
630 default:
631 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
632 break;
635 return 0;
638 /* ----------------------------------------------------------------
639 * ExecEndNode
641 * Recursively cleans up all the nodes in the plan rooted
642 * at 'node'.
644 * After this operation, the query plan will not be able to
645 * processed any further. This should be called only after
646 * the query plan has been fully executed.
647 * ----------------------------------------------------------------
649 void
650 ExecEndNode(PlanState *node)
653 * do nothing when we get to the end of a leaf on tree.
655 if (node == NULL)
656 return;
658 if (node->chgParam != NULL)
660 bms_free(node->chgParam);
661 node->chgParam = NULL;
664 switch (nodeTag(node))
667 * control nodes
669 case T_ResultState:
670 ExecEndResult((ResultState *) node);
671 break;
673 case T_AppendState:
674 ExecEndAppend((AppendState *) node);
675 break;
677 case T_RecursiveUnionState:
678 ExecEndRecursiveUnion((RecursiveUnionState *) node);
679 break;
681 case T_BitmapAndState:
682 ExecEndBitmapAnd((BitmapAndState *) node);
683 break;
685 case T_BitmapOrState:
686 ExecEndBitmapOr((BitmapOrState *) node);
687 break;
690 * scan nodes
692 case T_SeqScanState:
693 ExecEndSeqScan((SeqScanState *) node);
694 break;
696 case T_IndexScanState:
697 ExecEndIndexScan((IndexScanState *) node);
698 break;
700 case T_BitmapIndexScanState:
701 ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
702 break;
704 case T_BitmapHeapScanState:
705 ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
706 break;
708 case T_TidScanState:
709 ExecEndTidScan((TidScanState *) node);
710 break;
712 case T_SubqueryScanState:
713 ExecEndSubqueryScan((SubqueryScanState *) node);
714 break;
716 case T_FunctionScanState:
717 ExecEndFunctionScan((FunctionScanState *) node);
718 break;
720 case T_ValuesScanState:
721 ExecEndValuesScan((ValuesScanState *) node);
722 break;
724 case T_CteScanState:
725 ExecEndCteScan((CteScanState *) node);
726 break;
728 case T_WorkTableScanState:
729 ExecEndWorkTableScan((WorkTableScanState *) node);
730 break;
733 * join nodes
735 case T_NestLoopState:
736 ExecEndNestLoop((NestLoopState *) node);
737 break;
739 case T_MergeJoinState:
740 ExecEndMergeJoin((MergeJoinState *) node);
741 break;
743 case T_HashJoinState:
744 ExecEndHashJoin((HashJoinState *) node);
745 break;
748 * materialization nodes
750 case T_MaterialState:
751 ExecEndMaterial((MaterialState *) node);
752 break;
754 case T_SortState:
755 ExecEndSort((SortState *) node);
756 break;
758 case T_GroupState:
759 ExecEndGroup((GroupState *) node);
760 break;
762 case T_AggState:
763 ExecEndAgg((AggState *) node);
764 break;
766 case T_WindowAggState:
767 ExecEndWindowAgg((WindowAggState *) node);
768 break;
770 case T_UniqueState:
771 ExecEndUnique((UniqueState *) node);
772 break;
774 case T_HashState:
775 ExecEndHash((HashState *) node);
776 break;
778 case T_SetOpState:
779 ExecEndSetOp((SetOpState *) node);
780 break;
782 case T_LimitState:
783 ExecEndLimit((LimitState *) node);
784 break;
786 default:
787 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
788 break;