LCOV - code coverage report
Current view: top level - src/backend/executor - execSRF.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 95.8 % 377 361
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 9 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 62.6 % 211 132

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * execSRF.c
       4                 :             :  *        Routines implementing the API for set-returning functions
       5                 :             :  *
       6                 :             :  * This file serves nodeFunctionscan.c and nodeProjectSet.c, providing
       7                 :             :  * common code for calling set-returning functions according to the
       8                 :             :  * ReturnSetInfo API.
       9                 :             :  *
      10                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      11                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      12                 :             :  *
      13                 :             :  *
      14                 :             :  * IDENTIFICATION
      15                 :             :  *        src/backend/executor/execSRF.c
      16                 :             :  *
      17                 :             :  *-------------------------------------------------------------------------
      18                 :             :  */
      19                 :             : #include "postgres.h"
      20                 :             : 
      21                 :             : #include "access/htup_details.h"
      22                 :             : #include "catalog/objectaccess.h"
      23                 :             : #include "catalog/pg_proc.h"
      24                 :             : #include "funcapi.h"
      25                 :             : #include "miscadmin.h"
      26                 :             : #include "nodes/nodeFuncs.h"
      27                 :             : #include "parser/parse_coerce.h"
      28                 :             : #include "pgstat.h"
      29                 :             : #include "utils/acl.h"
      30                 :             : #include "utils/builtins.h"
      31                 :             : #include "utils/lsyscache.h"
      32                 :             : #include "utils/memutils.h"
      33                 :             : #include "utils/typcache.h"
      34                 :             : 
      35                 :             : 
      36                 :             : /* static function decls */
      37                 :             : static void init_sexpr(Oid foid, Oid input_collation, Expr *node,
      38                 :             :                                            SetExprState *sexpr, PlanState *parent,
      39                 :             :                                            MemoryContext sexprCxt, bool allowSRF, bool needDescForSRF);
      40                 :             : static void ShutdownSetExpr(Datum arg);
      41                 :             : static void ExecEvalFuncArgs(FunctionCallInfo fcinfo,
      42                 :             :                                                          List *argList, ExprContext *econtext);
      43                 :             : static void ExecPrepareTuplestoreResult(SetExprState *sexpr,
      44                 :             :                                                                                 ExprContext *econtext,
      45                 :             :                                                                                 Tuplestorestate *resultStore,
      46                 :             :                                                                                 TupleDesc resultDesc);
      47                 :             : static void tupledesc_match(TupleDesc dst_tupdesc, TupleDesc src_tupdesc);
      48                 :             : 
      49                 :             : 
      50                 :             : /*
      51                 :             :  * Prepare function call in FROM (ROWS FROM) for execution.
      52                 :             :  *
      53                 :             :  * This is used by nodeFunctionscan.c.
      54                 :             :  */
      55                 :             : SetExprState *
      56                 :        5622 : ExecInitTableFunctionResult(Expr *expr,
      57                 :             :                                                         ExprContext *econtext, PlanState *parent)
      58                 :             : {
      59                 :        5622 :         SetExprState *state = makeNode(SetExprState);
      60                 :             : 
      61                 :        5622 :         state->funcReturnsSet = false;
      62                 :        5622 :         state->expr = expr;
      63                 :        5622 :         state->func.fn_oid = InvalidOid;
      64                 :             : 
      65                 :             :         /*
      66                 :             :          * Normally the passed expression tree will be a FuncExpr, since the
      67                 :             :          * grammar only allows a function call at the top level of a table
      68                 :             :          * function reference.  However, if the function doesn't return set then
      69                 :             :          * the planner might have replaced the function call via constant-folding
      70                 :             :          * or inlining.  So if we see any other kind of expression node, execute
      71                 :             :          * it via the general ExecEvalExpr() code.  That code path will not
      72                 :             :          * support set-returning functions buried in the expression, though.
      73                 :             :          */
      74         [ +  + ]:        5622 :         if (IsA(expr, FuncExpr))
      75                 :             :         {
      76                 :        5603 :                 FuncExpr   *func = (FuncExpr *) expr;
      77                 :             : 
      78                 :        5603 :                 state->funcReturnsSet = func->funcretset;
      79                 :        5603 :                 state->args = ExecInitExprList(func->args, parent);
      80                 :             : 
      81                 :       11206 :                 init_sexpr(func->funcid, func->inputcollid, expr, state, parent,
      82                 :        5603 :                                    econtext->ecxt_per_query_memory, func->funcretset, false);
      83                 :        5603 :         }
      84                 :             :         else
      85                 :             :         {
      86                 :          19 :                 state->elidedFuncState = ExecInitExpr(expr, parent);
      87                 :             :         }
      88                 :             : 
      89                 :       11244 :         return state;
      90                 :        5622 : }
      91                 :             : 
      92                 :             : /*
      93                 :             :  *              ExecMakeTableFunctionResult
      94                 :             :  *
      95                 :             :  * Evaluate a table function, producing a materialized result in a Tuplestore
      96                 :             :  * object.
      97                 :             :  *
      98                 :             :  * This is used by nodeFunctionscan.c.
      99                 :             :  */
     100                 :             : Tuplestorestate *
     101                 :        7323 : ExecMakeTableFunctionResult(SetExprState *setexpr,
     102                 :             :                                                         ExprContext *econtext,
     103                 :             :                                                         MemoryContext argContext,
     104                 :             :                                                         TupleDesc expectedDesc,
     105                 :             :                                                         bool randomAccess)
     106                 :             : {
     107                 :        7323 :         Tuplestorestate *tupstore = NULL;
     108                 :        7323 :         TupleDesc       tupdesc = NULL;
     109                 :        7323 :         Oid                     funcrettype;
     110                 :        7323 :         bool            returnsTuple;
     111                 :        7323 :         bool            returnsSet = false;
     112                 :        7323 :         FunctionCallInfo fcinfo;
     113                 :        7323 :         PgStat_FunctionCallUsage fcusage;
     114                 :        7323 :         ReturnSetInfo rsinfo;
     115                 :        7323 :         HeapTupleData tmptup;
     116                 :        7323 :         MemoryContext callerContext;
     117                 :        7323 :         bool            first_time = true;
     118                 :             : 
     119                 :             :         /*
     120                 :             :          * Execute per-tablefunc actions in appropriate context.
     121                 :             :          *
     122                 :             :          * The FunctionCallInfo needs to live across all the calls to a
     123                 :             :          * ValuePerCall function, so it can't be allocated in the per-tuple
     124                 :             :          * context. Similarly, the function arguments need to be evaluated in a
     125                 :             :          * context that is longer lived than the per-tuple context: The argument
     126                 :             :          * values would otherwise disappear when we reset that context in the
     127                 :             :          * inner loop.  As the caller's CurrentMemoryContext is typically a
     128                 :             :          * query-lifespan context, we don't want to leak memory there.  We require
     129                 :             :          * the caller to pass a separate memory context that can be used for this,
     130                 :             :          * and can be reset each time through to avoid bloat.
     131                 :             :          */
     132                 :        7323 :         MemoryContextReset(argContext);
     133                 :        7323 :         callerContext = MemoryContextSwitchTo(argContext);
     134                 :             : 
     135                 :        7323 :         funcrettype = exprType((Node *) setexpr->expr);
     136                 :             : 
     137                 :        7323 :         returnsTuple = type_is_rowtype(funcrettype);
     138                 :             : 
     139                 :             :         /*
     140                 :             :          * Prepare a resultinfo node for communication.  We always do this even if
     141                 :             :          * not expecting a set result, so that we can pass expectedDesc.  In the
     142                 :             :          * generic-expression case, the expression doesn't actually get to see the
     143                 :             :          * resultinfo, but set it up anyway because we use some of the fields as
     144                 :             :          * our own state variables.
     145                 :             :          */
     146                 :        7323 :         rsinfo.type = T_ReturnSetInfo;
     147                 :        7323 :         rsinfo.econtext = econtext;
     148                 :        7323 :         rsinfo.expectedDesc = expectedDesc;
     149                 :        7323 :         rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize | SFRM_Materialize_Preferred);
     150         [ +  + ]:        7323 :         if (randomAccess)
     151                 :          11 :                 rsinfo.allowedModes |= (int) SFRM_Materialize_Random;
     152                 :        7323 :         rsinfo.returnMode = SFRM_ValuePerCall;
     153                 :             :         /* isDone is filled below */
     154                 :        7323 :         rsinfo.setResult = NULL;
     155                 :        7323 :         rsinfo.setDesc = NULL;
     156                 :             : 
     157                 :        7323 :         fcinfo = palloc(SizeForFunctionCallInfo(list_length(setexpr->args)));
     158                 :             : 
     159                 :             :         /*
     160                 :             :          * Normally the passed expression tree will be a SetExprState, since the
     161                 :             :          * grammar only allows a function call at the top level of a table
     162                 :             :          * function reference.  However, if the function doesn't return set then
     163                 :             :          * the planner might have replaced the function call via constant-folding
     164                 :             :          * or inlining.  So if we see any other kind of expression node, execute
     165                 :             :          * it via the general ExecEvalExpr() code; the only difference is that we
     166                 :             :          * don't get a chance to pass a special ReturnSetInfo to any functions
     167                 :             :          * buried in the expression.
     168                 :             :          */
     169         [ +  + ]:        7323 :         if (!setexpr->elidedFuncState)
     170                 :             :         {
     171                 :             :                 /*
     172                 :             :                  * This path is similar to ExecMakeFunctionResultSet.
     173                 :             :                  */
     174                 :        7304 :                 returnsSet = setexpr->funcReturnsSet;
     175                 :        7304 :                 InitFunctionCallInfoData(*fcinfo, &(setexpr->func),
     176                 :             :                                                                  list_length(setexpr->args),
     177                 :             :                                                                  setexpr->fcinfo->fncollation,
     178                 :             :                                                                  NULL, (Node *) &rsinfo);
     179                 :             :                 /* evaluate the function's argument list */
     180         [ +  - ]:        7304 :                 Assert(CurrentMemoryContext == argContext);
     181                 :        7304 :                 ExecEvalFuncArgs(fcinfo, setexpr->args, econtext);
     182                 :             : 
     183                 :             :                 /*
     184                 :             :                  * If function is strict, and there are any NULL arguments, skip
     185                 :             :                  * calling the function and act like it returned NULL (or an empty
     186                 :             :                  * set, in the returns-set case).
     187                 :             :                  */
     188         [ +  + ]:        7304 :                 if (setexpr->func.fn_strict)
     189                 :             :                 {
     190                 :        6333 :                         int                     i;
     191                 :             : 
     192         [ +  + ]:       20408 :                         for (i = 0; i < fcinfo->nargs; i++)
     193                 :             :                         {
     194         [ +  + ]:       14384 :                                 if (fcinfo->args[i].isnull)
     195                 :         309 :                                         goto no_function_result;
     196                 :       14075 :                         }
     197      [ +  +  + ]:        6333 :                 }
     198                 :        6995 :         }
     199                 :             :         else
     200                 :             :         {
     201                 :             :                 /* Treat setexpr as a generic expression */
     202                 :          19 :                 InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL);
     203                 :             :         }
     204                 :             : 
     205                 :             :         /*
     206                 :             :          * Switch to short-lived context for calling the function or expression.
     207                 :             :          */
     208                 :        7014 :         MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     209                 :             : 
     210                 :             :         /*
     211                 :             :          * Loop to handle the ValuePerCall protocol (which is also the same
     212                 :             :          * behavior needed in the generic ExecEvalExpr path).
     213                 :             :          */
     214                 :     1618983 :         for (;;)
     215                 :             :         {
     216                 :     1618983 :                 Datum           result;
     217                 :             : 
     218         [ +  - ]:     1618983 :                 CHECK_FOR_INTERRUPTS();
     219                 :             : 
     220                 :             :                 /*
     221                 :             :                  * Reset per-tuple memory context before each call of the function or
     222                 :             :                  * expression. This cleans up any local memory the function may leak
     223                 :             :                  * when called.
     224                 :             :                  */
     225                 :     1618983 :                 ResetExprContext(econtext);
     226                 :             : 
     227                 :             :                 /* Call the function or expression one time */
     228         [ +  + ]:     1618983 :                 if (!setexpr->elidedFuncState)
     229                 :             :                 {
     230                 :     1618255 :                         pgstat_init_function_usage(fcinfo, &fcusage);
     231                 :             : 
     232                 :     1618255 :                         fcinfo->isnull = false;
     233                 :     1618255 :                         rsinfo.isDone = ExprSingleResult;
     234                 :     1618255 :                         result = FunctionCallInvoke(fcinfo);
     235                 :             : 
     236                 :     1618255 :                         pgstat_end_function_usage(&fcusage,
     237                 :     1618255 :                                                                           rsinfo.isDone != ExprMultipleResult);
     238                 :     1618255 :                 }
     239                 :             :                 else
     240                 :             :                 {
     241                 :         728 :                         result =
     242                 :         728 :                                 ExecEvalExpr(setexpr->elidedFuncState, econtext, &fcinfo->isnull);
     243                 :         728 :                         rsinfo.isDone = ExprSingleResult;
     244                 :             :                 }
     245                 :             : 
     246                 :             :                 /* Which protocol does function want to use? */
     247         [ +  + ]:     1618983 :                 if (rsinfo.returnMode == SFRM_ValuePerCall)
     248                 :             :                 {
     249                 :             :                         /*
     250                 :             :                          * Check for end of result set.
     251                 :             :                          */
     252         [ +  + ]:     1616523 :                         if (rsinfo.isDone == ExprEndResult)
     253                 :        1757 :                                 break;
     254                 :             : 
     255                 :             :                         /*
     256                 :             :                          * If first time through, build tuplestore for result.  For a
     257                 :             :                          * scalar function result type, also make a suitable tupdesc.
     258                 :             :                          */
     259         [ +  + ]:     1614766 :                         if (first_time)
     260                 :             :                         {
     261                 :       10446 :                                 MemoryContext oldcontext =
     262                 :        5223 :                                         MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
     263                 :             : 
     264                 :        5223 :                                 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
     265                 :        5223 :                                 rsinfo.setResult = tupstore;
     266         [ +  + ]:        5223 :                                 if (!returnsTuple)
     267                 :             :                                 {
     268                 :        3458 :                                         tupdesc = CreateTemplateTupleDesc(1);
     269                 :        6916 :                                         TupleDescInitEntry(tupdesc,
     270                 :             :                                                                            (AttrNumber) 1,
     271                 :             :                                                                            "column",
     272                 :        3458 :                                                                            funcrettype,
     273                 :             :                                                                            -1,
     274                 :             :                                                                            0);
     275                 :        3458 :                                         rsinfo.setDesc = tupdesc;
     276                 :        3458 :                                 }
     277                 :        5223 :                                 MemoryContextSwitchTo(oldcontext);
     278                 :        5223 :                         }
     279                 :             : 
     280                 :             :                         /*
     281                 :             :                          * Store current resultset item.
     282                 :             :                          */
     283         [ +  + ]:     1614766 :                         if (returnsTuple)
     284                 :             :                         {
     285         [ +  + ]:        9249 :                                 if (!fcinfo->isnull)
     286                 :             :                                 {
     287                 :        9243 :                                         HeapTupleHeader td = DatumGetHeapTupleHeader(result);
     288                 :             : 
     289         [ +  + ]:        9243 :                                         if (tupdesc == NULL)
     290                 :             :                                         {
     291                 :        3526 :                                                 MemoryContext oldcontext =
     292                 :        1763 :                                                         MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
     293                 :             : 
     294                 :             :                                                 /*
     295                 :             :                                                  * This is the first non-NULL result from the
     296                 :             :                                                  * function.  Use the type info embedded in the
     297                 :             :                                                  * rowtype Datum to look up the needed tupdesc.  Make
     298                 :             :                                                  * a copy for the query.
     299                 :             :                                                  */
     300                 :        3526 :                                                 tupdesc = lookup_rowtype_tupdesc_copy(HeapTupleHeaderGetTypeId(td),
     301                 :        1763 :                                                                                                                           HeapTupleHeaderGetTypMod(td));
     302                 :        1763 :                                                 rsinfo.setDesc = tupdesc;
     303                 :        1763 :                                                 MemoryContextSwitchTo(oldcontext);
     304                 :        1763 :                                         }
     305                 :             :                                         else
     306                 :             :                                         {
     307                 :             :                                                 /*
     308                 :             :                                                  * Verify all later returned rows have same subtype;
     309                 :             :                                                  * necessary in case the type is RECORD.
     310                 :             :                                                  */
     311         [ +  - ]:        7480 :                                                 if (HeapTupleHeaderGetTypeId(td) != tupdesc->tdtypeid ||
     312                 :        7480 :                                                         HeapTupleHeaderGetTypMod(td) != tupdesc->tdtypmod)
     313   [ #  #  #  # ]:           0 :                                                         ereport(ERROR,
     314                 :             :                                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     315                 :             :                                                                          errmsg("rows returned by function are not all of the same row type")));
     316                 :             :                                         }
     317                 :             : 
     318                 :             :                                         /*
     319                 :             :                                          * tuplestore_puttuple needs a HeapTuple not a bare
     320                 :             :                                          * HeapTupleHeader, but it doesn't need all the fields.
     321                 :             :                                          */
     322                 :        9243 :                                         tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
     323                 :        9243 :                                         tmptup.t_data = td;
     324                 :             : 
     325                 :        9243 :                                         tuplestore_puttuple(tupstore, &tmptup);
     326                 :        9243 :                                 }
     327                 :             :                                 else
     328                 :             :                                 {
     329                 :             :                                         /*
     330                 :             :                                          * NULL result from a tuple-returning function; expand it
     331                 :             :                                          * to a row of all nulls.  We rely on the expectedDesc to
     332                 :             :                                          * form such rows.  (Note: this would be problematic if
     333                 :             :                                          * tuplestore_putvalues saved the tdtypeid/tdtypmod from
     334                 :             :                                          * the provided descriptor, since that might not match
     335                 :             :                                          * what we get from the function itself.  But it doesn't.)
     336                 :             :                                          */
     337                 :           6 :                                         int                     natts = expectedDesc->natts;
     338                 :           6 :                                         bool       *nullflags;
     339                 :             : 
     340                 :           6 :                                         nullflags = (bool *) palloc(natts * sizeof(bool));
     341                 :           6 :                                         memset(nullflags, true, natts * sizeof(bool));
     342                 :           6 :                                         tuplestore_putvalues(tupstore, expectedDesc, NULL, nullflags);
     343                 :           6 :                                 }
     344                 :        9249 :                         }
     345                 :             :                         else
     346                 :             :                         {
     347                 :             :                                 /* Scalar-type case: just store the function result */
     348                 :     1605517 :                                 tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo->isnull);
     349                 :             :                         }
     350                 :             : 
     351                 :             :                         /*
     352                 :             :                          * Are we done?
     353                 :             :                          */
     354         [ +  + ]:     1614766 :                         if (rsinfo.isDone != ExprMultipleResult)
     355                 :        3506 :                                 break;
     356                 :             : 
     357                 :             :                         /*
     358                 :             :                          * Check that set-returning functions were properly declared.
     359                 :             :                          * (Note: for historical reasons, we don't complain if a non-SRF
     360                 :             :                          * returns ExprEndResult; that's treated as returning NULL.)
     361                 :             :                          */
     362         [ +  - ]:     1611260 :                         if (!returnsSet)
     363   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     364                 :             :                                                 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
     365                 :             :                                                  errmsg("table-function protocol for value-per-call mode was not followed")));
     366                 :     1611260 :                 }
     367         [ +  - ]:        1004 :                 else if (rsinfo.returnMode == SFRM_Materialize)
     368                 :             :                 {
     369                 :             :                         /* check we're on the same page as the function author */
     370         [ +  - ]:        1004 :                         if (!first_time || rsinfo.isDone != ExprSingleResult || !returnsSet)
     371   [ #  #  #  # ]:           0 :                                 ereport(ERROR,
     372                 :             :                                                 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
     373                 :             :                                                  errmsg("table-function protocol for materialize mode was not followed")));
     374                 :             :                         /* Done evaluating the set result */
     375                 :        1004 :                         break;
     376                 :             :                 }
     377                 :             :                 else
     378   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     379                 :             :                                         (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
     380                 :             :                                          errmsg("unrecognized table-function returnMode: %d",
     381                 :             :                                                         (int) rsinfo.returnMode)));
     382                 :             : 
     383                 :     1611260 :                 first_time = false;
     384      [ -  +  + ]:     1617527 :         }
     385                 :             : 
     386                 :             : no_function_result:
     387                 :             : 
     388                 :             :         /*
     389                 :             :          * If we got nothing from the function (ie, an empty-set or NULL result),
     390                 :             :          * we have to create the tuplestore to return, and if it's a
     391                 :             :          * non-set-returning function then insert a single all-nulls row.  As
     392                 :             :          * above, we depend on the expectedDesc to manufacture the dummy row.
     393                 :             :          */
     394         [ +  + ]:        6562 :         if (rsinfo.setResult == NULL)
     395                 :             :         {
     396                 :         700 :                 MemoryContext oldcontext =
     397                 :         350 :                         MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
     398                 :             : 
     399                 :         350 :                 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
     400                 :         350 :                 rsinfo.setResult = tupstore;
     401                 :         350 :                 MemoryContextSwitchTo(oldcontext);
     402                 :             : 
     403         [ +  + ]:         350 :                 if (!returnsSet)
     404                 :             :                 {
     405                 :           1 :                         int                     natts = expectedDesc->natts;
     406                 :           1 :                         bool       *nullflags;
     407                 :             : 
     408                 :           1 :                         nullflags = (bool *) palloc(natts * sizeof(bool));
     409                 :           1 :                         memset(nullflags, true, natts * sizeof(bool));
     410                 :           1 :                         tuplestore_putvalues(tupstore, expectedDesc, NULL, nullflags);
     411                 :           1 :                 }
     412                 :         350 :         }
     413                 :             : 
     414                 :             :         /*
     415                 :             :          * If function provided a tupdesc, cross-check it.  We only really need to
     416                 :             :          * do this for functions returning RECORD, but might as well do it always.
     417                 :             :          */
     418         [ +  + ]:        6562 :         if (rsinfo.setDesc)
     419                 :             :         {
     420                 :        6210 :                 tupledesc_match(expectedDesc, rsinfo.setDesc);
     421                 :             : 
     422                 :             :                 /*
     423                 :             :                  * If it is a dynamically-allocated TupleDesc, free it: it is
     424                 :             :                  * typically allocated in a per-query context, so we must avoid
     425                 :             :                  * leaking it across multiple usages.
     426                 :             :                  */
     427         [ -  + ]:        6210 :                 if (rsinfo.setDesc->tdrefcount == -1)
     428                 :        6210 :                         FreeTupleDesc(rsinfo.setDesc);
     429                 :        6210 :         }
     430                 :             : 
     431                 :        6562 :         MemoryContextSwitchTo(callerContext);
     432                 :             : 
     433                 :             :         /* All done, pass back the tuplestore */
     434                 :        6562 :         return rsinfo.setResult;
     435                 :        7257 : }
     436                 :             : 
     437                 :             : 
     438                 :             : /*
     439                 :             :  * Prepare targetlist SRF function call for execution.
     440                 :             :  *
     441                 :             :  * This is used by nodeProjectSet.c.
     442                 :             :  */
     443                 :             : SetExprState *
     444                 :        2030 : ExecInitFunctionResultSet(Expr *expr,
     445                 :             :                                                   ExprContext *econtext, PlanState *parent)
     446                 :             : {
     447                 :        2030 :         SetExprState *state = makeNode(SetExprState);
     448                 :             : 
     449                 :        2030 :         state->funcReturnsSet = true;
     450                 :        2030 :         state->expr = expr;
     451                 :        2030 :         state->func.fn_oid = InvalidOid;
     452                 :             : 
     453                 :             :         /*
     454                 :             :          * Initialize metadata.  The expression node could be either a FuncExpr or
     455                 :             :          * an OpExpr.
     456                 :             :          */
     457         [ +  + ]:        2030 :         if (IsA(expr, FuncExpr))
     458                 :             :         {
     459                 :        2029 :                 FuncExpr   *func = (FuncExpr *) expr;
     460                 :             : 
     461                 :        2029 :                 state->args = ExecInitExprList(func->args, parent);
     462                 :        4058 :                 init_sexpr(func->funcid, func->inputcollid, expr, state, parent,
     463                 :        2029 :                                    econtext->ecxt_per_query_memory, true, true);
     464                 :        2029 :         }
     465         [ +  - ]:           1 :         else if (IsA(expr, OpExpr))
     466                 :             :         {
     467                 :           1 :                 OpExpr     *op = (OpExpr *) expr;
     468                 :             : 
     469                 :           1 :                 state->args = ExecInitExprList(op->args, parent);
     470                 :           2 :                 init_sexpr(op->opfuncid, op->inputcollid, expr, state, parent,
     471                 :           1 :                                    econtext->ecxt_per_query_memory, true, true);
     472                 :           1 :         }
     473                 :             :         else
     474   [ #  #  #  # ]:           0 :                 elog(ERROR, "unrecognized node type: %d",
     475                 :             :                          (int) nodeTag(expr));
     476                 :             : 
     477                 :             :         /* shouldn't get here unless the selected function returns set */
     478         [ +  - ]:        2030 :         Assert(state->func.fn_retset);
     479                 :             : 
     480                 :        4060 :         return state;
     481                 :        2030 : }
     482                 :             : 
     483                 :             : /*
     484                 :             :  *              ExecMakeFunctionResultSet
     485                 :             :  *
     486                 :             :  * Evaluate the arguments to a set-returning function and then call the
     487                 :             :  * function itself.  The argument expressions may not contain set-returning
     488                 :             :  * functions (the planner is supposed to have separated evaluation for those).
     489                 :             :  *
     490                 :             :  * This should be called in a short-lived (per-tuple) context, argContext
     491                 :             :  * needs to live until all rows have been returned (i.e. *isDone set to
     492                 :             :  * ExprEndResult or ExprSingleResult).
     493                 :             :  *
     494                 :             :  * This is used by nodeProjectSet.c.
     495                 :             :  */
     496                 :             : Datum
     497                 :      239978 : ExecMakeFunctionResultSet(SetExprState *fcache,
     498                 :             :                                                   ExprContext *econtext,
     499                 :             :                                                   MemoryContext argContext,
     500                 :             :                                                   bool *isNull,
     501                 :             :                                                   ExprDoneCond *isDone)
     502                 :             : {
     503                 :      239978 :         List       *arguments;
     504                 :      239978 :         Datum           result;
     505                 :      239978 :         FunctionCallInfo fcinfo;
     506                 :      239978 :         PgStat_FunctionCallUsage fcusage;
     507                 :      239978 :         ReturnSetInfo rsinfo;
     508                 :      239978 :         bool            callit;
     509                 :      239978 :         int                     i;
     510                 :             : 
     511                 :             : restart:
     512                 :             : 
     513                 :             :         /* Guard against stack overflow due to overly complex expressions */
     514                 :      242423 :         check_stack_depth();
     515                 :             : 
     516                 :             :         /*
     517                 :             :          * If a previous call of the function returned a set result in the form of
     518                 :             :          * a tuplestore, continue reading rows from the tuplestore until it's
     519                 :             :          * empty.
     520                 :             :          */
     521         [ +  + ]:      242423 :         if (fcache->funcResultStore)
     522                 :             :         {
     523                 :       13297 :                 TupleTableSlot *slot = fcache->funcResultSlot;
     524                 :       13297 :                 MemoryContext oldContext;
     525                 :       13297 :                 bool            foundTup;
     526                 :             : 
     527                 :             :                 /*
     528                 :             :                  * Have to make sure tuple in slot lives long enough, otherwise
     529                 :             :                  * clearing the slot could end up trying to free something already
     530                 :             :                  * freed.
     531                 :             :                  */
     532                 :       13297 :                 oldContext = MemoryContextSwitchTo(slot->tts_mcxt);
     533                 :       26594 :                 foundTup = tuplestore_gettupleslot(fcache->funcResultStore, true, false,
     534                 :       13297 :                                                                                    fcache->funcResultSlot);
     535                 :       13297 :                 MemoryContextSwitchTo(oldContext);
     536                 :             : 
     537         [ +  + ]:       13297 :                 if (foundTup)
     538                 :             :                 {
     539                 :       11096 :                         *isDone = ExprMultipleResult;
     540         [ +  + ]:       11096 :                         if (fcache->funcReturnsTuple)
     541                 :             :                         {
     542                 :             :                                 /* We must return the whole tuple as a Datum. */
     543                 :       10227 :                                 *isNull = false;
     544                 :       10227 :                                 return ExecFetchSlotHeapTupleDatum(fcache->funcResultSlot);
     545                 :             :                         }
     546                 :             :                         else
     547                 :             :                         {
     548                 :             :                                 /* Extract the first column and return it as a scalar. */
     549                 :         869 :                                 return slot_getattr(fcache->funcResultSlot, 1, isNull);
     550                 :             :                         }
     551                 :             :                 }
     552                 :             :                 /* Exhausted the tuplestore, so clean up */
     553                 :        2201 :                 tuplestore_end(fcache->funcResultStore);
     554                 :        2201 :                 fcache->funcResultStore = NULL;
     555                 :        2201 :                 *isDone = ExprEndResult;
     556                 :        2201 :                 *isNull = true;
     557                 :        2201 :                 return (Datum) 0;
     558                 :       13297 :         }
     559                 :             : 
     560                 :             :         /*
     561                 :             :          * arguments is a list of expressions to evaluate before passing to the
     562                 :             :          * function manager.  We skip the evaluation if it was already done in the
     563                 :             :          * previous call (ie, we are continuing the evaluation of a set-valued
     564                 :             :          * function).  Otherwise, collect the current argument values into fcinfo.
     565                 :             :          *
     566                 :             :          * The arguments have to live in a context that lives at least until all
     567                 :             :          * rows from this SRF have been returned, otherwise ValuePerCall SRFs
     568                 :             :          * would reference freed memory after the first returned row.
     569                 :             :          */
     570                 :      229126 :         fcinfo = fcache->fcinfo;
     571                 :      229126 :         arguments = fcache->args;
     572         [ +  + ]:      229126 :         if (!fcache->setArgsValid)
     573                 :             :         {
     574                 :       40245 :                 MemoryContext oldContext = MemoryContextSwitchTo(argContext);
     575                 :             : 
     576                 :       40245 :                 ExecEvalFuncArgs(fcinfo, arguments, econtext);
     577                 :       40245 :                 MemoryContextSwitchTo(oldContext);
     578                 :       40245 :         }
     579                 :             :         else
     580                 :             :         {
     581                 :             :                 /* Reset flag (we may set it again below) */
     582                 :      188881 :                 fcache->setArgsValid = false;
     583                 :             :         }
     584                 :             : 
     585                 :             :         /*
     586                 :             :          * Now call the function, passing the evaluated parameter values.
     587                 :             :          */
     588                 :             : 
     589                 :             :         /* Prepare a resultinfo node for communication. */
     590                 :      229126 :         fcinfo->resultinfo = (Node *) &rsinfo;
     591                 :      229126 :         rsinfo.type = T_ReturnSetInfo;
     592                 :      229126 :         rsinfo.econtext = econtext;
     593                 :      229126 :         rsinfo.expectedDesc = fcache->funcResultDesc;
     594                 :      229126 :         rsinfo.allowedModes = (int) (SFRM_ValuePerCall | SFRM_Materialize);
     595                 :             :         /* note we do not set SFRM_Materialize_Random or _Preferred */
     596                 :      229126 :         rsinfo.returnMode = SFRM_ValuePerCall;
     597                 :             :         /* isDone is filled below */
     598                 :      229126 :         rsinfo.setResult = NULL;
     599                 :      229126 :         rsinfo.setDesc = NULL;
     600                 :             : 
     601                 :             :         /*
     602                 :             :          * If function is strict, and there are any NULL arguments, skip calling
     603                 :             :          * the function.
     604                 :             :          */
     605                 :      229126 :         callit = true;
     606         [ +  + ]:      229126 :         if (fcache->func.fn_strict)
     607                 :             :         {
     608         [ +  + ]:      580490 :                 for (i = 0; i < fcinfo->nargs; i++)
     609                 :             :                 {
     610         [ +  + ]:      362964 :                         if (fcinfo->args[i].isnull)
     611                 :             :                         {
     612                 :       10868 :                                 callit = false;
     613                 :       10868 :                                 break;
     614                 :             :                         }
     615                 :      352096 :                 }
     616                 :      228394 :         }
     617                 :             : 
     618         [ +  + ]:      229126 :         if (callit)
     619                 :             :         {
     620                 :      218258 :                 pgstat_init_function_usage(fcinfo, &fcusage);
     621                 :             : 
     622                 :      218258 :                 fcinfo->isnull = false;
     623                 :      218258 :                 rsinfo.isDone = ExprSingleResult;
     624                 :      218258 :                 result = FunctionCallInvoke(fcinfo);
     625                 :      218258 :                 *isNull = fcinfo->isnull;
     626                 :      218258 :                 *isDone = rsinfo.isDone;
     627                 :             : 
     628                 :      218258 :                 pgstat_end_function_usage(&fcusage,
     629                 :      218258 :                                                                   rsinfo.isDone != ExprMultipleResult);
     630                 :      218258 :         }
     631                 :             :         else
     632                 :             :         {
     633                 :             :                 /* for a strict SRF, result for NULL is an empty set */
     634                 :       10868 :                 result = (Datum) 0;
     635                 :       10868 :                 *isNull = true;
     636                 :       10868 :                 *isDone = ExprEndResult;
     637                 :             :         }
     638                 :             : 
     639                 :             :         /* Which protocol does function want to use? */
     640         [ +  + ]:      229126 :         if (rsinfo.returnMode == SFRM_ValuePerCall)
     641                 :             :         {
     642         [ +  + ]:      226678 :                 if (*isDone != ExprEndResult)
     643                 :             :                 {
     644                 :             :                         /*
     645                 :             :                          * Save the current argument values to re-use on the next call.
     646                 :             :                          */
     647         [ -  + ]:      188894 :                         if (*isDone == ExprMultipleResult)
     648                 :             :                         {
     649                 :      188894 :                                 fcache->setArgsValid = true;
     650                 :             :                                 /* Register cleanup callback if we didn't already */
     651         [ +  + ]:      188894 :                                 if (!fcache->shutdown_reg)
     652                 :             :                                 {
     653                 :        4992 :                                         RegisterExprContextCallback(econtext,
     654                 :             :                                                                                                 ShutdownSetExpr,
     655                 :        2496 :                                                                                                 PointerGetDatum(fcache));
     656                 :        2496 :                                         fcache->shutdown_reg = true;
     657                 :        2496 :                                 }
     658                 :      188894 :                         }
     659                 :      188894 :                 }
     660                 :      226678 :         }
     661         [ +  - ]:        2448 :         else if (rsinfo.returnMode == SFRM_Materialize)
     662                 :             :         {
     663                 :             :                 /* check we're on the same page as the function author */
     664         [ +  - ]:        2448 :                 if (rsinfo.isDone != ExprSingleResult)
     665   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     666                 :             :                                         (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
     667                 :             :                                          errmsg("table-function protocol for materialize mode was not followed")));
     668         [ +  + ]:        2448 :                 if (rsinfo.setResult != NULL)
     669                 :             :                 {
     670                 :             :                         /* prepare to return values from the tuplestore */
     671                 :        4890 :                         ExecPrepareTuplestoreResult(fcache, econtext,
     672                 :        2445 :                                                                                 rsinfo.setResult,
     673                 :        2445 :                                                                                 rsinfo.setDesc);
     674                 :             :                         /* loop back to top to start returning from tuplestore */
     675                 :        2445 :                         goto restart;
     676                 :             :                 }
     677                 :             :                 /* if setResult was left null, treat it as empty set */
     678                 :           3 :                 *isDone = ExprEndResult;
     679                 :           3 :                 *isNull = true;
     680                 :           3 :                 result = (Datum) 0;
     681                 :           3 :         }
     682                 :             :         else
     683   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     684                 :             :                                 (errcode(ERRCODE_E_R_I_E_SRF_PROTOCOL_VIOLATED),
     685                 :             :                                  errmsg("unrecognized table-function returnMode: %d",
     686                 :             :                                                 (int) rsinfo.returnMode)));
     687                 :             : 
     688                 :      226681 :         return result;
     689                 :      239978 : }
     690                 :             : 
     691                 :             : 
     692                 :             : /*
     693                 :             :  * init_sexpr - initialize a SetExprState node during first use
     694                 :             :  */
     695                 :             : static void
     696                 :        7633 : init_sexpr(Oid foid, Oid input_collation, Expr *node,
     697                 :             :                    SetExprState *sexpr, PlanState *parent,
     698                 :             :                    MemoryContext sexprCxt, bool allowSRF, bool needDescForSRF)
     699                 :             : {
     700                 :        7633 :         AclResult       aclresult;
     701                 :        7633 :         size_t          numargs = list_length(sexpr->args);
     702                 :             : 
     703                 :             :         /* Check permission to call function */
     704                 :        7633 :         aclresult = object_aclcheck(ProcedureRelationId, foid, GetUserId(), ACL_EXECUTE);
     705         [ +  - ]:        7633 :         if (aclresult != ACLCHECK_OK)
     706                 :           0 :                 aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
     707         [ +  - ]:        7633 :         InvokeFunctionExecuteHook(foid);
     708                 :             : 
     709                 :             :         /*
     710                 :             :          * Safety check on nargs.  Under normal circumstances this should never
     711                 :             :          * fail, as parser should check sooner.  But possibly it might fail if
     712                 :             :          * server has been compiled with FUNC_MAX_ARGS smaller than some functions
     713                 :             :          * declared in pg_proc?
     714                 :             :          */
     715         [ +  - ]:        7633 :         if (list_length(sexpr->args) > FUNC_MAX_ARGS)
     716   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     717                 :             :                                 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
     718                 :             :                                  errmsg_plural("cannot pass more than %d argument to a function",
     719                 :             :                                                            "cannot pass more than %d arguments to a function",
     720                 :             :                                                            FUNC_MAX_ARGS,
     721                 :             :                                                            FUNC_MAX_ARGS)));
     722                 :             : 
     723                 :             :         /* Set up the primary fmgr lookup information */
     724                 :        7633 :         fmgr_info_cxt(foid, &(sexpr->func), sexprCxt);
     725                 :        7633 :         fmgr_info_set_expr((Node *) sexpr->expr, &(sexpr->func));
     726                 :             : 
     727                 :             :         /* Initialize the function call parameter struct as well */
     728                 :        7633 :         sexpr->fcinfo =
     729                 :        7633 :                 (FunctionCallInfo) palloc(SizeForFunctionCallInfo(numargs));
     730                 :        7633 :         InitFunctionCallInfoData(*sexpr->fcinfo, &(sexpr->func),
     731                 :             :                                                          numargs,
     732                 :             :                                                          input_collation, NULL, NULL);
     733                 :             : 
     734                 :             :         /* If function returns set, check if that's allowed by caller */
     735   [ +  +  +  - ]:        7633 :         if (sexpr->func.fn_retset && !allowSRF)
     736   [ #  #  #  #  :           0 :                 ereport(ERROR,
                   #  # ]
     737                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     738                 :             :                                  errmsg("set-valued function called in context that cannot accept a set"),
     739                 :             :                                  parent ? executor_errposition(parent->state,
     740                 :             :                                                                                            exprLocation((Node *) node)) : 0));
     741                 :             : 
     742                 :             :         /* Otherwise, caller should have marked the sexpr correctly */
     743         [ +  - ]:        7633 :         Assert(sexpr->func.fn_retset == sexpr->funcReturnsSet);
     744                 :             : 
     745                 :             :         /* If function returns set, prepare expected tuple descriptor */
     746   [ +  +  +  + ]:        7633 :         if (sexpr->func.fn_retset && needDescForSRF)
     747                 :             :         {
     748                 :        2030 :                 TypeFuncClass functypclass;
     749                 :        2030 :                 Oid                     funcrettype;
     750                 :        2030 :                 TupleDesc       tupdesc;
     751                 :        2030 :                 MemoryContext oldcontext;
     752                 :             : 
     753                 :        2030 :                 functypclass = get_expr_result_type(sexpr->func.fn_expr,
     754                 :             :                                                                                         &funcrettype,
     755                 :             :                                                                                         &tupdesc);
     756                 :             : 
     757                 :             :                 /* Must save tupdesc in sexpr's context */
     758                 :        2030 :                 oldcontext = MemoryContextSwitchTo(sexprCxt);
     759                 :             : 
     760   [ +  +  +  + ]:        2030 :                 if (functypclass == TYPEFUNC_COMPOSITE ||
     761                 :        1955 :                         functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
     762                 :             :                 {
     763                 :             :                         /* Composite data type, e.g. a table's row type */
     764         [ +  - ]:          81 :                         Assert(tupdesc);
     765                 :             :                         /* Must copy it out of typcache for safety */
     766                 :          81 :                         sexpr->funcResultDesc = CreateTupleDescCopy(tupdesc);
     767                 :          81 :                         sexpr->funcReturnsTuple = true;
     768                 :          81 :                 }
     769         [ +  + ]:        1949 :                 else if (functypclass == TYPEFUNC_SCALAR)
     770                 :             :                 {
     771                 :             :                         /* Base data type, i.e. scalar */
     772                 :        1936 :                         tupdesc = CreateTemplateTupleDesc(1);
     773                 :        3872 :                         TupleDescInitEntry(tupdesc,
     774                 :             :                                                            (AttrNumber) 1,
     775                 :             :                                                            NULL,
     776                 :        1936 :                                                            funcrettype,
     777                 :             :                                                            -1,
     778                 :             :                                                            0);
     779                 :        1936 :                         sexpr->funcResultDesc = tupdesc;
     780                 :        1936 :                         sexpr->funcReturnsTuple = false;
     781                 :        1936 :                 }
     782         [ +  - ]:          13 :                 else if (functypclass == TYPEFUNC_RECORD)
     783                 :             :                 {
     784                 :             :                         /* This will work if function doesn't need an expectedDesc */
     785                 :          13 :                         sexpr->funcResultDesc = NULL;
     786                 :          13 :                         sexpr->funcReturnsTuple = true;
     787                 :          13 :                 }
     788                 :             :                 else
     789                 :             :                 {
     790                 :             :                         /* Else, we will fail if function needs an expectedDesc */
     791                 :           0 :                         sexpr->funcResultDesc = NULL;
     792                 :             :                 }
     793                 :             : 
     794                 :        2030 :                 MemoryContextSwitchTo(oldcontext);
     795                 :        2030 :         }
     796                 :             :         else
     797                 :        5603 :                 sexpr->funcResultDesc = NULL;
     798                 :             : 
     799                 :             :         /* Initialize additional state */
     800                 :        7633 :         sexpr->funcResultStore = NULL;
     801                 :        7633 :         sexpr->funcResultSlot = NULL;
     802                 :        7633 :         sexpr->shutdown_reg = false;
     803                 :        7633 : }
     804                 :             : 
     805                 :             : /*
     806                 :             :  * callback function in case a SetExprState needs to be shut down before it
     807                 :             :  * has been run to completion
     808                 :             :  */
     809                 :             : static void
     810                 :        2676 : ShutdownSetExpr(Datum arg)
     811                 :             : {
     812                 :        2676 :         SetExprState *sexpr = castNode(SetExprState, DatumGetPointer(arg));
     813                 :             : 
     814                 :             :         /* If we have a slot, make sure it's let go of any tuplestore pointer */
     815         [ +  + ]:        2676 :         if (sexpr->funcResultSlot)
     816                 :         181 :                 ExecClearTuple(sexpr->funcResultSlot);
     817                 :             : 
     818                 :             :         /* Release any open tuplestore */
     819         [ +  + ]:        2676 :         if (sexpr->funcResultStore)
     820                 :           6 :                 tuplestore_end(sexpr->funcResultStore);
     821                 :        2676 :         sexpr->funcResultStore = NULL;
     822                 :             : 
     823                 :             :         /* Clear any active set-argument state */
     824                 :        2676 :         sexpr->setArgsValid = false;
     825                 :             : 
     826                 :             :         /* execUtils will deregister the callback... */
     827                 :        2676 :         sexpr->shutdown_reg = false;
     828                 :        2676 : }
     829                 :             : 
     830                 :             : /*
     831                 :             :  * Evaluate arguments for a function.
     832                 :             :  */
     833                 :             : static void
     834                 :       47549 : ExecEvalFuncArgs(FunctionCallInfo fcinfo,
     835                 :             :                                  List *argList,
     836                 :             :                                  ExprContext *econtext)
     837                 :             : {
     838                 :       47549 :         int                     i;
     839                 :       47549 :         ListCell   *arg;
     840                 :             : 
     841                 :       47549 :         i = 0;
     842   [ +  +  +  +  :      116403 :         foreach(arg, argList)
                   +  + ]
     843                 :             :         {
     844                 :       68854 :                 ExprState  *argstate = (ExprState *) lfirst(arg);
     845                 :             : 
     846                 :      137708 :                 fcinfo->args[i].value = ExecEvalExpr(argstate,
     847                 :       68854 :                                                                                          econtext,
     848                 :       68854 :                                                                                          &fcinfo->args[i].isnull);
     849                 :       68854 :                 i++;
     850                 :       68854 :         }
     851                 :             : 
     852         [ +  - ]:       47549 :         Assert(i == fcinfo->nargs);
     853                 :       47549 : }
     854                 :             : 
     855                 :             : /*
     856                 :             :  *              ExecPrepareTuplestoreResult
     857                 :             :  *
     858                 :             :  * Subroutine for ExecMakeFunctionResultSet: prepare to extract rows from a
     859                 :             :  * tuplestore function result.  We must set up a funcResultSlot (unless
     860                 :             :  * already done in a previous call cycle) and verify that the function
     861                 :             :  * returned the expected tuple descriptor.
     862                 :             :  */
     863                 :             : static void
     864                 :        2207 : ExecPrepareTuplestoreResult(SetExprState *sexpr,
     865                 :             :                                                         ExprContext *econtext,
     866                 :             :                                                         Tuplestorestate *resultStore,
     867                 :             :                                                         TupleDesc resultDesc)
     868                 :             : {
     869                 :        2207 :         sexpr->funcResultStore = resultStore;
     870                 :             : 
     871         [ +  + ]:        2207 :         if (sexpr->funcResultSlot == NULL)
     872                 :             :         {
     873                 :             :                 /* Create a slot so we can read data out of the tuplestore */
     874                 :         181 :                 TupleDesc       slotDesc;
     875                 :         181 :                 MemoryContext oldcontext;
     876                 :             : 
     877                 :         181 :                 oldcontext = MemoryContextSwitchTo(sexpr->func.fn_mcxt);
     878                 :             : 
     879                 :             :                 /*
     880                 :             :                  * If we were not able to determine the result rowtype from context,
     881                 :             :                  * and the function didn't return a tupdesc, we have to fail.
     882                 :             :                  */
     883         [ +  + ]:         181 :                 if (sexpr->funcResultDesc)
     884                 :         174 :                         slotDesc = sexpr->funcResultDesc;
     885         [ +  - ]:           7 :                 else if (resultDesc)
     886                 :             :                 {
     887                 :             :                         /* don't assume resultDesc is long-lived */
     888                 :           7 :                         slotDesc = CreateTupleDescCopy(resultDesc);
     889                 :           7 :                 }
     890                 :             :                 else
     891                 :             :                 {
     892   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     893                 :             :                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     894                 :             :                                          errmsg("function returning setof record called in "
     895                 :             :                                                         "context that cannot accept type record")));
     896                 :           0 :                         slotDesc = NULL;        /* keep compiler quiet */
     897                 :             :                 }
     898                 :             : 
     899                 :         181 :                 sexpr->funcResultSlot = MakeSingleTupleTableSlot(slotDesc,
     900                 :             :                                                                                                                  &TTSOpsMinimalTuple);
     901                 :         181 :                 MemoryContextSwitchTo(oldcontext);
     902                 :         181 :         }
     903                 :             : 
     904                 :             :         /*
     905                 :             :          * If function provided a tupdesc, cross-check it.  We only really need to
     906                 :             :          * do this for functions returning RECORD, but might as well do it always.
     907                 :             :          */
     908         [ -  + ]:        2207 :         if (resultDesc)
     909                 :             :         {
     910         [ +  + ]:        2207 :                 if (sexpr->funcResultDesc)
     911                 :        2198 :                         tupledesc_match(sexpr->funcResultDesc, resultDesc);
     912                 :             : 
     913                 :             :                 /*
     914                 :             :                  * If it is a dynamically-allocated TupleDesc, free it: it is
     915                 :             :                  * typically allocated in a per-query context, so we must avoid
     916                 :             :                  * leaking it across multiple usages.
     917                 :             :                  */
     918         [ -  + ]:        2207 :                 if (resultDesc->tdrefcount == -1)
     919                 :        2207 :                         FreeTupleDesc(resultDesc);
     920                 :        2207 :         }
     921                 :             : 
     922                 :             :         /* Register cleanup callback if we didn't already */
     923         [ +  + ]:        2207 :         if (!sexpr->shutdown_reg)
     924                 :             :         {
     925                 :         362 :                 RegisterExprContextCallback(econtext,
     926                 :             :                                                                         ShutdownSetExpr,
     927                 :         181 :                                                                         PointerGetDatum(sexpr));
     928                 :         181 :                 sexpr->shutdown_reg = true;
     929                 :         181 :         }
     930                 :        2207 : }
     931                 :             : 
     932                 :             : /*
     933                 :             :  * Check that function result tuple type (src_tupdesc) matches or can
     934                 :             :  * be considered to match what the query expects (dst_tupdesc). If
     935                 :             :  * they don't match, ereport.
     936                 :             :  *
     937                 :             :  * We really only care about number of attributes and data type.
     938                 :             :  * Also, we can ignore type mismatch on columns that are dropped in the
     939                 :             :  * destination type, so long as the physical storage matches.  This is
     940                 :             :  * helpful in some cases involving out-of-date cached plans.
     941                 :             :  */
     942                 :             : static void
     943                 :        8422 : tupledesc_match(TupleDesc dst_tupdesc, TupleDesc src_tupdesc)
     944                 :             : {
     945                 :        8422 :         int                     i;
     946                 :             : 
     947         [ +  + ]:        8422 :         if (dst_tupdesc->natts != src_tupdesc->natts)
     948   [ +  -  +  - ]:           8 :                 ereport(ERROR,
     949                 :             :                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
     950                 :             :                                  errmsg("function return row and query-specified return row do not match"),
     951                 :             :                                  errdetail_plural("Returned row contains %d attribute, but query expects %d.",
     952                 :             :                                                                   "Returned row contains %d attributes, but query expects %d.",
     953                 :             :                                                                   src_tupdesc->natts,
     954                 :             :                                                                   src_tupdesc->natts, dst_tupdesc->natts)));
     955                 :             : 
     956         [ +  + ]:       26800 :         for (i = 0; i < dst_tupdesc->natts; i++)
     957                 :             :         {
     958                 :       18392 :                 Form_pg_attribute dattr = TupleDescAttr(dst_tupdesc, i);
     959                 :       18392 :                 Form_pg_attribute sattr = TupleDescAttr(src_tupdesc, i);
     960                 :             : 
     961         [ +  + ]:       18392 :                 if (IsBinaryCoercible(sattr->atttypid, dattr->atttypid))
     962                 :       18386 :                         continue;                       /* no worries */
     963         [ -  + ]:           6 :                 if (!dattr->attisdropped)
     964   [ +  -  +  - ]:           6 :                         ereport(ERROR,
     965                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     966                 :             :                                          errmsg("function return row and query-specified return row do not match"),
     967                 :             :                                          errdetail("Returned type %s at ordinal position %d, but query expects %s.",
     968                 :             :                                                            format_type_be(sattr->atttypid),
     969                 :             :                                                            i + 1,
     970                 :             :                                                            format_type_be(dattr->atttypid))));
     971                 :             : 
     972         [ #  # ]:           0 :                 if (dattr->attlen != sattr->attlen ||
     973                 :           0 :                         dattr->attalign != sattr->attalign)
     974   [ #  #  #  # ]:           0 :                         ereport(ERROR,
     975                 :             :                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
     976                 :             :                                          errmsg("function return row and query-specified return row do not match"),
     977                 :             :                                          errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.",
     978                 :             :                                                            i + 1)));
     979      [ -  +  - ]:       18386 :         }
     980                 :        8408 : }
        

Generated by: LCOV version 2.3.2-1