Header And Logo

PostgreSQL
| The world's most advanced open source database.

execProcnode.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * execProcnode.c
00004  *   contains dispatch functions which call the appropriate "initialize",
00005  *   "get a tuple", and "cleanup" routines for the given node type.
00006  *   If the node has children, then it will presumably call ExecInitNode,
00007  *   ExecProcNode, or ExecEndNode on its subnodes and do the appropriate
00008  *   processing.
00009  *
00010  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00011  * Portions Copyright (c) 1994, Regents of the University of California
00012  *
00013  *
00014  * IDENTIFICATION
00015  *    src/backend/executor/execProcnode.c
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 /*
00020  *   INTERFACE ROUTINES
00021  *      ExecInitNode    -       initialize a plan node and its subplans
00022  *      ExecProcNode    -       get a tuple by executing the plan node
00023  *      ExecEndNode     -       shut down a plan node and its subplans
00024  *
00025  *   NOTES
00026  *      This used to be three files.  It is now all combined into
00027  *      one file so that it is easier to keep ExecInitNode, ExecProcNode,
00028  *      and ExecEndNode in sync when new nodes are added.
00029  *
00030  *   EXAMPLE
00031  *      Suppose we want the age of the manager of the shoe department and
00032  *      the number of employees in that department.  So we have the query:
00033  *
00034  *              select DEPT.no_emps, EMP.age
00035  *              from DEPT, EMP
00036  *              where EMP.name = DEPT.mgr and
00037  *                    DEPT.name = "shoe"
00038  *
00039  *      Suppose the planner gives us the following plan:
00040  *
00041  *                      Nest Loop (DEPT.mgr = EMP.name)
00042  *                      /       \
00043  *                     /         \
00044  *                 Seq Scan     Seq Scan
00045  *                  DEPT          EMP
00046  *              (name = "shoe")
00047  *
00048  *      ExecutorStart() is called first.
00049  *      It calls InitPlan() which calls ExecInitNode() on
00050  *      the root of the plan -- the nest loop node.
00051  *
00052  *    * ExecInitNode() notices that it is looking at a nest loop and
00053  *      as the code below demonstrates, it calls ExecInitNestLoop().
00054  *      Eventually this calls ExecInitNode() on the right and left subplans
00055  *      and so forth until the entire plan is initialized.  The result
00056  *      of ExecInitNode() is a plan state tree built with the same structure
00057  *      as the underlying plan tree.
00058  *
00059  *    * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
00060  *      ExecProcNode() repeatedly on the top node of the plan state tree.
00061  *      Each time this happens, ExecProcNode() will end up calling
00062  *      ExecNestLoop(), which calls ExecProcNode() on its subplans.
00063  *      Each of these subplans is a sequential scan so ExecSeqScan() is
00064  *      called.  The slots returned by ExecSeqScan() may contain
00065  *      tuples which contain the attributes ExecNestLoop() uses to
00066  *      form the tuples it returns.
00067  *
00068  *    * Eventually ExecSeqScan() stops returning tuples and the nest
00069  *      loop join ends.  Lastly, ExecutorEnd() calls ExecEndNode() which
00070  *      calls ExecEndNestLoop() which in turn calls ExecEndNode() on
00071  *      its subplans which result in ExecEndSeqScan().
00072  *
00073  *      This should show how the executor works by having
00074  *      ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
00075  *      their work to the appopriate node support routines which may
00076  *      in turn call these routines themselves on their subplans.
00077  */
00078 #include "postgres.h"
00079 
00080 #include "executor/executor.h"
00081 #include "executor/nodeAgg.h"
00082 #include "executor/nodeAppend.h"
00083 #include "executor/nodeBitmapAnd.h"
00084 #include "executor/nodeBitmapHeapscan.h"
00085 #include "executor/nodeBitmapIndexscan.h"
00086 #include "executor/nodeBitmapOr.h"
00087 #include "executor/nodeCtescan.h"
00088 #include "executor/nodeForeignscan.h"
00089 #include "executor/nodeFunctionscan.h"
00090 #include "executor/nodeGroup.h"
00091 #include "executor/nodeHash.h"
00092 #include "executor/nodeHashjoin.h"
00093 #include "executor/nodeIndexonlyscan.h"
00094 #include "executor/nodeIndexscan.h"
00095 #include "executor/nodeLimit.h"
00096 #include "executor/nodeLockRows.h"
00097 #include "executor/nodeMaterial.h"
00098 #include "executor/nodeMergeAppend.h"
00099 #include "executor/nodeMergejoin.h"
00100 #include "executor/nodeModifyTable.h"
00101 #include "executor/nodeNestloop.h"
00102 #include "executor/nodeRecursiveunion.h"
00103 #include "executor/nodeResult.h"
00104 #include "executor/nodeSeqscan.h"
00105 #include "executor/nodeSetOp.h"
00106 #include "executor/nodeSort.h"
00107 #include "executor/nodeSubplan.h"
00108 #include "executor/nodeSubqueryscan.h"
00109 #include "executor/nodeTidscan.h"
00110 #include "executor/nodeUnique.h"
00111 #include "executor/nodeValuesscan.h"
00112 #include "executor/nodeWindowAgg.h"
00113 #include "executor/nodeWorktablescan.h"
00114 #include "miscadmin.h"
00115 
00116 
00117 /* ------------------------------------------------------------------------
00118  *      ExecInitNode
00119  *
00120  *      Recursively initializes all the nodes in the plan tree rooted
00121  *      at 'node'.
00122  *
00123  *      Inputs:
00124  *        'node' is the current node of the plan produced by the query planner
00125  *        'estate' is the shared execution state for the plan tree
00126  *        'eflags' is a bitwise OR of flag bits described in executor.h
00127  *
00128  *      Returns a PlanState node corresponding to the given Plan node.
00129  * ------------------------------------------------------------------------
00130  */
00131 PlanState *
00132 ExecInitNode(Plan *node, EState *estate, int eflags)
00133 {
00134     PlanState  *result;
00135     List       *subps;
00136     ListCell   *l;
00137 
00138     /*
00139      * do nothing when we get to the end of a leaf on tree.
00140      */
00141     if (node == NULL)
00142         return NULL;
00143 
00144     switch (nodeTag(node))
00145     {
00146             /*
00147              * control nodes
00148              */
00149         case T_Result:
00150             result = (PlanState *) ExecInitResult((Result *) node,
00151                                                   estate, eflags);
00152             break;
00153 
00154         case T_ModifyTable:
00155             result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
00156                                                        estate, eflags);
00157             break;
00158 
00159         case T_Append:
00160             result = (PlanState *) ExecInitAppend((Append *) node,
00161                                                   estate, eflags);
00162             break;
00163 
00164         case T_MergeAppend:
00165             result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
00166                                                        estate, eflags);
00167             break;
00168 
00169         case T_RecursiveUnion:
00170             result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
00171                                                           estate, eflags);
00172             break;
00173 
00174         case T_BitmapAnd:
00175             result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
00176                                                      estate, eflags);
00177             break;
00178 
00179         case T_BitmapOr:
00180             result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
00181                                                     estate, eflags);
00182             break;
00183 
00184             /*
00185              * scan nodes
00186              */
00187         case T_SeqScan:
00188             result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
00189                                                    estate, eflags);
00190             break;
00191 
00192         case T_IndexScan:
00193             result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
00194                                                      estate, eflags);
00195             break;
00196 
00197         case T_IndexOnlyScan:
00198             result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
00199                                                          estate, eflags);
00200             break;
00201 
00202         case T_BitmapIndexScan:
00203             result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
00204                                                            estate, eflags);
00205             break;
00206 
00207         case T_BitmapHeapScan:
00208             result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
00209                                                           estate, eflags);
00210             break;
00211 
00212         case T_TidScan:
00213             result = (PlanState *) ExecInitTidScan((TidScan *) node,
00214                                                    estate, eflags);
00215             break;
00216 
00217         case T_SubqueryScan:
00218             result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
00219                                                         estate, eflags);
00220             break;
00221 
00222         case T_FunctionScan:
00223             result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
00224                                                         estate, eflags);
00225             break;
00226 
00227         case T_ValuesScan:
00228             result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
00229                                                       estate, eflags);
00230             break;
00231 
00232         case T_CteScan:
00233             result = (PlanState *) ExecInitCteScan((CteScan *) node,
00234                                                    estate, eflags);
00235             break;
00236 
00237         case T_WorkTableScan:
00238             result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
00239                                                          estate, eflags);
00240             break;
00241 
00242         case T_ForeignScan:
00243             result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
00244                                                        estate, eflags);
00245             break;
00246 
00247             /*
00248              * join nodes
00249              */
00250         case T_NestLoop:
00251             result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
00252                                                     estate, eflags);
00253             break;
00254 
00255         case T_MergeJoin:
00256             result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
00257                                                      estate, eflags);
00258             break;
00259 
00260         case T_HashJoin:
00261             result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
00262                                                     estate, eflags);
00263             break;
00264 
00265             /*
00266              * materialization nodes
00267              */
00268         case T_Material:
00269             result = (PlanState *) ExecInitMaterial((Material *) node,
00270                                                     estate, eflags);
00271             break;
00272 
00273         case T_Sort:
00274             result = (PlanState *) ExecInitSort((Sort *) node,
00275                                                 estate, eflags);
00276             break;
00277 
00278         case T_Group:
00279             result = (PlanState *) ExecInitGroup((Group *) node,
00280                                                  estate, eflags);
00281             break;
00282 
00283         case T_Agg:
00284             result = (PlanState *) ExecInitAgg((Agg *) node,
00285                                                estate, eflags);
00286             break;
00287 
00288         case T_WindowAgg:
00289             result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
00290                                                      estate, eflags);
00291             break;
00292 
00293         case T_Unique:
00294             result = (PlanState *) ExecInitUnique((Unique *) node,
00295                                                   estate, eflags);
00296             break;
00297 
00298         case T_Hash:
00299             result = (PlanState *) ExecInitHash((Hash *) node,
00300                                                 estate, eflags);
00301             break;
00302 
00303         case T_SetOp:
00304             result = (PlanState *) ExecInitSetOp((SetOp *) node,
00305                                                  estate, eflags);
00306             break;
00307 
00308         case T_LockRows:
00309             result = (PlanState *) ExecInitLockRows((LockRows *) node,
00310                                                     estate, eflags);
00311             break;
00312 
00313         case T_Limit:
00314             result = (PlanState *) ExecInitLimit((Limit *) node,
00315                                                  estate, eflags);
00316             break;
00317 
00318         default:
00319             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
00320             result = NULL;      /* keep compiler quiet */
00321             break;
00322     }
00323 
00324     /*
00325      * Initialize any initPlans present in this node.  The planner put them in
00326      * a separate list for us.
00327      */
00328     subps = NIL;
00329     foreach(l, node->initPlan)
00330     {
00331         SubPlan    *subplan = (SubPlan *) lfirst(l);
00332         SubPlanState *sstate;
00333 
00334         Assert(IsA(subplan, SubPlan));
00335         sstate = ExecInitSubPlan(subplan, result);
00336         subps = lappend(subps, sstate);
00337     }
00338     result->initPlan = subps;
00339 
00340     /* Set up instrumentation for this node if requested */
00341     if (estate->es_instrument)
00342         result->instrument = InstrAlloc(1, estate->es_instrument);
00343 
00344     return result;
00345 }
00346 
00347 
00348 /* ----------------------------------------------------------------
00349  *      ExecProcNode
00350  *
00351  *      Execute the given node to return a(nother) tuple.
00352  * ----------------------------------------------------------------
00353  */
00354 TupleTableSlot *
00355 ExecProcNode(PlanState *node)
00356 {
00357     TupleTableSlot *result;
00358 
00359     CHECK_FOR_INTERRUPTS();
00360 
00361     if (node->chgParam != NULL) /* something changed */
00362         ExecReScan(node);       /* let ReScan handle this */
00363 
00364     if (node->instrument)
00365         InstrStartNode(node->instrument);
00366 
00367     switch (nodeTag(node))
00368     {
00369             /*
00370              * control nodes
00371              */
00372         case T_ResultState:
00373             result = ExecResult((ResultState *) node);
00374             break;
00375 
00376         case T_ModifyTableState:
00377             result = ExecModifyTable((ModifyTableState *) node);
00378             break;
00379 
00380         case T_AppendState:
00381             result = ExecAppend((AppendState *) node);
00382             break;
00383 
00384         case T_MergeAppendState:
00385             result = ExecMergeAppend((MergeAppendState *) node);
00386             break;
00387 
00388         case T_RecursiveUnionState:
00389             result = ExecRecursiveUnion((RecursiveUnionState *) node);
00390             break;
00391 
00392             /* BitmapAndState does not yield tuples */
00393 
00394             /* BitmapOrState does not yield tuples */
00395 
00396             /*
00397              * scan nodes
00398              */
00399         case T_SeqScanState:
00400             result = ExecSeqScan((SeqScanState *) node);
00401             break;
00402 
00403         case T_IndexScanState:
00404             result = ExecIndexScan((IndexScanState *) node);
00405             break;
00406 
00407         case T_IndexOnlyScanState:
00408             result = ExecIndexOnlyScan((IndexOnlyScanState *) node);
00409             break;
00410 
00411             /* BitmapIndexScanState does not yield tuples */
00412 
00413         case T_BitmapHeapScanState:
00414             result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
00415             break;
00416 
00417         case T_TidScanState:
00418             result = ExecTidScan((TidScanState *) node);
00419             break;
00420 
00421         case T_SubqueryScanState:
00422             result = ExecSubqueryScan((SubqueryScanState *) node);
00423             break;
00424 
00425         case T_FunctionScanState:
00426             result = ExecFunctionScan((FunctionScanState *) node);
00427             break;
00428 
00429         case T_ValuesScanState:
00430             result = ExecValuesScan((ValuesScanState *) node);
00431             break;
00432 
00433         case T_CteScanState:
00434             result = ExecCteScan((CteScanState *) node);
00435             break;
00436 
00437         case T_WorkTableScanState:
00438             result = ExecWorkTableScan((WorkTableScanState *) node);
00439             break;
00440 
00441         case T_ForeignScanState:
00442             result = ExecForeignScan((ForeignScanState *) node);
00443             break;
00444 
00445             /*
00446              * join nodes
00447              */
00448         case T_NestLoopState:
00449             result = ExecNestLoop((NestLoopState *) node);
00450             break;
00451 
00452         case T_MergeJoinState:
00453             result = ExecMergeJoin((MergeJoinState *) node);
00454             break;
00455 
00456         case T_HashJoinState:
00457             result = ExecHashJoin((HashJoinState *) node);
00458             break;
00459 
00460             /*
00461              * materialization nodes
00462              */
00463         case T_MaterialState:
00464             result = ExecMaterial((MaterialState *) node);
00465             break;
00466 
00467         case T_SortState:
00468             result = ExecSort((SortState *) node);
00469             break;
00470 
00471         case T_GroupState:
00472             result = ExecGroup((GroupState *) node);
00473             break;
00474 
00475         case T_AggState:
00476             result = ExecAgg((AggState *) node);
00477             break;
00478 
00479         case T_WindowAggState:
00480             result = ExecWindowAgg((WindowAggState *) node);
00481             break;
00482 
00483         case T_UniqueState:
00484             result = ExecUnique((UniqueState *) node);
00485             break;
00486 
00487         case T_HashState:
00488             result = ExecHash((HashState *) node);
00489             break;
00490 
00491         case T_SetOpState:
00492             result = ExecSetOp((SetOpState *) node);
00493             break;
00494 
00495         case T_LockRowsState:
00496             result = ExecLockRows((LockRowsState *) node);
00497             break;
00498 
00499         case T_LimitState:
00500             result = ExecLimit((LimitState *) node);
00501             break;
00502 
00503         default:
00504             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
00505             result = NULL;
00506             break;
00507     }
00508 
00509     if (node->instrument)
00510         InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
00511 
00512     return result;
00513 }
00514 
00515 
00516 /* ----------------------------------------------------------------
00517  *      MultiExecProcNode
00518  *
00519  *      Execute a node that doesn't return individual tuples
00520  *      (it might return a hashtable, bitmap, etc).  Caller should
00521  *      check it got back the expected kind of Node.
00522  *
00523  * This has essentially the same responsibilities as ExecProcNode,
00524  * but it does not do InstrStartNode/InstrStopNode (mainly because
00525  * it can't tell how many returned tuples to count).  Each per-node
00526  * function must provide its own instrumentation support.
00527  * ----------------------------------------------------------------
00528  */
00529 Node *
00530 MultiExecProcNode(PlanState *node)
00531 {
00532     Node       *result;
00533 
00534     CHECK_FOR_INTERRUPTS();
00535 
00536     if (node->chgParam != NULL) /* something changed */
00537         ExecReScan(node);       /* let ReScan handle this */
00538 
00539     switch (nodeTag(node))
00540     {
00541             /*
00542              * Only node types that actually support multiexec will be listed
00543              */
00544 
00545         case T_HashState:
00546             result = MultiExecHash((HashState *) node);
00547             break;
00548 
00549         case T_BitmapIndexScanState:
00550             result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
00551             break;
00552 
00553         case T_BitmapAndState:
00554             result = MultiExecBitmapAnd((BitmapAndState *) node);
00555             break;
00556 
00557         case T_BitmapOrState:
00558             result = MultiExecBitmapOr((BitmapOrState *) node);
00559             break;
00560 
00561         default:
00562             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
00563             result = NULL;
00564             break;
00565     }
00566 
00567     return result;
00568 }
00569 
00570 
00571 /* ----------------------------------------------------------------
00572  *      ExecEndNode
00573  *
00574  *      Recursively cleans up all the nodes in the plan rooted
00575  *      at 'node'.
00576  *
00577  *      After this operation, the query plan will not be able to be
00578  *      processed any further.  This should be called only after
00579  *      the query plan has been fully executed.
00580  * ----------------------------------------------------------------
00581  */
00582 void
00583 ExecEndNode(PlanState *node)
00584 {
00585     /*
00586      * do nothing when we get to the end of a leaf on tree.
00587      */
00588     if (node == NULL)
00589         return;
00590 
00591     if (node->chgParam != NULL)
00592     {
00593         bms_free(node->chgParam);
00594         node->chgParam = NULL;
00595     }
00596 
00597     switch (nodeTag(node))
00598     {
00599             /*
00600              * control nodes
00601              */
00602         case T_ResultState:
00603             ExecEndResult((ResultState *) node);
00604             break;
00605 
00606         case T_ModifyTableState:
00607             ExecEndModifyTable((ModifyTableState *) node);
00608             break;
00609 
00610         case T_AppendState:
00611             ExecEndAppend((AppendState *) node);
00612             break;
00613 
00614         case T_MergeAppendState:
00615             ExecEndMergeAppend((MergeAppendState *) node);
00616             break;
00617 
00618         case T_RecursiveUnionState:
00619             ExecEndRecursiveUnion((RecursiveUnionState *) node);
00620             break;
00621 
00622         case T_BitmapAndState:
00623             ExecEndBitmapAnd((BitmapAndState *) node);
00624             break;
00625 
00626         case T_BitmapOrState:
00627             ExecEndBitmapOr((BitmapOrState *) node);
00628             break;
00629 
00630             /*
00631              * scan nodes
00632              */
00633         case T_SeqScanState:
00634             ExecEndSeqScan((SeqScanState *) node);
00635             break;
00636 
00637         case T_IndexScanState:
00638             ExecEndIndexScan((IndexScanState *) node);
00639             break;
00640 
00641         case T_IndexOnlyScanState:
00642             ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
00643             break;
00644 
00645         case T_BitmapIndexScanState:
00646             ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
00647             break;
00648 
00649         case T_BitmapHeapScanState:
00650             ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
00651             break;
00652 
00653         case T_TidScanState:
00654             ExecEndTidScan((TidScanState *) node);
00655             break;
00656 
00657         case T_SubqueryScanState:
00658             ExecEndSubqueryScan((SubqueryScanState *) node);
00659             break;
00660 
00661         case T_FunctionScanState:
00662             ExecEndFunctionScan((FunctionScanState *) node);
00663             break;
00664 
00665         case T_ValuesScanState:
00666             ExecEndValuesScan((ValuesScanState *) node);
00667             break;
00668 
00669         case T_CteScanState:
00670             ExecEndCteScan((CteScanState *) node);
00671             break;
00672 
00673         case T_WorkTableScanState:
00674             ExecEndWorkTableScan((WorkTableScanState *) node);
00675             break;
00676 
00677         case T_ForeignScanState:
00678             ExecEndForeignScan((ForeignScanState *) node);
00679             break;
00680 
00681             /*
00682              * join nodes
00683              */
00684         case T_NestLoopState:
00685             ExecEndNestLoop((NestLoopState *) node);
00686             break;
00687 
00688         case T_MergeJoinState:
00689             ExecEndMergeJoin((MergeJoinState *) node);
00690             break;
00691 
00692         case T_HashJoinState:
00693             ExecEndHashJoin((HashJoinState *) node);
00694             break;
00695 
00696             /*
00697              * materialization nodes
00698              */
00699         case T_MaterialState:
00700             ExecEndMaterial((MaterialState *) node);
00701             break;
00702 
00703         case T_SortState:
00704             ExecEndSort((SortState *) node);
00705             break;
00706 
00707         case T_GroupState:
00708             ExecEndGroup((GroupState *) node);
00709             break;
00710 
00711         case T_AggState:
00712             ExecEndAgg((AggState *) node);
00713             break;
00714 
00715         case T_WindowAggState:
00716             ExecEndWindowAgg((WindowAggState *) node);
00717             break;
00718 
00719         case T_UniqueState:
00720             ExecEndUnique((UniqueState *) node);
00721             break;
00722 
00723         case T_HashState:
00724             ExecEndHash((HashState *) node);
00725             break;
00726 
00727         case T_SetOpState:
00728             ExecEndSetOp((SetOpState *) node);
00729             break;
00730 
00731         case T_LockRowsState:
00732             ExecEndLockRows((LockRowsState *) node);
00733             break;
00734 
00735         case T_LimitState:
00736             ExecEndLimit((LimitState *) node);
00737             break;
00738 
00739         default:
00740             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
00741             break;
00742     }
00743 }