Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / executor / execProcnode.c
blob86b815874883b700f231038314fe7b52fe85e6dd
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-2008, 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/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 /* ------------------------------------------------------------------------
113 * ExecInitNode
115 * Recursively initializes all the nodes in the plan tree rooted
116 * at 'node'.
118 * Inputs:
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 * ------------------------------------------------------------------------
126 PlanState *
127 ExecInitNode(Plan *node, EState *estate, int eflags)
129 PlanState *result;
130 List *subps;
131 ListCell *l;
134 * do nothing when we get to the end of a leaf on tree.
136 if (node == NULL)
137 return NULL;
139 switch (nodeTag(node))
142 * control nodes
144 case T_Result:
145 result = (PlanState *) ExecInitResult((Result *) node,
146 estate, eflags);
147 break;
149 case T_Append:
150 result = (PlanState *) ExecInitAppend((Append *) node,
151 estate, eflags);
152 break;
154 case T_RecursiveUnion:
155 result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
156 estate, eflags);
157 break;
159 case T_BitmapAnd:
160 result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
161 estate, eflags);
162 break;
164 case T_BitmapOr:
165 result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
166 estate, eflags);
167 break;
170 * scan nodes
172 case T_SeqScan:
173 result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
174 estate, eflags);
175 break;
177 case T_IndexScan:
178 result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
179 estate, eflags);
180 break;
182 case T_BitmapIndexScan:
183 result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
184 estate, eflags);
185 break;
187 case T_BitmapHeapScan:
188 result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
189 estate, eflags);
190 break;
192 case T_TidScan:
193 result = (PlanState *) ExecInitTidScan((TidScan *) node,
194 estate, eflags);
195 break;
197 case T_SubqueryScan:
198 result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
199 estate, eflags);
200 break;
202 case T_FunctionScan:
203 result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
204 estate, eflags);
205 break;
207 case T_ValuesScan:
208 result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
209 estate, eflags);
210 break;
212 case T_CteScan:
213 result = (PlanState *) ExecInitCteScan((CteScan *) node,
214 estate, eflags);
215 break;
217 case T_WorkTableScan:
218 result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
219 estate, eflags);
220 break;
223 * join nodes
225 case T_NestLoop:
226 result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
227 estate, eflags);
228 break;
230 case T_MergeJoin:
231 result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
232 estate, eflags);
233 break;
235 case T_HashJoin:
236 result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
237 estate, eflags);
238 break;
241 * materialization nodes
243 case T_Material:
244 result = (PlanState *) ExecInitMaterial((Material *) node,
245 estate, eflags);
246 break;
248 case T_Sort:
249 result = (PlanState *) ExecInitSort((Sort *) node,
250 estate, eflags);
251 break;
253 case T_Group:
254 result = (PlanState *) ExecInitGroup((Group *) node,
255 estate, eflags);
256 break;
258 case T_Agg:
259 result = (PlanState *) ExecInitAgg((Agg *) node,
260 estate, eflags);
261 break;
263 case T_Unique:
264 result = (PlanState *) ExecInitUnique((Unique *) node,
265 estate, eflags);
266 break;
268 case T_Hash:
269 result = (PlanState *) ExecInitHash((Hash *) node,
270 estate, eflags);
271 break;
273 case T_SetOp:
274 result = (PlanState *) ExecInitSetOp((SetOp *) node,
275 estate, eflags);
276 break;
278 case T_Limit:
279 result = (PlanState *) ExecInitLimit((Limit *) node,
280 estate, eflags);
281 break;
283 default:
284 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
285 result = NULL; /* keep compiler quiet */
286 break;
290 * Initialize any initPlans present in this node. The planner put them in
291 * a separate list for us.
293 subps = NIL;
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);
309 return result;
313 /* ----------------------------------------------------------------
314 * ExecProcNode
316 * Execute the given node to return a(nother) tuple.
317 * ----------------------------------------------------------------
319 TupleTableSlot *
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))
335 * control nodes
337 case T_ResultState:
338 result = ExecResult((ResultState *) node);
339 break;
341 case T_AppendState:
342 result = ExecAppend((AppendState *) node);
343 break;
345 case T_RecursiveUnionState:
346 result = ExecRecursiveUnion((RecursiveUnionState *) node);
347 break;
349 /* BitmapAndState does not yield tuples */
351 /* BitmapOrState does not yield tuples */
354 * scan nodes
356 case T_SeqScanState:
357 result = ExecSeqScan((SeqScanState *) node);
358 break;
360 case T_IndexScanState:
361 result = ExecIndexScan((IndexScanState *) node);
362 break;
364 /* BitmapIndexScanState does not yield tuples */
366 case T_BitmapHeapScanState:
367 result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
368 break;
370 case T_TidScanState:
371 result = ExecTidScan((TidScanState *) node);
372 break;
374 case T_SubqueryScanState:
375 result = ExecSubqueryScan((SubqueryScanState *) node);
376 break;
378 case T_FunctionScanState:
379 result = ExecFunctionScan((FunctionScanState *) node);
380 break;
382 case T_ValuesScanState:
383 result = ExecValuesScan((ValuesScanState *) node);
384 break;
386 case T_CteScanState:
387 result = ExecCteScan((CteScanState *) node);
388 break;
390 case T_WorkTableScanState:
391 result = ExecWorkTableScan((WorkTableScanState *) node);
392 break;
395 * join nodes
397 case T_NestLoopState:
398 result = ExecNestLoop((NestLoopState *) node);
399 break;
401 case T_MergeJoinState:
402 result = ExecMergeJoin((MergeJoinState *) node);
403 break;
405 case T_HashJoinState:
406 result = ExecHashJoin((HashJoinState *) node);
407 break;
410 * materialization nodes
412 case T_MaterialState:
413 result = ExecMaterial((MaterialState *) node);
414 break;
416 case T_SortState:
417 result = ExecSort((SortState *) node);
418 break;
420 case T_GroupState:
421 result = ExecGroup((GroupState *) node);
422 break;
424 case T_AggState:
425 result = ExecAgg((AggState *) node);
426 break;
428 case T_UniqueState:
429 result = ExecUnique((UniqueState *) node);
430 break;
432 case T_HashState:
433 result = ExecHash((HashState *) node);
434 break;
436 case T_SetOpState:
437 result = ExecSetOp((SetOpState *) node);
438 break;
440 case T_LimitState:
441 result = ExecLimit((LimitState *) node);
442 break;
444 default:
445 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
446 result = NULL;
447 break;
450 if (node->instrument)
451 InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
453 return result;
457 /* ----------------------------------------------------------------
458 * MultiExecProcNode
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 * ----------------------------------------------------------------
470 Node *
471 MultiExecProcNode(PlanState *node)
473 Node *result;
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
486 case T_HashState:
487 result = MultiExecHash((HashState *) node);
488 break;
490 case T_BitmapIndexScanState:
491 result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
492 break;
494 case T_BitmapAndState:
495 result = MultiExecBitmapAnd((BitmapAndState *) node);
496 break;
498 case T_BitmapOrState:
499 result = MultiExecBitmapOr((BitmapOrState *) node);
500 break;
502 default:
503 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
504 result = NULL;
505 break;
508 return result;
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)
521 if (node == NULL)
522 return 0;
524 switch (nodeTag(node))
527 * control nodes
529 case T_Result:
530 return ExecCountSlotsResult((Result *) node);
532 case T_Append:
533 return ExecCountSlotsAppend((Append *) node);
535 case T_RecursiveUnion:
536 return ExecCountSlotsRecursiveUnion((RecursiveUnion *) node);
538 case T_BitmapAnd:
539 return ExecCountSlotsBitmapAnd((BitmapAnd *) node);
541 case T_BitmapOr:
542 return ExecCountSlotsBitmapOr((BitmapOr *) node);
545 * scan nodes
547 case T_SeqScan:
548 return ExecCountSlotsSeqScan((SeqScan *) node);
550 case T_IndexScan:
551 return ExecCountSlotsIndexScan((IndexScan *) node);
553 case T_BitmapIndexScan:
554 return ExecCountSlotsBitmapIndexScan((BitmapIndexScan *) node);
556 case T_BitmapHeapScan:
557 return ExecCountSlotsBitmapHeapScan((BitmapHeapScan *) node);
559 case T_TidScan:
560 return ExecCountSlotsTidScan((TidScan *) node);
562 case T_SubqueryScan:
563 return ExecCountSlotsSubqueryScan((SubqueryScan *) node);
565 case T_FunctionScan:
566 return ExecCountSlotsFunctionScan((FunctionScan *) node);
568 case T_ValuesScan:
569 return ExecCountSlotsValuesScan((ValuesScan *) node);
571 case T_CteScan:
572 return ExecCountSlotsCteScan((CteScan *) node);
574 case T_WorkTableScan:
575 return ExecCountSlotsWorkTableScan((WorkTableScan *) node);
578 * join nodes
580 case T_NestLoop:
581 return ExecCountSlotsNestLoop((NestLoop *) node);
583 case T_MergeJoin:
584 return ExecCountSlotsMergeJoin((MergeJoin *) node);
586 case T_HashJoin:
587 return ExecCountSlotsHashJoin((HashJoin *) node);
590 * materialization nodes
592 case T_Material:
593 return ExecCountSlotsMaterial((Material *) node);
595 case T_Sort:
596 return ExecCountSlotsSort((Sort *) node);
598 case T_Group:
599 return ExecCountSlotsGroup((Group *) node);
601 case T_Agg:
602 return ExecCountSlotsAgg((Agg *) node);
604 case T_Unique:
605 return ExecCountSlotsUnique((Unique *) node);
607 case T_Hash:
608 return ExecCountSlotsHash((Hash *) node);
610 case T_SetOp:
611 return ExecCountSlotsSetOp((SetOp *) node);
613 case T_Limit:
614 return ExecCountSlotsLimit((Limit *) node);
616 default:
617 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
618 break;
621 return 0;
624 /* ----------------------------------------------------------------
625 * ExecEndNode
627 * Recursively cleans up all the nodes in the plan rooted
628 * at 'node'.
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 * ----------------------------------------------------------------
635 void
636 ExecEndNode(PlanState *node)
639 * do nothing when we get to the end of a leaf on tree.
641 if (node == NULL)
642 return;
644 if (node->chgParam != NULL)
646 bms_free(node->chgParam);
647 node->chgParam = NULL;
650 switch (nodeTag(node))
653 * control nodes
655 case T_ResultState:
656 ExecEndResult((ResultState *) node);
657 break;
659 case T_AppendState:
660 ExecEndAppend((AppendState *) node);
661 break;
663 case T_RecursiveUnionState:
664 ExecEndRecursiveUnion((RecursiveUnionState *) node);
665 break;
667 case T_BitmapAndState:
668 ExecEndBitmapAnd((BitmapAndState *) node);
669 break;
671 case T_BitmapOrState:
672 ExecEndBitmapOr((BitmapOrState *) node);
673 break;
676 * scan nodes
678 case T_SeqScanState:
679 ExecEndSeqScan((SeqScanState *) node);
680 break;
682 case T_IndexScanState:
683 ExecEndIndexScan((IndexScanState *) node);
684 break;
686 case T_BitmapIndexScanState:
687 ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
688 break;
690 case T_BitmapHeapScanState:
691 ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
692 break;
694 case T_TidScanState:
695 ExecEndTidScan((TidScanState *) node);
696 break;
698 case T_SubqueryScanState:
699 ExecEndSubqueryScan((SubqueryScanState *) node);
700 break;
702 case T_FunctionScanState:
703 ExecEndFunctionScan((FunctionScanState *) node);
704 break;
706 case T_ValuesScanState:
707 ExecEndValuesScan((ValuesScanState *) node);
708 break;
710 case T_CteScanState:
711 ExecEndCteScan((CteScanState *) node);
712 break;
714 case T_WorkTableScanState:
715 ExecEndWorkTableScan((WorkTableScanState *) node);
716 break;
719 * join nodes
721 case T_NestLoopState:
722 ExecEndNestLoop((NestLoopState *) node);
723 break;
725 case T_MergeJoinState:
726 ExecEndMergeJoin((MergeJoinState *) node);
727 break;
729 case T_HashJoinState:
730 ExecEndHashJoin((HashJoinState *) node);
731 break;
734 * materialization nodes
736 case T_MaterialState:
737 ExecEndMaterial((MaterialState *) node);
738 break;
740 case T_SortState:
741 ExecEndSort((SortState *) node);
742 break;
744 case T_GroupState:
745 ExecEndGroup((GroupState *) node);
746 break;
748 case T_AggState:
749 ExecEndAgg((AggState *) node);
750 break;
752 case T_UniqueState:
753 ExecEndUnique((UniqueState *) node);
754 break;
756 case T_HashState:
757 ExecEndHash((HashState *) node);
758 break;
760 case T_SetOpState:
761 ExecEndSetOp((SetOpState *) node);
762 break;
764 case T_LimitState:
765 ExecEndLimit((LimitState *) node);
766 break;
768 default:
769 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
770 break;