LCOV - differential code coverage report
Current view: top level - src/backend/executor - nodeFunctionscan.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 98.1 % 161 158 3 158
Current Date: 2025-09-06 07:49:51 +0900 Functions: 83.3 % 6 5 1 5
Baseline: lcov-20250906-005545-baseline Branches: 88.8 % 98 87 11 87
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: 98.1 % 161 158 3 158
Function coverage date bins:
(360..) days: 83.3 % 6 5 1 5
Branch coverage date bins:
(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-2025, 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                 :                : 
                                 31                 :                : 
                                 32                 :                : /*
                                 33                 :                :  * Runtime data for each function being scanned.
                                 34                 :                :  */
                                 35                 :                : typedef struct FunctionScanPerFuncState
                                 36                 :                : {
                                 37                 :                :     SetExprState *setexpr;      /* state of the expression being evaluated */
                                 38                 :                :     TupleDesc   tupdesc;        /* desc of the function result type */
                                 39                 :                :     int         colcount;       /* expected number of result columns */
                                 40                 :                :     Tuplestorestate *tstore;    /* holds the function result set */
                                 41                 :                :     int64       rowcount;       /* # of rows in result set, -1 if not known */
                                 42                 :                :     TupleTableSlot *func_slot;  /* function result slot (or NULL) */
                                 43                 :                : } FunctionScanPerFuncState;
                                 44                 :                : 
                                 45                 :                : static TupleTableSlot *FunctionNext(FunctionScanState *node);
                                 46                 :                : 
                                 47                 :                : 
                                 48                 :                : /* ----------------------------------------------------------------
                                 49                 :                :  *                      Scan Support
                                 50                 :                :  * ----------------------------------------------------------------
                                 51                 :                :  */
                                 52                 :                : /* ----------------------------------------------------------------
                                 53                 :                :  *      FunctionNext
                                 54                 :                :  *
                                 55                 :                :  *      This is a workhorse for ExecFunctionScan
                                 56                 :                :  * ----------------------------------------------------------------
                                 57                 :                :  */
                                 58                 :                : static TupleTableSlot *
 8311 tgl@sss.pgh.pa.us          59                 :CBC     8516687 : FunctionNext(FunctionScanState *node)
                                 60                 :                : {
                                 61                 :                :     EState     *estate;
                                 62                 :                :     ScanDirection direction;
                                 63                 :                :     TupleTableSlot *scanslot;
                                 64                 :                :     bool        alldone;
                                 65                 :                :     int64       oldpos;
                                 66                 :                :     int         funcno;
                                 67                 :                :     int         att;
                                 68                 :                : 
                                 69                 :                :     /*
                                 70                 :                :      * get information from the estate and scan state
                                 71                 :                :      */
                                 72                 :        8516687 :     estate = node->ss.ps.state;
 8518                            73                 :        8516687 :     direction = estate->es_direction;
 4307                            74                 :        8516687 :     scanslot = node->ss.ss_ScanTupleSlot;
                                 75                 :                : 
                                 76         [ +  + ]:        8516687 :     if (node->simple)
                                 77                 :                :     {
                                 78                 :                :         /*
                                 79                 :                :          * Fast path for the trivial case: the function return type and scan
                                 80                 :                :          * result type are the same, so we fetch the function result straight
                                 81                 :                :          * into the scan result slot. No need to update ordinality or
                                 82                 :                :          * rowcounts either.
                                 83                 :                :          */
                                 84                 :        8503307 :         Tuplestorestate *tstore = node->funcstates[0].tstore;
                                 85                 :                : 
                                 86                 :                :         /*
                                 87                 :                :          * If first time through, read all tuples from function and put them
                                 88                 :                :          * in a tuplestore. Subsequent calls just fetch tuples from
                                 89                 :                :          * tuplestore.
                                 90                 :                :          */
                                 91         [ +  + ]:        8503307 :         if (tstore == NULL)
                                 92                 :                :         {
                                 93                 :          75187 :             node->funcstates[0].tstore = tstore =
 3098 andres@anarazel.de         94                 :          77799 :                 ExecMakeTableFunctionResult(node->funcstates[0].setexpr,
                                 95                 :                :                                             node->ss.ps.ps_ExprContext,
                                 96                 :                :                                             node->argcontext,
 4307 tgl@sss.pgh.pa.us          97                 :          77799 :                                             node->funcstates[0].tupdesc,
 2999                            98                 :          77799 :                                             node->eflags & EXEC_FLAG_BACKWARD);
                                 99                 :                : 
                                100                 :                :             /*
                                101                 :                :              * paranoia - cope if the function, which may have constructed the
                                102                 :                :              * tuplestore itself, didn't leave it pointing at the start. This
                                103                 :                :              * call is fast, so the overhead shouldn't be an issue.
                                104                 :                :              */
 4307                           105                 :          75187 :             tuplestore_rescan(tstore);
                                106                 :                :         }
                                107                 :                : 
                                108                 :                :         /*
                                109                 :                :          * Get the next tuple from tuplestore.
                                110                 :                :          */
                                111                 :        8500695 :         (void) tuplestore_gettupleslot(tstore,
                                112                 :                :                                        ScanDirectionIsForward(direction),
                                113                 :                :                                        false,
                                114                 :                :                                        scanslot);
                                115                 :        8500695 :         return scanslot;
                                116                 :                :     }
                                117                 :                : 
                                118                 :                :     /*
                                119                 :                :      * Increment or decrement ordinal counter before checking for end-of-data,
                                120                 :                :      * so that we can move off either end of the result by 1 (and no more than
                                121                 :                :      * 1) without losing correct count.  See PortalRunSelect for why we can
                                122                 :                :      * assume that we won't be called repeatedly in the end-of-data state.
                                123                 :                :      */
                                124                 :          13380 :     oldpos = node->ordinal;
 4422 stark@mit.edu             125         [ +  + ]:          13380 :     if (ScanDirectionIsForward(direction))
                                126                 :          13350 :         node->ordinal++;
                                127                 :                :     else
                                128                 :             30 :         node->ordinal--;
                                129                 :                : 
                                130                 :                :     /*
                                131                 :                :      * Main loop over functions.
                                132                 :                :      *
                                133                 :                :      * We fetch the function results into func_slots (which match the function
                                134                 :                :      * return types), and then copy the values to scanslot (which matches the
                                135                 :                :      * scan result type), setting the ordinal column (if any) as well.
                                136                 :                :      */
 4307 tgl@sss.pgh.pa.us         137                 :          13380 :     ExecClearTuple(scanslot);
                                138                 :          13380 :     att = 0;
                                139                 :          13380 :     alldone = true;
                                140         [ +  + ]:          36074 :     for (funcno = 0; funcno < node->nfuncs; funcno++)
                                141                 :                :     {
                                142                 :          22694 :         FunctionScanPerFuncState *fs = &node->funcstates[funcno];
                                143                 :                :         int         i;
                                144                 :                : 
                                145                 :                :         /*
                                146                 :                :          * If first time through, read all tuples from function and put them
                                147                 :                :          * in a tuplestore. Subsequent calls just fetch tuples from
                                148                 :                :          * tuplestore.
                                149                 :                :          */
                                150         [ +  + ]:          22694 :         if (fs->tstore == NULL)
                                151                 :                :         {
                                152                 :           1099 :             fs->tstore =
 3098 andres@anarazel.de        153                 :           1099 :                 ExecMakeTableFunctionResult(fs->setexpr,
                                154                 :                :                                             node->ss.ps.ps_ExprContext,
                                155                 :                :                                             node->argcontext,
                                156                 :                :                                             fs->tupdesc,
 2999 tgl@sss.pgh.pa.us         157                 :           1099 :                                             node->eflags & EXEC_FLAG_BACKWARD);
                                158                 :                : 
                                159                 :                :             /*
                                160                 :                :              * paranoia - cope if the function, which may have constructed the
                                161                 :                :              * tuplestore itself, didn't leave it pointing at the start. This
                                162                 :                :              * call is fast, so the overhead shouldn't be an issue.
                                163                 :                :              */
 4307                           164                 :           1099 :             tuplestore_rescan(fs->tstore);
                                165                 :                :         }
                                166                 :                : 
                                167                 :                :         /*
                                168                 :                :          * Get the next tuple from tuplestore.
                                169                 :                :          *
                                170                 :                :          * If we have a rowcount for the function, and we know the previous
                                171                 :                :          * read position was out of bounds, don't try the read. This allows
                                172                 :                :          * backward scan to work when there are mixed row counts present.
                                173                 :                :          */
                                174   [ +  +  +  + ]:          22694 :         if (fs->rowcount != -1 && fs->rowcount < oldpos)
                                175                 :             36 :             ExecClearTuple(fs->func_slot);
                                176                 :                :         else
                                177                 :          22658 :             (void) tuplestore_gettupleslot(fs->tstore,
                                178                 :                :                                            ScanDirectionIsForward(direction),
                                179                 :                :                                            false,
                                180                 :                :                                            fs->func_slot);
                                181                 :                : 
                                182   [ +  -  +  + ]:          22694 :         if (TupIsNull(fs->func_slot))
                                183                 :                :         {
                                184                 :                :             /*
                                185                 :                :              * If we ran out of data for this function in the forward
                                186                 :                :              * direction then we now know how many rows it returned. We need
                                187                 :                :              * to know this in order to handle backwards scans. The row count
                                188                 :                :              * we store is actually 1+ the actual number, because we have to
                                189                 :                :              * position the tuplestore 1 off its end sometimes.
                                190                 :                :              */
                                191   [ +  +  +  + ]:           1273 :             if (ScanDirectionIsForward(direction) && fs->rowcount == -1)
                                192                 :           1087 :                 fs->rowcount = node->ordinal;
                                193                 :                : 
                                194                 :                :             /*
                                195                 :                :              * populate the result cols with nulls
                                196                 :                :              */
                                197         [ +  + ]:           3040 :             for (i = 0; i < fs->colcount; i++)
                                198                 :                :             {
                                199                 :           1767 :                 scanslot->tts_values[att] = (Datum) 0;
                                200                 :           1767 :                 scanslot->tts_isnull[att] = true;
                                201                 :           1767 :                 att++;
                                202                 :                :             }
                                203                 :                :         }
                                204                 :                :         else
                                205                 :                :         {
                                206                 :                :             /*
                                207                 :                :              * we have a result, so just copy it to the result cols.
                                208                 :                :              */
                                209                 :          21421 :             slot_getallattrs(fs->func_slot);
                                210                 :                : 
                                211         [ +  + ]:          51667 :             for (i = 0; i < fs->colcount; i++)
                                212                 :                :             {
                                213                 :          30246 :                 scanslot->tts_values[att] = fs->func_slot->tts_values[i];
                                214                 :          30246 :                 scanslot->tts_isnull[att] = fs->func_slot->tts_isnull[i];
                                215                 :          30246 :                 att++;
                                216                 :                :             }
                                217                 :                : 
                                218                 :                :             /*
                                219                 :                :              * We're not done until every function result is exhausted; we pad
                                220                 :                :              * the shorter results with nulls until then.
                                221                 :                :              */
                                222                 :          21421 :             alldone = false;
                                223                 :                :         }
                                224                 :                :     }
                                225                 :                : 
                                226                 :                :     /*
                                227                 :                :      * ordinal col is always last, per spec.
                                228                 :                :      */
                                229         [ +  + ]:          13380 :     if (node->ordinality)
                                230                 :                :     {
                                231                 :          10756 :         scanslot->tts_values[att] = Int64GetDatumFast(node->ordinal);
                                232                 :          10756 :         scanslot->tts_isnull[att] = false;
                                233                 :                :     }
                                234                 :                : 
                                235                 :                :     /*
                                236                 :                :      * If alldone, we just return the previously-cleared scanslot.  Otherwise,
                                237                 :                :      * finish creating the virtual tuple.
                                238                 :                :      */
                                239         [ +  + ]:          13380 :     if (!alldone)
 4422 stark@mit.edu             240                 :          12534 :         ExecStoreVirtualTuple(scanslot);
                                241                 :                : 
                                242                 :          13380 :     return scanslot;
                                243                 :                : }
                                244                 :                : 
                                245                 :                : /*
                                246                 :                :  * FunctionRecheck -- access method routine to recheck a tuple in EvalPlanQual
                                247                 :                :  */
                                248                 :                : static bool
 5794 tgl@sss.pgh.pa.us         249                 :UBC           0 : FunctionRecheck(FunctionScanState *node, TupleTableSlot *slot)
                                250                 :                : {
                                251                 :                :     /* nothing to check */
                                252                 :              0 :     return true;
                                253                 :                : }
                                254                 :                : 
                                255                 :                : /* ----------------------------------------------------------------
                                256                 :                :  *      ExecFunctionScan(node)
                                257                 :                :  *
                                258                 :                :  *      Scans the function sequentially and returns the next qualifying
                                259                 :                :  *      tuple.
                                260                 :                :  *      We call the ExecScan() routine and pass it the appropriate
                                261                 :                :  *      access method functions.
                                262                 :                :  * ----------------------------------------------------------------
                                263                 :                :  */
                                264                 :                : static TupleTableSlot *
 2973 andres@anarazel.de        265                 :CBC     7655952 : ExecFunctionScan(PlanState *pstate)
                                266                 :                : {
                                267                 :        7655952 :     FunctionScanState *node = castNode(FunctionScanState, pstate);
                                268                 :                : 
 5794 tgl@sss.pgh.pa.us         269                 :        7655952 :     return ExecScan(&node->ss,
                                270                 :                :                     (ExecScanAccessMtd) FunctionNext,
                                271                 :                :                     (ExecScanRecheckMtd) FunctionRecheck);
                                272                 :                : }
                                273                 :                : 
                                274                 :                : /* ----------------------------------------------------------------
                                275                 :                :  *      ExecInitFunctionScan
                                276                 :                :  * ----------------------------------------------------------------
                                277                 :                :  */
                                278                 :                : FunctionScanState *
 7130                           279                 :          34095 : ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
                                280                 :                : {
                                281                 :                :     FunctionScanState *scanstate;
 4307                           282                 :          34095 :     int         nfuncs = list_length(node->functions);
                                283                 :                :     TupleDesc   scan_tupdesc;
                                284                 :                :     int         i,
                                285                 :                :                 natts;
                                286                 :                :     ListCell   *lc;
                                287                 :                : 
                                288                 :                :     /* check for unsupported flags */
 6184                           289         [ -  + ]:          34095 :     Assert(!(eflags & EXEC_FLAG_MARK));
                                290                 :                : 
                                291                 :                :     /*
                                292                 :                :      * FunctionScan should not have any children.
                                293                 :                :      */
 8311                           294         [ -  + ]:          34095 :     Assert(outerPlan(node) == NULL);
                                295         [ -  + ]:          34095 :     Assert(innerPlan(node) == NULL);
                                296                 :                : 
                                297                 :                :     /*
                                298                 :                :      * create new ScanState for node
                                299                 :                :      */
 8518                           300                 :          34095 :     scanstate = makeNode(FunctionScanState);
 8311                           301                 :          34095 :     scanstate->ss.ps.plan = (Plan *) node;
                                302                 :          34095 :     scanstate->ss.ps.state = estate;
 2973 andres@anarazel.de        303                 :          34095 :     scanstate->ss.ps.ExecProcNode = ExecFunctionScan;
 6156 tgl@sss.pgh.pa.us         304                 :          34095 :     scanstate->eflags = eflags;
                                305                 :                : 
                                306                 :                :     /*
                                307                 :                :      * are we adding an ordinality column?
                                308                 :                :      */
 4307                           309                 :          34095 :     scanstate->ordinality = node->funcordinality;
                                310                 :                : 
                                311                 :          34095 :     scanstate->nfuncs = nfuncs;
                                312   [ +  +  +  + ]:          34095 :     if (nfuncs == 1 && !node->funcordinality)
                                313                 :          33524 :         scanstate->simple = true;
                                314                 :                :     else
                                315                 :            571 :         scanstate->simple = false;
                                316                 :                : 
                                317                 :                :     /*
                                318                 :                :      * Ordinal 0 represents the "before the first row" position.
                                319                 :                :      *
                                320                 :                :      * We need to track ordinal position even when not adding an ordinality
                                321                 :                :      * column to the result, in order to handle backwards scanning properly
                                322                 :                :      * with multiple functions with different result sizes. (We can't position
                                323                 :                :      * any individual function's tuplestore any more than 1 place beyond its
                                324                 :                :      * end, so when scanning backwards, we need to know when to start
                                325                 :                :      * including the function in the scan again.)
                                326                 :                :      */
                                327                 :          34095 :     scanstate->ordinal = 0;
                                328                 :                : 
                                329                 :                :     /*
                                330                 :                :      * Miscellaneous initialization
                                331                 :                :      *
                                332                 :                :      * create expression context for node
                                333                 :                :      */
 8311                           334                 :          34095 :     ExecAssignExprContext(estate, &scanstate->ss.ps);
                                335                 :                : 
 4307                           336                 :          34095 :     scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
                                337                 :                : 
                                338                 :          34095 :     natts = 0;
                                339                 :          34095 :     i = 0;
                                340   [ +  -  +  +  :          68451 :     foreach(lc, node->functions)
                                              +  + ]
                                341                 :                :     {
                                342                 :          34360 :         RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                                343                 :          34360 :         Node       *funcexpr = rtfunc->funcexpr;
                                344                 :          34360 :         int         colcount = rtfunc->funccolcount;
                                345                 :          34360 :         FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
                                346                 :                :         TupleDesc   tupdesc;
                                347                 :                : 
 3098 andres@anarazel.de        348                 :          34356 :         fs->setexpr =
                                349                 :          34360 :             ExecInitTableFunctionResult((Expr *) funcexpr,
                                350                 :                :                                         scanstate->ss.ps.ps_ExprContext,
                                351                 :                :                                         &scanstate->ss.ps);
                                352                 :                : 
                                353                 :                :         /*
                                354                 :                :          * Don't allocate the tuplestores; the actual calls to the functions
                                355                 :                :          * do that.  NULL means that we have not called the function yet (or
                                356                 :                :          * need to call it again after a rescan).
                                357                 :                :          */
 4307 tgl@sss.pgh.pa.us         358                 :          34356 :         fs->tstore = NULL;
                                359                 :          34356 :         fs->rowcount = -1;
                                360                 :                : 
                                361                 :                :         /*
                                362                 :                :          * Now build a tupdesc showing the result type we expect from the
                                363                 :                :          * function.  If we have a coldeflist then that takes priority (note
                                364                 :                :          * the parser enforces that there is one if the function's nominal
                                365                 :                :          * output type is RECORD).  Otherwise use get_expr_result_type.
                                366                 :                :          *
                                367                 :                :          * Note that if the function returns a named composite type, that may
                                368                 :                :          * now contain more or different columns than it did when the plan was
                                369                 :                :          * made.  For both that and the RECORD case, we need to check tuple
                                370                 :                :          * compatibility.  ExecMakeTableFunctionResult handles some of this,
                                371                 :                :          * and CheckVarSlotCompatibility provides a backstop.
                                372                 :                :          */
  549                           373         [ +  + ]:          34356 :         if (rtfunc->funccolnames != NIL)
                                374                 :                :         {
 4307                           375                 :            782 :             tupdesc = BuildDescFromLists(rtfunc->funccolnames,
                                376                 :            391 :                                          rtfunc->funccoltypes,
                                377                 :            391 :                                          rtfunc->funccoltypmods,
                                378                 :            391 :                                          rtfunc->funccolcollations);
                                379                 :                : 
                                380                 :                :             /*
                                381                 :                :              * For RECORD results, make sure a typmod has been assigned.  (The
                                382                 :                :              * function should do this for itself, but let's cover things in
                                383                 :                :              * case it doesn't.)
                                384                 :                :              */
                                385                 :            391 :             BlessTupleDesc(tupdesc);
                                386                 :                :         }
                                387                 :                :         else
                                388                 :                :         {
                                389                 :                :             TypeFuncClass functypclass;
                                390                 :                :             Oid         funcrettype;
                                391                 :                : 
  549                           392                 :          33965 :             functypclass = get_expr_result_type(funcexpr,
                                393                 :                :                                                 &funcrettype,
                                394                 :                :                                                 &tupdesc);
                                395                 :                : 
                                396   [ +  +  +  + ]:          33965 :             if (functypclass == TYPEFUNC_COMPOSITE ||
                                397                 :                :                 functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
                                398                 :                :             {
                                399                 :                :                 /* Composite data type, e.g. a table's row type */
                                400         [ -  + ]:          19012 :                 Assert(tupdesc);
                                401                 :                :                 /* Must copy it out of typcache for safety */
                                402                 :          19012 :                 tupdesc = CreateTupleDescCopy(tupdesc);
                                403                 :                :             }
                                404         [ +  - ]:          14953 :             else if (functypclass == TYPEFUNC_SCALAR)
                                405                 :                :             {
                                406                 :                :                 /* Base data type, i.e. scalar */
                                407                 :          14953 :                 tupdesc = CreateTemplateTupleDesc(1);
                                408                 :          14953 :                 TupleDescInitEntry(tupdesc,
                                409                 :                :                                    (AttrNumber) 1,
                                410                 :                :                                    NULL,    /* don't care about the name here */
                                411                 :                :                                    funcrettype,
                                412                 :                :                                    -1,
                                413                 :                :                                    0);
                                414                 :          14953 :                 TupleDescInitEntryCollation(tupdesc,
                                415                 :                :                                             (AttrNumber) 1,
                                416                 :                :                                             exprCollation(funcexpr));
                                417                 :                :             }
                                418                 :                :             else
                                419                 :                :             {
                                420                 :                :                 /* crummy error message, but parser should have caught this */
  549 tgl@sss.pgh.pa.us         421         [ #  # ]:UBC           0 :                 elog(ERROR, "function in FROM has unsupported return type");
                                422                 :                :             }
                                423                 :                :         }
                                424                 :                : 
 4307 tgl@sss.pgh.pa.us         425                 :CBC       34356 :         fs->tupdesc = tupdesc;
                                426                 :          34356 :         fs->colcount = colcount;
                                427                 :                : 
                                428                 :                :         /*
                                429                 :                :          * We only need separate slots for the function results if we are
                                430                 :                :          * doing ordinality or multiple functions; otherwise, we'll fetch
                                431                 :                :          * function results directly into the scan slot.
                                432                 :                :          */
                                433         [ +  + ]:          34356 :         if (!scanstate->simple)
                                434                 :                :         {
 2487 andres@anarazel.de        435                 :            836 :             fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc,
                                436                 :                :                                                    &TTSOpsMinimalTuple);
                                437                 :                :         }
                                438                 :                :         else
 4307 tgl@sss.pgh.pa.us         439                 :          33520 :             fs->func_slot = NULL;
                                440                 :                : 
                                441                 :          34356 :         natts += colcount;
                                442                 :          34356 :         i++;
                                443                 :                :     }
                                444                 :                : 
                                445                 :                :     /*
                                446                 :                :      * Create the combined TupleDesc
                                447                 :                :      *
                                448                 :                :      * If there is just one function without ordinality, the scan result
                                449                 :                :      * tupdesc is the same as the function result tupdesc --- except that we
                                450                 :                :      * may stuff new names into it below, so drop any rowtype label.
                                451                 :                :      */
                                452         [ +  + ]:          34091 :     if (scanstate->simple)
                                453                 :                :     {
                                454                 :          33520 :         scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
                                455                 :          33520 :         scan_tupdesc->tdtypeid = RECORDOID;
                                456                 :          33520 :         scan_tupdesc->tdtypmod = -1;
                                457                 :                :     }
                                458                 :                :     else
                                459                 :                :     {
                                460                 :            571 :         AttrNumber  attno = 0;
                                461                 :                : 
                                462         [ +  + ]:            571 :         if (node->funcordinality)
                                463                 :            535 :             natts++;
                                464                 :                : 
 2482 andres@anarazel.de        465                 :            571 :         scan_tupdesc = CreateTemplateTupleDesc(natts);
                                466                 :                : 
 4307 tgl@sss.pgh.pa.us         467         [ +  + ]:           1407 :         for (i = 0; i < nfuncs; i++)
                                468                 :                :         {
                                469                 :            836 :             TupleDesc   tupdesc = scanstate->funcstates[i].tupdesc;
                                470                 :            836 :             int         colcount = scanstate->funcstates[i].colcount;
                                471                 :                :             int         j;
                                472                 :                : 
                                473         [ +  + ]:           1992 :             for (j = 1; j <= colcount; j++)
                                474                 :           1156 :                 TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
                                475                 :                :         }
                                476                 :                : 
                                477                 :                :         /* If doing ordinality, add a column of type "bigint" at the end */
                                478         [ +  + ]:            571 :         if (node->funcordinality)
                                479                 :                :         {
                                480                 :            535 :             TupleDescInitEntry(scan_tupdesc,
                                481                 :                :                                ++attno,
                                482                 :                :                                NULL,    /* don't care about the name here */
                                483                 :                :                                INT8OID,
                                484                 :                :                                -1,
                                485                 :                :                                0);
                                486                 :                :         }
                                487                 :                : 
                                488         [ -  + ]:            571 :         Assert(attno == natts);
                                489                 :                :     }
                                490                 :                : 
                                491                 :                :     /*
                                492                 :                :      * Initialize scan slot and type.
                                493                 :                :      */
 2487 andres@anarazel.de        494                 :          34091 :     ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
                                495                 :                :                           &TTSOpsMinimalTuple);
                                496                 :                : 
                                497                 :                :     /*
                                498                 :                :      * Initialize result slot, type and projection.
                                499                 :                :      */
 2493                           500                 :          34091 :     ExecInitResultTypeTL(&scanstate->ss.ps);
 7412 tgl@sss.pgh.pa.us         501                 :          34091 :     ExecAssignScanProjectionInfo(&scanstate->ss);
                                502                 :                : 
                                503                 :                :     /*
                                504                 :                :      * initialize child expressions
                                505                 :                :      */
 2759 andres@anarazel.de        506                 :          34091 :     scanstate->ss.ps.qual =
                                507                 :          34091 :         ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
                                508                 :                : 
                                509                 :                :     /*
                                510                 :                :      * Create a memory context that ExecMakeTableFunctionResult can use to
                                511                 :                :      * evaluate function arguments in.  We can't use the per-tuple context for
                                512                 :                :      * this because it gets reset too often; but we don't want to leak
                                513                 :                :      * evaluation results into the query-lifespan context either.  We just
                                514                 :                :      * need one context, because we evaluate each function separately.
                                515                 :                :      */
 4097 tgl@sss.pgh.pa.us         516                 :          34091 :     scanstate->argcontext = AllocSetContextCreate(CurrentMemoryContext,
                                517                 :                :                                                   "Table function arguments",
                                518                 :                :                                                   ALLOCSET_DEFAULT_SIZES);
                                519                 :                : 
 8311                           520                 :          34091 :     return scanstate;
                                521                 :                : }
                                522                 :                : 
                                523                 :                : /* ----------------------------------------------------------------
                                524                 :                :  *      ExecEndFunctionScan
                                525                 :                :  *
                                526                 :                :  *      frees any storage allocated through C routines.
                                527                 :                :  * ----------------------------------------------------------------
                                528                 :                :  */
                                529                 :                : void
                                530                 :          31383 : ExecEndFunctionScan(FunctionScanState *node)
                                531                 :                : {
                                532                 :                :     int         i;
                                533                 :                : 
                                534                 :                :     /*
                                535                 :                :      * Release slots and tuplestore resources
                                536                 :                :      */
 4307                           537         [ +  + ]:          63025 :     for (i = 0; i < node->nfuncs; i++)
                                538                 :                :     {
                                539                 :          31642 :         FunctionScanPerFuncState *fs = &node->funcstates[i];
                                540                 :                : 
                                541         [ +  + ]:          31642 :         if (fs->tstore != NULL)
                                542                 :                :         {
                                543                 :          28617 :             tuplestore_end(node->funcstates[i].tstore);
                                544                 :          28617 :             fs->tstore = NULL;
                                545                 :                :         }
                                546                 :                :     }
 8518                           547                 :          31383 : }
                                548                 :                : 
                                549                 :                : /* ----------------------------------------------------------------
                                550                 :                :  *      ExecReScanFunctionScan
                                551                 :                :  *
                                552                 :                :  *      Rescans the relation.
                                553                 :                :  * ----------------------------------------------------------------
                                554                 :                :  */
                                555                 :                : void
 5535                           556                 :          57341 : ExecReScanFunctionScan(FunctionScanState *node)
                                557                 :                : {
 4307                           558                 :          57341 :     FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
                                559                 :                :     int         i;
                                560                 :          57341 :     Bitmapset  *chgparam = node->ss.ps.chgParam;
                                561                 :                : 
 2493 andres@anarazel.de        562         [ +  + ]:          57341 :     if (node->ss.ps.ps_ResultTupleSlot)
                                563                 :          30253 :         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
 4307 tgl@sss.pgh.pa.us         564         [ +  + ]:         114736 :     for (i = 0; i < node->nfuncs; i++)
                                565                 :                :     {
                                566                 :          57395 :         FunctionScanPerFuncState *fs = &node->funcstates[i];
                                567                 :                : 
                                568         [ +  + ]:          57395 :         if (fs->func_slot)
                                569                 :            543 :             ExecClearTuple(fs->func_slot);
                                570                 :                :     }
                                571                 :                : 
                                572                 :          57341 :     ExecScanReScan(&node->ss);
                                573                 :                : 
                                574                 :                :     /*
                                575                 :                :      * Here we have a choice whether to drop the tuplestores (and recompute
                                576                 :                :      * the function outputs) or just rescan them.  We must recompute if an
                                577                 :                :      * expression contains changed parameters, else we rescan.
                                578                 :                :      *
                                579                 :                :      * XXX maybe we should recompute if the function is volatile?  But in
                                580                 :                :      * general the executor doesn't conditionalize its actions on that.
                                581                 :                :      */
                                582         [ +  + ]:          57341 :     if (chgparam)
                                583                 :                :     {
                                584                 :                :         ListCell   *lc;
                                585                 :                : 
                                586                 :          56085 :         i = 0;
                                587   [ +  -  +  +  :         112212 :         foreach(lc, scan->functions)
                                              +  + ]
                                588                 :                :         {
                                589                 :          56127 :             RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
                                590                 :                : 
                                591         [ +  + ]:          56127 :             if (bms_overlap(chgparam, rtfunc->funcparams))
                                592                 :                :             {
                                593         [ +  + ]:          53144 :                 if (node->funcstates[i].tstore != NULL)
                                594                 :                :                 {
                                595                 :          47567 :                     tuplestore_end(node->funcstates[i].tstore);
                                596                 :          47567 :                     node->funcstates[i].tstore = NULL;
                                597                 :                :                 }
                                598                 :          53144 :                 node->funcstates[i].rowcount = -1;
                                599                 :                :             }
                                600                 :          56127 :             i++;
                                601                 :                :         }
                                602                 :                :     }
                                603                 :                : 
                                604                 :                :     /* Reset ordinality counter */
                                605                 :          57341 :     node->ordinal = 0;
                                606                 :                : 
                                607                 :                :     /* Make sure we rewind any remaining tuplestores */
                                608         [ +  + ]:         114736 :     for (i = 0; i < node->nfuncs; i++)
                                609                 :                :     {
                                610         [ +  + ]:          57395 :         if (node->funcstates[i].tstore != NULL)
                                611                 :           3868 :             tuplestore_rescan(node->funcstates[i].tstore);
                                612                 :                :     }
 8518                           613                 :          57341 : }
        

Generated by: LCOV version 2.4-beta