LCOV - differential code coverage report
Current view: top level - src/backend/executor - execSRF.c (source / functions) Coverage Total Hit LBC UIC UBC GIC GNC CBC EUB ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 95.2 % 291 277 6 6 2 181 1 95 12 176
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 9 9 9 9
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a