LCOV - code coverage report
Current view: top level - src/backend/executor - nodeTableFuncscan.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 96.7 % 209 202
Test Date: 2026-01-26 10:56:24 Functions: 88.9 % 9 8
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 72.0 % 100 72

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

Generated by: LCOV version 2.3.2-1