LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeResult.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 80.3 % 61 49 12 49
Current Date: 2025-09-06 07:49:51 +0900 Functions: 66.7 % 6 4 2 4
Baseline: lcov-20250906-005545-baseline Branches: 62.5 % 32 20 12 20
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 80.3 % 61 49 12 49
Function coverage date bins:
(360..) days: 66.7 % 6 4 2 4
Branch coverage date bins:
(360..) days: 62.5 % 32 20 12 20

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nodeResult.c
                                  4                 :                :  *    support for constant nodes needing special code.
                                  5                 :                :  *
                                  6                 :                :  * DESCRIPTION
                                  7                 :                :  *
                                  8                 :                :  *      Result nodes are used in queries where no relations are scanned.
                                  9                 :                :  *      Examples of such queries are:
                                 10                 :                :  *
                                 11                 :                :  *              select 1 * 2
                                 12                 :                :  *
                                 13                 :                :  *              insert into emp values ('mike', 15000)
                                 14                 :                :  *
                                 15                 :                :  *      (Remember that in an INSERT or UPDATE, we need a plan tree that
                                 16                 :                :  *      generates the new rows.)
                                 17                 :                :  *
                                 18                 :                :  *      Result nodes are also used to optimise queries with constant
                                 19                 :                :  *      qualifications (ie, quals that do not depend on the scanned data),
                                 20                 :                :  *      such as:
                                 21                 :                :  *
                                 22                 :                :  *              select * from emp where 2 > 1
                                 23                 :                :  *
                                 24                 :                :  *      In this case, the plan generated is
                                 25                 :                :  *
                                 26                 :                :  *                      Result  (with 2 > 1 qual)
                                 27                 :                :  *                      /
                                 28                 :                :  *                 SeqScan (emp.*)
                                 29                 :                :  *
                                 30                 :                :  *      At runtime, the Result node evaluates the constant qual once,
                                 31                 :                :  *      which is shown by EXPLAIN as a One-Time Filter.  If it's
                                 32                 :                :  *      false, we can return an empty result set without running the
                                 33                 :                :  *      controlled plan at all.  If it's true, we run the controlled
                                 34                 :                :  *      plan normally and pass back the results.
                                 35                 :                :  *
                                 36                 :                :  *
                                 37                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 38                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 39                 :                :  *
                                 40                 :                :  * IDENTIFICATION
                                 41                 :                :  *    src/backend/executor/nodeResult.c
                                 42                 :                :  *
                                 43                 :                :  *-------------------------------------------------------------------------
                                 44                 :                :  */
                                 45                 :                : 
                                 46                 :                : #include "postgres.h"
                                 47                 :                : 
                                 48                 :                : #include "executor/executor.h"
                                 49                 :                : #include "executor/nodeResult.h"
                                 50                 :                : #include "miscadmin.h"
                                 51                 :                : 
                                 52                 :                : 
                                 53                 :                : /* ----------------------------------------------------------------
                                 54                 :                :  *      ExecResult(node)
                                 55                 :                :  *
                                 56                 :                :  *      returns the tuples from the outer plan which satisfy the
                                 57                 :                :  *      qualification clause.  Since result nodes with right
                                 58                 :                :  *      subtrees are never planned, we ignore the right subtree
                                 59                 :                :  *      entirely (for now).. -cim 10/7/89
                                 60                 :                :  *
                                 61                 :                :  *      The qualification containing only constant clauses are
                                 62                 :                :  *      checked first before any processing is done. It always returns
                                 63                 :                :  *      'nil' if the constant qualification is not satisfied.
                                 64                 :                :  * ----------------------------------------------------------------
                                 65                 :                :  */
                                 66                 :                : static TupleTableSlot *
 2973 andres@anarazel.de         67                 :CBC      555277 : ExecResult(PlanState *pstate)
                                 68                 :                : {
                                 69                 :         555277 :     ResultState *node = castNode(ResultState, pstate);
                                 70                 :                :     TupleTableSlot *outerTupleSlot;
                                 71                 :                :     PlanState  *outerPlan;
                                 72                 :                :     ExprContext *econtext;
                                 73                 :                : 
 2965                            74         [ +  + ]:         555277 :     CHECK_FOR_INTERRUPTS();
                                 75                 :                : 
 8311 tgl@sss.pgh.pa.us          76                 :         555275 :     econtext = node->ps.ps_ExprContext;
                                 77                 :                : 
                                 78                 :                :     /*
                                 79                 :                :      * check constant qualifications like (2 > 1), if not already done
                                 80                 :                :      */
                                 81         [ +  + ]:         555275 :     if (node->rs_checkqual)
                                 82                 :                :     {
 3098 andres@anarazel.de         83                 :           8417 :         bool        qualResult = ExecQual(node->resconstantqual, econtext);
                                 84                 :                : 
 8311 tgl@sss.pgh.pa.us          85                 :           8417 :         node->rs_checkqual = false;
                                 86         [ +  + ]:           8417 :         if (!qualResult)
                                 87                 :                :         {
                                 88                 :           5090 :             node->rs_done = true;
10226 bruce@momjian.us           89                 :           5090 :             return NULL;
                                 90                 :                :         }
                                 91                 :                :     }
                                 92                 :                : 
                                 93                 :                :     /*
                                 94                 :                :      * Reset per-tuple memory context to free any expression evaluation
                                 95                 :                :      * storage allocated in the previous tuple cycle.
                                 96                 :                :      */
 9144 tgl@sss.pgh.pa.us          97                 :         550185 :     ResetExprContext(econtext);
                                 98                 :                : 
                                 99                 :                :     /*
                                100                 :                :      * if rs_done is true then it means that we were asked to return a
                                101                 :                :      * constant tuple and we already did the last time ExecResult() was
                                102                 :                :      * called, OR that we failed the constant qual check. Either way, now we
                                103                 :                :      * are through.
                                104                 :                :      */
 1576 drowley@postgresql.o      105         [ +  + ]:         550185 :     if (!node->rs_done)
                                106                 :                :     {
 8311 tgl@sss.pgh.pa.us         107                 :         448350 :         outerPlan = outerPlanState(node);
                                108                 :                : 
10226 bruce@momjian.us          109         [ +  + ]:         448350 :         if (outerPlan != NULL)
                                110                 :                :         {
                                111                 :                :             /*
                                112                 :                :              * retrieve tuples from the outer plan until there are no more.
                                113                 :                :              */
 8311 tgl@sss.pgh.pa.us         114                 :         305714 :             outerTupleSlot = ExecProcNode(outerPlan);
                                115                 :                : 
10226 bruce@momjian.us          116   [ +  +  +  + ]:         305707 :             if (TupIsNull(outerTupleSlot))
                                117                 :           3980 :                 return NULL;
                                118                 :                : 
                                119                 :                :             /*
                                120                 :                :              * prepare to compute projection expressions, which will expect to
                                121                 :                :              * access the input tuples as varno OUTER.
                                122                 :                :              */
 9187 tgl@sss.pgh.pa.us         123                 :         301727 :             econtext->ecxt_outertuple = outerTupleSlot;
                                124                 :                :         }
                                125                 :                :         else
                                126                 :                :         {
                                127                 :                :             /*
                                128                 :                :              * if we don't have an outer plan, then we are just generating the
                                129                 :                :              * results from a constant target list.  Do it only once.
                                130                 :                :              */
 8311                           131                 :         142636 :             node->rs_done = true;
                                132                 :                :         }
                                133                 :                : 
                                134                 :                :         /* form the result tuple using ExecProject(), and return it */
 3152 andres@anarazel.de        135                 :         444363 :         return ExecProject(node->ps.ps_ProjInfo);
                                136                 :                :     }
                                137                 :                : 
10226 bruce@momjian.us          138                 :         101835 :     return NULL;
                                139                 :                : }
                                140                 :                : 
                                141                 :                : /* ----------------------------------------------------------------
                                142                 :                :  *      ExecResultMarkPos
                                143                 :                :  * ----------------------------------------------------------------
                                144                 :                :  */
                                145                 :                : void
 6778 tgl@sss.pgh.pa.us         146                 :UBC           0 : ExecResultMarkPos(ResultState *node)
                                147                 :                : {
                                148                 :              0 :     PlanState  *outerPlan = outerPlanState(node);
                                149                 :                : 
                                150         [ #  # ]:              0 :     if (outerPlan != NULL)
                                151                 :              0 :         ExecMarkPos(outerPlan);
                                152                 :                :     else
                                153         [ #  # ]:              0 :         elog(DEBUG2, "Result nodes do not support mark/restore");
                                154                 :              0 : }
                                155                 :                : 
                                156                 :                : /* ----------------------------------------------------------------
                                157                 :                :  *      ExecResultRestrPos
                                158                 :                :  * ----------------------------------------------------------------
                                159                 :                :  */
                                160                 :                : void
                                161                 :              0 : ExecResultRestrPos(ResultState *node)
                                162                 :                : {
                                163                 :              0 :     PlanState  *outerPlan = outerPlanState(node);
                                164                 :                : 
                                165         [ #  # ]:              0 :     if (outerPlan != NULL)
                                166                 :              0 :         ExecRestrPos(outerPlan);
                                167                 :                :     else
                                168         [ #  # ]:              0 :         elog(ERROR, "Result nodes do not support mark/restore");
                                169                 :              0 : }
                                170                 :                : 
                                171                 :                : /* ----------------------------------------------------------------
                                172                 :                :  *      ExecInitResult
                                173                 :                :  *
                                174                 :                :  *      Creates the run-time state information for the result node
                                175                 :                :  *      produced by the planner and initializes outer relations
                                176                 :                :  *      (child nodes).
                                177                 :                :  * ----------------------------------------------------------------
                                178                 :                :  */
                                179                 :                : ResultState *
 7130 tgl@sss.pgh.pa.us         180                 :CBC      136780 : ExecInitResult(Result *node, EState *estate, int eflags)
                                181                 :                : {
                                182                 :                :     ResultState *resstate;
                                183                 :                : 
                                184                 :                :     /* check for unsupported flags */
 6778                           185   [ -  +  -  - ]:         136780 :     Assert(!(eflags & (EXEC_FLAG_MARK | EXEC_FLAG_BACKWARD)) ||
                                186                 :                :            outerPlan(node) != NULL);
                                187                 :                : 
                                188                 :                :     /*
                                189                 :                :      * create state structure
                                190                 :                :      */
10226 bruce@momjian.us          191                 :         136780 :     resstate = makeNode(ResultState);
 8311 tgl@sss.pgh.pa.us         192                 :         136780 :     resstate->ps.plan = (Plan *) node;
                                193                 :         136780 :     resstate->ps.state = estate;
 2973 andres@anarazel.de        194                 :         136780 :     resstate->ps.ExecProcNode = ExecResult;
                                195                 :                : 
10067 vadim4o@yahoo.com         196                 :         136780 :     resstate->rs_done = false;
 1426 michael@paquier.xyz       197                 :         136780 :     resstate->rs_checkqual = (node->resconstantqual != NULL);
                                198                 :                : 
                                199                 :                :     /*
                                200                 :                :      * Miscellaneous initialization
                                201                 :                :      *
                                202                 :                :      * create expression context for node
                                203                 :                :      */
 8311 tgl@sss.pgh.pa.us         204                 :         136780 :     ExecAssignExprContext(estate, &resstate->ps);
                                205                 :                : 
                                206                 :                :     /*
                                207                 :                :      * initialize child nodes
                                208                 :                :      */
 7130                           209                 :         136780 :     outerPlanState(resstate) = ExecInitNode(outerPlan(node), estate, eflags);
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * we don't use inner plan
                                213                 :                :      */
10226 bruce@momjian.us          214         [ -  + ]:         136780 :     Assert(innerPlan(node) == NULL);
                                215                 :                : 
                                216                 :                :     /*
                                217                 :                :      * Initialize result slot, type and projection.
                                218                 :                :      */
 2487 andres@anarazel.de        219                 :         136780 :     ExecInitResultTupleSlotTL(&resstate->ps, &TTSOpsVirtual);
 6791 tgl@sss.pgh.pa.us         220                 :         136780 :     ExecAssignProjectionInfo(&resstate->ps, NULL);
                                221                 :                : 
                                222                 :                :     /*
                                223                 :                :      * initialize child expressions
                                224                 :                :      */
 2759 andres@anarazel.de        225                 :         136746 :     resstate->ps.qual =
                                226                 :         136746 :         ExecInitQual(node->plan.qual, (PlanState *) resstate);
                                227                 :         136746 :     resstate->resconstantqual =
                                228                 :         136746 :         ExecInitQual((List *) node->resconstantqual, (PlanState *) resstate);
                                229                 :                : 
 8311 tgl@sss.pgh.pa.us         230                 :         136746 :     return resstate;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : /* ----------------------------------------------------------------
                                234                 :                :  *      ExecEndResult
                                235                 :                :  *
                                236                 :                :  *      frees up storage allocated through C routines
                                237                 :                :  * ----------------------------------------------------------------
                                238                 :                :  */
                                239                 :                : void
                                240                 :         128230 : ExecEndResult(ResultState *node)
                                241                 :                : {
                                242                 :                :     /*
                                243                 :                :      * shut down subplans
                                244                 :                :      */
                                245                 :         128230 :     ExecEndNode(outerPlanState(node));
10651 scrappy@hub.org           246                 :         128230 : }
                                247                 :                : 
                                248                 :                : void
 5535 tgl@sss.pgh.pa.us         249                 :          20890 : ExecReScanResult(ResultState *node)
                                250                 :                : {
 1157                           251                 :          20890 :     PlanState  *outerPlan = outerPlanState(node);
                                252                 :                : 
 8311                           253                 :          20890 :     node->rs_done = false;
 1426 michael@paquier.xyz       254                 :          20890 :     node->rs_checkqual = (node->resconstantqual != NULL);
                                255                 :                : 
                                256                 :                :     /*
                                257                 :                :      * If chgParam of subnode is not null then plan will be re-scanned by
                                258                 :                :      * first ExecProcNode.
                                259                 :                :      */
 1157 tgl@sss.pgh.pa.us         260   [ +  +  +  + ]:          20890 :     if (outerPlan && outerPlan->chgParam == NULL)
                                261                 :             60 :         ExecReScan(outerPlan);
10067 vadim4o@yahoo.com         262                 :          20890 : }
        

Generated by: LCOV version 2.4-beta