LCOV - differential code coverage report
Current view: top level - src/backend/executor - execAmi.c (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 80.1 % 291 233 58 233
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 7 7 7
Baseline: lcov-20261128-baseline Branches: 71.1 % 152 108 44 108
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 2 2 2
(360..) days: 79.9 % 289 231 58 231
Function coverage date bins:
(360..) days: 100.0 % 7 7 7
Branch coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 70.7 % 150 106 44 106

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * execAmi.c
                                  4                 :                :  *    miscellaneous executor access method routines
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *  src/backend/executor/execAmi.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : #include "postgres.h"
                                 14                 :                : 
                                 15                 :                : #include "access/amapi.h"
                                 16                 :                : #include "access/htup_details.h"
                                 17                 :                : #include "catalog/pg_class.h"
                                 18                 :                : #include "executor/executor.h"
                                 19                 :                : #include "executor/instrument.h"
                                 20                 :                : #include "executor/nodeAgg.h"
                                 21                 :                : #include "executor/nodeAppend.h"
                                 22                 :                : #include "executor/nodeBitmapAnd.h"
                                 23                 :                : #include "executor/nodeBitmapHeapscan.h"
                                 24                 :                : #include "executor/nodeBitmapIndexscan.h"
                                 25                 :                : #include "executor/nodeBitmapOr.h"
                                 26                 :                : #include "executor/nodeCtescan.h"
                                 27                 :                : #include "executor/nodeCustom.h"
                                 28                 :                : #include "executor/nodeForeignscan.h"
                                 29                 :                : #include "executor/nodeFunctionscan.h"
                                 30                 :                : #include "executor/nodeGather.h"
                                 31                 :                : #include "executor/nodeGatherMerge.h"
                                 32                 :                : #include "executor/nodeGroup.h"
                                 33                 :                : #include "executor/nodeHash.h"
                                 34                 :                : #include "executor/nodeHashjoin.h"
                                 35                 :                : #include "executor/nodeIncrementalSort.h"
                                 36                 :                : #include "executor/nodeIndexonlyscan.h"
                                 37                 :                : #include "executor/nodeIndexscan.h"
                                 38                 :                : #include "executor/nodeLimit.h"
                                 39                 :                : #include "executor/nodeLockRows.h"
                                 40                 :                : #include "executor/nodeMaterial.h"
                                 41                 :                : #include "executor/nodeMemoize.h"
                                 42                 :                : #include "executor/nodeMergeAppend.h"
                                 43                 :                : #include "executor/nodeMergejoin.h"
                                 44                 :                : #include "executor/nodeModifyTable.h"
                                 45                 :                : #include "executor/nodeNamedtuplestorescan.h"
                                 46                 :                : #include "executor/nodeNestloop.h"
                                 47                 :                : #include "executor/nodeProjectSet.h"
                                 48                 :                : #include "executor/nodeRecursiveunion.h"
                                 49                 :                : #include "executor/nodeResult.h"
                                 50                 :                : #include "executor/nodeSamplescan.h"
                                 51                 :                : #include "executor/nodeSeqscan.h"
                                 52                 :                : #include "executor/nodeSetOp.h"
                                 53                 :                : #include "executor/nodeSort.h"
                                 54                 :                : #include "executor/nodeSubplan.h"
                                 55                 :                : #include "executor/nodeSubqueryscan.h"
                                 56                 :                : #include "executor/nodeTableFuncscan.h"
                                 57                 :                : #include "executor/nodeTidrangescan.h"
                                 58                 :                : #include "executor/nodeTidscan.h"
                                 59                 :                : #include "executor/nodeUnique.h"
                                 60                 :                : #include "executor/nodeValuesscan.h"
                                 61                 :                : #include "executor/nodeWindowAgg.h"
                                 62                 :                : #include "executor/nodeWorktablescan.h"
                                 63                 :                : #include "nodes/extensible.h"
                                 64                 :                : #include "nodes/pathnodes.h"
                                 65                 :                : #include "utils/syscache.h"
                                 66                 :                : 
                                 67                 :                : static bool IndexSupportsBackwardScan(Oid indexid);
                                 68                 :                : 
                                 69                 :                : 
                                 70                 :                : /*
                                 71                 :                :  * ExecReScan
                                 72                 :                :  *      Reset a plan node so that its output can be re-scanned.
                                 73                 :                :  *
                                 74                 :                :  * Note that if the plan node has parameters that have changed value,
                                 75                 :                :  * the output might be different from last time.
                                 76                 :                :  */
                                 77                 :                : void
 5983 tgl@sss.pgh.pa.us          78                 :CBC     2664960 : ExecReScan(PlanState *node)
                                 79                 :                : {
                                 80                 :                :     /* If collecting timing stats, update them */
 9202                            81         [ +  + ]:        2664960 :     if (node->instrument)
                                 82                 :          28583 :         InstrEndLoop(node->instrument);
                                 83                 :                : 
                                 84                 :                :     /*
                                 85                 :                :      * If we have changed parameters, propagate that info.
                                 86                 :                :      *
                                 87                 :                :      * Note: ExecReScanSetParamPlan() can add bits to node->chgParam,
                                 88                 :                :      * corresponding to the output param(s) that the InitPlan will update.
                                 89                 :                :      * Since we make only one pass over the list, that means that an InitPlan
                                 90                 :                :      * can depend on the output param(s) of a sibling InitPlan only if that
                                 91                 :                :      * sibling appears earlier in the list.  This is workable for now given
                                 92                 :                :      * the limited ways in which one InitPlan could depend on another, but
                                 93                 :                :      * eventually we might need to work harder (or else make the planner
                                 94                 :                :      * enlarge the extParam/allParam sets to include the params of depended-on
                                 95                 :                :      * InitPlans).
                                 96                 :                :      */
 8693                            97         [ +  + ]:        2664960 :     if (node->chgParam != NULL)
                                 98                 :                :     {
                                 99                 :                :         ListCell   *l;
                                100                 :                : 
 8221 neilc@samurai.com         101   [ +  +  +  +  :        2419006 :         foreach(l, node->initPlan)
                                              +  + ]
                                102                 :                :         {
                                103                 :           1353 :             SubPlanState *sstate = (SubPlanState *) lfirst(l);
 8751 tgl@sss.pgh.pa.us         104                 :           1353 :             PlanState  *splan = sstate->planstate;
                                105                 :                : 
 8693                           106         [ +  + ]:           1353 :             if (splan->plan->extParam != NULL)    /* don't care about child
                                107                 :                :                                                  * local Params */
                                108                 :           1196 :                 UpdateChangedParamSet(splan, node->chgParam);
                                109         [ +  + ]:           1353 :             if (splan->chgParam != NULL)
 8751                           110                 :           1036 :                 ExecReScanSetParamPlan(sstate, node);
                                111                 :                :         }
 8221 neilc@samurai.com         112   [ +  +  +  +  :        2458662 :         foreach(l, node->subPlan)
                                              +  + ]
                                113                 :                :         {
                                114                 :          41009 :             SubPlanState *sstate = (SubPlanState *) lfirst(l);
 8751 tgl@sss.pgh.pa.us         115                 :          41009 :             PlanState  *splan = sstate->planstate;
                                116                 :                : 
 8693                           117         [ +  + ]:          41009 :             if (splan->plan->extParam != NULL)
                                118                 :            785 :                 UpdateChangedParamSet(splan, node->chgParam);
                                119                 :                : 
                                120                 :                :             /*
                                121                 :                :              * Mark the hash table stale.  This is tracked per SubPlanState,
                                122                 :                :              * since several of them can share one subplan PlanState.
                                123                 :                :              */
   26 rguo@postgresql.org       124         [ +  + ]:          41009 :             if (splan->chgParam != NULL)
                                125                 :            480 :                 sstate->hashtablestale = true;
                                126                 :                :         }
                                127                 :                :         /* Well. Now set chgParam for child trees. */
 1605 tgl@sss.pgh.pa.us         128         [ +  + ]:        2417653 :         if (outerPlanState(node) != NULL)
                                129                 :        1032634 :             UpdateChangedParamSet(outerPlanState(node), node->chgParam);
                                130         [ +  + ]:        2417653 :         if (innerPlanState(node) != NULL)
                                131                 :          52744 :             UpdateChangedParamSet(innerPlanState(node), node->chgParam);
                                132                 :                :     }
                                133                 :                : 
                                134                 :                :     /* Call expression callbacks */
 8381                           135         [ +  + ]:        2664960 :     if (node->ps_ExprContext)
                                136                 :        2480601 :         ReScanExprContext(node->ps_ExprContext);
                                137                 :                : 
                                138                 :                :     /* And do node-type-specific processing */
10674 bruce@momjian.us          139   [ +  +  -  +  :        2664960 :     switch (nodeTag(node))
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  -  
                                     +  +  -  +  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                        +  +  +  - ]
                                140                 :                :     {
 8759 tgl@sss.pgh.pa.us         141                 :         108946 :         case T_ResultState:
 5983                           142                 :         108946 :             ExecReScanResult((ResultState *) node);
10515 vadim4o@yahoo.com         143                 :         108946 :             break;
                                144                 :                : 
 3601 andres@anarazel.de        145                 :           9792 :         case T_ProjectSetState:
                                146                 :           9792 :             ExecReScanProjectSet((ProjectSetState *) node);
                                147                 :           9792 :             break;
                                148                 :                : 
 6258 tgl@sss.pgh.pa.us         149                 :UBC           0 :         case T_ModifyTableState:
 5983                           150                 :              0 :             ExecReScanModifyTable((ModifyTableState *) node);
 6258                           151                 :              0 :             break;
                                152                 :                : 
 8759 tgl@sss.pgh.pa.us         153                 :CBC       12685 :         case T_AppendState:
 5983                           154                 :          12685 :             ExecReScanAppend((AppendState *) node);
10515 vadim4o@yahoo.com         155                 :          12685 :             break;
                                156                 :                : 
 5889 tgl@sss.pgh.pa.us         157                 :             17 :         case T_MergeAppendState:
                                158                 :             17 :             ExecReScanMergeAppend((MergeAppendState *) node);
                                159                 :             17 :             break;
                                160                 :                : 
 6629                           161                 :              8 :         case T_RecursiveUnionState:
 5983                           162                 :              8 :             ExecReScanRecursiveUnion((RecursiveUnionState *) node);
 6629                           163                 :              8 :             break;
                                164                 :                : 
 7893                           165                 :             90 :         case T_BitmapAndState:
 5983                           166                 :             90 :             ExecReScanBitmapAnd((BitmapAndState *) node);
 7893                           167                 :             90 :             break;
                                168                 :                : 
                                169                 :              4 :         case T_BitmapOrState:
 5983                           170                 :              4 :             ExecReScanBitmapOr((BitmapOrState *) node);
 7893                           171                 :              4 :             break;
                                172                 :                : 
 8759                           173                 :         730799 :         case T_SeqScanState:
 5983                           174                 :         730799 :             ExecReScanSeqScan((SeqScanState *) node);
 9556                           175                 :         730799 :             break;
                                176                 :                : 
 4215 simon@2ndQuadrant.co      177                 :             65 :         case T_SampleScanState:
                                178                 :             65 :             ExecReScanSampleScan((SampleScanState *) node);
                                179                 :             65 :             break;
                                180                 :                : 
 4077 rhaas@postgresql.org      181                 :            212 :         case T_GatherState:
                                182                 :            212 :             ExecReScanGather((GatherState *) node);
                                183                 :            212 :             break;
                                184                 :                : 
 3392                           185                 :             32 :         case T_GatherMergeState:
                                186                 :             32 :             ExecReScanGatherMerge((GatherMergeState *) node);
                                187                 :             32 :             break;
                                188                 :                : 
 8759 tgl@sss.pgh.pa.us         189                 :         362655 :         case T_IndexScanState:
 5983                           190                 :         362655 :             ExecReScanIndexScan((IndexScanState *) node);
 9556                           191                 :         362655 :             break;
                                192                 :                : 
 5527                           193                 :         144133 :         case T_IndexOnlyScanState:
                                194                 :         144133 :             ExecReScanIndexOnlyScan((IndexOnlyScanState *) node);
                                195                 :         144133 :             break;
                                196                 :                : 
 7893                           197                 :           3252 :         case T_BitmapIndexScanState:
 5983                           198                 :           3252 :             ExecReScanBitmapIndexScan((BitmapIndexScanState *) node);
 7893                           199                 :           3252 :             break;
                                200                 :                : 
                                201                 :           2848 :         case T_BitmapHeapScanState:
 5983                           202                 :           2848 :             ExecReScanBitmapHeapScan((BitmapHeapScanState *) node);
 7893                           203                 :           2848 :             break;
                                204                 :                : 
 8759                           205                 :           7677 :         case T_TidScanState:
 5983                           206                 :           7677 :             ExecReScanTidScan((TidScanState *) node);
 8966                           207                 :           7677 :             break;
                                208                 :                : 
 2100 drowley@postgresql.o      209                 :             80 :         case T_TidRangeScanState:
                                210                 :             80 :             ExecReScanTidRangeScan((TidRangeScanState *) node);
                                211                 :             80 :             break;
                                212                 :                : 
 8759 tgl@sss.pgh.pa.us         213                 :            534 :         case T_SubqueryScanState:
 5983                           214                 :            534 :             ExecReScanSubqueryScan((SubqueryScanState *) node);
10515 vadim4o@yahoo.com         215                 :            534 :             break;
                                216                 :                : 
 8759 tgl@sss.pgh.pa.us         217                 :          74014 :         case T_FunctionScanState:
 5983                           218                 :          74014 :             ExecReScanFunctionScan((FunctionScanState *) node);
10515 vadim4o@yahoo.com         219                 :          74014 :             break;
                                220                 :                : 
 3552 alvherre@alvh.no-ip.      221                 :            344 :         case T_TableFuncScanState:
                                222                 :            344 :             ExecReScanTableFuncScan((TableFuncScanState *) node);
                                223                 :            344 :             break;
                                224                 :                : 
 7423 mail@joeconway.com        225                 :          40262 :         case T_ValuesScanState:
 5983 tgl@sss.pgh.pa.us         226                 :          40262 :             ExecReScanValuesScan((ValuesScanState *) node);
 7423 mail@joeconway.com        227                 :          40262 :             break;
                                228                 :                : 
 6629 tgl@sss.pgh.pa.us         229                 :           5100 :         case T_CteScanState:
 5983                           230                 :           5100 :             ExecReScanCteScan((CteScanState *) node);
 6629                           231                 :           5100 :             break;
                                232                 :                : 
 3529 kgrittn@postgresql.o      233                 :UBC           0 :         case T_NamedTuplestoreScanState:
                                234                 :              0 :             ExecReScanNamedTuplestoreScan((NamedTuplestoreScanState *) node);
                                235                 :              0 :             break;
                                236                 :                : 
 6629 tgl@sss.pgh.pa.us         237                 :CBC        4478 :         case T_WorkTableScanState:
 5983                           238                 :           4478 :             ExecReScanWorkTableScan((WorkTableScanState *) node);
 6629                           239                 :           4478 :             break;
                                240                 :                : 
 5760                           241                 :            458 :         case T_ForeignScanState:
                                242                 :            458 :             ExecReScanForeignScan((ForeignScanState *) node);
                                243                 :            458 :             break;
                                244                 :                : 
 4404 rhaas@postgresql.org      245                 :UBC           0 :         case T_CustomScanState:
                                246                 :              0 :             ExecReScanCustomScan((CustomScanState *) node);
                                247                 :              0 :             break;
                                248                 :                : 
 8759 tgl@sss.pgh.pa.us         249                 :CBC       48925 :         case T_NestLoopState:
 5983                           250                 :          48925 :             ExecReScanNestLoop((NestLoopState *) node);
10515 vadim4o@yahoo.com         251                 :          48925 :             break;
                                252                 :                : 
 8759 tgl@sss.pgh.pa.us         253                 :            610 :         case T_MergeJoinState:
 5983                           254                 :            610 :             ExecReScanMergeJoin((MergeJoinState *) node);
10515 vadim4o@yahoo.com         255                 :            610 :             break;
                                256                 :                : 
 8759 tgl@sss.pgh.pa.us         257                 :           3269 :         case T_HashJoinState:
 5983                           258                 :           3269 :             ExecReScanHashJoin((HashJoinState *) node);
10515 vadim4o@yahoo.com         259                 :           3269 :             break;
                                260                 :                : 
 8759 tgl@sss.pgh.pa.us         261                 :         139270 :         case T_MaterialState:
 5983                           262                 :         139270 :             ExecReScanMaterial((MaterialState *) node);
10362 bruce@momjian.us          263                 :         139270 :             break;
                                264                 :                : 
 1963 drowley@postgresql.o      265                 :         860044 :         case T_MemoizeState:
                                266                 :         860044 :             ExecReScanMemoize((MemoizeState *) node);
 2066                           267                 :         860044 :             break;
                                268                 :                : 
 8759 tgl@sss.pgh.pa.us         269                 :          29005 :         case T_SortState:
 5983                           270                 :          29005 :             ExecReScanSort((SortState *) node);
10515 vadim4o@yahoo.com         271                 :          29005 :             break;
                                272                 :                : 
 2427 tomas.vondra@postgre      273                 :             20 :         case T_IncrementalSortState:
                                274                 :             20 :             ExecReScanIncrementalSort((IncrementalSortState *) node);
                                275                 :             20 :             break;
                                276                 :                : 
 8759 tgl@sss.pgh.pa.us         277                 :             23 :         case T_GroupState:
 5983                           278                 :             23 :             ExecReScanGroup((GroupState *) node);
10505 vadim4o@yahoo.com         279                 :             23 :             break;
                                280                 :                : 
 8759 tgl@sss.pgh.pa.us         281                 :          32003 :         case T_AggState:
 5983                           282                 :          32003 :             ExecReScanAgg((AggState *) node);
 9550                           283                 :          32003 :             break;
                                284                 :                : 
 6544                           285                 :             64 :         case T_WindowAggState:
 5983                           286                 :             64 :             ExecReScanWindowAgg((WindowAggState *) node);
 6544                           287                 :             64 :             break;
                                288                 :                : 
 8759                           289                 :             28 :         case T_UniqueState:
 5983                           290                 :             28 :             ExecReScanUnique((UniqueState *) node);
 9529                           291                 :             28 :             break;
                                292                 :                : 
 8759                           293                 :           1724 :         case T_HashState:
 5983                           294                 :           1724 :             ExecReScanHash((HashState *) node);
10505 vadim4o@yahoo.com         295                 :           1724 :             break;
                                296                 :                : 
 8759 tgl@sss.pgh.pa.us         297                 :            800 :         case T_SetOpState:
 5983                           298                 :            800 :             ExecReScanSetOp((SetOpState *) node);
10501 vadim4o@yahoo.com         299                 :            800 :             break;
                                300                 :                : 
 6256 tgl@sss.pgh.pa.us         301                 :              8 :         case T_LockRowsState:
 5983                           302                 :              8 :             ExecReScanLockRows((LockRowsState *) node);
 6256                           303                 :              8 :             break;
                                304                 :                : 
 8759                           305                 :          40682 :         case T_LimitState:
 5983                           306                 :          40682 :             ExecReScanLimit((LimitState *) node);
10363 bruce@momjian.us          307                 :          40682 :             break;
                                308                 :                : 
10673 bruce@momjian.us          309                 :UBC           0 :         default:
 8531 tgl@sss.pgh.pa.us         310         [ #  # ]:              0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
                                311                 :                :             break;
                                312                 :                :     }
                                313                 :                : 
 8693 tgl@sss.pgh.pa.us         314         [ +  + ]:CBC     2664960 :     if (node->chgParam != NULL)
                                315                 :                :     {
                                316                 :        2417653 :         bms_free(node->chgParam);
                                317                 :        2417653 :         node->chgParam = NULL;
                                318                 :                :     }
11099 scrappy@hub.org           319                 :        2664960 : }
                                320                 :                : 
                                321                 :                : /*
                                322                 :                :  * ExecMarkPos
                                323                 :                :  *
                                324                 :                :  * Marks the current scan position.
                                325                 :                :  *
                                326                 :                :  * NOTE: mark/restore capability is currently needed only for plan nodes
                                327                 :                :  * that are the immediate inner child of a MergeJoin node.  Since MergeJoin
                                328                 :                :  * requires sorted input, there is never any need to support mark/restore in
                                329                 :                :  * node types that cannot produce sorted output.  There are some cases in
                                330                 :                :  * which a node can pass through sorted data from its child; if we don't
                                331                 :                :  * implement mark/restore for such a node type, the planner compensates by
                                332                 :                :  * inserting a Material node above that node.
                                333                 :                :  */
                                334                 :                : void
 8513 bruce@momjian.us          335                 :         451280 : ExecMarkPos(PlanState *node)
                                336                 :                : {
10674                           337   [ +  +  -  +  :         451280 :     switch (nodeTag(node))
                                           +  -  - ]
                                338                 :                :     {
 8759 tgl@sss.pgh.pa.us         339                 :           4400 :         case T_IndexScanState:
                                340                 :           4400 :             ExecIndexMarkPos((IndexScanState *) node);
10673 bruce@momjian.us          341                 :           4400 :             break;
                                342                 :                : 
 5527 tgl@sss.pgh.pa.us         343                 :          86019 :         case T_IndexOnlyScanState:
                                344                 :          86019 :             ExecIndexOnlyMarkPos((IndexOnlyScanState *) node);
                                345                 :          86019 :             break;
                                346                 :                : 
 4404 rhaas@postgresql.org      347                 :UBC           0 :         case T_CustomScanState:
                                348                 :              0 :             ExecCustomMarkPos((CustomScanState *) node);
                                349                 :              0 :             break;
                                350                 :                : 
 8759 tgl@sss.pgh.pa.us         351                 :CBC        4381 :         case T_MaterialState:
                                352                 :           4381 :             ExecMaterialMarkPos((MaterialState *) node);
 9659                           353                 :           4381 :             break;
                                354                 :                : 
 8759                           355                 :         356480 :         case T_SortState:
                                356                 :         356480 :             ExecSortMarkPos((SortState *) node);
10673 bruce@momjian.us          357                 :         356480 :             break;
                                358                 :                : 
 7226 tgl@sss.pgh.pa.us         359                 :UBC           0 :         case T_ResultState:
                                360                 :              0 :             ExecResultMarkPos((ResultState *) node);
                                361                 :              0 :             break;
                                362                 :                : 
10673 bruce@momjian.us          363                 :              0 :         default:
                                364                 :                :             /* don't make hard error unless caller asks to restore... */
 8531 tgl@sss.pgh.pa.us         365         [ #  # ]:              0 :             elog(DEBUG2, "unrecognized node type: %d", (int) nodeTag(node));
10673 bruce@momjian.us          366                 :              0 :             break;
                                367                 :                :     }
11099 scrappy@hub.org           368                 :CBC      451280 : }
                                369                 :                : 
                                370                 :                : /*
                                371                 :                :  * ExecRestrPos
                                372                 :                :  *
                                373                 :                :  * restores the scan position previously saved with ExecMarkPos()
                                374                 :                :  *
                                375                 :                :  * NOTE: the semantics of this are that the first ExecProcNode following
                                376                 :                :  * the restore operation will yield the same tuple as the first one following
                                377                 :                :  * the mark operation.  It is unspecified what happens to the plan node's
                                378                 :                :  * result TupleTableSlot.  (In most cases the result slot is unchanged by
                                379                 :                :  * a restore, but the node may choose to clear it or to load it with the
                                380                 :                :  * restored-to tuple.)  Hence the caller should discard any previously
                                381                 :                :  * returned TupleTableSlot after doing a restore.
                                382                 :                :  */
                                383                 :                : void
 8513 bruce@momjian.us          384                 :         116562 : ExecRestrPos(PlanState *node)
                                385                 :                : {
10674                           386   [ +  +  -  +  :         116562 :     switch (nodeTag(node))
                                           +  -  - ]
                                387                 :                :     {
 8759 tgl@sss.pgh.pa.us         388                 :          36030 :         case T_IndexScanState:
                                389                 :          36030 :             ExecIndexRestrPos((IndexScanState *) node);
 9622                           390                 :          36030 :             break;
                                391                 :                : 
 5527                           392                 :          16000 :         case T_IndexOnlyScanState:
                                393                 :          16000 :             ExecIndexOnlyRestrPos((IndexOnlyScanState *) node);
                                394                 :          16000 :             break;
                                395                 :                : 
 4404 rhaas@postgresql.org      396                 :UBC           0 :         case T_CustomScanState:
                                397                 :              0 :             ExecCustomRestrPos((CustomScanState *) node);
                                398                 :              0 :             break;
                                399                 :                : 
 8759 tgl@sss.pgh.pa.us         400                 :CBC       36208 :         case T_MaterialState:
                                401                 :          36208 :             ExecMaterialRestrPos((MaterialState *) node);
 9622                           402                 :          36208 :             break;
                                403                 :                : 
 8759                           404                 :          28324 :         case T_SortState:
                                405                 :          28324 :             ExecSortRestrPos((SortState *) node);
 9622                           406                 :          28324 :             break;
                                407                 :                : 
 7226 tgl@sss.pgh.pa.us         408                 :UBC           0 :         case T_ResultState:
                                409                 :              0 :             ExecResultRestrPos((ResultState *) node);
                                410                 :              0 :             break;
                                411                 :                : 
10673 bruce@momjian.us          412                 :              0 :         default:
 8531 tgl@sss.pgh.pa.us         413         [ #  # ]:              0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
                                414                 :                :             break;
                                415                 :                :     }
11099 scrappy@hub.org           416                 :CBC      116562 : }
                                417                 :                : 
                                418                 :                : /*
                                419                 :                :  * ExecSupportsMarkRestore - does a Path support mark/restore?
                                420                 :                :  *
                                421                 :                :  * This is used during planning and so must accept a Path, not a Plan.
                                422                 :                :  * We keep it here to be adjacent to the routines above, which also must
                                423                 :                :  * know which plan types support mark/restore.
                                424                 :                :  */
                                425                 :                : bool
 4404 rhaas@postgresql.org      426                 :           7549 : ExecSupportsMarkRestore(Path *pathnode)
                                427                 :                : {
                                428                 :                :     /*
                                429                 :                :      * For consistency with the routines above, we do not examine the nodeTag
                                430                 :                :      * but rather the pathtype, which is the Plan node type the Path would
                                431                 :                :      * produce.
                                432                 :                :      */
                                433   [ +  -  -  -  :           7549 :     switch (pathnode->pathtype)
                                           +  +  + ]
                                434                 :                :     {
 8764 tgl@sss.pgh.pa.us         435                 :           5053 :         case T_IndexScan:
                                436                 :                :         case T_IndexOnlyScan:
                                437                 :                : 
                                438                 :                :             /*
                                439                 :                :              * Not all index types support mark/restore.
                                440                 :                :              */
 2195 rhodiumtoad@postgres      441                 :           5053 :             return castNode(IndexPath, pathnode)->indexinfo->amcanmarkpos;
                                442                 :                : 
 8764 tgl@sss.pgh.pa.us         443                 :UBC           0 :         case T_Material:
                                444                 :                :         case T_Sort:
                                445                 :              0 :             return true;
                                446                 :                : 
 4391                           447                 :              0 :         case T_CustomScan:
 1971                           448         [ #  # ]:              0 :             if (castNode(CustomPath, pathnode)->flags & CUSTOMPATH_SUPPORT_MARK_RESTORE)
                                449                 :              0 :                 return true;
                                450                 :              0 :             return false;
                                451                 :                : 
 7226                           452                 :              0 :         case T_Result:
                                453                 :                : 
                                454                 :                :             /*
                                455                 :                :              * Result supports mark/restore iff it has a child plan that does.
                                456                 :                :              *
                                457                 :                :              * We have to be careful here because there is more than one Path
                                458                 :                :              * type that can produce a Result plan node.
                                459                 :                :              */
 3918                           460         [ #  # ]:              0 :             if (IsA(pathnode, ProjectionPath))
                                461                 :              0 :                 return ExecSupportsMarkRestore(((ProjectionPath *) pathnode)->subpath);
                                462         [ #  # ]:              0 :             else if (IsA(pathnode, MinMaxAggPath))
                                463                 :              0 :                 return false;   /* childless Result */
 2861                           464         [ #  # ]:              0 :             else if (IsA(pathnode, GroupResultPath))
                                465                 :              0 :                 return false;   /* childless Result */
                                466                 :                :             else
                                467                 :                :             {
                                468                 :                :                 /* Simple RTE_RESULT base relation */
                                469         [ #  # ]:              0 :                 Assert(IsA(pathnode, Path));
 3918                           470                 :              0 :                 return false;   /* childless Result */
                                471                 :                :             }
                                472                 :                : 
 2805 tgl@sss.pgh.pa.us         473                 :CBC         484 :         case T_Append:
                                474                 :                :             {
                                475                 :            484 :                 AppendPath *appendPath = castNode(AppendPath, pathnode);
                                476                 :                : 
                                477                 :                :                 /*
                                478                 :                :                  * If there's exactly one child, then there will be no Append
                                479                 :                :                  * in the final plan, so we can handle mark/restore if the
                                480                 :                :                  * child plan node can.
                                481                 :                :                  */
                                482         [ -  + ]:            484 :                 if (list_length(appendPath->subpaths) == 1)
 2805 tgl@sss.pgh.pa.us         483                 :UBC           0 :                     return ExecSupportsMarkRestore((Path *) linitial(appendPath->subpaths));
                                484                 :                :                 /* Otherwise, Append can't handle it */
 2805 tgl@sss.pgh.pa.us         485                 :CBC         484 :                 return false;
                                486                 :                :             }
                                487                 :                : 
                                488                 :             26 :         case T_MergeAppend:
                                489                 :                :             {
                                490                 :             26 :                 MergeAppendPath *mapath = castNode(MergeAppendPath, pathnode);
                                491                 :                : 
                                492                 :                :                 /*
                                493                 :                :                  * Like the Append case above, single-subpath MergeAppends
                                494                 :                :                  * won't be in the final plan, so just return the child's
                                495                 :                :                  * mark/restore ability.
                                496                 :                :                  */
                                497         [ -  + ]:             26 :                 if (list_length(mapath->subpaths) == 1)
 2805 tgl@sss.pgh.pa.us         498                 :UBC           0 :                     return ExecSupportsMarkRestore((Path *) linitial(mapath->subpaths));
                                499                 :                :                 /* Otherwise, MergeAppend can't handle it */
 2805 tgl@sss.pgh.pa.us         500                 :CBC          26 :                 return false;
                                501                 :                :             }
                                502                 :                : 
 8764                           503                 :           1986 :         default:
                                504                 :           1986 :             break;
                                505                 :                :     }
                                506                 :                : 
                                507                 :           1986 :     return false;
                                508                 :                : }
                                509                 :                : 
                                510                 :                : /*
                                511                 :                :  * ExecSupportsBackwardScan - does a plan type support backwards scanning?
                                512                 :                :  *
                                513                 :                :  * Ideally, all plan types would support backwards scan, but that seems
                                514                 :                :  * unlikely to happen soon.  In some cases, a plan node passes the backwards
                                515                 :                :  * scan down to its children, and so supports backwards scan only if its
                                516                 :                :  * children do.  Therefore, this routine must be passed a complete plan tree.
                                517                 :                :  */
                                518                 :                : bool
 8664                           519                 :           3264 : ExecSupportsBackwardScan(Plan *node)
                                520                 :                : {
                                521         [ -  + ]:           3264 :     if (node == NULL)
 8664 tgl@sss.pgh.pa.us         522                 :UBC           0 :         return false;
                                523                 :                : 
                                524                 :                :     /*
                                525                 :                :      * Parallel-aware nodes return a subset of the tuples in each worker, and
                                526                 :                :      * in general we can't expect to have enough bookkeeping state to know
                                527                 :                :      * which ones we returned in this worker as opposed to some other worker.
                                528                 :                :      */
 4035 rhaas@postgresql.org      529         [ -  + ]:CBC        3264 :     if (node->parallel_aware)
 4035 rhaas@postgresql.org      530                 :UBC           0 :         return false;
                                531                 :                : 
 8664 tgl@sss.pgh.pa.us         532   [ +  +  +  -  :CBC        3264 :     switch (nodeTag(node))
                                     +  +  -  -  +  
                                           +  +  + ]
                                533                 :                :     {
                                534                 :             67 :         case T_Result:
                                535         [ -  + ]:             67 :             if (outerPlan(node) != NULL)
 3600 andres@anarazel.de        536                 :UBC           0 :                 return ExecSupportsBackwardScan(outerPlan(node));
                                537                 :                :             else
 8664 tgl@sss.pgh.pa.us         538                 :CBC          67 :                 return false;
                                539                 :                : 
                                540                 :             56 :         case T_Append:
                                541                 :                :             {
                                542                 :                :                 ListCell   *l;
                                543                 :                : 
                                544                 :                :                 /* With async, tuples may be interleaved, so can't back up. */
 2068 efujita@postgresql.o      545         [ -  + ]:             56 :                 if (((Append *) node)->nasyncplans > 0)
 2068 efujita@postgresql.o      546                 :UBC           0 :                     return false;
                                547                 :                : 
 8517 bruce@momjian.us          548   [ +  -  +  +  :CBC         166 :                 foreach(l, ((Append *) node)->appendplans)
                                              +  + ]
                                549                 :                :                 {
                                550         [ +  + ]:            119 :                     if (!ExecSupportsBackwardScan((Plan *) lfirst(l)))
                                551                 :              9 :                         return false;
                                552                 :                :                 }
                                553                 :                :                 /* need not check tlist because Append doesn't evaluate it */
                                554                 :             47 :                 return true;
                                555                 :                :             }
                                556                 :                : 
 4144 tgl@sss.pgh.pa.us         557                 :              5 :         case T_SampleScan:
                                558                 :                :             /* Simplify life for tablesample methods by disallowing this */
                                559                 :              5 :             return false;
                                560                 :                : 
 4077 rhaas@postgresql.org      561                 :UBC           0 :         case T_Gather:
                                562                 :              0 :             return false;
                                563                 :                : 
 6616 tgl@sss.pgh.pa.us         564                 :CBC         244 :         case T_IndexScan:
 3600 andres@anarazel.de        565                 :            244 :             return IndexSupportsBackwardScan(((IndexScan *) node)->indexid);
                                566                 :                : 
 5527 tgl@sss.pgh.pa.us         567                 :             27 :         case T_IndexOnlyScan:
 3600 andres@anarazel.de        568                 :             27 :             return IndexSupportsBackwardScan(((IndexOnlyScan *) node)->indexid);
                                569                 :                : 
 8664 tgl@sss.pgh.pa.us         570                 :UBC           0 :         case T_SubqueryScan:
 3600 andres@anarazel.de        571                 :              0 :             return ExecSupportsBackwardScan(((SubqueryScan *) node)->subplan);
                                572                 :                : 
 4404 rhaas@postgresql.org      573                 :              0 :         case T_CustomScan:
 1971 tgl@sss.pgh.pa.us         574         [ #  # ]:              0 :             if (((CustomScan *) node)->flags & CUSTOMPATH_SUPPORT_BACKWARD_SCAN)
                                575                 :              0 :                 return true;
 4404 rhaas@postgresql.org      576                 :              0 :             return false;
                                577                 :                : 
 3600 andres@anarazel.de        578                 :CBC        2362 :         case T_SeqScan:
                                579                 :                :         case T_TidScan:
                                580                 :                :         case T_TidRangeScan:
                                581                 :                :         case T_FunctionScan:
                                582                 :                :         case T_ValuesScan:
                                583                 :                :         case T_CteScan:
                                584                 :                :         case T_Material:
                                585                 :                :         case T_Sort:
                                586                 :                :             /* these don't evaluate tlist */
 8664 tgl@sss.pgh.pa.us         587                 :           2362 :             return true;
                                588                 :                : 
 2427 tomas.vondra@postgre      589                 :              2 :         case T_IncrementalSort:
                                590                 :                : 
                                591                 :                :             /*
                                592                 :                :              * Unlike full sort, incremental sort keeps only a single group of
                                593                 :                :              * tuples in memory, so it can't scan backwards.
                                594                 :                :              */
                                595                 :              2 :             return false;
                                596                 :                : 
 6256 tgl@sss.pgh.pa.us         597                 :             85 :         case T_LockRows:
                                598                 :                :         case T_Limit:
 8664                           599                 :             85 :             return ExecSupportsBackwardScan(outerPlan(node));
                                600                 :                : 
                                601                 :            416 :         default:
                                602                 :            416 :             return false;
                                603                 :                :     }
                                604                 :                : }
                                605                 :                : 
                                606                 :                : /*
                                607                 :                :  * An IndexScan or IndexOnlyScan node supports backward scan only if the
                                608                 :                :  * index's AM does.
                                609                 :                :  */
                                610                 :                : static bool
 6616                           611                 :            271 : IndexSupportsBackwardScan(Oid indexid)
                                612                 :                : {
                                613                 :                :     bool        result;
                                614                 :                :     HeapTuple   ht_idxrel;
                                615                 :                :     Form_pg_class idxrelrec;
                                616                 :                :     const IndexAmRoutine *amroutine;
                                617                 :                : 
                                618                 :                :     /* Fetch the pg_class tuple of the index relation */
 6131 rhaas@postgresql.org      619                 :            271 :     ht_idxrel = SearchSysCache1(RELOID, ObjectIdGetDatum(indexid));
 6616 tgl@sss.pgh.pa.us         620         [ -  + ]:            271 :     if (!HeapTupleIsValid(ht_idxrel))
 6616 tgl@sss.pgh.pa.us         621         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for relation %u", indexid);
 6616 tgl@sss.pgh.pa.us         622                 :CBC         271 :     idxrelrec = (Form_pg_class) GETSTRUCT(ht_idxrel);
                                623                 :                : 
                                624                 :                :     /* Fetch the index AM's API struct */
 3759                           625                 :            271 :     amroutine = GetIndexAmRoutineByAmId(idxrelrec->relam, false);
                                626                 :                : 
 3968                           627                 :            271 :     result = amroutine->amcanbackward;
                                628                 :                : 
 6616                           629                 :            271 :     ReleaseSysCache(ht_idxrel);
                                630                 :                : 
                                631                 :            271 :     return result;
                                632                 :                : }
                                633                 :                : 
                                634                 :                : /*
                                635                 :                :  * ExecMaterializesOutput - does a plan type materialize its output?
                                636                 :                :  *
                                637                 :                :  * Returns true if the plan node type is one that automatically materializes
                                638                 :                :  * its output (typically by keeping it in a tuplestore).  For such plans,
                                639                 :                :  * a rescan without any parameter change will have zero startup cost and
                                640                 :                :  * very low per-tuple cost.
                                641                 :                :  */
                                642                 :                : bool
 6286                           643                 :         539488 : ExecMaterializesOutput(NodeTag plantype)
                                644                 :                : {
                                645         [ +  + ]:         539488 :     switch (plantype)
                                646                 :                :     {
                                647                 :          17927 :         case T_Material:
                                648                 :                :         case T_FunctionScan:
                                649                 :                :         case T_TableFuncScan:
                                650                 :                :         case T_CteScan:
                                651                 :                :         case T_NamedTuplestoreScan:
                                652                 :                :         case T_WorkTableScan:
                                653                 :                :         case T_Sort:
                                654                 :          17927 :             return true;
                                655                 :                : 
                                656                 :         521561 :         default:
                                657                 :         521561 :             break;
                                658                 :                :     }
                                659                 :                : 
                                660                 :         521561 :     return false;
                                661                 :                : }
        

Generated by: LCOV version 2.0-1