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

Generated by: LCOV version 2.0-1