LCOV - code coverage report
Current view: top level - src/backend/executor - execProcnode.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 254 267 95.1 %
Date: 2019-11-22 07:06:56 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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-2019, 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             :  *   NOTES
      21             :  *      This used to be three files.  It is now all combined into
      22             :  *      one file so that it is easier to keep the dispatch routines
      23             :  *      in sync when new nodes are added.
      24             :  *
      25             :  *   EXAMPLE
      26             :  *      Suppose we want the age of the manager of the shoe department and
      27             :  *      the number of employees in that department.  So we have the query:
      28             :  *
      29             :  *              select DEPT.no_emps, EMP.age
      30             :  *              from DEPT, EMP
      31             :  *              where EMP.name = DEPT.mgr and
      32             :  *                    DEPT.name = "shoe"
      33             :  *
      34             :  *      Suppose the planner gives us the following plan:
      35             :  *
      36             :  *                      Nest Loop (DEPT.mgr = EMP.name)
      37             :  *                      /       \
      38             :  *                     /         \
      39             :  *                 Seq Scan     Seq Scan
      40             :  *                  DEPT          EMP
      41             :  *              (name = "shoe")
      42             :  *
      43             :  *      ExecutorStart() is called first.
      44             :  *      It calls InitPlan() which calls ExecInitNode() on
      45             :  *      the root of the plan -- the nest loop node.
      46             :  *
      47             :  *    * ExecInitNode() notices that it is looking at a nest loop and
      48             :  *      as the code below demonstrates, it calls ExecInitNestLoop().
      49             :  *      Eventually this calls ExecInitNode() on the right and left subplans
      50             :  *      and so forth until the entire plan is initialized.  The result
      51             :  *      of ExecInitNode() is a plan state tree built with the same structure
      52             :  *      as the underlying plan tree.
      53             :  *
      54             :  *    * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
      55             :  *      ExecProcNode() repeatedly on the top node of the plan state tree.
      56             :  *      Each time this happens, ExecProcNode() will end up calling
      57             :  *      ExecNestLoop(), which calls ExecProcNode() on its subplans.
      58             :  *      Each of these subplans is a sequential scan so ExecSeqScan() is
      59             :  *      called.  The slots returned by ExecSeqScan() may contain
      60             :  *      tuples which contain the attributes ExecNestLoop() uses to
      61             :  *      form the tuples it returns.
      62             :  *
      63             :  *    * Eventually ExecSeqScan() stops returning tuples and the nest
      64             :  *      loop join ends.  Lastly, ExecutorEnd() calls ExecEndNode() which
      65             :  *      calls ExecEndNestLoop() which in turn calls ExecEndNode() on
      66             :  *      its subplans which result in ExecEndSeqScan().
      67             :  *
      68             :  *      This should show how the executor works by having
      69             :  *      ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
      70             :  *      their work to the appropriate node support routines which may
      71             :  *      in turn call these routines themselves on their subplans.
      72             :  */
      73             : #include "postgres.h"
      74             : 
      75             : #include "executor/executor.h"
      76             : #include "executor/nodeAgg.h"
      77             : #include "executor/nodeAppend.h"
      78             : #include "executor/nodeBitmapAnd.h"
      79             : #include "executor/nodeBitmapHeapscan.h"
      80             : #include "executor/nodeBitmapIndexscan.h"
      81             : #include "executor/nodeBitmapOr.h"
      82             : #include "executor/nodeCtescan.h"
      83             : #include "executor/nodeCustom.h"
      84             : #include "executor/nodeForeignscan.h"
      85             : #include "executor/nodeFunctionscan.h"
      86             : #include "executor/nodeGather.h"
      87             : #include "executor/nodeGatherMerge.h"
      88             : #include "executor/nodeGroup.h"
      89             : #include "executor/nodeHash.h"
      90             : #include "executor/nodeHashjoin.h"
      91             : #include "executor/nodeIndexonlyscan.h"
      92             : #include "executor/nodeIndexscan.h"
      93             : #include "executor/nodeLimit.h"
      94             : #include "executor/nodeLockRows.h"
      95             : #include "executor/nodeMaterial.h"
      96             : #include "executor/nodeMergeAppend.h"
      97             : #include "executor/nodeMergejoin.h"
      98             : #include "executor/nodeModifyTable.h"
      99             : #include "executor/nodeNamedtuplestorescan.h"
     100             : #include "executor/nodeNestloop.h"
     101             : #include "executor/nodeProjectSet.h"
     102             : #include "executor/nodeRecursiveunion.h"
     103             : #include "executor/nodeResult.h"
     104             : #include "executor/nodeSamplescan.h"
     105             : #include "executor/nodeSeqscan.h"
     106             : #include "executor/nodeSetOp.h"
     107             : #include "executor/nodeSort.h"
     108             : #include "executor/nodeSubplan.h"
     109             : #include "executor/nodeSubqueryscan.h"
     110             : #include "executor/nodeTableFuncscan.h"
     111             : #include "executor/nodeTidscan.h"
     112             : #include "executor/nodeUnique.h"
     113             : #include "executor/nodeValuesscan.h"
     114             : #include "executor/nodeWindowAgg.h"
     115             : #include "executor/nodeWorktablescan.h"
     116             : #include "miscadmin.h"
     117             : #include "nodes/nodeFuncs.h"
     118             : 
     119             : static TupleTableSlot *ExecProcNodeFirst(PlanState *node);
     120             : static TupleTableSlot *ExecProcNodeInstr(PlanState *node);
     121             : 
     122             : 
     123             : /* ------------------------------------------------------------------------
     124             :  *      ExecInitNode
     125             :  *
     126             :  *      Recursively initializes all the nodes in the plan tree rooted
     127             :  *      at 'node'.
     128             :  *
     129             :  *      Inputs:
     130             :  *        'node' is the current node of the plan produced by the query planner
     131             :  *        'estate' is the shared execution state for the plan tree
     132             :  *        'eflags' is a bitwise OR of flag bits described in executor.h
     133             :  *
     134             :  *      Returns a PlanState node corresponding to the given Plan node.
     135             :  * ------------------------------------------------------------------------
     136             :  */
     137             : PlanState *
     138      947622 : ExecInitNode(Plan *node, EState *estate, int eflags)
     139             : {
     140             :     PlanState  *result;
     141             :     List       *subps;
     142             :     ListCell   *l;
     143             : 
     144             :     /*
     145             :      * do nothing when we get to the end of a leaf on tree.
     146             :      */
     147      947622 :     if (node == NULL)
     148      204722 :         return NULL;
     149             : 
     150             :     /*
     151             :      * Make sure there's enough stack available. Need to check here, in
     152             :      * addition to ExecProcNode() (via ExecProcNodeFirst()), to ensure the
     153             :      * stack isn't overrun while initializing the node tree.
     154             :      */
     155      742900 :     check_stack_depth();
     156             : 
     157      742900 :     switch (nodeTag(node))
     158             :     {
     159             :             /*
     160             :              * control nodes
     161             :              */
     162             :         case T_Result:
     163      208020 :             result = (PlanState *) ExecInitResult((Result *) node,
     164             :                                                   estate, eflags);
     165      207990 :             break;
     166             : 
     167             :         case T_ProjectSet:
     168        4008 :             result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
     169             :                                                       estate, eflags);
     170        4006 :             break;
     171             : 
     172             :         case T_ModifyTable:
     173       73128 :             result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
     174             :                                                        estate, eflags);
     175       73118 :             break;
     176             : 
     177             :         case T_Append:
     178        5848 :             result = (PlanState *) ExecInitAppend((Append *) node,
     179             :                                                   estate, eflags);
     180        5848 :             break;
     181             : 
     182             :         case T_MergeAppend:
     183         248 :             result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
     184             :                                                        estate, eflags);
     185         248 :             break;
     186             : 
     187             :         case T_RecursiveUnion:
     188         320 :             result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
     189             :                                                           estate, eflags);
     190         320 :             break;
     191             : 
     192             :         case T_BitmapAnd:
     193          44 :             result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
     194             :                                                      estate, eflags);
     195          44 :             break;
     196             : 
     197             :         case T_BitmapOr:
     198         104 :             result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
     199             :                                                     estate, eflags);
     200         104 :             break;
     201             : 
     202             :             /*
     203             :              * scan nodes
     204             :              */
     205             :         case T_SeqScan:
     206      121600 :             result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
     207             :                                                    estate, eflags);
     208      121592 :             break;
     209             : 
     210             :         case T_SampleScan:
     211         180 :             result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
     212             :                                                       estate, eflags);
     213         180 :             break;
     214             : 
     215             :         case T_IndexScan:
     216       77728 :             result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
     217             :                                                      estate, eflags);
     218       77728 :             break;
     219             : 
     220             :         case T_IndexOnlyScan:
     221        8606 :             result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
     222             :                                                          estate, eflags);
     223        8606 :             break;
     224             : 
     225             :         case T_BitmapIndexScan:
     226       19664 :             result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
     227             :                                                            estate, eflags);
     228       19664 :             break;
     229             : 
     230             :         case T_BitmapHeapScan:
     231       19496 :             result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
     232             :                                                           estate, eflags);
     233       19496 :             break;
     234             : 
     235             :         case T_TidScan:
     236         450 :             result = (PlanState *) ExecInitTidScan((TidScan *) node,
     237             :                                                    estate, eflags);
     238         450 :             break;
     239             : 
     240             :         case T_SubqueryScan:
     241        2156 :             result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
     242             :                                                         estate, eflags);
     243        2156 :             break;
     244             : 
     245             :         case T_FunctionScan:
     246       31494 :             result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
     247             :                                                         estate, eflags);
     248       31494 :             break;
     249             : 
     250             :         case T_TableFuncScan:
     251         144 :             result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
     252             :                                                          estate, eflags);
     253         144 :             break;
     254             : 
     255             :         case T_ValuesScan:
     256        4310 :             result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
     257             :                                                       estate, eflags);
     258        4310 :             break;
     259             : 
     260             :         case T_CteScan:
     261         888 :             result = (PlanState *) ExecInitCteScan((CteScan *) node,
     262             :                                                    estate, eflags);
     263         888 :             break;
     264             : 
     265             :         case T_NamedTuplestoreScan:
     266         384 :             result = (PlanState *) ExecInitNamedTuplestoreScan((NamedTuplestoreScan *) node,
     267             :                                                                estate, eflags);
     268         384 :             break;
     269             : 
     270             :         case T_WorkTableScan:
     271         320 :             result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
     272             :                                                          estate, eflags);
     273         320 :             break;
     274             : 
     275             :         case T_ForeignScan:
     276        1360 :             result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
     277             :                                                        estate, eflags);
     278        1354 :             break;
     279             : 
     280             :         case T_CustomScan:
     281           0 :             result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
     282             :                                                       estate, eflags);
     283           0 :             break;
     284             : 
     285             :             /*
     286             :              * join nodes
     287             :              */
     288             :         case T_NestLoop:
     289       31680 :             result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
     290             :                                                     estate, eflags);
     291       31680 :             break;
     292             : 
     293             :         case T_MergeJoin:
     294        2120 :             result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
     295             :                                                      estate, eflags);
     296        2120 :             break;
     297             : 
     298             :         case T_HashJoin:
     299       26972 :             result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
     300             :                                                     estate, eflags);
     301       26972 :             break;
     302             : 
     303             :             /*
     304             :              * materialization nodes
     305             :              */
     306             :         case T_Material:
     307        2754 :             result = (PlanState *) ExecInitMaterial((Material *) node,
     308             :                                                     estate, eflags);
     309        2754 :             break;
     310             : 
     311             :         case T_Sort:
     312       32558 :             result = (PlanState *) ExecInitSort((Sort *) node,
     313             :                                                 estate, eflags);
     314       32554 :             break;
     315             : 
     316             :         case T_Group:
     317         138 :             result = (PlanState *) ExecInitGroup((Group *) node,
     318             :                                                  estate, eflags);
     319         138 :             break;
     320             : 
     321             :         case T_Agg:
     322       28720 :             result = (PlanState *) ExecInitAgg((Agg *) node,
     323             :                                                estate, eflags);
     324       28716 :             break;
     325             : 
     326             :         case T_WindowAgg:
     327        1160 :             result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
     328             :                                                      estate, eflags);
     329        1160 :             break;
     330             : 
     331             :         case T_Unique:
     332         326 :             result = (PlanState *) ExecInitUnique((Unique *) node,
     333             :                                                   estate, eflags);
     334         326 :             break;
     335             : 
     336             :         case T_Gather:
     337         518 :             result = (PlanState *) ExecInitGather((Gather *) node,
     338             :                                                   estate, eflags);
     339         518 :             break;
     340             : 
     341             :         case T_GatherMerge:
     342         136 :             result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
     343             :                                                        estate, eflags);
     344         136 :             break;
     345             : 
     346             :         case T_Hash:
     347       26972 :             result = (PlanState *) ExecInitHash((Hash *) node,
     348             :                                                 estate, eflags);
     349       26972 :             break;
     350             : 
     351             :         case T_SetOp:
     352         208 :             result = (PlanState *) ExecInitSetOp((SetOp *) node,
     353             :                                                  estate, eflags);
     354         208 :             break;
     355             : 
     356             :         case T_LockRows:
     357        5058 :             result = (PlanState *) ExecInitLockRows((LockRows *) node,
     358             :                                                     estate, eflags);
     359        5058 :             break;
     360             : 
     361             :         case T_Limit:
     362        3080 :             result = (PlanState *) ExecInitLimit((Limit *) node,
     363             :                                                  estate, eflags);
     364        3080 :             break;
     365             : 
     366             :         default:
     367           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     368             :             result = NULL;      /* keep compiler quiet */
     369             :             break;
     370             :     }
     371             : 
     372      742836 :     ExecSetExecProcNode(result, result->ExecProcNode);
     373             : 
     374             :     /*
     375             :      * Initialize any initPlans present in this node.  The planner put them in
     376             :      * a separate list for us.
     377             :      */
     378      742836 :     subps = NIL;
     379      755616 :     foreach(l, node->initPlan)
     380             :     {
     381       12780 :         SubPlan    *subplan = (SubPlan *) lfirst(l);
     382             :         SubPlanState *sstate;
     383             : 
     384             :         Assert(IsA(subplan, SubPlan));
     385       12780 :         sstate = ExecInitSubPlan(subplan, result);
     386       12780 :         subps = lappend(subps, sstate);
     387             :     }
     388      742836 :     result->initPlan = subps;
     389             : 
     390             :     /* Set up instrumentation for this node if requested */
     391      742836 :     if (estate->es_instrument)
     392        4012 :         result->instrument = InstrAlloc(1, estate->es_instrument);
     393             : 
     394      742836 :     return result;
     395             : }
     396             : 
     397             : 
     398             : /*
     399             :  * If a node wants to change its ExecProcNode function after ExecInitNode()
     400             :  * has finished, it should do so with this function.  That way any wrapper
     401             :  * functions can be reinstalled, without the node having to know how that
     402             :  * works.
     403             :  */
     404             : void
     405      743080 : ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function)
     406             : {
     407             :     /*
     408             :      * Add a wrapper around the ExecProcNode callback that checks stack depth
     409             :      * during the first execution and maybe adds an instrumentation wrapper.
     410             :      * When the callback is changed after execution has already begun that
     411             :      * means we'll superfluously execute ExecProcNodeFirst, but that seems ok.
     412             :      */
     413      743080 :     node->ExecProcNodeReal = function;
     414      743080 :     node->ExecProcNode = ExecProcNodeFirst;
     415      743080 : }
     416             : 
     417             : 
     418             : /*
     419             :  * ExecProcNode wrapper that performs some one-time checks, before calling
     420             :  * the relevant node method (possibly via an instrumentation wrapper).
     421             :  */
     422             : static TupleTableSlot *
     423      629312 : ExecProcNodeFirst(PlanState *node)
     424             : {
     425             :     /*
     426             :      * Perform stack depth check during the first execution of the node.  We
     427             :      * only do so the first time round because it turns out to not be cheap on
     428             :      * some common architectures (eg. x86).  This relies on the assumption
     429             :      * that ExecProcNode calls for a given plan node will always be made at
     430             :      * roughly the same stack depth.
     431             :      */
     432      629312 :     check_stack_depth();
     433             : 
     434             :     /*
     435             :      * If instrumentation is required, change the wrapper to one that just
     436             :      * does instrumentation.  Otherwise we can dispense with all wrappers and
     437             :      * have ExecProcNode() directly call the relevant function from now on.
     438             :      */
     439      629312 :     if (node->instrument)
     440        2768 :         node->ExecProcNode = ExecProcNodeInstr;
     441             :     else
     442      626544 :         node->ExecProcNode = node->ExecProcNodeReal;
     443             : 
     444      629312 :     return node->ExecProcNode(node);
     445             : }
     446             : 
     447             : 
     448             : /*
     449             :  * ExecProcNode wrapper that performs instrumentation calls.  By keeping
     450             :  * this a separate function, we avoid overhead in the normal case where
     451             :  * no instrumentation is wanted.
     452             :  */
     453             : static TupleTableSlot *
     454     8062808 : ExecProcNodeInstr(PlanState *node)
     455             : {
     456             :     TupleTableSlot *result;
     457             : 
     458     8062808 :     InstrStartNode(node->instrument);
     459             : 
     460     8062808 :     result = node->ExecProcNodeReal(node);
     461             : 
     462     8062800 :     InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
     463             : 
     464     8062800 :     return result;
     465             : }
     466             : 
     467             : 
     468             : /* ----------------------------------------------------------------
     469             :  *      MultiExecProcNode
     470             :  *
     471             :  *      Execute a node that doesn't return individual tuples
     472             :  *      (it might return a hashtable, bitmap, etc).  Caller should
     473             :  *      check it got back the expected kind of Node.
     474             :  *
     475             :  * This has essentially the same responsibilities as ExecProcNode,
     476             :  * but it does not do InstrStartNode/InstrStopNode (mainly because
     477             :  * it can't tell how many returned tuples to count).  Each per-node
     478             :  * function must provide its own instrumentation support.
     479             :  * ----------------------------------------------------------------
     480             :  */
     481             : Node *
     482      269428 : MultiExecProcNode(PlanState *node)
     483             : {
     484             :     Node       *result;
     485             : 
     486      269428 :     check_stack_depth();
     487             : 
     488      269428 :     CHECK_FOR_INTERRUPTS();
     489             : 
     490      269428 :     if (node->chgParam != NULL) /* something changed */
     491      243588 :         ExecReScan(node);       /* let ReScan handle this */
     492             : 
     493      269428 :     switch (nodeTag(node))
     494             :     {
     495             :             /*
     496             :              * Only node types that actually support multiexec will be listed
     497             :              */
     498             : 
     499             :         case T_HashState:
     500      255290 :             result = MultiExecHash((HashState *) node);
     501      255290 :             break;
     502             : 
     503             :         case T_BitmapIndexScanState:
     504       14034 :             result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
     505       14034 :             break;
     506             : 
     507             :         case T_BitmapAndState:
     508          40 :             result = MultiExecBitmapAnd((BitmapAndState *) node);
     509          40 :             break;
     510             : 
     511             :         case T_BitmapOrState:
     512          64 :             result = MultiExecBitmapOr((BitmapOrState *) node);
     513          64 :             break;
     514             : 
     515             :         default:
     516           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     517             :             result = NULL;
     518             :             break;
     519             :     }
     520             : 
     521      269428 :     return result;
     522             : }
     523             : 
     524             : 
     525             : /* ----------------------------------------------------------------
     526             :  *      ExecEndNode
     527             :  *
     528             :  *      Recursively cleans up all the nodes in the plan rooted
     529             :  *      at 'node'.
     530             :  *
     531             :  *      After this operation, the query plan will not be able to be
     532             :  *      processed any further.  This should be called only after
     533             :  *      the query plan has been fully executed.
     534             :  * ----------------------------------------------------------------
     535             :  */
     536             : void
     537      920918 : ExecEndNode(PlanState *node)
     538             : {
     539             :     /*
     540             :      * do nothing when we get to the end of a leaf on tree.
     541             :      */
     542      920918 :     if (node == NULL)
     543      194326 :         return;
     544             : 
     545             :     /*
     546             :      * Make sure there's enough stack available. Need to check here, in
     547             :      * addition to ExecProcNode() (via ExecProcNodeFirst()), because it's not
     548             :      * guaranteed that ExecProcNode() is reached for all nodes.
     549             :      */
     550      726592 :     check_stack_depth();
     551             : 
     552      726592 :     if (node->chgParam != NULL)
     553             :     {
     554        3852 :         bms_free(node->chgParam);
     555        3852 :         node->chgParam = NULL;
     556             :     }
     557             : 
     558      726592 :     switch (nodeTag(node))
     559             :     {
     560             :             /*
     561             :              * control nodes
     562             :              */
     563             :         case T_ResultState:
     564      197624 :             ExecEndResult((ResultState *) node);
     565      197624 :             break;
     566             : 
     567             :         case T_ProjectSetState:
     568        3646 :             ExecEndProjectSet((ProjectSetState *) node);
     569        3646 :             break;
     570             : 
     571             :         case T_ModifyTableState:
     572       70872 :             ExecEndModifyTable((ModifyTableState *) node);
     573       70872 :             break;
     574             : 
     575             :         case T_AppendState:
     576        5794 :             ExecEndAppend((AppendState *) node);
     577        5794 :             break;
     578             : 
     579             :         case T_MergeAppendState:
     580         248 :             ExecEndMergeAppend((MergeAppendState *) node);
     581         248 :             break;
     582             : 
     583             :         case T_RecursiveUnionState:
     584         320 :             ExecEndRecursiveUnion((RecursiveUnionState *) node);
     585         320 :             break;
     586             : 
     587             :         case T_BitmapAndState:
     588          44 :             ExecEndBitmapAnd((BitmapAndState *) node);
     589          44 :             break;
     590             : 
     591             :         case T_BitmapOrState:
     592         104 :             ExecEndBitmapOr((BitmapOrState *) node);
     593         104 :             break;
     594             : 
     595             :             /*
     596             :              * scan nodes
     597             :              */
     598             :         case T_SeqScanState:
     599      120290 :             ExecEndSeqScan((SeqScanState *) node);
     600      120290 :             break;
     601             : 
     602             :         case T_SampleScanState:
     603         152 :             ExecEndSampleScan((SampleScanState *) node);
     604         152 :             break;
     605             : 
     606             :         case T_GatherState:
     607         514 :             ExecEndGather((GatherState *) node);
     608         514 :             break;
     609             : 
     610             :         case T_GatherMergeState:
     611         136 :             ExecEndGatherMerge((GatherMergeState *) node);
     612         136 :             break;
     613             : 
     614             :         case T_IndexScanState:
     615       77388 :             ExecEndIndexScan((IndexScanState *) node);
     616       77388 :             break;
     617             : 
     618             :         case T_IndexOnlyScanState:
     619        8536 :             ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
     620        8536 :             break;
     621             : 
     622             :         case T_BitmapIndexScanState:
     623       19622 :             ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
     624       19622 :             break;
     625             : 
     626             :         case T_BitmapHeapScanState:
     627       19454 :             ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
     628       19454 :             break;
     629             : 
     630             :         case T_TidScanState:
     631         372 :             ExecEndTidScan((TidScanState *) node);
     632         372 :             break;
     633             : 
     634             :         case T_SubqueryScanState:
     635        2156 :             ExecEndSubqueryScan((SubqueryScanState *) node);
     636        2156 :             break;
     637             : 
     638             :         case T_FunctionScanState:
     639       30774 :             ExecEndFunctionScan((FunctionScanState *) node);
     640       30774 :             break;
     641             : 
     642             :         case T_TableFuncScanState:
     643         132 :             ExecEndTableFuncScan((TableFuncScanState *) node);
     644         132 :             break;
     645             : 
     646             :         case T_ValuesScanState:
     647        4246 :             ExecEndValuesScan((ValuesScanState *) node);
     648        4246 :             break;
     649             : 
     650             :         case T_CteScanState:
     651         866 :             ExecEndCteScan((CteScanState *) node);
     652         866 :             break;
     653             : 
     654             :         case T_NamedTuplestoreScanState:
     655         384 :             ExecEndNamedTuplestoreScan((NamedTuplestoreScanState *) node);
     656         384 :             break;
     657             : 
     658             :         case T_WorkTableScanState:
     659         320 :             ExecEndWorkTableScan((WorkTableScanState *) node);
     660         320 :             break;
     661             : 
     662             :         case T_ForeignScanState:
     663        1322 :             ExecEndForeignScan((ForeignScanState *) node);
     664        1322 :             break;
     665             : 
     666             :         case T_CustomScanState:
     667           0 :             ExecEndCustomScan((CustomScanState *) node);
     668           0 :             break;
     669             : 
     670             :             /*
     671             :              * join nodes
     672             :              */
     673             :         case T_NestLoopState:
     674       31598 :             ExecEndNestLoop((NestLoopState *) node);
     675       31598 :             break;
     676             : 
     677             :         case T_MergeJoinState:
     678        2116 :             ExecEndMergeJoin((MergeJoinState *) node);
     679        2116 :             break;
     680             : 
     681             :         case T_HashJoinState:
     682       26936 :             ExecEndHashJoin((HashJoinState *) node);
     683       26936 :             break;
     684             : 
     685             :             /*
     686             :              * materialization nodes
     687             :              */
     688             :         case T_MaterialState:
     689        2690 :             ExecEndMaterial((MaterialState *) node);
     690        2690 :             break;
     691             : 
     692             :         case T_SortState:
     693       32508 :             ExecEndSort((SortState *) node);
     694       32508 :             break;
     695             : 
     696             :         case T_GroupState:
     697         138 :             ExecEndGroup((GroupState *) node);
     698         138 :             break;
     699             : 
     700             :         case T_AggState:
     701       28668 :             ExecEndAgg((AggState *) node);
     702       28668 :             break;
     703             : 
     704             :         case T_WindowAggState:
     705        1128 :             ExecEndWindowAgg((WindowAggState *) node);
     706        1128 :             break;
     707             : 
     708             :         case T_UniqueState:
     709         326 :             ExecEndUnique((UniqueState *) node);
     710         326 :             break;
     711             : 
     712             :         case T_HashState:
     713       26936 :             ExecEndHash((HashState *) node);
     714       26936 :             break;
     715             : 
     716             :         case T_SetOpState:
     717         208 :             ExecEndSetOp((SetOpState *) node);
     718         208 :             break;
     719             : 
     720             :         case T_LockRowsState:
     721        4982 :             ExecEndLockRows((LockRowsState *) node);
     722        4982 :             break;
     723             : 
     724             :         case T_LimitState:
     725        3042 :             ExecEndLimit((LimitState *) node);
     726        3042 :             break;
     727             : 
     728             :         default:
     729           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     730             :             break;
     731             :     }
     732             : }
     733             : 
     734             : /*
     735             :  * ExecShutdownNode
     736             :  *
     737             :  * Give execution nodes a chance to stop asynchronous resource consumption
     738             :  * and release any resources still held.
     739             :  */
     740             : bool
     741      703606 : ExecShutdownNode(PlanState *node)
     742             : {
     743      703606 :     if (node == NULL)
     744           0 :         return false;
     745             : 
     746      703606 :     check_stack_depth();
     747             : 
     748      703606 :     planstate_tree_walker(node, ExecShutdownNode, NULL);
     749             : 
     750             :     /*
     751             :      * Treat the node as running while we shut it down, but only if it's run
     752             :      * at least once already.  We don't expect much CPU consumption during
     753             :      * node shutdown, but in the case of Gather or Gather Merge, we may shut
     754             :      * down workers at this stage.  If so, their buffer usage will get
     755             :      * propagated into pgBufferUsage at this point, and we want to make sure
     756             :      * that it gets associated with the Gather node.  We skip this if the node
     757             :      * has never been executed, so as to avoid incorrectly making it appear
     758             :      * that it has.
     759             :      */
     760      703606 :     if (node->instrument && node->instrument->running)
     761        3076 :         InstrStartNode(node->instrument);
     762             : 
     763      703606 :     switch (nodeTag(node))
     764             :     {
     765             :         case T_GatherState:
     766         302 :             ExecShutdownGather((GatherState *) node);
     767         302 :             break;
     768             :         case T_ForeignScanState:
     769         790 :             ExecShutdownForeignScan((ForeignScanState *) node);
     770         790 :             break;
     771             :         case T_CustomScanState:
     772           0 :             ExecShutdownCustomScan((CustomScanState *) node);
     773           0 :             break;
     774             :         case T_GatherMergeState:
     775          80 :             ExecShutdownGatherMerge((GatherMergeState *) node);
     776          80 :             break;
     777             :         case T_HashState:
     778       25954 :             ExecShutdownHash((HashState *) node);
     779       25954 :             break;
     780             :         case T_HashJoinState:
     781       25954 :             ExecShutdownHashJoin((HashJoinState *) node);
     782       25954 :             break;
     783             :         default:
     784      650526 :             break;
     785             :     }
     786             : 
     787             :     /* Stop the node if we started it above, reporting 0 tuples. */
     788      703606 :     if (node->instrument && node->instrument->running)
     789        3076 :         InstrStopNode(node->instrument, 0);
     790             : 
     791      703606 :     return false;
     792             : }
     793             : 
     794             : /*
     795             :  * ExecSetTupleBound
     796             :  *
     797             :  * Set a tuple bound for a planstate node.  This lets child plan nodes
     798             :  * optimize based on the knowledge that the maximum number of tuples that
     799             :  * their parent will demand is limited.  The tuple bound for a node may
     800             :  * only be changed between scans (i.e., after node initialization or just
     801             :  * before an ExecReScan call).
     802             :  *
     803             :  * Any negative tuples_needed value means "no limit", which should be the
     804             :  * default assumption when this is not called at all for a particular node.
     805             :  *
     806             :  * Note: if this is called repeatedly on a plan tree, the exact same set
     807             :  * of nodes must be updated with the new limit each time; be careful that
     808             :  * only unchanging conditions are tested here.
     809             :  */
     810             : void
     811        4806 : ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
     812             : {
     813             :     /*
     814             :      * Since this function recurses, in principle we should check stack depth
     815             :      * here.  In practice, it's probably pointless since the earlier node
     816             :      * initialization tree traversal would surely have consumed more stack.
     817             :      */
     818             : 
     819        4806 :     if (IsA(child_node, SortState))
     820             :     {
     821             :         /*
     822             :          * If it is a Sort node, notify it that it can use bounded sort.
     823             :          *
     824             :          * Note: it is the responsibility of nodeSort.c to react properly to
     825             :          * changes of these parameters.  If we ever redesign this, it'd be a
     826             :          * good idea to integrate this signaling with the parameter-change
     827             :          * mechanism.
     828             :          */
     829        1066 :         SortState  *sortState = (SortState *) child_node;
     830             : 
     831        1066 :         if (tuples_needed < 0)
     832             :         {
     833             :             /* make sure flag gets reset if needed upon rescan */
     834         146 :             sortState->bounded = false;
     835             :         }
     836             :         else
     837             :         {
     838         920 :             sortState->bounded = true;
     839         920 :             sortState->bound = tuples_needed;
     840             :         }
     841             :     }
     842        3740 :     else if (IsA(child_node, AppendState))
     843             :     {
     844             :         /*
     845             :          * If it is an Append, we can apply the bound to any nodes that are
     846             :          * children of the Append, since the Append surely need read no more
     847             :          * than that many tuples from any one input.
     848             :          */
     849          52 :         AppendState *aState = (AppendState *) child_node;
     850             :         int         i;
     851             : 
     852         172 :         for (i = 0; i < aState->as_nplans; i++)
     853         120 :             ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
     854             :     }
     855        3688 :     else if (IsA(child_node, MergeAppendState))
     856             :     {
     857             :         /*
     858             :          * If it is a MergeAppend, we can apply the bound to any nodes that
     859             :          * are children of the MergeAppend, since the MergeAppend surely need
     860             :          * read no more than that many tuples from any one input.
     861             :          */
     862          36 :         MergeAppendState *maState = (MergeAppendState *) child_node;
     863             :         int         i;
     864             : 
     865         156 :         for (i = 0; i < maState->ms_nplans; i++)
     866         120 :             ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
     867             :     }
     868        3652 :     else if (IsA(child_node, ResultState))
     869             :     {
     870             :         /*
     871             :          * Similarly, for a projecting Result, we can apply the bound to its
     872             :          * child node.
     873             :          *
     874             :          * If Result supported qual checking, we'd have to punt on seeing a
     875             :          * qual.  Note that having a resconstantqual is not a showstopper: if
     876             :          * that condition succeeds it affects nothing, while if it fails, no
     877             :          * rows will be demanded from the Result child anyway.
     878             :          */
     879         254 :         if (outerPlanState(child_node))
     880          76 :             ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     881             :     }
     882        3398 :     else if (IsA(child_node, SubqueryScanState))
     883             :     {
     884             :         /*
     885             :          * We can also descend through SubqueryScan, but only if it has no
     886             :          * qual (otherwise it might discard rows).
     887             :          */
     888           8 :         SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
     889             : 
     890           8 :         if (subqueryState->ss.ps.qual == NULL)
     891           8 :             ExecSetTupleBound(tuples_needed, subqueryState->subplan);
     892             :     }
     893        3390 :     else if (IsA(child_node, GatherState))
     894             :     {
     895             :         /*
     896             :          * A Gather node can propagate the bound to its workers.  As with
     897             :          * MergeAppend, no one worker could possibly need to return more
     898             :          * tuples than the Gather itself needs to.
     899             :          *
     900             :          * Note: As with Sort, the Gather node is responsible for reacting
     901             :          * properly to changes to this parameter.
     902             :          */
     903           0 :         GatherState *gstate = (GatherState *) child_node;
     904             : 
     905           0 :         gstate->tuples_needed = tuples_needed;
     906             : 
     907             :         /* Also pass down the bound to our own copy of the child plan */
     908           0 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     909             :     }
     910        3390 :     else if (IsA(child_node, GatherMergeState))
     911             :     {
     912             :         /* Same comments as for Gather */
     913          12 :         GatherMergeState *gstate = (GatherMergeState *) child_node;
     914             : 
     915          12 :         gstate->tuples_needed = tuples_needed;
     916             : 
     917          12 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     918             :     }
     919             : 
     920             :     /*
     921             :      * In principle we could descend through any plan node type that is
     922             :      * certain not to discard or combine input rows; but on seeing a node that
     923             :      * can do that, we can't propagate the bound any further.  For the moment
     924             :      * it's unclear that any other cases are worth checking here.
     925             :      */
     926        4806 : }

Generated by: LCOV version 1.13