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

Generated by: LCOV version 1.14