LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeFunctionscan.c (source / functions) Coverage Total Hit UNC UBC GNC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 76.9 % 212 163 46 3 3 160
Current Date: 2026-11-28 16:48:44 +0900 Functions: 45.5 % 11 5 5 1 3 2
Baseline: lcov-20261128-baseline Branches: 74.2 % 120 89 20 11 2 87
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: 6.1 % 49 3 46 3
(30,360] days: 100.0 % 3 3 3
(360..) days: 98.1 % 160 157 3 157
Function coverage date bins:
(7,30] days: 0.0 % 5 0 5
(360..) days: 83.3 % 6 5 1 3 2
Branch coverage date bins:
(7,30] days: 9.1 % 22 2 20 2
(360..) days: 88.8 % 98 87 11 87

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nodeFunctionscan.c
                                  4                 :                :  *    Support routines for scanning RangeFunctions (functions in rangetable).
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/executor/nodeFunctionscan.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : /*
                                 16                 :                :  * INTERFACE ROUTINES
                                 17                 :                :  *      ExecFunctionScan        scans a function.
                                 18                 :                :  *      ExecFunctionNext        retrieve next tuple in sequential order.
                                 19                 :                :  *      ExecInitFunctionScan    creates and initializes a functionscan node.
                                 20                 :                :  *      ExecEndFunctionScan     releases any storage allocated.
                                 21                 :                :  *      ExecReScanFunctionScan  rescans the function
                                 22                 :                :  */
                                 23                 :                : #include "postgres.h"
                                 24                 :                : 
                                 25                 :                : #include "catalog/pg_type.h"
                                 26                 :                : #include "executor/nodeFunctionscan.h"
                                 27                 :                : #include "funcapi.h"
                                 28                 :                : #include "nodes/nodeFuncs.h"
                                 29                 :                : #include "utils/memutils.h"
                                 30                 :                : #include "utils/tuplestore.h"
                                 31                 :                : 
                                 32                 :                : static TupleTableSlot *FunctionNext(FunctionScanState *node);
                                 33                 :                : static void update_storage_stats(FunctionScanState *node);
                                 34                 :                : 
                                 35                 :                : 
                                 36                 :                : /* ----------------------------------------------------------------
                                 37                 :                :  *                      Scan Support
                                 38                 :                :  * ----------------------------------------------------------------
                                 39                 :                :  */
                                 40                 :                : /* ----------------------------------------------------------------
                                 41                 :                :  *      FunctionNext
                                 42                 :                :  *
                                 43                 :                :  *      This is a workhorse for ExecFunctionScan
                                 44                 :                :  * ----------------------------------------------------------------
                                 45                 :                :  */
                                 46                 :                : static TupleTableSlot *
 8759 tgl@sss.pgh.pa.us          47                 :CBC    12539746 : FunctionNext(FunctionScanState *node)
                                 48                 :                : {
                                 49                 :                :     EState     *estate;
                                 50                 :                :     ScanDirection direction;
                                 51                 :                :     TupleTableSlot *scanslot;
                                 52                 :                :     bool        alldone;
                                 53                 :                :     int64       oldpos;
                                 54                 :                :     int         funcno;
                                 55                 :                :     int         att;
                                 56                 :                : 
                                 57                 :                :     /*
                                 58                 :                :      * get information from the estate and scan state
                                 59                 :                :      */
                                 60                 :       12539746 :     estate = node->ss.ps.state;
 8966                            61                 :       12539746 :     direction = estate->es_direction;
 4755                            62                 :       12539746 :     scanslot = node->ss.ss_ScanTupleSlot;
                                 63                 :                : 
                                 64         [ +  + ]:       12539746 :     if (node->simple)
                                 65                 :                :     {
                                 66                 :                :         /*
                                 67                 :                :          * Fast path for the trivial case: the function return type and scan
                                 68                 :                :          * result type are the same, so we fetch the function result straight
                                 69                 :                :          * into the scan result slot. No need to update ordinality or
                                 70                 :                :          * rowcounts either.
                                 71                 :                :          */
                                 72                 :       12514655 :         Tuplestorestate *tstore = node->funcstates[0].tstore;
                                 73                 :                : 
                                 74                 :                :         /*
                                 75                 :                :          * If first time through, read all tuples from function and put them
                                 76                 :                :          * in a tuplestore. Subsequent calls just fetch tuples from
                                 77                 :                :          * tuplestore.
                                 78                 :                :          */
                                 79         [ +  + ]:       12514655 :         if (tstore == NULL)
                                 80                 :                :         {
                                 81                 :         103873 :             node->funcstates[0].tstore = tstore =
 3546 andres@anarazel.de         82                 :         106703 :                 ExecMakeTableFunctionResult(node->funcstates[0].setexpr,
                                 83                 :                :                                             node->ss.ps.ps_ExprContext,
                                 84                 :                :                                             node->argcontext,
 4755 tgl@sss.pgh.pa.us          85                 :         106703 :                                             node->funcstates[0].tupdesc,
 3447                            86                 :         106703 :                                             node->eflags & EXEC_FLAG_BACKWARD);
                                 87                 :                : 
                                 88                 :                :             /*
                                 89                 :                :              * paranoia - cope if the function, which may have constructed the
                                 90                 :                :              * tuplestore itself, didn't leave it pointing at the start. This
                                 91                 :                :              * call is fast, so the overhead shouldn't be an issue.
                                 92                 :                :              */
 4755                            93                 :         103873 :             tuplestore_rescan(tstore);
                                 94                 :                :         }
                                 95                 :                : 
                                 96                 :                :         /*
                                 97                 :                :          * Get the next tuple from tuplestore.
                                 98                 :                :          */
                                 99                 :       12511825 :         (void) tuplestore_gettupleslot(tstore,
                                100                 :                :                                        ScanDirectionIsForward(direction),
                                101                 :                :                                        false,
                                102                 :                :                                        scanslot);
                                103                 :       12511825 :         return scanslot;
                                104                 :                :     }
                                105                 :                : 
                                106                 :                :     /*
                                107                 :                :      * Increment or decrement ordinal counter before checking for end-of-data,
                                108                 :                :      * so that we can move off either end of the result by 1 (and no more than
                                109                 :                :      * 1) without losing correct count.  See PortalRunSelect for why we can
                                110                 :                :      * assume that we won't be called repeatedly in the end-of-data state.
                                111                 :                :      */
                                112                 :          25091 :     oldpos = node->ordinal;
 4870 stark@mit.edu             113         [ +  + ]:          25091 :     if (ScanDirectionIsForward(direction))
                                114                 :          25051 :         node->ordinal++;
                                115                 :                :     else
                                116                 :             40 :         node->ordinal--;
                                117                 :                : 
                                118                 :                :     /*
                                119                 :                :      * Main loop over functions.
                                120                 :                :      *
                                121                 :                :      * We fetch the function results into func_slots (which match the function
                                122                 :                :      * return types), and then copy the values to scanslot (which matches the
                                123                 :                :      * scan result type), setting the ordinal column (if any) as well.
                                124                 :                :      */
 4755 tgl@sss.pgh.pa.us         125                 :          25091 :     ExecClearTuple(scanslot);
                                126                 :          25091 :     att = 0;
                                127                 :          25091 :     alldone = true;
                                128         [ +  + ]:          66926 :     for (funcno = 0; funcno < node->nfuncs; funcno++)
                                129                 :                :     {
                                130                 :          41835 :         FunctionScanPerFuncState *fs = &node->funcstates[funcno];
                                131                 :                :         int         i;
                                132                 :                : 
                                133                 :                :         /*
                                134                 :                :          * If first time through, read all tuples from function and put them
                                135                 :                :          * in a tuplestore. Subsequent calls just fetch tuples from
                                136                 :                :          * tuplestore.
                                137                 :                :          */
                                138         [ +  + ]:          41835 :         if (fs->tstore == NULL)
                                139                 :                :         {
                                140                 :           1342 :             fs->tstore =
 3546 andres@anarazel.de        141                 :           1342 :                 ExecMakeTableFunctionResult(fs->setexpr,
                                142                 :                :                                             node->ss.ps.ps_ExprContext,
                                143                 :                :                                             node->argcontext,
                                144                 :                :                                             fs->tupdesc,
 3447 tgl@sss.pgh.pa.us         145                 :           1342 :                                             node->eflags & EXEC_FLAG_BACKWARD);
                                146                 :                : 
                                147                 :                :             /*
                                148                 :                :              * paranoia - cope if the function, which may have constructed the
                                149                 :                :              * tuplestore itself, didn't leave it pointing at the start. This
                                150                 :                :              * call is fast, so the overhead shouldn't be an issue.
                                151                 :                :              */
 4755                           152                 :           1342 :             tuplestore_rescan(fs->tstore);
                                153                 :                :         }
                                154                 :                : 
                                155                 :                :         /*
                                156                 :                :          * Get the next tuple from tuplestore.
                                157                 :                :          *
                                158                 :                :          * If we have a rowcount for the function, and we know the previous
                                159                 :                :          * read position was out of bounds, don't try the read. This allows
                                160                 :                :          * backward scan to work when there are mixed row counts present.
                                161                 :                :          */
                                162   [ +  +  +  + ]:          41835 :         if (fs->rowcount != -1 && fs->rowcount < oldpos)
                                163                 :             48 :             ExecClearTuple(fs->func_slot);
                                164                 :                :         else
                                165                 :          41787 :             (void) tuplestore_gettupleslot(fs->tstore,
                                166                 :                :                                            ScanDirectionIsForward(direction),
                                167                 :                :                                            false,
                                168                 :                :                                            fs->func_slot);
                                169                 :                : 
                                170   [ +  -  +  + ]:          41835 :         if (TupIsNull(fs->func_slot))
                                171                 :                :         {
                                172                 :                :             /*
                                173                 :                :              * If we ran out of data for this function in the forward
                                174                 :                :              * direction then we now know how many rows it returned. We need
                                175                 :                :              * to know this in order to handle backwards scans. The row count
                                176                 :                :              * we store is actually 1+ the actual number, because we have to
                                177                 :                :              * position the tuplestore 1 off its end sometimes.
                                178                 :                :              */
                                179   [ +  +  +  + ]:           1574 :             if (ScanDirectionIsForward(direction) && fs->rowcount == -1)
                                180                 :           1326 :                 fs->rowcount = node->ordinal;
                                181                 :                : 
                                182                 :                :             /*
                                183                 :                :              * populate the result cols with nulls
                                184                 :                :              */
                                185         [ +  + ]:           3806 :             for (i = 0; i < fs->colcount; i++)
                                186                 :                :             {
                                187                 :           2232 :                 scanslot->tts_values[att] = (Datum) 0;
                                188                 :           2232 :                 scanslot->tts_isnull[att] = true;
                                189                 :           2232 :                 att++;
                                190                 :                :             }
                                191                 :                :         }
                                192                 :                :         else
                                193                 :                :         {
                                194                 :                :             /*
                                195                 :                :              * we have a result, so just copy it to the result cols.
                                196                 :                :              */
                                197                 :          40261 :             slot_getallattrs(fs->func_slot);
                                198                 :                : 
                                199         [ +  + ]:          89620 :             for (i = 0; i < fs->colcount; i++)
                                200                 :                :             {
                                201                 :          49359 :                 scanslot->tts_values[att] = fs->func_slot->tts_values[i];
                                202                 :          49359 :                 scanslot->tts_isnull[att] = fs->func_slot->tts_isnull[i];
                                203                 :          49359 :                 att++;
                                204                 :                :             }
                                205                 :                : 
                                206                 :                :             /*
                                207                 :                :              * We're not done until every function result is exhausted; we pad
                                208                 :                :              * the shorter results with nulls until then.
                                209                 :                :              */
                                210                 :          40261 :             alldone = false;
                                211                 :                :         }
                                212                 :                :     }
                                213                 :                : 
                                214                 :                :     /*
                                215                 :                :      * ordinal col is always last, per spec.
                                216                 :                :      */
                                217         [ +  + ]:          25091 :     if (node->ordinality)
                                218                 :                :     {
                                219                 :          11860 :         scanslot->tts_values[att] = Int64GetDatumFast(node->ordinal);
                                220                 :          11860 :         scanslot->tts_isnull[att] = false;
                                221                 :                :     }
                                222                 :                : 
                                223                 :                :     /*
                                224                 :                :      * If alldone, we just return the previously-cleared scanslot.  Otherwise,
                                225                 :                :      * finish creating the virtual tuple.
                                226                 :                :      */
                                227         [ +  + ]:          25091 :     if (!alldone)
 4870 stark@mit.edu             228                 :          23951 :         ExecStoreVirtualTuple(scanslot);
                                229                 :                : 
                                230                 :          25091 :     return scanslot;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : /*
                                234                 :                :  * FunctionRecheck -- access method routine to recheck a tuple in EvalPlanQual
                                235                 :                :  */
                                236                 :                : static bool
 6242 tgl@sss.pgh.pa.us         237                 :UBC           0 : FunctionRecheck(FunctionScanState *node, TupleTableSlot *slot)
                                238                 :                : {
                                239                 :                :     /* nothing to check */
                                240                 :              0 :     return true;
                                241                 :                : }
                                242                 :                : 
                                243                 :                : /* ----------------------------------------------------------------
                                244                 :                :  *      ExecFunctionScan(node)
                                245                 :                :  *
                                246                 :                :  *      Scans the function sequentially and returns the next qualifying
                                247                 :                :  *      tuple.
                                248                 :                :  *      We call the ExecScan() routine and pass it the appropriate
                                249                 :                :  *      access method functions.
                                250                 :                :  * ----------------------------------------------------------------
                                251                 :                :  */
                                252                 :                : static TupleTableSlot *
 3421 andres@anarazel.de        253                 :CBC    10414025 : ExecFunctionScan(PlanState *pstate)
                                254                 :                : {
                                255                 :       10414025 :     FunctionScanState *node = castNode(FunctionScanState, pstate);
                                256                 :                : 
 6242 tgl@sss.pgh.pa.us         257                 :       10414025 :     return ExecScan(&node->ss,
                                258                 :                :                     (ExecScanAccessMtd) FunctionNext,
                                259                 :                :                     (ExecScanRecheckMtd) FunctionRecheck);
                                260                 :                : }
                                261                 :                : 
                                262                 :                : /* ----------------------------------------------------------------
                                263                 :                :  *      ExecInitFunctionScan
                                264                 :                :  * ----------------------------------------------------------------
                                265                 :                :  */
                                266                 :                : FunctionScanState *
 7578                           267                 :          49036 : ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
                                268                 :                : {
                                269                 :                :     FunctionScanState *scanstate;
 4755                           270                 :          49036 :     int         nfuncs = list_length(node->functions);
                                271                 :                :     TupleDesc   scan_tupdesc;
                                272                 :                :     int         i,
                                273                 :                :                 natts;
                                274                 :                :     ListCell   *lc;
                                275                 :                : 
                                276                 :                :     /* check for unsupported flags */
 6632                           277         [ -  + ]:          49036 :     Assert(!(eflags & EXEC_FLAG_MARK));
                                278                 :                : 
                                279                 :                :     /*
                                280                 :                :      * FunctionScan should not have any children.
                                281                 :                :      */
 8759                           282         [ -  + ]:          49036 :     Assert(outerPlan(node) == NULL);
                                283         [ -  + ]:          49036 :     Assert(innerPlan(node) == NULL);
                                284                 :                : 
                                285                 :                :     /*
                                286                 :                :      * create new ScanState for node
                                287                 :                :      */
 8966                           288                 :          49036 :     scanstate = makeNode(FunctionScanState);
 8759                           289                 :          49036 :     scanstate->ss.ps.plan = (Plan *) node;
                                290                 :          49036 :     scanstate->ss.ps.state = estate;
 3421 andres@anarazel.de        291                 :          49036 :     scanstate->ss.ps.ExecProcNode = ExecFunctionScan;
 6604 tgl@sss.pgh.pa.us         292                 :          49036 :     scanstate->eflags = eflags;
                                293                 :                : 
                                294                 :                :     /*
                                295                 :                :      * are we adding an ordinality column?
                                296                 :                :      */
 4755                           297                 :          49036 :     scanstate->ordinality = node->funcordinality;
                                298                 :                : 
                                299                 :          49036 :     scanstate->nfuncs = nfuncs;
                                300   [ +  +  +  + ]:          49036 :     if (nfuncs == 1 && !node->funcordinality)
                                301                 :          48253 :         scanstate->simple = true;
                                302                 :                :     else
                                303                 :            783 :         scanstate->simple = false;
                                304                 :                : 
                                305                 :                :     /*
                                306                 :                :      * Ordinal 0 represents the "before the first row" position.
                                307                 :                :      *
                                308                 :                :      * We need to track ordinal position even when not adding an ordinality
                                309                 :                :      * column to the result, in order to handle backwards scanning properly
                                310                 :                :      * with multiple functions with different result sizes. (We can't position
                                311                 :                :      * any individual function's tuplestore any more than 1 place beyond its
                                312                 :                :      * end, so when scanning backwards, we need to know when to start
                                313                 :                :      * including the function in the scan again.)
                                314                 :                :      */
                                315                 :          49036 :     scanstate->ordinal = 0;
                                316                 :                : 
                                317                 :                :     /*
                                318                 :                :      * Miscellaneous initialization
                                319                 :                :      *
                                320                 :                :      * create expression context for node
                                321                 :                :      */
 8759                           322                 :          49036 :     ExecAssignExprContext(estate, &scanstate->ss.ps);
                                323                 :                : 
  353 michael@paquier.xyz       324                 :          49036 :     scanstate->funcstates = palloc_array(FunctionScanPerFuncState, nfuncs);
                                325                 :                : 
 4755 tgl@sss.pgh.pa.us         326                 :          49036 :     natts = 0;
                                327                 :          49036 :     i = 0;
                                328   [ +  -  +  +  :          98287 :     foreach(lc, node->functions)
                                              +  + ]
                                329                 :                :     {
                                330                 :          49255 :         RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                                331                 :          49255 :         Node       *funcexpr = rtfunc->funcexpr;
                                332                 :          49255 :         int         colcount = rtfunc->funccolcount;
                                333                 :          49255 :         FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
                                334                 :                :         TupleDesc   tupdesc;
                                335                 :                : 
 3546 andres@anarazel.de        336                 :          49251 :         fs->setexpr =
                                337                 :          49255 :             ExecInitTableFunctionResult((Expr *) funcexpr,
                                338                 :                :                                         scanstate->ss.ps.ps_ExprContext,
                                339                 :                :                                         &scanstate->ss.ps);
                                340                 :                : 
                                341                 :                :         /*
                                342                 :                :          * Don't allocate the tuplestores; the actual calls to the functions
                                343                 :                :          * do that.  NULL means that we have not called the function yet (or
                                344                 :                :          * need to call it again after a rescan).
                                345                 :                :          */
 4755 tgl@sss.pgh.pa.us         346                 :          49251 :         fs->tstore = NULL;
                                347                 :          49251 :         fs->rowcount = -1;
                                348                 :                : 
                                349                 :                :         /*
                                350                 :                :          * Now build a tupdesc showing the result type we expect from the
                                351                 :                :          * function.  If we have a coldeflist then that takes priority (note
                                352                 :                :          * the parser enforces that there is one if the function's nominal
                                353                 :                :          * output type is RECORD).  Otherwise use get_expr_result_type.
                                354                 :                :          *
                                355                 :                :          * Note that if the function returns a named composite type, that may
                                356                 :                :          * now contain more or different columns than it did when the plan was
                                357                 :                :          * made.  For both that and the RECORD case, we need to check tuple
                                358                 :                :          * compatibility.  ExecMakeTableFunctionResult handles some of this,
                                359                 :                :          * and CheckVarSlotCompatibility provides a backstop.
                                360                 :                :          */
  997                           361         [ +  + ]:          49251 :         if (rtfunc->funccolnames != NIL)
                                362                 :                :         {
 4755                           363                 :            980 :             tupdesc = BuildDescFromLists(rtfunc->funccolnames,
                                364                 :            490 :                                          rtfunc->funccoltypes,
                                365                 :            490 :                                          rtfunc->funccoltypmods,
                                366                 :            490 :                                          rtfunc->funccolcollations);
                                367                 :                : 
                                368                 :                :             /*
                                369                 :                :              * For RECORD results, make sure a typmod has been assigned.  (The
                                370                 :                :              * function should do this for itself, but let's cover things in
                                371                 :                :              * case it doesn't.)
                                372                 :                :              */
                                373                 :            490 :             BlessTupleDesc(tupdesc);
                                374                 :                :         }
                                375                 :                :         else
                                376                 :                :         {
                                377                 :                :             TypeFuncClass functypclass;
                                378                 :                :             Oid         funcrettype;
                                379                 :                : 
  997                           380                 :          48761 :             functypclass = get_expr_result_type(funcexpr,
                                381                 :                :                                                 &funcrettype,
                                382                 :                :                                                 &tupdesc);
                                383                 :                : 
                                384   [ +  +  +  + ]:          48761 :             if (functypclass == TYPEFUNC_COMPOSITE ||
                                385                 :                :                 functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
                                386                 :                :             {
                                387                 :                :                 /* Composite data type, e.g. a table's row type */
                                388         [ -  + ]:          25075 :                 Assert(tupdesc);
                                389                 :                :                 /* Must copy it out of typcache for safety */
                                390                 :          25075 :                 tupdesc = CreateTupleDescCopy(tupdesc);
                                391                 :                :             }
                                392         [ +  - ]:          23686 :             else if (functypclass == TYPEFUNC_SCALAR)
                                393                 :                :             {
                                394                 :                :                 /* Base data type, i.e. scalar */
                                395                 :          23686 :                 tupdesc = CreateTemplateTupleDesc(1);
                                396                 :          23686 :                 TupleDescInitEntry(tupdesc,
                                397                 :                :                                    (AttrNumber) 1,
                                398                 :                :                                    NULL,    /* don't care about the name here */
                                399                 :                :                                    funcrettype,
                                400                 :                :                                    -1,
                                401                 :                :                                    0);
                                402                 :          23686 :                 TupleDescInitEntryCollation(tupdesc,
                                403                 :                :                                             (AttrNumber) 1,
                                404                 :                :                                             exprCollation(funcexpr));
  257 drowley@postgresql.o      405                 :          23686 :                 TupleDescFinalize(tupdesc);
                                406                 :                :             }
                                407                 :                :             else
                                408                 :                :             {
                                409                 :                :                 /* crummy error message, but parser should have caught this */
  997 tgl@sss.pgh.pa.us         410         [ #  # ]:UBC           0 :                 elog(ERROR, "function in FROM has unsupported return type");
                                411                 :                :             }
                                412                 :                :         }
                                413                 :                : 
 4755 tgl@sss.pgh.pa.us         414                 :CBC       49251 :         fs->tupdesc = tupdesc;
                                415                 :          49251 :         fs->colcount = colcount;
                                416                 :                : 
                                417                 :                :         /*
                                418                 :                :          * We only need separate slots for the function results if we are
                                419                 :                :          * doing ordinality or multiple functions; otherwise, we'll fetch
                                420                 :                :          * function results directly into the scan slot.
                                421                 :                :          */
                                422         [ +  + ]:          49251 :         if (!scanstate->simple)
                                423                 :                :         {
 2935 andres@anarazel.de        424                 :           1002 :             fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc,
                                425                 :                :                                                    &TTSOpsMinimalTuple);
                                426                 :                :         }
                                427                 :                :         else
 4755 tgl@sss.pgh.pa.us         428                 :          48249 :             fs->func_slot = NULL;
                                429                 :                : 
                                430                 :          49251 :         natts += colcount;
                                431                 :          49251 :         i++;
                                432                 :                :     }
                                433                 :                : 
                                434                 :                :     /*
                                435                 :                :      * Create the combined TupleDesc
                                436                 :                :      *
                                437                 :                :      * If there is just one function without ordinality, the scan result
                                438                 :                :      * tupdesc is the same as the function result tupdesc --- except that we
                                439                 :                :      * may stuff new names into it below, so drop any rowtype label.
                                440                 :                :      */
                                441         [ +  + ]:          49032 :     if (scanstate->simple)
                                442                 :                :     {
                                443                 :          48249 :         scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
                                444                 :          48249 :         scan_tupdesc->tdtypeid = RECORDOID;
                                445                 :          48249 :         scan_tupdesc->tdtypmod = -1;
                                446                 :                :     }
                                447                 :                :     else
                                448                 :                :     {
                                449                 :            783 :         AttrNumber  attno = 0;
                                450                 :                : 
                                451         [ +  + ]:            783 :         if (node->funcordinality)
                                452                 :            724 :             natts++;
                                453                 :                : 
 2930 andres@anarazel.de        454                 :            783 :         scan_tupdesc = CreateTemplateTupleDesc(natts);
                                455                 :                : 
 4755 tgl@sss.pgh.pa.us         456         [ +  + ]:           1785 :         for (i = 0; i < nfuncs; i++)
                                457                 :                :         {
                                458                 :           1002 :             TupleDesc   tupdesc = scanstate->funcstates[i].tupdesc;
                                459                 :           1002 :             int         colcount = scanstate->funcstates[i].colcount;
                                460                 :                :             int         j;
                                461                 :                : 
                                462         [ +  + ]:           2430 :             for (j = 1; j <= colcount; j++)
                                463                 :           1428 :                 TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
                                464                 :                :         }
                                465                 :                : 
                                466                 :                :         /* If doing ordinality, add a column of type "bigint" at the end */
                                467         [ +  + ]:            783 :         if (node->funcordinality)
                                468                 :                :         {
                                469                 :            724 :             TupleDescInitEntry(scan_tupdesc,
                                470                 :                :                                ++attno,
                                471                 :                :                                NULL,    /* don't care about the name here */
                                472                 :                :                                INT8OID,
                                473                 :                :                                -1,
                                474                 :                :                                0);
                                475                 :                :         }
                                476                 :                : 
  257 drowley@postgresql.o      477                 :            783 :         TupleDescFinalize(scan_tupdesc);
 4755 tgl@sss.pgh.pa.us         478         [ -  + ]:            783 :         Assert(attno == natts);
                                479                 :                :     }
                                480                 :                : 
                                481                 :                :     /*
                                482                 :                :      * Initialize scan slot and type.
                                483                 :                :      */
 2935 andres@anarazel.de        484                 :          49032 :     ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
                                485                 :                :                           &TTSOpsMinimalTuple, 0);
                                486                 :                : 
                                487                 :                :     /*
                                488                 :                :      * Initialize result slot, type and projection.
                                489                 :                :      */
 2941                           490                 :          49032 :     ExecInitResultTypeTL(&scanstate->ss.ps);
 7860 tgl@sss.pgh.pa.us         491                 :          49032 :     ExecAssignScanProjectionInfo(&scanstate->ss);
                                492                 :                : 
                                493                 :                :     /*
                                494                 :                :      * initialize child expressions
                                495                 :                :      */
 3207 andres@anarazel.de        496                 :          49032 :     scanstate->ss.ps.qual =
                                497                 :          49032 :         ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
                                498                 :                : 
                                499                 :                :     /*
                                500                 :                :      * Create a memory context that ExecMakeTableFunctionResult can use to
                                501                 :                :      * evaluate function arguments in.  We can't use the per-tuple context for
                                502                 :                :      * this because it gets reset too often; but we don't want to leak
                                503                 :                :      * evaluation results into the query-lifespan context either.  We just
                                504                 :                :      * need one context, because we evaluate each function separately.
                                505                 :                :      */
 4545 tgl@sss.pgh.pa.us         506                 :          49032 :     scanstate->argcontext = AllocSetContextCreate(CurrentMemoryContext,
                                507                 :                :                                                   "Table function arguments",
                                508                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                509                 :                : 
   18 drowley@postgresql.o      510                 :GNC       49032 :     scanstate->shared_info = NULL;
                                511                 :                : 
 8759 tgl@sss.pgh.pa.us         512                 :CBC       49032 :     return scanstate;
                                513                 :                : }
                                514                 :                : 
                                515                 :                : /* ----------------------------------------------------------------
                                516                 :                :  *      ExecEndFunctionScan
                                517                 :                :  *
                                518                 :                :  *      frees any storage allocated through C routines.
                                519                 :                :  * ----------------------------------------------------------------
                                520                 :                :  */
                                521                 :                : void
                                522                 :          46052 : ExecEndFunctionScan(FunctionScanState *node)
                                523                 :                : {
                                524                 :                :     int         i;
                                525                 :                : 
                                526                 :                :     /*
                                527                 :                :      * When ending a parallel worker, copy the tuplestore usage details into
                                528                 :                :      * shared memory so that they can be picked up by the main process to
                                529                 :                :      * report in EXPLAIN ANALYZE.  The Gather node above us may be rescanned,
                                530                 :                :      * in which case a new set of workers will write to the same slots, so
                                531                 :                :      * merge with any existing values rather than overwriting them.
                                532                 :                :      */
   18 drowley@postgresql.o      533   [ -  +  -  - ]:GNC       46052 :     if (node->shared_info != NULL && IsParallelWorker())
                                534                 :                :     {
                                535                 :                :         FunctionScanInstrumentation *si;
                                536                 :                : 
                                537                 :                :         /* fetch storage info from tuplestores and store in node->stats */
   18 drowley@postgresql.o      538                 :UNC           0 :         update_storage_stats(node);
                                539                 :                : 
                                540         [ #  # ]:              0 :         Assert(ParallelWorkerNumber < node->shared_info->num_workers);
                                541                 :              0 :         si = &node->shared_info->sinstrument[ParallelWorkerNumber];
                                542                 :              0 :         si->maxSpaceUsed = Max(si->maxSpaceUsed, node->stats.maxSpaceUsed);
                                543                 :              0 :         si->usedDisk |= node->stats.usedDisk;
                                544                 :                :     }
                                545                 :                : 
                                546                 :                :     /*
                                547                 :                :      * Release slots and tuplestore resources
                                548                 :                :      */
 4755 tgl@sss.pgh.pa.us         549         [ +  + ]:CBC       92315 :     for (i = 0; i < node->nfuncs; i++)
                                550                 :                :     {
                                551                 :          46263 :         FunctionScanPerFuncState *fs = &node->funcstates[i];
                                552                 :                : 
                                553         [ +  + ]:          46263 :         if (fs->tstore != NULL)
                                554                 :                :         {
                                555                 :          41789 :             tuplestore_end(node->funcstates[i].tstore);
                                556                 :          41789 :             fs->tstore = NULL;
                                557                 :                :         }
                                558                 :                :     }
 8966                           559                 :          46052 : }
                                560                 :                : 
                                561                 :                : /*
                                562                 :                :  * update_storage_stats
                                563                 :                :  *      Update the storage statistics for EXPLAIN ANALYZE with the storage
                                564                 :                :  *      space used by each function's tuplestore.
                                565                 :                :  *
                                566                 :                :  * We expect this to be called before starting new scans so that the
                                567                 :                :  * maxSpaceUsed and usedDisk fields are not forgotten between rescans.  We
                                568                 :                :  * calculate the total space used by all tuplestores at the end of each scan
                                569                 :                :  * and store that if it's higher than any previous scan's.  "usedDisk" is
                                570                 :                :  * marked if any tuplestore spilled during any scan.
                                571                 :                :  */
                                572                 :                : static void
   18 drowley@postgresql.o      573                 :UNC           0 : update_storage_stats(FunctionScanState *node)
                                574                 :                : {
                                575                 :              0 :     int64       totalSpaceUsed = 0;
                                576                 :                : 
                                577                 :                :     /* Calculate the sum of the space used by each function's tuplestore. */
                                578         [ #  # ]:              0 :     for (int i = 0; i < node->nfuncs; i++)
                                579                 :                :     {
                                580                 :              0 :         FunctionScanPerFuncState *fs = &node->funcstates[i];
                                581                 :                :         bool        usedDisk;
                                582                 :                :         int64       spaceUsed;
                                583                 :                : 
                                584         [ #  # ]:              0 :         if (fs->tstore == NULL)
                                585                 :              0 :             continue;
                                586                 :                : 
                                587                 :              0 :         tuplestore_get_stats(fs->tstore, &usedDisk, &spaceUsed);
                                588                 :              0 :         totalSpaceUsed += spaceUsed;
                                589                 :              0 :         node->stats.usedDisk |= usedDisk;
                                590                 :                :     }
                                591                 :                : 
                                592                 :                :     /* Record this one if it beats the previous best. */
                                593                 :              0 :     node->stats.maxSpaceUsed = Max(node->stats.maxSpaceUsed, totalSpaceUsed);
                                594                 :              0 : }
                                595                 :                : 
                                596                 :                : /* ----------------------------------------------------------------
                                597                 :                :  *      ExecReScanFunctionScan
                                598                 :                :  *
                                599                 :                :  *      Rescans the relation.
                                600                 :                :  * ----------------------------------------------------------------
                                601                 :                :  */
                                602                 :                : void
 5983 tgl@sss.pgh.pa.us         603                 :CBC       74014 : ExecReScanFunctionScan(FunctionScanState *node)
                                604                 :                : {
 4755                           605                 :          74014 :     FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
                                606                 :                :     int         i;
                                607                 :          74014 :     Bitmapset  *chgparam = node->ss.ps.chgParam;
                                608                 :                : 
 2941 andres@anarazel.de        609         [ +  + ]:          74014 :     if (node->ss.ps.ps_ResultTupleSlot)
                                610                 :          32112 :         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
 4755 tgl@sss.pgh.pa.us         611         [ +  + ]:         148101 :     for (i = 0; i < node->nfuncs; i++)
                                612                 :                :     {
                                613                 :          74087 :         FunctionScanPerFuncState *fs = &node->funcstates[i];
                                614                 :                : 
                                615         [ +  + ]:          74087 :         if (fs->func_slot)
                                616                 :            734 :             ExecClearTuple(fs->func_slot);
                                617                 :                :     }
                                618                 :                : 
                                619                 :          74014 :     ExecScanReScan(&node->ss);
                                620                 :                : 
                                621                 :                :     /*
                                622                 :                :      * Here we have a choice whether to drop the tuplestores (and recompute
                                623                 :                :      * the function outputs) or just rescan them.  We must recompute if an
                                624                 :                :      * expression contains changed parameters, else we rescan.
                                625                 :                :      *
                                626                 :                :      * XXX maybe we should recompute if the function is volatile?  But in
                                627                 :                :      * general the executor doesn't conditionalize its actions on that.
                                628                 :                :      */
                                629         [ +  + ]:          74014 :     if (chgparam)
                                630                 :                :     {
                                631                 :                :         ListCell   *lc;
                                632                 :                : 
                                633                 :                :         /* Track tuplestore storage stats, if needed */
   18 drowley@postgresql.o      634         [ -  + ]:GNC       72318 :         if (node->ss.ps.instrument)
   18 drowley@postgresql.o      635                 :UNC           0 :             update_storage_stats(node);
                                636                 :                : 
 4755 tgl@sss.pgh.pa.us         637                 :CBC       72318 :         i = 0;
                                638   [ +  -  +  +  :         144693 :         foreach(lc, scan->functions)
                                              +  + ]
                                639                 :                :         {
                                640                 :          72375 :             RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                                641                 :                : 
                                642         [ +  + ]:          72375 :             if (bms_overlap(chgparam, rtfunc->funcparams))
                                643                 :                :             {
                                644         [ +  + ]:          69341 :                 if (node->funcstates[i].tstore != NULL)
                                645                 :                :                 {
                                646                 :          63271 :                     tuplestore_end(node->funcstates[i].tstore);
                                647                 :          63271 :                     node->funcstates[i].tstore = NULL;
                                648                 :                :                 }
                                649                 :          69341 :                 node->funcstates[i].rowcount = -1;
                                650                 :                :             }
                                651                 :          72375 :             i++;
                                652                 :                :         }
                                653                 :                :     }
                                654                 :                : 
                                655                 :                :     /* Reset ordinality counter */
                                656                 :          74014 :     node->ordinal = 0;
                                657                 :                : 
                                658                 :                :     /* Make sure we rewind any remaining tuplestores */
                                659         [ +  + ]:         148101 :     for (i = 0; i < node->nfuncs; i++)
                                660                 :                :     {
                                661         [ +  + ]:          74087 :         if (node->funcstates[i].tstore != NULL)
                                662                 :           4266 :             tuplestore_rescan(node->funcstates[i].tstore);
                                663                 :                :     }
 8966                           664                 :          74014 : }
                                665                 :                : 
                                666                 :                : /* ----------------------------------------------------------------
                                667                 :                :  *                      Parallel Query Support
                                668                 :                :  * ----------------------------------------------------------------
                                669                 :                :  */
                                670                 :                : 
                                671                 :                : /* ----------------------------------------------------------------
                                672                 :                :  *      ExecFunctionScanEstimate
                                673                 :                :  *
                                674                 :                :  *      Estimate space required to propagate function scan statistics.
                                675                 :                :  * ----------------------------------------------------------------
                                676                 :                :  */
                                677                 :                : void
   18 drowley@postgresql.o      678                 :UNC           0 : ExecFunctionScanEstimate(FunctionScanState *node, ParallelContext *pcxt)
                                679                 :                : {
                                680                 :                :     Size        size;
                                681                 :                : 
                                682                 :                :     /* don't need this if not instrumenting or no workers */
                                683   [ #  #  #  # ]:              0 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                684                 :              0 :         return;
                                685                 :                : 
                                686                 :              0 :     size = mul_size(pcxt->nworkers, sizeof(FunctionScanInstrumentation));
                                687                 :              0 :     size = add_size(size, offsetof(SharedFunctionScanInfo, sinstrument));
                                688                 :              0 :     shm_toc_estimate_chunk(&pcxt->estimator, size);
                                689                 :              0 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
                                690                 :                : }
                                691                 :                : 
                                692                 :                : /* ----------------------------------------------------------------
                                693                 :                :  *      ExecFunctionScanInitializeDSM
                                694                 :                :  *
                                695                 :                :  *      Initialize DSM space for function scan statistics.
                                696                 :                :  * ----------------------------------------------------------------
                                697                 :                :  */
                                698                 :                : void
                                699                 :              0 : ExecFunctionScanInitializeDSM(FunctionScanState *node, ParallelContext *pcxt)
                                700                 :                : {
                                701                 :                :     Size        size;
                                702                 :                : 
                                703                 :                :     /* don't need this if not instrumenting or no workers */
                                704   [ #  #  #  # ]:              0 :     if (!node->ss.ps.instrument || pcxt->nworkers == 0)
                                705                 :              0 :         return;
                                706                 :                : 
                                707                 :              0 :     size = offsetof(SharedFunctionScanInfo, sinstrument)
                                708                 :              0 :         + pcxt->nworkers * sizeof(FunctionScanInstrumentation);
                                709                 :              0 :     node->shared_info = shm_toc_allocate(pcxt->toc, size);
                                710                 :                :     /* ensure any unfilled slots will contain zeroes */
                                711                 :              0 :     memset(node->shared_info, 0, size);
                                712                 :              0 :     node->shared_info->num_workers = pcxt->nworkers;
                                713                 :              0 :     shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
                                714                 :              0 :                    node->shared_info);
                                715                 :                : }
                                716                 :                : 
                                717                 :                : /* ----------------------------------------------------------------
                                718                 :                :  *      ExecFunctionScanInitializeWorker
                                719                 :                :  *
                                720                 :                :  *      Attach worker to DSM space for function scan statistics.
                                721                 :                :  * ----------------------------------------------------------------
                                722                 :                :  */
                                723                 :                : void
                                724                 :              0 : ExecFunctionScanInitializeWorker(FunctionScanState *node,
                                725                 :                :                                  ParallelWorkerContext *pwcxt)
                                726                 :                : {
                                727                 :              0 :     node->shared_info =
                                728                 :              0 :         shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
                                729                 :              0 : }
                                730                 :                : 
                                731                 :                : /* ----------------------------------------------------------------
                                732                 :                :  *      ExecFunctionScanRetrieveInstrumentation
                                733                 :                :  *
                                734                 :                :  *      Transfer function scan statistics from DSM to private memory.
                                735                 :                :  * ----------------------------------------------------------------
                                736                 :                :  */
                                737                 :                : void
                                738                 :              0 : ExecFunctionScanRetrieveInstrumentation(FunctionScanState *node)
                                739                 :                : {
                                740                 :                :     Size        size;
                                741                 :                :     SharedFunctionScanInfo *si;
                                742                 :                : 
                                743         [ #  # ]:              0 :     if (node->shared_info == NULL)
                                744                 :              0 :         return;
                                745                 :                : 
                                746                 :              0 :     size = offsetof(SharedFunctionScanInfo, sinstrument)
                                747                 :              0 :         + node->shared_info->num_workers * sizeof(FunctionScanInstrumentation);
                                748                 :              0 :     si = palloc(size);
                                749                 :              0 :     memcpy(si, node->shared_info, size);
                                750                 :              0 :     node->shared_info = si;
                                751                 :                : }
        

Generated by: LCOV version 2.0-1