LCOV - code coverage report
Current view: top level - src/backend/executor - execProcnode.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 364 384 94.8 %
Date: 2021-12-03 03:09:03 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-2021, 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/nodeIncrementalSort.h"
      92             : #include "executor/nodeIndexonlyscan.h"
      93             : #include "executor/nodeIndexscan.h"
      94             : #include "executor/nodeLimit.h"
      95             : #include "executor/nodeLockRows.h"
      96             : #include "executor/nodeMaterial.h"
      97             : #include "executor/nodeMemoize.h"
      98             : #include "executor/nodeMergeAppend.h"
      99             : #include "executor/nodeMergejoin.h"
     100             : #include "executor/nodeModifyTable.h"
     101             : #include "executor/nodeNamedtuplestorescan.h"
     102             : #include "executor/nodeNestloop.h"
     103             : #include "executor/nodeProjectSet.h"
     104             : #include "executor/nodeRecursiveunion.h"
     105             : #include "executor/nodeResult.h"
     106             : #include "executor/nodeSamplescan.h"
     107             : #include "executor/nodeSeqscan.h"
     108             : #include "executor/nodeSetOp.h"
     109             : #include "executor/nodeSort.h"
     110             : #include "executor/nodeSubplan.h"
     111             : #include "executor/nodeSubqueryscan.h"
     112             : #include "executor/nodeTableFuncscan.h"
     113             : #include "executor/nodeTidrangescan.h"
     114             : #include "executor/nodeTidscan.h"
     115             : #include "executor/nodeUnique.h"
     116             : #include "executor/nodeValuesscan.h"
     117             : #include "executor/nodeWindowAgg.h"
     118             : #include "executor/nodeWorktablescan.h"
     119             : #include "miscadmin.h"
     120             : #include "nodes/nodeFuncs.h"
     121             : 
     122             : static TupleTableSlot *ExecProcNodeFirst(PlanState *node);
     123             : static TupleTableSlot *ExecProcNodeInstr(PlanState *node);
     124             : 
     125             : 
     126             : /* ------------------------------------------------------------------------
     127             :  *      ExecInitNode
     128             :  *
     129             :  *      Recursively initializes all the nodes in the plan tree rooted
     130             :  *      at 'node'.
     131             :  *
     132             :  *      Inputs:
     133             :  *        'node' is the current node of the plan produced by the query planner
     134             :  *        'estate' is the shared execution state for the plan tree
     135             :  *        'eflags' is a bitwise OR of flag bits described in executor.h
     136             :  *
     137             :  *      Returns a PlanState node corresponding to the given Plan node.
     138             :  * ------------------------------------------------------------------------
     139             :  */
     140             : PlanState *
     141     1447306 : ExecInitNode(Plan *node, EState *estate, int eflags)
     142             : {
     143             :     PlanState  *result;
     144             :     List       *subps;
     145             :     ListCell   *l;
     146             : 
     147             :     /*
     148             :      * do nothing when we get to the end of a leaf on tree.
     149             :      */
     150     1447306 :     if (node == NULL)
     151      236648 :         return NULL;
     152             : 
     153             :     /*
     154             :      * Make sure there's enough stack available. Need to check here, in
     155             :      * addition to ExecProcNode() (via ExecProcNodeFirst()), to ensure the
     156             :      * stack isn't overrun while initializing the node tree.
     157             :      */
     158     1210658 :     check_stack_depth();
     159             : 
     160     1210658 :     switch (nodeTag(node))
     161             :     {
     162             :             /*
     163             :              * control nodes
     164             :              */
     165      240704 :         case T_Result:
     166      240704 :             result = (PlanState *) ExecInitResult((Result *) node,
     167             :                                                   estate, eflags);
     168      240644 :             break;
     169             : 
     170        4606 :         case T_ProjectSet:
     171        4606 :             result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
     172             :                                                       estate, eflags);
     173        4602 :             break;
     174             : 
     175       97566 :         case T_ModifyTable:
     176       97566 :             result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
     177             :                                                        estate, eflags);
     178       97516 :             break;
     179             : 
     180        8386 :         case T_Append:
     181        8386 :             result = (PlanState *) ExecInitAppend((Append *) node,
     182             :                                                   estate, eflags);
     183        8386 :             break;
     184             : 
     185         268 :         case T_MergeAppend:
     186         268 :             result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
     187             :                                                        estate, eflags);
     188         268 :             break;
     189             : 
     190         454 :         case T_RecursiveUnion:
     191         454 :             result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
     192             :                                                           estate, eflags);
     193         454 :             break;
     194             : 
     195          60 :         case T_BitmapAnd:
     196          60 :             result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
     197             :                                                      estate, eflags);
     198          60 :             break;
     199             : 
     200         152 :         case T_BitmapOr:
     201         152 :             result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
     202             :                                                     estate, eflags);
     203         152 :             break;
     204             : 
     205             :             /*
     206             :              * scan nodes
     207             :              */
     208      192736 :         case T_SeqScan:
     209      192736 :             result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
     210             :                                                    estate, eflags);
     211      192728 :             break;
     212             : 
     213         180 :         case T_SampleScan:
     214         180 :             result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
     215             :                                                       estate, eflags);
     216         180 :             break;
     217             : 
     218      171462 :         case T_IndexScan:
     219      171462 :             result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
     220             :                                                      estate, eflags);
     221      171462 :             break;
     222             : 
     223        9150 :         case T_IndexOnlyScan:
     224        9150 :             result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
     225             :                                                          estate, eflags);
     226        9150 :             break;
     227             : 
     228       16608 :         case T_BitmapIndexScan:
     229       16608 :             result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
     230             :                                                            estate, eflags);
     231       16608 :             break;
     232             : 
     233       16360 :         case T_BitmapHeapScan:
     234       16360 :             result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
     235             :                                                           estate, eflags);
     236       16360 :             break;
     237             : 
     238         542 :         case T_TidScan:
     239         542 :             result = (PlanState *) ExecInitTidScan((TidScan *) node,
     240             :                                                    estate, eflags);
     241         542 :             break;
     242             : 
     243         136 :         case T_TidRangeScan:
     244         136 :             result = (PlanState *) ExecInitTidRangeScan((TidRangeScan *) node,
     245             :                                                         estate, eflags);
     246         136 :             break;
     247             : 
     248        3208 :         case T_SubqueryScan:
     249        3208 :             result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
     250             :                                                         estate, eflags);
     251        3208 :             break;
     252             : 
     253       78424 :         case T_FunctionScan:
     254       78424 :             result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
     255             :                                                         estate, eflags);
     256       78422 :             break;
     257             : 
     258         144 :         case T_TableFuncScan:
     259         144 :             result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
     260             :                                                          estate, eflags);
     261         144 :             break;
     262             : 
     263        5252 :         case T_ValuesScan:
     264        5252 :             result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
     265             :                                                       estate, eflags);
     266        5252 :             break;
     267             : 
     268        1714 :         case T_CteScan:
     269        1714 :             result = (PlanState *) ExecInitCteScan((CteScan *) node,
     270             :                                                    estate, eflags);
     271        1714 :             break;
     272             : 
     273         408 :         case T_NamedTuplestoreScan:
     274         408 :             result = (PlanState *) ExecInitNamedTuplestoreScan((NamedTuplestoreScan *) node,
     275             :                                                                estate, eflags);
     276         408 :             break;
     277             : 
     278         454 :         case T_WorkTableScan:
     279         454 :             result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
     280             :                                                          estate, eflags);
     281         454 :             break;
     282             : 
     283        1714 :         case T_ForeignScan:
     284        1714 :             result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
     285             :                                                        estate, eflags);
     286        1698 :             break;
     287             : 
     288           0 :         case T_CustomScan:
     289           0 :             result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
     290             :                                                       estate, eflags);
     291           0 :             break;
     292             : 
     293             :             /*
     294             :              * join nodes
     295             :              */
     296       98704 :         case T_NestLoop:
     297       98704 :             result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
     298             :                                                     estate, eflags);
     299       98704 :             break;
     300             : 
     301        2650 :         case T_MergeJoin:
     302        2650 :             result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
     303             :                                                      estate, eflags);
     304        2650 :             break;
     305             : 
     306       46062 :         case T_HashJoin:
     307       46062 :             result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
     308             :                                                     estate, eflags);
     309       46062 :             break;
     310             : 
     311             :             /*
     312             :              * materialization nodes
     313             :              */
     314       10902 :         case T_Material:
     315       10902 :             result = (PlanState *) ExecInitMaterial((Material *) node,
     316             :                                                     estate, eflags);
     317       10902 :             break;
     318             : 
     319       84464 :         case T_Sort:
     320       84464 :             result = (PlanState *) ExecInitSort((Sort *) node,
     321             :                                                 estate, eflags);
     322       84460 :             break;
     323             : 
     324         242 :         case T_IncrementalSort:
     325         242 :             result = (PlanState *) ExecInitIncrementalSort((IncrementalSort *) node,
     326             :                                                            estate, eflags);
     327         242 :             break;
     328             : 
     329         924 :         case T_Memoize:
     330         924 :             result = (PlanState *) ExecInitMemoize((Memoize *) node, estate,
     331             :                                                    eflags);
     332         924 :             break;
     333             : 
     334         152 :         case T_Group:
     335         152 :             result = (PlanState *) ExecInitGroup((Group *) node,
     336             :                                                  estate, eflags);
     337         152 :             break;
     338             : 
     339       58022 :         case T_Agg:
     340       58022 :             result = (PlanState *) ExecInitAgg((Agg *) node,
     341             :                                                estate, eflags);
     342       58018 :             break;
     343             : 
     344        1236 :         case T_WindowAgg:
     345        1236 :             result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
     346             :                                                      estate, eflags);
     347        1236 :             break;
     348             : 
     349         554 :         case T_Unique:
     350         554 :             result = (PlanState *) ExecInitUnique((Unique *) node,
     351             :                                                   estate, eflags);
     352         554 :             break;
     353             : 
     354         610 :         case T_Gather:
     355         610 :             result = (PlanState *) ExecInitGather((Gather *) node,
     356             :                                                   estate, eflags);
     357         610 :             break;
     358             : 
     359         180 :         case T_GatherMerge:
     360         180 :             result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
     361             :                                                        estate, eflags);
     362         180 :             break;
     363             : 
     364       46062 :         case T_Hash:
     365       46062 :             result = (PlanState *) ExecInitHash((Hash *) node,
     366             :                                                 estate, eflags);
     367       46062 :             break;
     368             : 
     369         340 :         case T_SetOp:
     370         340 :             result = (PlanState *) ExecInitSetOp((SetOp *) node,
     371             :                                                  estate, eflags);
     372         340 :             break;
     373             : 
     374        5372 :         case T_LockRows:
     375        5372 :             result = (PlanState *) ExecInitLockRows((LockRows *) node,
     376             :                                                     estate, eflags);
     377        5372 :             break;
     378             : 
     379        3494 :         case T_Limit:
     380        3494 :             result = (PlanState *) ExecInitLimit((Limit *) node,
     381             :                                                  estate, eflags);
     382        3494 :             break;
     383             : 
     384           0 :         default:
     385           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     386             :             result = NULL;      /* keep compiler quiet */
     387             :             break;
     388             :     }
     389             : 
     390     1210510 :     ExecSetExecProcNode(result, result->ExecProcNode);
     391             : 
     392             :     /*
     393             :      * Initialize any initPlans present in this node.  The planner put them in
     394             :      * a separate list for us.
     395             :      */
     396     1210510 :     subps = NIL;
     397     1223652 :     foreach(l, node->initPlan)
     398             :     {
     399       13142 :         SubPlan    *subplan = (SubPlan *) lfirst(l);
     400             :         SubPlanState *sstate;
     401             : 
     402             :         Assert(IsA(subplan, SubPlan));
     403       13142 :         sstate = ExecInitSubPlan(subplan, result);
     404       13142 :         subps = lappend(subps, sstate);
     405             :     }
     406     1210510 :     result->initPlan = subps;
     407             : 
     408             :     /* Set up instrumentation for this node if requested */
     409     1210510 :     if (estate->es_instrument)
     410        6292 :         result->instrument = InstrAlloc(1, estate->es_instrument,
     411        6292 :                                         result->async_capable);
     412             : 
     413     1210510 :     return result;
     414             : }
     415             : 
     416             : 
     417             : /*
     418             :  * If a node wants to change its ExecProcNode function after ExecInitNode()
     419             :  * has finished, it should do so with this function.  That way any wrapper
     420             :  * functions can be reinstalled, without the node having to know how that
     421             :  * works.
     422             :  */
     423             : void
     424     1210746 : ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function)
     425             : {
     426             :     /*
     427             :      * Add a wrapper around the ExecProcNode callback that checks stack depth
     428             :      * during the first execution and maybe adds an instrumentation wrapper.
     429             :      * When the callback is changed after execution has already begun that
     430             :      * means we'll superfluously execute ExecProcNodeFirst, but that seems ok.
     431             :      */
     432     1210746 :     node->ExecProcNodeReal = function;
     433     1210746 :     node->ExecProcNode = ExecProcNodeFirst;
     434     1210746 : }
     435             : 
     436             : 
     437             : /*
     438             :  * ExecProcNode wrapper that performs some one-time checks, before calling
     439             :  * the relevant node method (possibly via an instrumentation wrapper).
     440             :  */
     441             : static TupleTableSlot *
     442     1074554 : ExecProcNodeFirst(PlanState *node)
     443             : {
     444             :     /*
     445             :      * Perform stack depth check during the first execution of the node.  We
     446             :      * only do so the first time round because it turns out to not be cheap on
     447             :      * some common architectures (eg. x86).  This relies on the assumption
     448             :      * that ExecProcNode calls for a given plan node will always be made at
     449             :      * roughly the same stack depth.
     450             :      */
     451     1074554 :     check_stack_depth();
     452             : 
     453             :     /*
     454             :      * If instrumentation is required, change the wrapper to one that just
     455             :      * does instrumentation.  Otherwise we can dispense with all wrappers and
     456             :      * have ExecProcNode() directly call the relevant function from now on.
     457             :      */
     458     1074554 :     if (node->instrument)
     459        4700 :         node->ExecProcNode = ExecProcNodeInstr;
     460             :     else
     461     1069854 :         node->ExecProcNode = node->ExecProcNodeReal;
     462             : 
     463     1074554 :     return node->ExecProcNode(node);
     464             : }
     465             : 
     466             : 
     467             : /*
     468             :  * ExecProcNode wrapper that performs instrumentation calls.  By keeping
     469             :  * this a separate function, we avoid overhead in the normal case where
     470             :  * no instrumentation is wanted.
     471             :  */
     472             : static TupleTableSlot *
     473     8225120 : ExecProcNodeInstr(PlanState *node)
     474             : {
     475             :     TupleTableSlot *result;
     476             : 
     477     8225120 :     InstrStartNode(node->instrument);
     478             : 
     479     8225120 :     result = node->ExecProcNodeReal(node);
     480             : 
     481     8225112 :     InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
     482             : 
     483     8225112 :     return result;
     484             : }
     485             : 
     486             : 
     487             : /* ----------------------------------------------------------------
     488             :  *      MultiExecProcNode
     489             :  *
     490             :  *      Execute a node that doesn't return individual tuples
     491             :  *      (it might return a hashtable, bitmap, etc).  Caller should
     492             :  *      check it got back the expected kind of Node.
     493             :  *
     494             :  * This has essentially the same responsibilities as ExecProcNode,
     495             :  * but it does not do InstrStartNode/InstrStopNode (mainly because
     496             :  * it can't tell how many returned tuples to count).  Each per-node
     497             :  * function must provide its own instrumentation support.
     498             :  * ----------------------------------------------------------------
     499             :  */
     500             : Node *
     501      409050 : MultiExecProcNode(PlanState *node)
     502             : {
     503             :     Node       *result;
     504             : 
     505      409050 :     check_stack_depth();
     506             : 
     507      409050 :     CHECK_FOR_INTERRUPTS();
     508             : 
     509      409050 :     if (node->chgParam != NULL) /* something changed */
     510      363700 :         ExecReScan(node);       /* let ReScan handle this */
     511             : 
     512      409050 :     switch (nodeTag(node))
     513             :     {
     514             :             /*
     515             :              * Only node types that actually support multiexec will be listed
     516             :              */
     517             : 
     518      394422 :         case T_HashState:
     519      394422 :             result = MultiExecHash((HashState *) node);
     520      394422 :             break;
     521             : 
     522       14464 :         case T_BitmapIndexScanState:
     523       14464 :             result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
     524       14464 :             break;
     525             : 
     526          44 :         case T_BitmapAndState:
     527          44 :             result = MultiExecBitmapAnd((BitmapAndState *) node);
     528          44 :             break;
     529             : 
     530         120 :         case T_BitmapOrState:
     531         120 :             result = MultiExecBitmapOr((BitmapOrState *) node);
     532         120 :             break;
     533             : 
     534           0 :         default:
     535           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     536             :             result = NULL;
     537             :             break;
     538             :     }
     539             : 
     540      409050 :     return result;
     541             : }
     542             : 
     543             : 
     544             : /* ----------------------------------------------------------------
     545             :  *      ExecEndNode
     546             :  *
     547             :  *      Recursively cleans up all the nodes in the plan rooted
     548             :  *      at 'node'.
     549             :  *
     550             :  *      After this operation, the query plan will not be able to be
     551             :  *      processed any further.  This should be called only after
     552             :  *      the query plan has been fully executed.
     553             :  * ----------------------------------------------------------------
     554             :  */
     555             : void
     556     1416846 : ExecEndNode(PlanState *node)
     557             : {
     558             :     /*
     559             :      * do nothing when we get to the end of a leaf on tree.
     560             :      */
     561     1416846 :     if (node == NULL)
     562      225920 :         return;
     563             : 
     564             :     /*
     565             :      * Make sure there's enough stack available. Need to check here, in
     566             :      * addition to ExecProcNode() (via ExecProcNodeFirst()), because it's not
     567             :      * guaranteed that ExecProcNode() is reached for all nodes.
     568             :      */
     569     1190926 :     check_stack_depth();
     570             : 
     571     1190926 :     if (node->chgParam != NULL)
     572             :     {
     573        5398 :         bms_free(node->chgParam);
     574        5398 :         node->chgParam = NULL;
     575             :     }
     576             : 
     577     1190926 :     switch (nodeTag(node))
     578             :     {
     579             :             /*
     580             :              * control nodes
     581             :              */
     582      229998 :         case T_ResultState:
     583      229998 :             ExecEndResult((ResultState *) node);
     584      229998 :             break;
     585             : 
     586        4218 :         case T_ProjectSetState:
     587        4218 :             ExecEndProjectSet((ProjectSetState *) node);
     588        4218 :             break;
     589             : 
     590       94992 :         case T_ModifyTableState:
     591       94992 :             ExecEndModifyTable((ModifyTableState *) node);
     592       94992 :             break;
     593             : 
     594        8218 :         case T_AppendState:
     595        8218 :             ExecEndAppend((AppendState *) node);
     596        8218 :             break;
     597             : 
     598         268 :         case T_MergeAppendState:
     599         268 :             ExecEndMergeAppend((MergeAppendState *) node);
     600         268 :             break;
     601             : 
     602         454 :         case T_RecursiveUnionState:
     603         454 :             ExecEndRecursiveUnion((RecursiveUnionState *) node);
     604         454 :             break;
     605             : 
     606          60 :         case T_BitmapAndState:
     607          60 :             ExecEndBitmapAnd((BitmapAndState *) node);
     608          60 :             break;
     609             : 
     610         152 :         case T_BitmapOrState:
     611         152 :             ExecEndBitmapOr((BitmapOrState *) node);
     612         152 :             break;
     613             : 
     614             :             /*
     615             :              * scan nodes
     616             :              */
     617      191318 :         case T_SeqScanState:
     618      191318 :             ExecEndSeqScan((SeqScanState *) node);
     619      191318 :             break;
     620             : 
     621         152 :         case T_SampleScanState:
     622         152 :             ExecEndSampleScan((SampleScanState *) node);
     623         152 :             break;
     624             : 
     625         606 :         case T_GatherState:
     626         606 :             ExecEndGather((GatherState *) node);
     627         606 :             break;
     628             : 
     629         180 :         case T_GatherMergeState:
     630         180 :             ExecEndGatherMerge((GatherMergeState *) node);
     631         180 :             break;
     632             : 
     633      170974 :         case T_IndexScanState:
     634      170974 :             ExecEndIndexScan((IndexScanState *) node);
     635      170974 :             break;
     636             : 
     637        9074 :         case T_IndexOnlyScanState:
     638        9074 :             ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
     639        9074 :             break;
     640             : 
     641       16564 :         case T_BitmapIndexScanState:
     642       16564 :             ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
     643       16564 :             break;
     644             : 
     645       16316 :         case T_BitmapHeapScanState:
     646       16316 :             ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
     647       16316 :             break;
     648             : 
     649         460 :         case T_TidScanState:
     650         460 :             ExecEndTidScan((TidScanState *) node);
     651         460 :             break;
     652             : 
     653         136 :         case T_TidRangeScanState:
     654         136 :             ExecEndTidRangeScan((TidRangeScanState *) node);
     655         136 :             break;
     656             : 
     657        3208 :         case T_SubqueryScanState:
     658        3208 :             ExecEndSubqueryScan((SubqueryScanState *) node);
     659        3208 :             break;
     660             : 
     661       75470 :         case T_FunctionScanState:
     662       75470 :             ExecEndFunctionScan((FunctionScanState *) node);
     663       75470 :             break;
     664             : 
     665         132 :         case T_TableFuncScanState:
     666         132 :             ExecEndTableFuncScan((TableFuncScanState *) node);
     667         132 :             break;
     668             : 
     669        5172 :         case T_ValuesScanState:
     670        5172 :             ExecEndValuesScan((ValuesScanState *) node);
     671        5172 :             break;
     672             : 
     673        1694 :         case T_CteScanState:
     674        1694 :             ExecEndCteScan((CteScanState *) node);
     675        1694 :             break;
     676             : 
     677         408 :         case T_NamedTuplestoreScanState:
     678         408 :             ExecEndNamedTuplestoreScan((NamedTuplestoreScanState *) node);
     679         408 :             break;
     680             : 
     681         454 :         case T_WorkTableScanState:
     682         454 :             ExecEndWorkTableScan((WorkTableScanState *) node);
     683         454 :             break;
     684             : 
     685        1652 :         case T_ForeignScanState:
     686        1652 :             ExecEndForeignScan((ForeignScanState *) node);
     687        1652 :             break;
     688             : 
     689           0 :         case T_CustomScanState:
     690           0 :             ExecEndCustomScan((CustomScanState *) node);
     691           0 :             break;
     692             : 
     693             :             /*
     694             :              * join nodes
     695             :              */
     696       98530 :         case T_NestLoopState:
     697       98530 :             ExecEndNestLoop((NestLoopState *) node);
     698       98530 :             break;
     699             : 
     700        2646 :         case T_MergeJoinState:
     701        2646 :             ExecEndMergeJoin((MergeJoinState *) node);
     702        2646 :             break;
     703             : 
     704       46022 :         case T_HashJoinState:
     705       46022 :             ExecEndHashJoin((HashJoinState *) node);
     706       46022 :             break;
     707             : 
     708             :             /*
     709             :              * materialization nodes
     710             :              */
     711       10838 :         case T_MaterialState:
     712       10838 :             ExecEndMaterial((MaterialState *) node);
     713       10838 :             break;
     714             : 
     715       84412 :         case T_SortState:
     716       84412 :             ExecEndSort((SortState *) node);
     717       84412 :             break;
     718             : 
     719         242 :         case T_IncrementalSortState:
     720         242 :             ExecEndIncrementalSort((IncrementalSortState *) node);
     721         242 :             break;
     722             : 
     723         924 :         case T_MemoizeState:
     724         924 :             ExecEndMemoize((MemoizeState *) node);
     725         924 :             break;
     726             : 
     727         152 :         case T_GroupState:
     728         152 :             ExecEndGroup((GroupState *) node);
     729         152 :             break;
     730             : 
     731       57966 :         case T_AggState:
     732       57966 :             ExecEndAgg((AggState *) node);
     733       57966 :             break;
     734             : 
     735        1204 :         case T_WindowAggState:
     736        1204 :             ExecEndWindowAgg((WindowAggState *) node);
     737        1204 :             break;
     738             : 
     739         554 :         case T_UniqueState:
     740         554 :             ExecEndUnique((UniqueState *) node);
     741         554 :             break;
     742             : 
     743       46022 :         case T_HashState:
     744       46022 :             ExecEndHash((HashState *) node);
     745       46022 :             break;
     746             : 
     747         340 :         case T_SetOpState:
     748         340 :             ExecEndSetOp((SetOpState *) node);
     749         340 :             break;
     750             : 
     751        5292 :         case T_LockRowsState:
     752        5292 :             ExecEndLockRows((LockRowsState *) node);
     753        5292 :             break;
     754             : 
     755        3452 :         case T_LimitState:
     756        3452 :             ExecEndLimit((LimitState *) node);
     757        3452 :             break;
     758             : 
     759           0 :         default:
     760           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     761             :             break;
     762             :     }
     763             : }
     764             : 
     765             : /*
     766             :  * ExecShutdownNode
     767             :  *
     768             :  * Give execution nodes a chance to stop asynchronous resource consumption
     769             :  * and release any resources still held.
     770             :  */
     771             : bool
     772     1159838 : ExecShutdownNode(PlanState *node)
     773             : {
     774     1159838 :     if (node == NULL)
     775           0 :         return false;
     776             : 
     777     1159838 :     check_stack_depth();
     778             : 
     779             :     /*
     780             :      * Treat the node as running while we shut it down, but only if it's run
     781             :      * at least once already.  We don't expect much CPU consumption during
     782             :      * node shutdown, but in the case of Gather or Gather Merge, we may shut
     783             :      * down workers at this stage.  If so, their buffer usage will get
     784             :      * propagated into pgBufferUsage at this point, and we want to make sure
     785             :      * that it gets associated with the Gather node.  We skip this if the node
     786             :      * has never been executed, so as to avoid incorrectly making it appear
     787             :      * that it has.
     788             :      */
     789     1159838 :     if (node->instrument && node->instrument->running)
     790        5224 :         InstrStartNode(node->instrument);
     791             : 
     792     1159838 :     planstate_tree_walker(node, ExecShutdownNode, NULL);
     793             : 
     794     1159838 :     switch (nodeTag(node))
     795             :     {
     796         342 :         case T_GatherState:
     797         342 :             ExecShutdownGather((GatherState *) node);
     798         342 :             break;
     799         950 :         case T_ForeignScanState:
     800         950 :             ExecShutdownForeignScan((ForeignScanState *) node);
     801         950 :             break;
     802           0 :         case T_CustomScanState:
     803           0 :             ExecShutdownCustomScan((CustomScanState *) node);
     804           0 :             break;
     805          80 :         case T_GatherMergeState:
     806          80 :             ExecShutdownGatherMerge((GatherMergeState *) node);
     807          80 :             break;
     808       44106 :         case T_HashState:
     809       44106 :             ExecShutdownHash((HashState *) node);
     810       44106 :             break;
     811       44106 :         case T_HashJoinState:
     812       44106 :             ExecShutdownHashJoin((HashJoinState *) node);
     813       44106 :             break;
     814     1070254 :         default:
     815     1070254 :             break;
     816             :     }
     817             : 
     818             :     /* Stop the node if we started it above, reporting 0 tuples. */
     819     1159838 :     if (node->instrument && node->instrument->running)
     820        5224 :         InstrStopNode(node->instrument, 0);
     821             : 
     822     1159838 :     return false;
     823             : }
     824             : 
     825             : /*
     826             :  * ExecSetTupleBound
     827             :  *
     828             :  * Set a tuple bound for a planstate node.  This lets child plan nodes
     829             :  * optimize based on the knowledge that the maximum number of tuples that
     830             :  * their parent will demand is limited.  The tuple bound for a node may
     831             :  * only be changed between scans (i.e., after node initialization or just
     832             :  * before an ExecReScan call).
     833             :  *
     834             :  * Any negative tuples_needed value means "no limit", which should be the
     835             :  * default assumption when this is not called at all for a particular node.
     836             :  *
     837             :  * Note: if this is called repeatedly on a plan tree, the exact same set
     838             :  * of nodes must be updated with the new limit each time; be careful that
     839             :  * only unchanging conditions are tested here.
     840             :  */
     841             : void
     842        5306 : ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
     843             : {
     844             :     /*
     845             :      * Since this function recurses, in principle we should check stack depth
     846             :      * here.  In practice, it's probably pointless since the earlier node
     847             :      * initialization tree traversal would surely have consumed more stack.
     848             :      */
     849             : 
     850        5306 :     if (IsA(child_node, SortState))
     851             :     {
     852             :         /*
     853             :          * If it is a Sort node, notify it that it can use bounded sort.
     854             :          *
     855             :          * Note: it is the responsibility of nodeSort.c to react properly to
     856             :          * changes of these parameters.  If we ever redesign this, it'd be a
     857             :          * good idea to integrate this signaling with the parameter-change
     858             :          * mechanism.
     859             :          */
     860        1282 :         SortState  *sortState = (SortState *) child_node;
     861             : 
     862        1282 :         if (tuples_needed < 0)
     863             :         {
     864             :             /* make sure flag gets reset if needed upon rescan */
     865         180 :             sortState->bounded = false;
     866             :         }
     867             :         else
     868             :         {
     869        1102 :             sortState->bounded = true;
     870        1102 :             sortState->bound = tuples_needed;
     871             :         }
     872             :     }
     873        4024 :     else if (IsA(child_node, IncrementalSortState))
     874             :     {
     875             :         /*
     876             :          * If it is an IncrementalSort node, notify it that it can use bounded
     877             :          * sort.
     878             :          *
     879             :          * Note: it is the responsibility of nodeIncrementalSort.c to react
     880             :          * properly to changes of these parameters.  If we ever redesign this,
     881             :          * it'd be a good idea to integrate this signaling with the
     882             :          * parameter-change mechanism.
     883             :          */
     884          98 :         IncrementalSortState *sortState = (IncrementalSortState *) child_node;
     885             : 
     886          98 :         if (tuples_needed < 0)
     887             :         {
     888             :             /* make sure flag gets reset if needed upon rescan */
     889           0 :             sortState->bounded = false;
     890             :         }
     891             :         else
     892             :         {
     893          98 :             sortState->bounded = true;
     894          98 :             sortState->bound = tuples_needed;
     895             :         }
     896             :     }
     897        3926 :     else if (IsA(child_node, AppendState))
     898             :     {
     899             :         /*
     900             :          * If it is an Append, we can apply the bound to any nodes that are
     901             :          * children of the Append, since the Append surely need read no more
     902             :          * than that many tuples from any one input.
     903             :          */
     904          80 :         AppendState *aState = (AppendState *) child_node;
     905             :         int         i;
     906             : 
     907         260 :         for (i = 0; i < aState->as_nplans; i++)
     908         180 :             ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
     909             :     }
     910        3846 :     else if (IsA(child_node, MergeAppendState))
     911             :     {
     912             :         /*
     913             :          * If it is a MergeAppend, we can apply the bound to any nodes that
     914             :          * are children of the MergeAppend, since the MergeAppend surely need
     915             :          * read no more than that many tuples from any one input.
     916             :          */
     917          40 :         MergeAppendState *maState = (MergeAppendState *) child_node;
     918             :         int         i;
     919             : 
     920         160 :         for (i = 0; i < maState->ms_nplans; i++)
     921         120 :             ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
     922             :     }
     923        3806 :     else if (IsA(child_node, ResultState))
     924             :     {
     925             :         /*
     926             :          * Similarly, for a projecting Result, we can apply the bound to its
     927             :          * child node.
     928             :          *
     929             :          * If Result supported qual checking, we'd have to punt on seeing a
     930             :          * qual.  Note that having a resconstantqual is not a showstopper: if
     931             :          * that condition succeeds it affects nothing, while if it fails, no
     932             :          * rows will be demanded from the Result child anyway.
     933             :          */
     934         250 :         if (outerPlanState(child_node))
     935          68 :             ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     936             :     }
     937        3556 :     else if (IsA(child_node, SubqueryScanState))
     938             :     {
     939             :         /*
     940             :          * We can also descend through SubqueryScan, but only if it has no
     941             :          * qual (otherwise it might discard rows).
     942             :          */
     943          20 :         SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
     944             : 
     945          20 :         if (subqueryState->ss.ps.qual == NULL)
     946           8 :             ExecSetTupleBound(tuples_needed, subqueryState->subplan);
     947             :     }
     948        3536 :     else if (IsA(child_node, GatherState))
     949             :     {
     950             :         /*
     951             :          * A Gather node can propagate the bound to its workers.  As with
     952             :          * MergeAppend, no one worker could possibly need to return more
     953             :          * tuples than the Gather itself needs to.
     954             :          *
     955             :          * Note: As with Sort, the Gather node is responsible for reacting
     956             :          * properly to changes to this parameter.
     957             :          */
     958           0 :         GatherState *gstate = (GatherState *) child_node;
     959             : 
     960           0 :         gstate->tuples_needed = tuples_needed;
     961             : 
     962             :         /* Also pass down the bound to our own copy of the child plan */
     963           0 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     964             :     }
     965        3536 :     else if (IsA(child_node, GatherMergeState))
     966             :     {
     967             :         /* Same comments as for Gather */
     968          20 :         GatherMergeState *gstate = (GatherMergeState *) child_node;
     969             : 
     970          20 :         gstate->tuples_needed = tuples_needed;
     971             : 
     972          20 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     973             :     }
     974             : 
     975             :     /*
     976             :      * In principle we could descend through any plan node type that is
     977             :      * certain not to discard or combine input rows; but on seeing a node that
     978             :      * can do that, we can't propagate the bound any further.  For the moment
     979             :      * it's unclear that any other cases are worth checking here.
     980             :      */
     981        5306 : }

Generated by: LCOV version 1.14