LCOV - code coverage report
Current view: top level - src/backend/executor - execProcnode.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 367 387 94.8 %
Date: 2023-06-06 09:15:10 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-2023, 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     1434116 : 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     1434116 :     if (node == NULL)
     152      290834 :         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     1143282 :     check_stack_depth();
     160             : 
     161     1143282 :     switch (nodeTag(node))
     162             :     {
     163             :             /*
     164             :              * control nodes
     165             :              */
     166      297912 :         case T_Result:
     167      297912 :             result = (PlanState *) ExecInitResult((Result *) node,
     168             :                                                   estate, eflags);
     169      297842 :             break;
     170             : 
     171        7010 :         case T_ProjectSet:
     172        7010 :             result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
     173             :                                                       estate, eflags);
     174        7006 :             break;
     175             : 
     176      134480 :         case T_ModifyTable:
     177      134480 :             result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
     178             :                                                        estate, eflags);
     179      134212 :             break;
     180             : 
     181       12964 :         case T_Append:
     182       12964 :             result = (PlanState *) ExecInitAppend((Append *) node,
     183             :                                                   estate, eflags);
     184       12964 :             break;
     185             : 
     186         414 :         case T_MergeAppend:
     187         414 :             result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
     188             :                                                        estate, eflags);
     189         414 :             break;
     190             : 
     191         702 :         case T_RecursiveUnion:
     192         702 :             result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
     193             :                                                           estate, eflags);
     194         702 :             break;
     195             : 
     196          76 :         case T_BitmapAnd:
     197          76 :             result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
     198             :                                                      estate, eflags);
     199          76 :             break;
     200             : 
     201         222 :         case T_BitmapOr:
     202         222 :             result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
     203             :                                                     estate, eflags);
     204         222 :             break;
     205             : 
     206             :             /*
     207             :              * scan nodes
     208             :              */
     209      186396 :         case T_SeqScan:
     210      186396 :             result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
     211             :                                                    estate, eflags);
     212      186384 :             break;
     213             : 
     214         252 :         case T_SampleScan:
     215         252 :             result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
     216             :                                                       estate, eflags);
     217         252 :             break;
     218             : 
     219      116108 :         case T_IndexScan:
     220      116108 :             result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
     221             :                                                      estate, eflags);
     222      116108 :             break;
     223             : 
     224       14324 :         case T_IndexOnlyScan:
     225       14324 :             result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
     226             :                                                          estate, eflags);
     227       14324 :             break;
     228             : 
     229       21830 :         case T_BitmapIndexScan:
     230       21830 :             result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
     231             :                                                            estate, eflags);
     232       21830 :             break;
     233             : 
     234       21478 :         case T_BitmapHeapScan:
     235       21478 :             result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
     236             :                                                           estate, eflags);
     237       21478 :             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        9050 :         case T_SubqueryScan:
     250        9050 :             result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
     251             :                                                         estate, eflags);
     252        9050 :             break;
     253             : 
     254       58204 :         case T_FunctionScan:
     255       58204 :             result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
     256             :                                                         estate, eflags);
     257       58196 :             break;
     258             : 
     259         216 :         case T_TableFuncScan:
     260         216 :             result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
     261             :                                                          estate, eflags);
     262         216 :             break;
     263             : 
     264        8026 :         case T_ValuesScan:
     265        8026 :             result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
     266             :                                                       estate, eflags);
     267        8026 :             break;
     268             : 
     269        2478 :         case T_CteScan:
     270        2478 :             result = (PlanState *) ExecInitCteScan((CteScan *) node,
     271             :                                                    estate, eflags);
     272        2478 :             break;
     273             : 
     274         660 :         case T_NamedTuplestoreScan:
     275         660 :             result = (PlanState *) ExecInitNamedTuplestoreScan((NamedTuplestoreScan *) node,
     276             :                                                                estate, eflags);
     277         660 :             break;
     278             : 
     279         702 :         case T_WorkTableScan:
     280         702 :             result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
     281             :                                                          estate, eflags);
     282         702 :             break;
     283             : 
     284        1904 :         case T_ForeignScan:
     285        1904 :             result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
     286             :                                                        estate, eflags);
     287        1888 :             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       67118 :         case T_NestLoop:
     298       67118 :             result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
     299             :                                                     estate, eflags);
     300       67118 :             break;
     301             : 
     302        4628 :         case T_MergeJoin:
     303        4628 :             result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
     304             :                                                      estate, eflags);
     305        4628 :             break;
     306             : 
     307       29766 :         case T_HashJoin:
     308       29766 :             result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
     309             :                                                     estate, eflags);
     310       29766 :             break;
     311             : 
     312             :             /*
     313             :              * materialization nodes
     314             :              */
     315        3708 :         case T_Material:
     316        3708 :             result = (PlanState *) ExecInitMaterial((Material *) node,
     317             :                                                     estate, eflags);
     318        3708 :             break;
     319             : 
     320       50562 :         case T_Sort:
     321       50562 :             result = (PlanState *) ExecInitSort((Sort *) node,
     322             :                                                 estate, eflags);
     323       50556 :             break;
     324             : 
     325         584 :         case T_IncrementalSort:
     326         584 :             result = (PlanState *) ExecInitIncrementalSort((IncrementalSort *) node,
     327             :                                                            estate, eflags);
     328         584 :             break;
     329             : 
     330        1010 :         case T_Memoize:
     331        1010 :             result = (PlanState *) ExecInitMemoize((Memoize *) node, estate,
     332             :                                                    eflags);
     333        1010 :             break;
     334             : 
     335         222 :         case T_Group:
     336         222 :             result = (PlanState *) ExecInitGroup((Group *) node,
     337             :                                                  estate, eflags);
     338         222 :             break;
     339             : 
     340       41944 :         case T_Agg:
     341       41944 :             result = (PlanState *) ExecInitAgg((Agg *) node,
     342             :                                                estate, eflags);
     343       41938 :             break;
     344             : 
     345        2178 :         case T_WindowAgg:
     346        2178 :             result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
     347             :                                                      estate, eflags);
     348        2178 :             break;
     349             : 
     350        1638 :         case T_Unique:
     351        1638 :             result = (PlanState *) ExecInitUnique((Unique *) node,
     352             :                                                   estate, eflags);
     353        1638 :             break;
     354             : 
     355         962 :         case T_Gather:
     356         962 :             result = (PlanState *) ExecInitGather((Gather *) node,
     357             :                                                   estate, eflags);
     358         962 :             break;
     359             : 
     360         276 :         case T_GatherMerge:
     361         276 :             result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
     362             :                                                        estate, eflags);
     363         276 :             break;
     364             : 
     365       29766 :         case T_Hash:
     366       29766 :             result = (PlanState *) ExecInitHash((Hash *) node,
     367             :                                                 estate, eflags);
     368       29766 :             break;
     369             : 
     370         594 :         case T_SetOp:
     371         594 :             result = (PlanState *) ExecInitSetOp((SetOp *) node,
     372             :                                                  estate, eflags);
     373         594 :             break;
     374             : 
     375        7022 :         case T_LockRows:
     376        7022 :             result = (PlanState *) ExecInitLockRows((LockRows *) node,
     377             :                                                     estate, eflags);
     378        7022 :             break;
     379             : 
     380        4976 :         case T_Limit:
     381        4976 :             result = (PlanState *) ExecInitLimit((Limit *) node,
     382             :                                                  estate, eflags);
     383        4976 :             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     1142892 :     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     1142892 :     subps = NIL;
     398     1160312 :     foreach(l, node->initPlan)
     399             :     {
     400       17420 :         SubPlan    *subplan = (SubPlan *) lfirst(l);
     401             :         SubPlanState *sstate;
     402             : 
     403             :         Assert(IsA(subplan, SubPlan));
     404       17420 :         sstate = ExecInitSubPlan(subplan, result);
     405       17420 :         subps = lappend(subps, sstate);
     406             :     }
     407     1142892 :     result->initPlan = subps;
     408             : 
     409             :     /* Set up instrumentation for this node if requested */
     410     1142892 :     if (estate->es_instrument)
     411        9876 :         result->instrument = InstrAlloc(1, estate->es_instrument,
     412        9876 :                                         result->async_capable);
     413             : 
     414     1142892 :     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     1143320 : 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     1143320 :     node->ExecProcNodeReal = function;
     434     1143320 :     node->ExecProcNode = ExecProcNodeFirst;
     435     1143320 : }
     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      986658 : 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      986658 :     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      986658 :     if (node->instrument)
     460        7488 :         node->ExecProcNode = ExecProcNodeInstr;
     461             :     else
     462      979170 :         node->ExecProcNode = node->ExecProcNodeReal;
     463             : 
     464      986658 :     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    12439856 : ExecProcNodeInstr(PlanState *node)
     475             : {
     476             :     TupleTableSlot *result;
     477             : 
     478    12439856 :     InstrStartNode(node->instrument);
     479             : 
     480    12439856 :     result = node->ExecProcNodeReal(node);
     481             : 
     482    12439844 :     InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
     483             : 
     484    12439844 :     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       39464 : MultiExecProcNode(PlanState *node)
     503             : {
     504             :     Node       *result;
     505             : 
     506       39464 :     check_stack_depth();
     507             : 
     508       39464 :     CHECK_FOR_INTERRUPTS();
     509             : 
     510       39464 :     if (node->chgParam != NULL) /* something changed */
     511        4320 :         ExecReScan(node);       /* let ReScan handle this */
     512             : 
     513       39464 :     switch (nodeTag(node))
     514             :     {
     515             :             /*
     516             :              * Only node types that actually support multiexec will be listed
     517             :              */
     518             : 
     519       22016 :         case T_HashState:
     520       22016 :             result = MultiExecHash((HashState *) node);
     521       22016 :             break;
     522             : 
     523       17232 :         case T_BitmapIndexScanState:
     524       17232 :             result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
     525       17232 :             break;
     526             : 
     527          52 :         case T_BitmapAndState:
     528          52 :             result = MultiExecBitmapAnd((BitmapAndState *) node);
     529          52 :             break;
     530             : 
     531         164 :         case T_BitmapOrState:
     532         164 :             result = MultiExecBitmapOr((BitmapOrState *) node);
     533         164 :             break;
     534             : 
     535           0 :         default:
     536           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     537             :             result = NULL;
     538             :             break;
     539             :     }
     540             : 
     541       39464 :     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     1388780 : ExecEndNode(PlanState *node)
     558             : {
     559             :     /*
     560             :      * do nothing when we get to the end of a leaf on tree.
     561             :      */
     562     1388780 :     if (node == NULL)
     563      275332 :         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     1113448 :     check_stack_depth();
     571             : 
     572     1113448 :     if (node->chgParam != NULL)
     573             :     {
     574        5502 :         bms_free(node->chgParam);
     575        5502 :         node->chgParam = NULL;
     576             :     }
     577             : 
     578     1113448 :     switch (nodeTag(node))
     579             :     {
     580             :             /*
     581             :              * control nodes
     582             :              */
     583      282444 :         case T_ResultState:
     584      282444 :             ExecEndResult((ResultState *) node);
     585      282444 :             break;
     586             : 
     587        6394 :         case T_ProjectSetState:
     588        6394 :             ExecEndProjectSet((ProjectSetState *) node);
     589        6394 :             break;
     590             : 
     591      130534 :         case T_ModifyTableState:
     592      130534 :             ExecEndModifyTable((ModifyTableState *) node);
     593      130534 :             break;
     594             : 
     595       12698 :         case T_AppendState:
     596       12698 :             ExecEndAppend((AppendState *) node);
     597       12698 :             break;
     598             : 
     599         414 :         case T_MergeAppendState:
     600         414 :             ExecEndMergeAppend((MergeAppendState *) node);
     601         414 :             break;
     602             : 
     603         702 :         case T_RecursiveUnionState:
     604         702 :             ExecEndRecursiveUnion((RecursiveUnionState *) node);
     605         702 :             break;
     606             : 
     607          76 :         case T_BitmapAndState:
     608          76 :             ExecEndBitmapAnd((BitmapAndState *) node);
     609          76 :             break;
     610             : 
     611         222 :         case T_BitmapOrState:
     612         222 :             ExecEndBitmapOr((BitmapOrState *) node);
     613         222 :             break;
     614             : 
     615             :             /*
     616             :              * scan nodes
     617             :              */
     618      184212 :         case T_SeqScanState:
     619      184212 :             ExecEndSeqScan((SeqScanState *) node);
     620      184212 :             break;
     621             : 
     622         212 :         case T_SampleScanState:
     623         212 :             ExecEndSampleScan((SampleScanState *) node);
     624         212 :             break;
     625             : 
     626         956 :         case T_GatherState:
     627         956 :             ExecEndGather((GatherState *) node);
     628         956 :             break;
     629             : 
     630         276 :         case T_GatherMergeState:
     631         276 :             ExecEndGatherMerge((GatherMergeState *) node);
     632         276 :             break;
     633             : 
     634      115466 :         case T_IndexScanState:
     635      115466 :             ExecEndIndexScan((IndexScanState *) node);
     636      115466 :             break;
     637             : 
     638       14192 :         case T_IndexOnlyScanState:
     639       14192 :             ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
     640       14192 :             break;
     641             : 
     642       21764 :         case T_BitmapIndexScanState:
     643       21764 :             ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
     644       21764 :             break;
     645             : 
     646       21412 :         case T_BitmapHeapScanState:
     647       21412 :             ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
     648       21412 :             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        9050 :         case T_SubqueryScanState:
     659        9050 :             ExecEndSubqueryScan((SubqueryScanState *) node);
     660        9050 :             break;
     661             : 
     662       52968 :         case T_FunctionScanState:
     663       52968 :             ExecEndFunctionScan((FunctionScanState *) node);
     664       52968 :             break;
     665             : 
     666         198 :         case T_TableFuncScanState:
     667         198 :             ExecEndTableFuncScan((TableFuncScanState *) node);
     668         198 :             break;
     669             : 
     670        7898 :         case T_ValuesScanState:
     671        7898 :             ExecEndValuesScan((ValuesScanState *) node);
     672        7898 :             break;
     673             : 
     674        2452 :         case T_CteScanState:
     675        2452 :             ExecEndCteScan((CteScanState *) node);
     676        2452 :             break;
     677             : 
     678         660 :         case T_NamedTuplestoreScanState:
     679         660 :             ExecEndNamedTuplestoreScan((NamedTuplestoreScanState *) node);
     680         660 :             break;
     681             : 
     682         702 :         case T_WorkTableScanState:
     683         702 :             ExecEndWorkTableScan((WorkTableScanState *) node);
     684         702 :             break;
     685             : 
     686        1842 :         case T_ForeignScanState:
     687        1842 :             ExecEndForeignScan((ForeignScanState *) node);
     688        1842 :             break;
     689             : 
     690           0 :         case T_CustomScanState:
     691           0 :             ExecEndCustomScan((CustomScanState *) node);
     692           0 :             break;
     693             : 
     694             :             /*
     695             :              * join nodes
     696             :              */
     697       66926 :         case T_NestLoopState:
     698       66926 :             ExecEndNestLoop((NestLoopState *) node);
     699       66926 :             break;
     700             : 
     701        4622 :         case T_MergeJoinState:
     702        4622 :             ExecEndMergeJoin((MergeJoinState *) node);
     703        4622 :             break;
     704             : 
     705       29682 :         case T_HashJoinState:
     706       29682 :             ExecEndHashJoin((HashJoinState *) node);
     707       29682 :             break;
     708             : 
     709             :             /*
     710             :              * materialization nodes
     711             :              */
     712        3648 :         case T_MaterialState:
     713        3648 :             ExecEndMaterial((MaterialState *) node);
     714        3648 :             break;
     715             : 
     716       50484 :         case T_SortState:
     717       50484 :             ExecEndSort((SortState *) node);
     718       50484 :             break;
     719             : 
     720         584 :         case T_IncrementalSortState:
     721         584 :             ExecEndIncrementalSort((IncrementalSortState *) node);
     722         584 :             break;
     723             : 
     724        1010 :         case T_MemoizeState:
     725        1010 :             ExecEndMemoize((MemoizeState *) node);
     726        1010 :             break;
     727             : 
     728         222 :         case T_GroupState:
     729         222 :             ExecEndGroup((GroupState *) node);
     730         222 :             break;
     731             : 
     732       41832 :         case T_AggState:
     733       41832 :             ExecEndAgg((AggState *) node);
     734       41832 :             break;
     735             : 
     736        2130 :         case T_WindowAggState:
     737        2130 :             ExecEndWindowAgg((WindowAggState *) node);
     738        2130 :             break;
     739             : 
     740        1638 :         case T_UniqueState:
     741        1638 :             ExecEndUnique((UniqueState *) node);
     742        1638 :             break;
     743             : 
     744       29682 :         case T_HashState:
     745       29682 :             ExecEndHash((HashState *) node);
     746       29682 :             break;
     747             : 
     748         594 :         case T_SetOpState:
     749         594 :             ExecEndSetOp((SetOpState *) node);
     750         594 :             break;
     751             : 
     752        6936 :         case T_LockRowsState:
     753        6936 :             ExecEndLockRows((LockRowsState *) node);
     754        6936 :             break;
     755             : 
     756        4914 :         case T_LimitState:
     757        4914 :             ExecEndLimit((LimitState *) node);
     758        4914 :             break;
     759             : 
     760           0 :         default:
     761           0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
     762             :             break;
     763             :     }
     764             : }
     765             : 
     766             : /*
     767             :  * ExecShutdownNode
     768             :  *
     769             :  * Give execution nodes a chance to stop asynchronous resource consumption
     770             :  * and release any resources still held.
     771             :  */
     772             : void
     773      503460 : ExecShutdownNode(PlanState *node)
     774             : {
     775      503460 :     (void) ExecShutdownNode_walker(node, NULL);
     776      503460 : }
     777             : 
     778             : static bool
     779     1065682 : ExecShutdownNode_walker(PlanState *node, void *context)
     780             : {
     781     1065682 :     if (node == NULL)
     782           0 :         return false;
     783             : 
     784     1065682 :     check_stack_depth();
     785             : 
     786             :     /*
     787             :      * Treat the node as running while we shut it down, but only if it's run
     788             :      * at least once already.  We don't expect much CPU consumption during
     789             :      * node shutdown, but in the case of Gather or Gather Merge, we may shut
     790             :      * down workers at this stage.  If so, their buffer usage will get
     791             :      * propagated into pgBufferUsage at this point, and we want to make sure
     792             :      * that it gets associated with the Gather node.  We skip this if the node
     793             :      * has never been executed, so as to avoid incorrectly making it appear
     794             :      * that it has.
     795             :      */
     796     1065682 :     if (node->instrument && node->instrument->running)
     797        8264 :         InstrStartNode(node->instrument);
     798             : 
     799     1065682 :     planstate_tree_walker(node, ExecShutdownNode_walker, context);
     800             : 
     801     1065682 :     switch (nodeTag(node))
     802             :     {
     803         548 :         case T_GatherState:
     804         548 :             ExecShutdownGather((GatherState *) node);
     805         548 :             break;
     806        1086 :         case T_ForeignScanState:
     807        1086 :             ExecShutdownForeignScan((ForeignScanState *) node);
     808        1086 :             break;
     809           0 :         case T_CustomScanState:
     810           0 :             ExecShutdownCustomScan((CustomScanState *) node);
     811           0 :             break;
     812         120 :         case T_GatherMergeState:
     813         120 :             ExecShutdownGatherMerge((GatherMergeState *) node);
     814         120 :             break;
     815       26786 :         case T_HashState:
     816       26786 :             ExecShutdownHash((HashState *) node);
     817       26786 :             break;
     818       26786 :         case T_HashJoinState:
     819       26786 :             ExecShutdownHashJoin((HashJoinState *) node);
     820       26786 :             break;
     821     1010356 :         default:
     822     1010356 :             break;
     823             :     }
     824             : 
     825             :     /* Stop the node if we started it above, reporting 0 tuples. */
     826     1065682 :     if (node->instrument && node->instrument->running)
     827        8264 :         InstrStopNode(node->instrument, 0);
     828             : 
     829     1065682 :     return false;
     830             : }
     831             : 
     832             : /*
     833             :  * ExecSetTupleBound
     834             :  *
     835             :  * Set a tuple bound for a planstate node.  This lets child plan nodes
     836             :  * optimize based on the knowledge that the maximum number of tuples that
     837             :  * their parent will demand is limited.  The tuple bound for a node may
     838             :  * only be changed between scans (i.e., after node initialization or just
     839             :  * before an ExecReScan call).
     840             :  *
     841             :  * Any negative tuples_needed value means "no limit", which should be the
     842             :  * default assumption when this is not called at all for a particular node.
     843             :  *
     844             :  * Note: if this is called repeatedly on a plan tree, the exact same set
     845             :  * of nodes must be updated with the new limit each time; be careful that
     846             :  * only unchanging conditions are tested here.
     847             :  */
     848             : void
     849        7794 : ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
     850             : {
     851             :     /*
     852             :      * Since this function recurses, in principle we should check stack depth
     853             :      * here.  In practice, it's probably pointless since the earlier node
     854             :      * initialization tree traversal would surely have consumed more stack.
     855             :      */
     856             : 
     857        7794 :     if (IsA(child_node, SortState))
     858             :     {
     859             :         /*
     860             :          * If it is a Sort node, notify it that it can use bounded sort.
     861             :          *
     862             :          * Note: it is the responsibility of nodeSort.c to react properly to
     863             :          * changes of these parameters.  If we ever redesign this, it'd be a
     864             :          * good idea to integrate this signaling with the parameter-change
     865             :          * mechanism.
     866             :          */
     867        1730 :         SortState  *sortState = (SortState *) child_node;
     868             : 
     869        1730 :         if (tuples_needed < 0)
     870             :         {
     871             :             /* make sure flag gets reset if needed upon rescan */
     872         268 :             sortState->bounded = false;
     873             :         }
     874             :         else
     875             :         {
     876        1462 :             sortState->bounded = true;
     877        1462 :             sortState->bound = tuples_needed;
     878             :         }
     879             :     }
     880        6064 :     else if (IsA(child_node, IncrementalSortState))
     881             :     {
     882             :         /*
     883             :          * If it is an IncrementalSort node, notify it that it can use bounded
     884             :          * sort.
     885             :          *
     886             :          * Note: it is the responsibility of nodeIncrementalSort.c to react
     887             :          * properly to changes of these parameters.  If we ever redesign this,
     888             :          * it'd be a good idea to integrate this signaling with the
     889             :          * parameter-change mechanism.
     890             :          */
     891         146 :         IncrementalSortState *sortState = (IncrementalSortState *) child_node;
     892             : 
     893         146 :         if (tuples_needed < 0)
     894             :         {
     895             :             /* make sure flag gets reset if needed upon rescan */
     896           0 :             sortState->bounded = false;
     897             :         }
     898             :         else
     899             :         {
     900         146 :             sortState->bounded = true;
     901         146 :             sortState->bound = tuples_needed;
     902             :         }
     903             :     }
     904        5918 :     else if (IsA(child_node, AppendState))
     905             :     {
     906             :         /*
     907             :          * If it is an Append, we can apply the bound to any nodes that are
     908             :          * children of the Append, since the Append surely need read no more
     909             :          * than that many tuples from any one input.
     910             :          */
     911         148 :         AppendState *aState = (AppendState *) child_node;
     912             :         int         i;
     913             : 
     914         472 :         for (i = 0; i < aState->as_nplans; i++)
     915         324 :             ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
     916             :     }
     917        5770 :     else if (IsA(child_node, MergeAppendState))
     918             :     {
     919             :         /*
     920             :          * If it is a MergeAppend, we can apply the bound to any nodes that
     921             :          * are children of the MergeAppend, since the MergeAppend surely need
     922             :          * read no more than that many tuples from any one input.
     923             :          */
     924          60 :         MergeAppendState *maState = (MergeAppendState *) child_node;
     925             :         int         i;
     926             : 
     927         240 :         for (i = 0; i < maState->ms_nplans; i++)
     928         180 :             ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
     929             :     }
     930        5710 :     else if (IsA(child_node, ResultState))
     931             :     {
     932             :         /*
     933             :          * Similarly, for a projecting Result, we can apply the bound to its
     934             :          * child node.
     935             :          *
     936             :          * If Result supported qual checking, we'd have to punt on seeing a
     937             :          * qual.  Note that having a resconstantqual is not a showstopper: if
     938             :          * that condition succeeds it affects nothing, while if it fails, no
     939             :          * rows will be demanded from the Result child anyway.
     940             :          */
     941         564 :         if (outerPlanState(child_node))
     942         104 :             ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     943             :     }
     944        5146 :     else if (IsA(child_node, SubqueryScanState))
     945             :     {
     946             :         /*
     947             :          * We can also descend through SubqueryScan, but only if it has no
     948             :          * qual (otherwise it might discard rows).
     949             :          */
     950          94 :         SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
     951             : 
     952          94 :         if (subqueryState->ss.ps.qual == NULL)
     953          72 :             ExecSetTupleBound(tuples_needed, subqueryState->subplan);
     954             :     }
     955        5052 :     else if (IsA(child_node, GatherState))
     956             :     {
     957             :         /*
     958             :          * A Gather node can propagate the bound to its workers.  As with
     959             :          * MergeAppend, no one worker could possibly need to return more
     960             :          * tuples than the Gather itself needs to.
     961             :          *
     962             :          * Note: As with Sort, the Gather node is responsible for reacting
     963             :          * properly to changes to this parameter.
     964             :          */
     965           0 :         GatherState *gstate = (GatherState *) child_node;
     966             : 
     967           0 :         gstate->tuples_needed = tuples_needed;
     968             : 
     969             :         /* Also pass down the bound to our own copy of the child plan */
     970           0 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     971             :     }
     972        5052 :     else if (IsA(child_node, GatherMergeState))
     973             :     {
     974             :         /* Same comments as for Gather */
     975          30 :         GatherMergeState *gstate = (GatherMergeState *) child_node;
     976             : 
     977          30 :         gstate->tuples_needed = tuples_needed;
     978             : 
     979          30 :         ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
     980             :     }
     981             : 
     982             :     /*
     983             :      * In principle we could descend through any plan node type that is
     984             :      * certain not to discard or combine input rows; but on seeing a node that
     985             :      * can do that, we can't propagate the bound any further.  For the moment
     986             :      * it's unclear that any other cases are worth checking here.
     987             :      */
     988        7794 : }

Generated by: LCOV version 1.14