LCOV - code coverage report
Current view: top level - src/backend/executor - nodeTableFuncscan.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 95.2 % 166 158
Test Date: 2026-10-23 11:15:58 Functions: 88.9 % 9 8
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 78.9 % 90 71

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * nodeTableFuncscan.c
       4                 :             :  *    Support routines for scanning RangeTableFunc (XMLTABLE like functions).
       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/nodeTableFuncscan.c
      12                 :             :  *
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : /*
      16                 :             :  * INTERFACE ROUTINES
      17                 :             :  *      ExecTableFuncScan       scans a function.
      18                 :             :  *      ExecFunctionNext        retrieve next tuple in sequential order.
      19                 :             :  *      ExecInitTableFuncScan   creates and initializes a TableFuncscan node.
      20                 :             :  *      ExecEndTableFuncScan        releases any storage allocated.
      21                 :             :  *      ExecReScanTableFuncScan rescans the function
      22                 :             :  */
      23                 :             : #include "postgres.h"
      24                 :             : 
      25                 :             : #include "executor/executor.h"
      26                 :             : #include "executor/nodeTableFuncscan.h"
      27                 :             : #include "executor/tablefunc.h"
      28                 :             : #include "miscadmin.h"
      29                 :             : #include "nodes/execnodes.h"
      30                 :             : #include "utils/builtins.h"
      31                 :             : #include "utils/jsonpath.h"
      32                 :             : #include "utils/lsyscache.h"
      33                 :             : #include "utils/memutils.h"
      34                 :             : #include "utils/tuplestore.h"
      35                 :             : #include "utils/xml.h"
      36                 :             : 
      37                 :             : static TupleTableSlot *TableFuncNext(TableFuncScanState *node);
      38                 :             : static bool TableFuncRecheck(TableFuncScanState *node, TupleTableSlot *slot);
      39                 :             : 
      40                 :             : static void tfuncFetchRows(TableFuncScanState *tstate, ExprContext *econtext);
      41                 :             : static void tfuncInitialize(TableFuncScanState *tstate, ExprContext *econtext, Datum doc);
      42                 :             : static void tfuncLoadRows(TableFuncScanState *tstate, ExprContext *econtext);
      43                 :             : 
      44                 :             : /* ----------------------------------------------------------------
      45                 :             :  *                      Scan Support
      46                 :             :  * ----------------------------------------------------------------
      47                 :             :  */
      48                 :             : /* ----------------------------------------------------------------
      49                 :             :  *      TableFuncNext
      50                 :             :  *
      51                 :             :  *      This is a workhorse for ExecTableFuncScan
      52                 :             :  * ----------------------------------------------------------------
      53                 :             :  */
      54                 :             : static TupleTableSlot *
      55                 :       17145 : TableFuncNext(TableFuncScanState *node)
      56                 :             : {
      57                 :             :     TupleTableSlot *scanslot;
      58                 :             : 
      59                 :       17145 :     scanslot = node->ss.ss_ScanTupleSlot;
      60                 :             : 
      61                 :             :     /*
      62                 :             :      * If first time through (or first since a parameter change), read all
      63                 :             :      * tuples from function and put them in a tuplestore.  Subsequent calls
      64                 :             :      * just fetch tuples from tuplestore.
      65                 :             :      */
      66         [ +  + ]:       17145 :     if (!node->tupstore_filled)
      67                 :             :     {
      68                 :         592 :         tfuncFetchRows(node, node->ss.ps.ps_ExprContext);
      69                 :         516 :         node->tupstore_filled = true;
      70                 :             :     }
      71                 :             : 
      72                 :             :     /*
      73                 :             :      * Get the next tuple from tuplestore.
      74                 :             :      */
      75                 :       17069 :     (void) tuplestore_gettupleslot(node->tupstore,
      76                 :             :                                    true,
      77                 :             :                                    false,
      78                 :             :                                    scanslot);
      79                 :       17069 :     return scanslot;
      80                 :             : }
      81                 :             : 
      82                 :             : /*
      83                 :             :  * TableFuncRecheck -- access method routine to recheck a tuple in EvalPlanQual
      84                 :             :  */
      85                 :             : static bool
      86                 :           0 : TableFuncRecheck(TableFuncScanState *node, TupleTableSlot *slot)
      87                 :             : {
      88                 :             :     /* nothing to check */
      89                 :           0 :     return true;
      90                 :             : }
      91                 :             : 
      92                 :             : /* ----------------------------------------------------------------
      93                 :             :  *      ExecTableFuncScan(node)
      94                 :             :  *
      95                 :             :  *      Scans the function sequentially and returns the next qualifying
      96                 :             :  *      tuple.
      97                 :             :  *      We call the ExecScan() routine and pass it the appropriate
      98                 :             :  *      access method functions.
      99                 :             :  * ----------------------------------------------------------------
     100                 :             :  */
     101                 :             : static TupleTableSlot *
     102                 :       17109 : ExecTableFuncScan(PlanState *pstate)
     103                 :             : {
     104                 :       17109 :     TableFuncScanState *node = castNode(TableFuncScanState, pstate);
     105                 :             : 
     106                 :       17109 :     return ExecScan(&node->ss,
     107                 :             :                     (ExecScanAccessMtd) TableFuncNext,
     108                 :             :                     (ExecScanRecheckMtd) TableFuncRecheck);
     109                 :             : }
     110                 :             : 
     111                 :             : /* ----------------------------------------------------------------
     112                 :             :  *      ExecInitTableFuncScan
     113                 :             :  * ----------------------------------------------------------------
     114                 :             :  */
     115                 :             : TableFuncScanState *
     116                 :         542 : ExecInitTableFuncScan(TableFuncScan *node, EState *estate, int eflags)
     117                 :             : {
     118                 :             :     TableFuncScanState *scanstate;
     119                 :         542 :     TableFunc  *tf = node->tablefunc;
     120                 :             :     TupleDesc   tupdesc;
     121                 :             :     int         i;
     122                 :             : 
     123                 :             :     /* check for unsupported flags */
     124                 :             :     Assert(!(eflags & EXEC_FLAG_MARK));
     125                 :             : 
     126                 :             :     /*
     127                 :             :      * TableFuncscan should not have any children.
     128                 :             :      */
     129                 :             :     Assert(outerPlan(node) == NULL);
     130                 :             :     Assert(innerPlan(node) == NULL);
     131                 :             : 
     132                 :             :     /*
     133                 :             :      * create new ScanState for node
     134                 :             :      */
     135                 :         542 :     scanstate = makeNode(TableFuncScanState);
     136                 :         542 :     scanstate->ss.ps.plan = (Plan *) node;
     137                 :         542 :     scanstate->ss.ps.state = estate;
     138                 :         542 :     scanstate->ss.ps.ExecProcNode = ExecTableFuncScan;
     139                 :             : 
     140                 :             :     /*
     141                 :             :      * Miscellaneous initialization
     142                 :             :      *
     143                 :             :      * create expression context for node
     144                 :             :      */
     145                 :         542 :     ExecAssignExprContext(estate, &scanstate->ss.ps);
     146                 :             : 
     147                 :             :     /*
     148                 :             :      * initialize source tuple type
     149                 :             :      */
     150                 :         542 :     tupdesc = BuildDescFromLists(tf->colnames,
     151                 :         542 :                                  tf->coltypes,
     152                 :         542 :                                  tf->coltypmods,
     153                 :         542 :                                  tf->colcollations);
     154                 :             :     /* and the corresponding scan slot */
     155                 :         542 :     ExecInitScanTupleSlot(estate, &scanstate->ss, tupdesc,
     156                 :             :                           &TTSOpsMinimalTuple, 0);
     157                 :             : 
     158                 :             :     /*
     159                 :             :      * Initialize result type and projection.
     160                 :             :      */
     161                 :         542 :     ExecInitResultTypeTL(&scanstate->ss.ps);
     162                 :         542 :     ExecAssignScanProjectionInfo(&scanstate->ss);
     163                 :             : 
     164                 :             :     /*
     165                 :             :      * initialize child expressions
     166                 :             :      */
     167                 :         542 :     scanstate->ss.ps.qual =
     168                 :         542 :         ExecInitQual(node->scan.plan.qual, &scanstate->ss.ps);
     169                 :             : 
     170                 :             :     /* Only XMLTABLE and JSON_TABLE are supported currently */
     171                 :         542 :     scanstate->routine =
     172         [ +  + ]:         542 :         tf->functype == TFT_XMLTABLE ? &XmlTableRoutine : &JsonbTableRoutine;
     173                 :             : 
     174                 :         542 :     scanstate->perTableCxt =
     175                 :         542 :         AllocSetContextCreate(CurrentMemoryContext,
     176                 :             :                               "TableFunc per value context",
     177                 :             :                               ALLOCSET_DEFAULT_SIZES);
     178                 :         542 :     scanstate->opaque = NULL;    /* initialized at runtime */
     179                 :         542 :     scanstate->tupstore_filled = false;
     180                 :             : 
     181                 :         542 :     scanstate->ns_names = tf->ns_names;
     182                 :             : 
     183                 :         542 :     scanstate->ns_uris =
     184                 :         542 :         ExecInitExprList(tf->ns_uris, (PlanState *) scanstate);
     185                 :         542 :     scanstate->docexpr =
     186                 :         542 :         ExecInitExpr((Expr *) tf->docexpr, (PlanState *) scanstate);
     187                 :         542 :     scanstate->rowexpr =
     188                 :         542 :         ExecInitExpr((Expr *) tf->rowexpr, (PlanState *) scanstate);
     189                 :         542 :     scanstate->colexprs =
     190                 :         542 :         ExecInitExprList(tf->colexprs, (PlanState *) scanstate);
     191                 :         542 :     scanstate->coldefexprs =
     192                 :         542 :         ExecInitExprList(tf->coldefexprs, (PlanState *) scanstate);
     193                 :         542 :     scanstate->colvalexprs =
     194                 :         542 :         ExecInitExprList(tf->colvalexprs, (PlanState *) scanstate);
     195                 :         542 :     scanstate->passingvalexprs =
     196                 :         542 :         ExecInitExprList(tf->passingvalexprs, (PlanState *) scanstate);
     197                 :             : 
     198                 :         542 :     scanstate->notnulls = tf->notnulls;
     199                 :             : 
     200                 :             :     /* these are allocated now and initialized later */
     201                 :         542 :     scanstate->in_functions = palloc_array(FmgrInfo, tupdesc->natts);
     202                 :         542 :     scanstate->typioparams = palloc_array(Oid, tupdesc->natts);
     203                 :             : 
     204                 :             :     /*
     205                 :             :      * Fill in the necessary fmgr infos.
     206                 :             :      */
     207         [ +  + ]:        2066 :     for (i = 0; i < tupdesc->natts; i++)
     208                 :             :     {
     209                 :             :         Oid         in_funcid;
     210                 :             : 
     211                 :        1524 :         getTypeInputInfo(TupleDescAttr(tupdesc, i)->atttypid,
     212                 :        1524 :                          &in_funcid, &scanstate->typioparams[i]);
     213                 :        1524 :         fmgr_info(in_funcid, &scanstate->in_functions[i]);
     214                 :             :     }
     215                 :             : 
     216                 :         542 :     return scanstate;
     217                 :             : }
     218                 :             : 
     219                 :             : /* ----------------------------------------------------------------
     220                 :             :  *      ExecEndTableFuncScan
     221                 :             :  *
     222                 :             :  *      frees any storage allocated through C routines.
     223                 :             :  * ----------------------------------------------------------------
     224                 :             :  */
     225                 :             : void
     226                 :         466 : ExecEndTableFuncScan(TableFuncScanState *node)
     227                 :             : {
     228                 :             :     /*
     229                 :             :      * Release tuplestore resources
     230                 :             :      */
     231         [ +  + ]:         466 :     if (node->tupstore != NULL)
     232                 :         352 :         tuplestore_end(node->tupstore);
     233                 :         466 :     node->tupstore = NULL;
     234                 :         466 : }
     235                 :             : 
     236                 :             : /* ----------------------------------------------------------------
     237                 :             :  *      ExecReScanTableFuncScan
     238                 :             :  *
     239                 :             :  *      Rescans the relation.
     240                 :             :  * ----------------------------------------------------------------
     241                 :             :  */
     242                 :             : void
     243                 :         332 : ExecReScanTableFuncScan(TableFuncScanState *node)
     244                 :             : {
     245                 :         332 :     Bitmapset  *chgparam = node->ss.ps.chgParam;
     246                 :             : 
     247         [ -  + ]:         332 :     if (node->ss.ps.ps_ResultTupleSlot)
     248                 :           0 :         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
     249                 :         332 :     ExecScanReScan(&node->ss);
     250                 :             : 
     251                 :             :     /*
     252                 :             :      * Recompute when parameters are changed.  It's important to use
     253                 :             :      * tuplestore_clear() rather than tuplestore_end() here so that we keep
     254                 :             :      * track of the maximum storage used by all rescans.
     255                 :             :      */
     256         [ +  - ]:         332 :     if (chgparam)
     257                 :             :     {
     258         [ +  + ]:         332 :         if (node->tupstore != NULL)
     259                 :         164 :             tuplestore_clear(node->tupstore);
     260                 :         332 :         node->tupstore_filled = false;
     261                 :             :     }
     262         [ #  # ]:           0 :     else if (node->tupstore != NULL)
     263                 :           0 :         tuplestore_rescan(node->tupstore);
     264                 :         332 : }
     265                 :             : 
     266                 :             : /* ----------------------------------------------------------------
     267                 :             :  *      tfuncFetchRows
     268                 :             :  *
     269                 :             :  *      Read rows from a TableFunc producer
     270                 :             :  * ----------------------------------------------------------------
     271                 :             :  */
     272                 :             : static void
     273                 :         592 : tfuncFetchRows(TableFuncScanState *tstate, ExprContext *econtext)
     274                 :             : {
     275                 :         592 :     const TableFuncRoutine *routine = tstate->routine;
     276                 :             :     MemoryContext oldcxt;
     277                 :             :     Datum       value;
     278                 :             :     bool        isnull;
     279                 :             : 
     280                 :             :     Assert(tstate->opaque == NULL);
     281                 :             : 
     282                 :             :     /* build tuplestore for the result, unless we have one from a prior scan */
     283                 :         592 :     oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
     284         [ +  + ]:         592 :     if (tstate->tupstore == NULL)
     285                 :         428 :         tstate->tupstore = tuplestore_begin_heap(false, false, work_mem);
     286                 :             : 
     287                 :             :     /*
     288                 :             :      * Each call to fetch a new set of rows - of which there may be very many
     289                 :             :      * if XMLTABLE or JSON_TABLE is being used in a lateral join - will
     290                 :             :      * allocate a possibly substantial amount of memory, so we cannot use the
     291                 :             :      * per-query context here. perTableCxt now serves the same function as
     292                 :             :      * "argcontext" does in FunctionScan - a place to store per-one-call (i.e.
     293                 :             :      * one result table) lifetime data (as opposed to per-query or
     294                 :             :      * per-result-tuple).
     295                 :             :      */
     296                 :         592 :     MemoryContextSwitchTo(tstate->perTableCxt);
     297                 :             : 
     298         [ +  + ]:         592 :     PG_TRY();
     299                 :             :     {
     300                 :         592 :         routine->InitOpaque(tstate,
     301                 :         592 :                             tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor->natts);
     302                 :             : 
     303                 :             :         /*
     304                 :             :          * If evaluating the document expression returns NULL, the table
     305                 :             :          * expression is empty and we return immediately.
     306                 :             :          */
     307                 :         592 :         value = ExecEvalExpr(tstate->docexpr, econtext, &isnull);
     308                 :             : 
     309         [ +  + ]:         592 :         if (!isnull)
     310                 :             :         {
     311                 :             :             /* otherwise, pass the document value to the table builder */
     312                 :         588 :             tfuncInitialize(tstate, econtext, value);
     313                 :             : 
     314                 :             :             /* initialize ordinality counter */
     315                 :         580 :             tstate->ordinal = 1;
     316                 :             : 
     317                 :             :             /* Load all rows into the tuplestore, and we're done */
     318                 :         580 :             tfuncLoadRows(tstate, econtext);
     319                 :             :         }
     320                 :             :     }
     321                 :          76 :     PG_CATCH();
     322                 :             :     {
     323         [ +  - ]:          76 :         if (tstate->opaque != NULL)
     324                 :          76 :             routine->DestroyOpaque(tstate);
     325                 :          76 :         PG_RE_THROW();
     326                 :             :     }
     327         [ -  + ]:         516 :     PG_END_TRY();
     328                 :             : 
     329                 :             :     /* clean up and return to original memory context */
     330                 :             : 
     331         [ +  - ]:         516 :     if (tstate->opaque != NULL)
     332                 :             :     {
     333                 :         516 :         routine->DestroyOpaque(tstate);
     334                 :         516 :         tstate->opaque = NULL;
     335                 :             :     }
     336                 :             : 
     337                 :         516 :     MemoryContextSwitchTo(oldcxt);
     338                 :         516 :     MemoryContextReset(tstate->perTableCxt);
     339                 :         516 : }
     340                 :             : 
     341                 :             : /*
     342                 :             :  * Fill in namespace declarations, the row filter, and column filters in a
     343                 :             :  * table expression builder context.
     344                 :             :  */
     345                 :             : static void
     346                 :         588 : tfuncInitialize(TableFuncScanState *tstate, ExprContext *econtext, Datum doc)
     347                 :             : {
     348                 :         588 :     const TableFuncRoutine *routine = tstate->routine;
     349                 :             :     TupleDesc   tupdesc;
     350                 :             :     ListCell   *lc1,
     351                 :             :                *lc2;
     352                 :             :     bool        isnull;
     353                 :             :     int         colno;
     354                 :             :     Datum       value;
     355                 :         588 :     int         ordinalitycol =
     356                 :         588 :         ((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
     357                 :             : 
     358                 :             :     /*
     359                 :             :      * Install the document as a possibly-toasted Datum into the tablefunc
     360                 :             :      * context.
     361                 :             :      */
     362                 :         588 :     routine->SetDocument(tstate, doc);
     363                 :             : 
     364                 :             :     /* Evaluate namespace specifications */
     365   [ +  +  +  +  :         592 :     forboth(lc1, tstate->ns_uris, lc2, tstate->ns_names)
          +  +  +  +  +  
             +  +  -  +  
                      + ]
     366                 :             :     {
     367                 :          12 :         ExprState  *expr = (ExprState *) lfirst(lc1);
     368                 :          12 :         String     *ns_node = lfirst_node(String, lc2);
     369                 :             :         char       *ns_uri;
     370                 :             :         char       *ns_name;
     371                 :             : 
     372                 :          12 :         value = ExecEvalExpr(expr, econtext, &isnull);
     373         [ -  + ]:          12 :         if (isnull)
     374         [ #  # ]:           0 :             ereport(ERROR,
     375                 :             :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     376                 :             :                      errmsg("namespace URI must not be null")));
     377                 :          12 :         ns_uri = TextDatumGetCString(value);
     378                 :             : 
     379                 :             :         /* DEFAULT is passed down to SetNamespace as NULL */
     380         [ +  + ]:          12 :         ns_name = ns_node ? strVal(ns_node) : NULL;
     381                 :             : 
     382                 :          12 :         routine->SetNamespace(tstate, ns_name, ns_uri);
     383                 :             :     }
     384                 :             : 
     385                 :             :     /*
     386                 :             :      * Install the row filter expression, if any, into the table builder
     387                 :             :      * context.
     388                 :             :      */
     389         [ +  + ]:         580 :     if (routine->SetRowFilter)
     390                 :             :     {
     391                 :         172 :         value = ExecEvalExpr(tstate->rowexpr, econtext, &isnull);
     392         [ -  + ]:         172 :         if (isnull)
     393         [ #  # ]:           0 :             ereport(ERROR,
     394                 :             :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     395                 :             :                      errmsg("row filter expression must not be null")));
     396                 :             : 
     397                 :         172 :         routine->SetRowFilter(tstate, TextDatumGetCString(value));
     398                 :             :     }
     399                 :             : 
     400                 :             :     /*
     401                 :             :      * Install the column filter expressions into the table builder context.
     402                 :             :      * If an expression is given, use that; otherwise the column name itself
     403                 :             :      * is the column filter.
     404                 :             :      */
     405                 :         580 :     colno = 0;
     406                 :         580 :     tupdesc = tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
     407   [ +  +  +  +  :        1144 :     foreach(lc1, tstate->colexprs)
                   +  + ]
     408                 :             :     {
     409                 :             :         char       *colfilter;
     410                 :         564 :         Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
     411                 :             : 
     412         [ +  + ]:         564 :         if (colno != ordinalitycol)
     413                 :             :         {
     414                 :         516 :             ExprState  *colexpr = lfirst(lc1);
     415                 :             : 
     416         [ +  + ]:         516 :             if (colexpr != NULL)
     417                 :             :             {
     418                 :         404 :                 value = ExecEvalExpr(colexpr, econtext, &isnull);
     419         [ -  + ]:         404 :                 if (isnull)
     420         [ #  # ]:           0 :                     ereport(ERROR,
     421                 :             :                             (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     422                 :             :                              errmsg("column filter expression must not be null"),
     423                 :             :                              errdetail("Filter for column \"%s\" is null.",
     424                 :             :                                        NameStr(att->attname))));
     425                 :         404 :                 colfilter = TextDatumGetCString(value);
     426                 :             :             }
     427                 :             :             else
     428                 :         112 :                 colfilter = NameStr(att->attname);
     429                 :             : 
     430                 :         516 :             routine->SetColumnFilter(tstate, colfilter, colno);
     431                 :             :         }
     432                 :             : 
     433                 :         564 :         colno++;
     434                 :             :     }
     435                 :         580 : }
     436                 :             : 
     437                 :             : /*
     438                 :             :  * Load all the rows from the TableFunc table builder into a tuplestore.
     439                 :             :  */
     440                 :             : static void
     441                 :         580 : tfuncLoadRows(TableFuncScanState *tstate, ExprContext *econtext)
     442                 :             : {
     443                 :         580 :     const TableFuncRoutine *routine = tstate->routine;
     444                 :         580 :     TupleTableSlot *slot = tstate->ss.ss_ScanTupleSlot;
     445                 :         580 :     TupleDesc   tupdesc = slot->tts_tupleDescriptor;
     446                 :         580 :     Datum      *values = slot->tts_values;
     447                 :         580 :     bool       *nulls = slot->tts_isnull;
     448                 :         580 :     int         natts = tupdesc->natts;
     449                 :             :     MemoryContext oldcxt;
     450                 :             :     int         ordinalitycol;
     451                 :             : 
     452                 :         580 :     ordinalitycol =
     453                 :         580 :         ((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
     454                 :             : 
     455                 :             :     /*
     456                 :             :      * We need a short-lived memory context that we can clean up each time
     457                 :             :      * around the loop, to avoid wasting space. Our default per-tuple context
     458                 :             :      * is fine for the job, since we won't have used it for anything yet in
     459                 :             :      * this tuple cycle.
     460                 :             :      */
     461                 :         580 :     oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     462                 :             : 
     463                 :             :     /*
     464                 :             :      * Keep requesting rows from the table builder until there aren't any.
     465                 :             :      */
     466         [ +  + ]:       17133 :     while (routine->FetchRow(tstate))
     467                 :             :     {
     468                 :       16621 :         ListCell   *cell = list_head(tstate->coldefexprs);
     469                 :             :         int         colno;
     470                 :             : 
     471         [ -  + ]:       16621 :         CHECK_FOR_INTERRUPTS();
     472                 :             : 
     473                 :       16621 :         ExecClearTuple(tstate->ss.ss_ScanTupleSlot);
     474                 :             : 
     475                 :             :         /*
     476                 :             :          * Obtain the value of each column for this row, installing them into
     477                 :             :          * the slot; then add the tuple to the tuplestore.
     478                 :             :          */
     479         [ +  + ]:      113511 :         for (colno = 0; colno < natts; colno++)
     480                 :             :         {
     481                 :       96958 :             Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
     482                 :             : 
     483         [ +  + ]:       96958 :             if (colno == ordinalitycol)
     484                 :             :             {
     485                 :             :                 /* Fast path for ordinality column */
     486                 :         180 :                 values[colno] = Int32GetDatum(tstate->ordinal++);
     487                 :         180 :                 nulls[colno] = false;
     488                 :             :             }
     489                 :             :             else
     490                 :             :             {
     491                 :             :                 bool        isnull;
     492                 :             : 
     493                 :       96778 :                 values[colno] = routine->GetValue(tstate,
     494                 :             :                                                   colno,
     495                 :             :                                                   att->atttypid,
     496                 :             :                                                   att->atttypmod,
     497                 :             :                                                   &isnull);
     498                 :             : 
     499                 :             :                 /* No value?  Evaluate and apply the default, if any */
     500   [ +  +  +  + ]:       96714 :                 if (isnull && cell != NULL)
     501                 :             :                 {
     502                 :       15296 :                     ExprState  *coldefexpr = (ExprState *) lfirst(cell);
     503                 :             : 
     504         [ +  + ]:       15296 :                     if (coldefexpr != NULL)
     505                 :         148 :                         values[colno] = ExecEvalExpr(coldefexpr, econtext,
     506                 :             :                                                      &isnull);
     507                 :             :                 }
     508                 :             : 
     509                 :             :                 /* Verify a possible NOT NULL constraint */
     510   [ +  +  +  + ]:       96714 :                 if (isnull && bms_is_member(colno, tstate->notnulls))
     511         [ +  - ]:           4 :                     ereport(ERROR,
     512                 :             :                             (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
     513                 :             :                              errmsg("null is not allowed in column \"%s\"",
     514                 :             :                                     NameStr(att->attname))));
     515                 :             : 
     516                 :       96710 :                 nulls[colno] = isnull;
     517                 :             :             }
     518                 :             : 
     519                 :             :             /* advance list of default expressions */
     520         [ +  + ]:       96890 :             if (cell != NULL)
     521                 :       91806 :                 cell = lnext(tstate->coldefexprs, cell);
     522                 :             :         }
     523                 :             : 
     524                 :       16553 :         tuplestore_putvalues(tstate->tupstore, tupdesc, values, nulls);
     525                 :             : 
     526                 :       16553 :         MemoryContextReset(econtext->ecxt_per_tuple_memory);
     527                 :             :     }
     528                 :             : 
     529                 :         512 :     MemoryContextSwitchTo(oldcxt);
     530                 :         512 : }
        

Generated by: LCOV version 2.0-1