LCOV - code coverage report
Current view: top level - src/backend/utils/fmgr - funcapi.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 596 714 83.5 %
Date: 2020-05-29 01:06:25 Functions: 21 24 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * funcapi.c
       4             :  *    Utility and convenience functions for fmgr functions that return
       5             :  *    sets and/or composite types, or deal with VARIADIC inputs.
       6             :  *
       7             :  * Copyright (c) 2002-2020, PostgreSQL Global Development Group
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/fmgr/funcapi.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/htup_details.h"
      17             : #include "access/relation.h"
      18             : #include "catalog/namespace.h"
      19             : #include "catalog/pg_proc.h"
      20             : #include "catalog/pg_type.h"
      21             : #include "funcapi.h"
      22             : #include "nodes/nodeFuncs.h"
      23             : #include "utils/array.h"
      24             : #include "utils/builtins.h"
      25             : #include "utils/lsyscache.h"
      26             : #include "utils/memutils.h"
      27             : #include "utils/regproc.h"
      28             : #include "utils/rel.h"
      29             : #include "utils/syscache.h"
      30             : #include "utils/typcache.h"
      31             : 
      32             : 
      33             : typedef struct polymorphic_actuals
      34             : {
      35             :     Oid         anyelement_type;    /* anyelement mapping, if known */
      36             :     Oid         anyarray_type;  /* anyarray mapping, if known */
      37             :     Oid         anyrange_type;  /* anyrange mapping, if known */
      38             : } polymorphic_actuals;
      39             : 
      40             : static void shutdown_MultiFuncCall(Datum arg);
      41             : static TypeFuncClass internal_get_result_type(Oid funcid,
      42             :                                               Node *call_expr,
      43             :                                               ReturnSetInfo *rsinfo,
      44             :                                               Oid *resultTypeId,
      45             :                                               TupleDesc *resultTupleDesc);
      46             : static void resolve_anyelement_from_others(polymorphic_actuals *actuals);
      47             : static void resolve_anyarray_from_others(polymorphic_actuals *actuals);
      48             : static void resolve_anyrange_from_others(polymorphic_actuals *actuals);
      49             : static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc,
      50             :                                         oidvector *declared_args,
      51             :                                         Node *call_expr);
      52             : static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid);
      53             : 
      54             : 
      55             : /*
      56             :  * init_MultiFuncCall
      57             :  * Create an empty FuncCallContext data structure
      58             :  * and do some other basic Multi-function call setup
      59             :  * and error checking
      60             :  */
      61             : FuncCallContext *
      62     1463476 : init_MultiFuncCall(PG_FUNCTION_ARGS)
      63             : {
      64             :     FuncCallContext *retval;
      65             : 
      66             :     /*
      67             :      * Bail if we're called in the wrong context
      68             :      */
      69     1463476 :     if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
      70           0 :         ereport(ERROR,
      71             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
      72             :                  errmsg("set-valued function called in context that cannot accept a set")));
      73             : 
      74     1463476 :     if (fcinfo->flinfo->fn_extra == NULL)
      75             :     {
      76             :         /*
      77             :          * First call
      78             :          */
      79     1463476 :         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
      80             :         MemoryContext multi_call_ctx;
      81             : 
      82             :         /*
      83             :          * Create a suitably long-lived context to hold cross-call data
      84             :          */
      85     1463476 :         multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
      86             :                                                "SRF multi-call context",
      87             :                                                ALLOCSET_SMALL_SIZES);
      88             : 
      89             :         /*
      90             :          * Allocate suitably long-lived space and zero it
      91             :          */
      92             :         retval = (FuncCallContext *)
      93     1463476 :             MemoryContextAllocZero(multi_call_ctx,
      94             :                                    sizeof(FuncCallContext));
      95             : 
      96             :         /*
      97             :          * initialize the elements
      98             :          */
      99     1463476 :         retval->call_cntr = 0;
     100     1463476 :         retval->max_calls = 0;
     101     1463476 :         retval->user_fctx = NULL;
     102     1463476 :         retval->attinmeta = NULL;
     103     1463476 :         retval->tuple_desc = NULL;
     104     1463476 :         retval->multi_call_memory_ctx = multi_call_ctx;
     105             : 
     106             :         /*
     107             :          * save the pointer for cross-call use
     108             :          */
     109     1463476 :         fcinfo->flinfo->fn_extra = retval;
     110             : 
     111             :         /*
     112             :          * Ensure we will get shut down cleanly if the exprcontext is not run
     113             :          * to completion.
     114             :          */
     115     1463476 :         RegisterExprContextCallback(rsi->econtext,
     116             :                                     shutdown_MultiFuncCall,
     117     1463476 :                                     PointerGetDatum(fcinfo->flinfo));
     118             :     }
     119             :     else
     120             :     {
     121             :         /* second and subsequent calls */
     122           0 :         elog(ERROR, "init_MultiFuncCall cannot be called more than once");
     123             : 
     124             :         /* never reached, but keep compiler happy */
     125             :         retval = NULL;
     126             :     }
     127             : 
     128     1463476 :     return retval;
     129             : }
     130             : 
     131             : /*
     132             :  * per_MultiFuncCall
     133             :  *
     134             :  * Do Multi-function per-call setup
     135             :  */
     136             : FuncCallContext *
     137    15642776 : per_MultiFuncCall(PG_FUNCTION_ARGS)
     138             : {
     139    15642776 :     FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
     140             : 
     141    15642776 :     return retval;
     142             : }
     143             : 
     144             : /*
     145             :  * end_MultiFuncCall
     146             :  * Clean up after init_MultiFuncCall
     147             :  */
     148             : void
     149     1463090 : end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
     150             : {
     151     1463090 :     ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
     152             : 
     153             :     /* Deregister the shutdown callback */
     154     1463090 :     UnregisterExprContextCallback(rsi->econtext,
     155             :                                   shutdown_MultiFuncCall,
     156     1463090 :                                   PointerGetDatum(fcinfo->flinfo));
     157             : 
     158             :     /* But use it to do the real work */
     159     1463090 :     shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
     160     1463090 : }
     161             : 
     162             : /*
     163             :  * shutdown_MultiFuncCall
     164             :  * Shutdown function to clean up after init_MultiFuncCall
     165             :  */
     166             : static void
     167     1463122 : shutdown_MultiFuncCall(Datum arg)
     168             : {
     169     1463122 :     FmgrInfo   *flinfo = (FmgrInfo *) DatumGetPointer(arg);
     170     1463122 :     FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
     171             : 
     172             :     /* unbind from flinfo */
     173     1463122 :     flinfo->fn_extra = NULL;
     174             : 
     175             :     /*
     176             :      * Delete context that holds all multi-call data, including the
     177             :      * FuncCallContext itself
     178             :      */
     179     1463122 :     MemoryContextDelete(funcctx->multi_call_memory_ctx);
     180     1463122 : }
     181             : 
     182             : 
     183             : /*
     184             :  * get_call_result_type
     185             :  *      Given a function's call info record, determine the kind of datatype
     186             :  *      it is supposed to return.  If resultTypeId isn't NULL, *resultTypeId
     187             :  *      receives the actual datatype OID (this is mainly useful for scalar
     188             :  *      result types).  If resultTupleDesc isn't NULL, *resultTupleDesc
     189             :  *      receives a pointer to a TupleDesc when the result is of a composite
     190             :  *      type, or NULL when it's a scalar result.
     191             :  *
     192             :  * One hard case that this handles is resolution of actual rowtypes for
     193             :  * functions returning RECORD (from either the function's OUT parameter
     194             :  * list, or a ReturnSetInfo context node).  TYPEFUNC_RECORD is returned
     195             :  * only when we couldn't resolve the actual rowtype for lack of information.
     196             :  *
     197             :  * The other hard case that this handles is resolution of polymorphism.
     198             :  * We will never return polymorphic pseudotypes (ANYELEMENT etc), either
     199             :  * as a scalar result type or as a component of a rowtype.
     200             :  *
     201             :  * This function is relatively expensive --- in a function returning set,
     202             :  * try to call it only the first time through.
     203             :  */
     204             : TypeFuncClass
     205       26698 : get_call_result_type(FunctionCallInfo fcinfo,
     206             :                      Oid *resultTypeId,
     207             :                      TupleDesc *resultTupleDesc)
     208             : {
     209       53396 :     return internal_get_result_type(fcinfo->flinfo->fn_oid,
     210       26698 :                                     fcinfo->flinfo->fn_expr,
     211       26698 :                                     (ReturnSetInfo *) fcinfo->resultinfo,
     212             :                                     resultTypeId,
     213             :                                     resultTupleDesc);
     214             : }
     215             : 
     216             : /*
     217             :  * get_expr_result_type
     218             :  *      As above, but work from a calling expression node tree
     219             :  */
     220             : TypeFuncClass
     221      146800 : get_expr_result_type(Node *expr,
     222             :                      Oid *resultTypeId,
     223             :                      TupleDesc *resultTupleDesc)
     224             : {
     225             :     TypeFuncClass result;
     226             : 
     227      146800 :     if (expr && IsA(expr, FuncExpr))
     228      143592 :         result = internal_get_result_type(((FuncExpr *) expr)->funcid,
     229             :                                           expr,
     230             :                                           NULL,
     231             :                                           resultTypeId,
     232             :                                           resultTupleDesc);
     233        3208 :     else if (expr && IsA(expr, OpExpr))
     234           8 :         result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
     235             :                                           expr,
     236             :                                           NULL,
     237             :                                           resultTypeId,
     238             :                                           resultTupleDesc);
     239        3200 :     else if (expr && IsA(expr, RowExpr) &&
     240          36 :              ((RowExpr *) expr)->row_typeid == RECORDOID)
     241             :     {
     242             :         /* We can resolve the record type by generating the tupdesc directly */
     243          36 :         RowExpr    *rexpr = (RowExpr *) expr;
     244             :         TupleDesc   tupdesc;
     245          36 :         AttrNumber  i = 1;
     246             :         ListCell   *lcc,
     247             :                    *lcn;
     248             : 
     249          36 :         tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
     250             :         Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
     251         108 :         forboth(lcc, rexpr->args, lcn, rexpr->colnames)
     252             :         {
     253          72 :             Node       *col = (Node *) lfirst(lcc);
     254          72 :             char       *colname = strVal(lfirst(lcn));
     255             : 
     256          72 :             TupleDescInitEntry(tupdesc, i,
     257             :                                colname,
     258             :                                exprType(col),
     259             :                                exprTypmod(col),
     260             :                                0);
     261          72 :             TupleDescInitEntryCollation(tupdesc, i,
     262             :                                         exprCollation(col));
     263          72 :             i++;
     264             :         }
     265          36 :         if (resultTypeId)
     266           0 :             *resultTypeId = rexpr->row_typeid;
     267          36 :         if (resultTupleDesc)
     268          36 :             *resultTupleDesc = BlessTupleDesc(tupdesc);
     269          36 :         return TYPEFUNC_COMPOSITE;
     270             :     }
     271             :     else
     272             :     {
     273             :         /* handle as a generic expression; no chance to resolve RECORD */
     274        3164 :         Oid         typid = exprType(expr);
     275             :         Oid         base_typid;
     276             : 
     277        3164 :         if (resultTypeId)
     278         280 :             *resultTypeId = typid;
     279        3164 :         if (resultTupleDesc)
     280        3164 :             *resultTupleDesc = NULL;
     281        3164 :         result = get_type_func_class(typid, &base_typid);
     282        3164 :         if ((result == TYPEFUNC_COMPOSITE ||
     283        2916 :              result == TYPEFUNC_COMPOSITE_DOMAIN) &&
     284             :             resultTupleDesc)
     285        2916 :             *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
     286             :     }
     287             : 
     288      146764 :     return result;
     289             : }
     290             : 
     291             : /*
     292             :  * get_func_result_type
     293             :  *      As above, but work from a function's OID only
     294             :  *
     295             :  * This will not be able to resolve pure-RECORD results nor polymorphism.
     296             :  */
     297             : TypeFuncClass
     298        5306 : get_func_result_type(Oid functionId,
     299             :                      Oid *resultTypeId,
     300             :                      TupleDesc *resultTupleDesc)
     301             : {
     302        5306 :     return internal_get_result_type(functionId,
     303             :                                     NULL,
     304             :                                     NULL,
     305             :                                     resultTypeId,
     306             :                                     resultTupleDesc);
     307             : }
     308             : 
     309             : /*
     310             :  * internal_get_result_type -- workhorse code implementing all the above
     311             :  *
     312             :  * funcid must always be supplied.  call_expr and rsinfo can be NULL if not
     313             :  * available.  We will return TYPEFUNC_RECORD, and store NULL into
     314             :  * *resultTupleDesc, if we cannot deduce the complete result rowtype from
     315             :  * the available information.
     316             :  */
     317             : static TypeFuncClass
     318      175604 : internal_get_result_type(Oid funcid,
     319             :                          Node *call_expr,
     320             :                          ReturnSetInfo *rsinfo,
     321             :                          Oid *resultTypeId,
     322             :                          TupleDesc *resultTupleDesc)
     323             : {
     324             :     TypeFuncClass result;
     325             :     HeapTuple   tp;
     326             :     Form_pg_proc procform;
     327             :     Oid         rettype;
     328             :     Oid         base_rettype;
     329             :     TupleDesc   tupdesc;
     330             : 
     331             :     /* First fetch the function's pg_proc row to inspect its rettype */
     332      175604 :     tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
     333      175604 :     if (!HeapTupleIsValid(tp))
     334           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
     335      175604 :     procform = (Form_pg_proc) GETSTRUCT(tp);
     336             : 
     337      175604 :     rettype = procform->prorettype;
     338             : 
     339             :     /* Check for OUT parameters defining a RECORD result */
     340      175604 :     tupdesc = build_function_result_tupdesc_t(tp);
     341      175604 :     if (tupdesc)
     342             :     {
     343             :         /*
     344             :          * It has OUT parameters, so it's basically like a regular composite
     345             :          * type, except we have to be able to resolve any polymorphic OUT
     346             :          * parameters.
     347             :          */
     348       92644 :         if (resultTypeId)
     349       32650 :             *resultTypeId = rettype;
     350             : 
     351       92644 :         if (resolve_polymorphic_tupdesc(tupdesc,
     352             :                                         &procform->proargtypes,
     353             :                                         call_expr))
     354             :         {
     355       92644 :             if (tupdesc->tdtypeid == RECORDOID &&
     356       92644 :                 tupdesc->tdtypmod < 0)
     357       92644 :                 assign_record_type_typmod(tupdesc);
     358       92644 :             if (resultTupleDesc)
     359       92644 :                 *resultTupleDesc = tupdesc;
     360       92644 :             result = TYPEFUNC_COMPOSITE;
     361             :         }
     362             :         else
     363             :         {
     364           0 :             if (resultTupleDesc)
     365           0 :                 *resultTupleDesc = NULL;
     366           0 :             result = TYPEFUNC_RECORD;
     367             :         }
     368             : 
     369       92644 :         ReleaseSysCache(tp);
     370             : 
     371       92644 :         return result;
     372             :     }
     373             : 
     374             :     /*
     375             :      * If scalar polymorphic result, try to resolve it.
     376             :      */
     377       82960 :     if (IsPolymorphicType(rettype))
     378             :     {
     379       38304 :         Oid         newrettype = exprType(call_expr);
     380             : 
     381       38304 :         if (newrettype == InvalidOid)   /* this probably should not happen */
     382           0 :             ereport(ERROR,
     383             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     384             :                      errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
     385             :                             NameStr(procform->proname),
     386             :                             format_type_be(rettype))));
     387       38304 :         rettype = newrettype;
     388             :     }
     389             : 
     390       82960 :     if (resultTypeId)
     391       73598 :         *resultTypeId = rettype;
     392       82960 :     if (resultTupleDesc)
     393       82960 :         *resultTupleDesc = NULL;    /* default result */
     394             : 
     395             :     /* Classify the result type */
     396       82960 :     result = get_type_func_class(rettype, &base_rettype);
     397       82960 :     switch (result)
     398             :     {
     399        3752 :         case TYPEFUNC_COMPOSITE:
     400             :         case TYPEFUNC_COMPOSITE_DOMAIN:
     401        3752 :             if (resultTupleDesc)
     402        3752 :                 *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
     403             :             /* Named composite types can't have any polymorphic columns */
     404        3752 :             break;
     405       77528 :         case TYPEFUNC_SCALAR:
     406       77528 :             break;
     407        1672 :         case TYPEFUNC_RECORD:
     408             :             /* We must get the tupledesc from call context */
     409        1672 :             if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
     410         426 :                 rsinfo->expectedDesc != NULL)
     411             :             {
     412         394 :                 result = TYPEFUNC_COMPOSITE;
     413         394 :                 if (resultTupleDesc)
     414         394 :                     *resultTupleDesc = rsinfo->expectedDesc;
     415             :                 /* Assume no polymorphic columns here, either */
     416             :             }
     417        1672 :             break;
     418           8 :         default:
     419           8 :             break;
     420             :     }
     421             : 
     422       82960 :     ReleaseSysCache(tp);
     423             : 
     424       82960 :     return result;
     425             : }
     426             : 
     427             : /*
     428             :  * get_expr_result_tupdesc
     429             :  *      Get a tupdesc describing the result of a composite-valued expression
     430             :  *
     431             :  * If expression is not composite or rowtype can't be determined, returns NULL
     432             :  * if noError is true, else throws error.
     433             :  *
     434             :  * This is a simpler version of get_expr_result_type() for use when the caller
     435             :  * is only interested in determinate rowtype results.
     436             :  */
     437             : TupleDesc
     438       49084 : get_expr_result_tupdesc(Node *expr, bool noError)
     439             : {
     440             :     TupleDesc   tupleDesc;
     441             :     TypeFuncClass functypclass;
     442             : 
     443       49084 :     functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
     444             : 
     445       49084 :     if (functypclass == TYPEFUNC_COMPOSITE ||
     446             :         functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
     447       49060 :         return tupleDesc;
     448             : 
     449          24 :     if (!noError)
     450             :     {
     451           0 :         Oid         exprTypeId = exprType(expr);
     452             : 
     453           0 :         if (exprTypeId != RECORDOID)
     454           0 :             ereport(ERROR,
     455             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     456             :                      errmsg("type %s is not composite",
     457             :                             format_type_be(exprTypeId))));
     458             :         else
     459           0 :             ereport(ERROR,
     460             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     461             :                      errmsg("record type has not been registered")));
     462             :     }
     463             : 
     464          24 :     return NULL;
     465             : }
     466             : 
     467             : /*
     468             :  * Resolve actual type of ANYELEMENT from other polymorphic inputs
     469             :  *
     470             :  * Note: the error cases here and in the sibling functions below are not
     471             :  * really user-facing; they could only occur if the function signature is
     472             :  * incorrect or the parser failed to enforce consistency of the actual
     473             :  * argument types.  Hence, we don't sweat too much over the error messages.
     474             :  */
     475             : static void
     476        6886 : resolve_anyelement_from_others(polymorphic_actuals *actuals)
     477             : {
     478        6886 :     if (OidIsValid(actuals->anyarray_type))
     479             :     {
     480             :         /* Use the element type corresponding to actual type */
     481        6810 :         Oid         array_base_type = getBaseType(actuals->anyarray_type);
     482        6810 :         Oid         array_typelem = get_element_type(array_base_type);
     483             : 
     484        6810 :         if (!OidIsValid(array_typelem))
     485           0 :             ereport(ERROR,
     486             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     487             :                      errmsg("argument declared %s is not an array but type %s",
     488             :                             "anyarray",
     489             :                             format_type_be(array_base_type))));
     490        6810 :         actuals->anyelement_type = array_typelem;
     491             :     }
     492          76 :     else if (OidIsValid(actuals->anyrange_type))
     493             :     {
     494             :         /* Use the element type corresponding to actual type */
     495          76 :         Oid         range_base_type = getBaseType(actuals->anyrange_type);
     496          76 :         Oid         range_typelem = get_range_subtype(range_base_type);
     497             : 
     498          76 :         if (!OidIsValid(range_typelem))
     499           0 :             ereport(ERROR,
     500             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
     501             :                      errmsg("argument declared %s is not a range type but type %s",
     502             :                             "anyrange",
     503             :                             format_type_be(range_base_type))));
     504          76 :         actuals->anyelement_type = range_typelem;
     505             :     }
     506             :     else
     507           0 :         elog(ERROR, "could not determine polymorphic type");
     508        6886 : }
     509             : 
     510             : /*
     511             :  * Resolve actual type of ANYARRAY from other polymorphic inputs
     512             :  */
     513             : static void
     514         184 : resolve_anyarray_from_others(polymorphic_actuals *actuals)
     515             : {
     516             :     /* If we don't know ANYELEMENT, resolve that first */
     517         184 :     if (!OidIsValid(actuals->anyelement_type))
     518          12 :         resolve_anyelement_from_others(actuals);
     519             : 
     520         184 :     if (OidIsValid(actuals->anyelement_type))
     521             :     {
     522             :         /* Use the array type corresponding to actual type */
     523         184 :         Oid         array_typeid = get_array_type(actuals->anyelement_type);
     524             : 
     525         184 :         if (!OidIsValid(array_typeid))
     526           0 :             ereport(ERROR,
     527             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
     528             :                      errmsg("could not find array type for data type %s",
     529             :                             format_type_be(actuals->anyelement_type))));
     530         184 :         actuals->anyarray_type = array_typeid;
     531             :     }
     532             :     else
     533           0 :         elog(ERROR, "could not determine polymorphic type");
     534         184 : }
     535             : 
     536             : /*
     537             :  * Resolve actual type of ANYRANGE from other polymorphic inputs
     538             :  */
     539             : static void
     540           0 : resolve_anyrange_from_others(polymorphic_actuals *actuals)
     541             : {
     542             :     /*
     543             :      * We can't deduce a range type from other polymorphic inputs, because
     544             :      * there may be multiple range types with the same subtype.
     545             :      */
     546           0 :     elog(ERROR, "could not determine polymorphic type");
     547             : }
     548             : 
     549             : /*
     550             :  * Given the result tuple descriptor for a function with OUT parameters,
     551             :  * replace any polymorphic column types (ANYELEMENT etc) in the tupdesc
     552             :  * with concrete data types deduced from the input arguments.
     553             :  * declared_args is an oidvector of the function's declared input arg types
     554             :  * (showing which are polymorphic), and call_expr is the call expression.
     555             :  *
     556             :  * Returns true if able to deduce all types, false if necessary information
     557             :  * is not provided (call_expr is NULL or arg types aren't identifiable).
     558             :  */
     559             : static bool
     560       92644 : resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args,
     561             :                             Node *call_expr)
     562             : {
     563       92644 :     int         natts = tupdesc->natts;
     564       92644 :     int         nargs = declared_args->dim1;
     565       92644 :     bool        have_polymorphic_result = false;
     566       92644 :     bool        have_anyelement_result = false;
     567       92644 :     bool        have_anyarray_result = false;
     568       92644 :     bool        have_anyrange_result = false;
     569       92644 :     bool        have_anycompatible_result = false;
     570       92644 :     bool        have_anycompatible_array_result = false;
     571       92644 :     bool        have_anycompatible_range_result = false;
     572             :     polymorphic_actuals poly_actuals;
     573             :     polymorphic_actuals anyc_actuals;
     574       92644 :     Oid         anycollation = InvalidOid;
     575       92644 :     Oid         anycompatcollation = InvalidOid;
     576             :     int         i;
     577             : 
     578             :     /* See if there are any polymorphic outputs; quick out if not */
     579     1195864 :     for (i = 0; i < natts; i++)
     580             :     {
     581     1103220 :         switch (TupleDescAttr(tupdesc, i)->atttypid)
     582             :         {
     583        6894 :             case ANYELEMENTOID:
     584             :             case ANYNONARRAYOID:
     585             :             case ANYENUMOID:
     586        6894 :                 have_polymorphic_result = true;
     587        6894 :                 have_anyelement_result = true;
     588        6894 :                 break;
     589         144 :             case ANYARRAYOID:
     590         144 :                 have_polymorphic_result = true;
     591         144 :                 have_anyarray_result = true;
     592         144 :                 break;
     593          24 :             case ANYRANGEOID:
     594          24 :                 have_polymorphic_result = true;
     595          24 :                 have_anyrange_result = true;
     596          24 :                 break;
     597          60 :             case ANYCOMPATIBLEOID:
     598             :             case ANYCOMPATIBLENONARRAYOID:
     599          60 :                 have_polymorphic_result = true;
     600          60 :                 have_anycompatible_result = true;
     601          60 :                 break;
     602         132 :             case ANYCOMPATIBLEARRAYOID:
     603         132 :                 have_polymorphic_result = true;
     604         132 :                 have_anycompatible_array_result = true;
     605         132 :                 break;
     606          12 :             case ANYCOMPATIBLERANGEOID:
     607          12 :                 have_polymorphic_result = true;
     608          12 :                 have_anycompatible_range_result = true;
     609          12 :                 break;
     610     1095954 :             default:
     611     1095954 :                 break;
     612             :         }
     613             :     }
     614       92644 :     if (!have_polymorphic_result)
     615       85606 :         return true;
     616             : 
     617             :     /*
     618             :      * Otherwise, extract actual datatype(s) from input arguments.  (We assume
     619             :      * the parser already validated consistency of the arguments.  Also, for
     620             :      * the ANYCOMPATIBLE pseudotype family, we expect that all matching
     621             :      * arguments were coerced to the selected common supertype, so that it
     622             :      * doesn't matter which one's exposed type we look at.)
     623             :      */
     624        7038 :     if (!call_expr)
     625           0 :         return false;           /* no hope */
     626             : 
     627        7038 :     memset(&poly_actuals, 0, sizeof(poly_actuals));
     628        7038 :     memset(&anyc_actuals, 0, sizeof(anyc_actuals));
     629             : 
     630       14316 :     for (i = 0; i < nargs; i++)
     631             :     {
     632        7278 :         switch (declared_args->values[i])
     633             :         {
     634         120 :             case ANYELEMENTOID:
     635             :             case ANYNONARRAYOID:
     636             :             case ANYENUMOID:
     637         120 :                 if (!OidIsValid(poly_actuals.anyelement_type))
     638             :                 {
     639         120 :                     poly_actuals.anyelement_type =
     640         120 :                         get_call_expr_argtype(call_expr, i);
     641         120 :                     if (!OidIsValid(poly_actuals.anyelement_type))
     642           0 :                         return false;
     643             :                 }
     644         120 :                 break;
     645        6882 :             case ANYARRAYOID:
     646        6882 :                 if (!OidIsValid(poly_actuals.anyarray_type))
     647             :                 {
     648        6882 :                     poly_actuals.anyarray_type =
     649        6882 :                         get_call_expr_argtype(call_expr, i);
     650        6882 :                     if (!OidIsValid(poly_actuals.anyarray_type))
     651           0 :                         return false;
     652             :                 }
     653        6882 :                 break;
     654          48 :             case ANYRANGEOID:
     655          48 :                 if (!OidIsValid(poly_actuals.anyrange_type))
     656             :                 {
     657          48 :                     poly_actuals.anyrange_type =
     658          48 :                         get_call_expr_argtype(call_expr, i);
     659          48 :                     if (!OidIsValid(poly_actuals.anyrange_type))
     660           0 :                         return false;
     661             :                 }
     662          48 :                 break;
     663         168 :             case ANYCOMPATIBLEOID:
     664             :             case ANYCOMPATIBLENONARRAYOID:
     665         168 :                 if (!OidIsValid(anyc_actuals.anyelement_type))
     666             :                 {
     667          96 :                     anyc_actuals.anyelement_type =
     668          96 :                         get_call_expr_argtype(call_expr, i);
     669          96 :                     if (!OidIsValid(anyc_actuals.anyelement_type))
     670           0 :                         return false;
     671             :                 }
     672         168 :                 break;
     673          24 :             case ANYCOMPATIBLEARRAYOID:
     674          24 :                 if (!OidIsValid(anyc_actuals.anyarray_type))
     675             :                 {
     676          24 :                     anyc_actuals.anyarray_type =
     677          24 :                         get_call_expr_argtype(call_expr, i);
     678          24 :                     if (!OidIsValid(anyc_actuals.anyarray_type))
     679           0 :                         return false;
     680             :                 }
     681          24 :                 break;
     682          36 :             case ANYCOMPATIBLERANGEOID:
     683          36 :                 if (!OidIsValid(anyc_actuals.anyrange_type))
     684             :                 {
     685          36 :                     anyc_actuals.anyrange_type =
     686          36 :                         get_call_expr_argtype(call_expr, i);
     687          36 :                     if (!OidIsValid(anyc_actuals.anyrange_type))
     688           0 :                         return false;
     689             :                 }
     690          36 :                 break;
     691           0 :             default:
     692           0 :                 break;
     693             :         }
     694             :     }
     695             : 
     696             :     /* If needed, deduce one polymorphic type from others */
     697        7038 :     if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
     698        6834 :         resolve_anyelement_from_others(&poly_actuals);
     699             : 
     700        7038 :     if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
     701          60 :         resolve_anyarray_from_others(&poly_actuals);
     702             : 
     703        7038 :     if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
     704           0 :         resolve_anyrange_from_others(&poly_actuals);
     705             : 
     706        7038 :     if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
     707          36 :         resolve_anyelement_from_others(&anyc_actuals);
     708             : 
     709        7038 :     if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
     710         108 :         resolve_anyarray_from_others(&anyc_actuals);
     711             : 
     712        7038 :     if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
     713           0 :         resolve_anyrange_from_others(&anyc_actuals);
     714             : 
     715             :     /*
     716             :      * Identify the collation to use for polymorphic OUT parameters. (It'll
     717             :      * necessarily be the same for both anyelement and anyarray, likewise for
     718             :      * anycompatible and anycompatiblearray.)  Note that range types are not
     719             :      * collatable, so any possible internal collation of a range type is not
     720             :      * considered here.
     721             :      */
     722        7038 :     if (OidIsValid(poly_actuals.anyelement_type))
     723        6966 :         anycollation = get_typcollation(poly_actuals.anyelement_type);
     724          72 :     else if (OidIsValid(poly_actuals.anyarray_type))
     725           0 :         anycollation = get_typcollation(poly_actuals.anyarray_type);
     726             : 
     727        7038 :     if (OidIsValid(anyc_actuals.anyelement_type))
     728         132 :         anycompatcollation = get_typcollation(anyc_actuals.anyelement_type);
     729        6906 :     else if (OidIsValid(anyc_actuals.anyarray_type))
     730           0 :         anycompatcollation = get_typcollation(anyc_actuals.anyarray_type);
     731             : 
     732        7038 :     if (OidIsValid(anycollation) || OidIsValid(anycompatcollation))
     733             :     {
     734             :         /*
     735             :          * The types are collatable, so consider whether to use a nondefault
     736             :          * collation.  We do so if we can identify the input collation used
     737             :          * for the function.
     738             :          */
     739          44 :         Oid         inputcollation = exprInputCollation(call_expr);
     740             : 
     741          44 :         if (OidIsValid(inputcollation))
     742             :         {
     743          28 :             if (OidIsValid(anycollation))
     744          28 :                 anycollation = inputcollation;
     745          28 :             if (OidIsValid(anycompatcollation))
     746           0 :                 anycompatcollation = inputcollation;
     747             :         }
     748             :     }
     749             : 
     750             :     /* And finally replace the tuple column types as needed */
     751       21126 :     for (i = 0; i < natts; i++)
     752             :     {
     753       14088 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
     754             : 
     755       14088 :         switch (att->atttypid)
     756             :         {
     757        6894 :             case ANYELEMENTOID:
     758             :             case ANYNONARRAYOID:
     759             :             case ANYENUMOID:
     760       13788 :                 TupleDescInitEntry(tupdesc, i + 1,
     761        6894 :                                    NameStr(att->attname),
     762             :                                    poly_actuals.anyelement_type,
     763             :                                    -1,
     764             :                                    0);
     765        6894 :                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
     766        6894 :                 break;
     767         144 :             case ANYARRAYOID:
     768         288 :                 TupleDescInitEntry(tupdesc, i + 1,
     769         144 :                                    NameStr(att->attname),
     770             :                                    poly_actuals.anyarray_type,
     771             :                                    -1,
     772             :                                    0);
     773         144 :                 TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
     774         144 :                 break;
     775          24 :             case ANYRANGEOID:
     776          48 :                 TupleDescInitEntry(tupdesc, i + 1,
     777          24 :                                    NameStr(att->attname),
     778             :                                    poly_actuals.anyrange_type,
     779             :                                    -1,
     780             :                                    0);
     781             :                 /* no collation should be attached to a range type */
     782          24 :                 break;
     783          60 :             case ANYCOMPATIBLEOID:
     784             :             case ANYCOMPATIBLENONARRAYOID:
     785         120 :                 TupleDescInitEntry(tupdesc, i + 1,
     786          60 :                                    NameStr(att->attname),
     787             :                                    anyc_actuals.anyelement_type,
     788             :                                    -1,
     789             :                                    0);
     790          60 :                 TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
     791          60 :                 break;
     792         132 :             case ANYCOMPATIBLEARRAYOID:
     793         264 :                 TupleDescInitEntry(tupdesc, i + 1,
     794         132 :                                    NameStr(att->attname),
     795             :                                    anyc_actuals.anyarray_type,
     796             :                                    -1,
     797             :                                    0);
     798         132 :                 TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
     799         132 :                 break;
     800          12 :             case ANYCOMPATIBLERANGEOID:
     801          24 :                 TupleDescInitEntry(tupdesc, i + 1,
     802          12 :                                    NameStr(att->attname),
     803             :                                    anyc_actuals.anyrange_type,
     804             :                                    -1,
     805             :                                    0);
     806             :                 /* no collation should be attached to a range type */
     807          12 :                 break;
     808        6822 :             default:
     809        6822 :                 break;
     810             :         }
     811             :     }
     812             : 
     813        7038 :     return true;
     814             : }
     815             : 
     816             : /*
     817             :  * Given the declared argument types and modes for a function, replace any
     818             :  * polymorphic types (ANYELEMENT etc) in argtypes[] with concrete data types
     819             :  * deduced from the input arguments found in call_expr.
     820             :  *
     821             :  * Returns true if able to deduce all types, false if necessary information
     822             :  * is not provided (call_expr is NULL or arg types aren't identifiable).
     823             :  *
     824             :  * This is the same logic as resolve_polymorphic_tupdesc, but with a different
     825             :  * argument representation, and slightly different output responsibilities.
     826             :  *
     827             :  * argmodes may be NULL, in which case all arguments are assumed to be IN mode.
     828             :  */
     829             : bool
     830        6354 : resolve_polymorphic_argtypes(int numargs, Oid *argtypes, char *argmodes,
     831             :                              Node *call_expr)
     832             : {
     833        6354 :     bool        have_polymorphic_result = false;
     834        6354 :     bool        have_anyelement_result = false;
     835        6354 :     bool        have_anyarray_result = false;
     836        6354 :     bool        have_anyrange_result = false;
     837        6354 :     bool        have_anycompatible_result = false;
     838        6354 :     bool        have_anycompatible_array_result = false;
     839        6354 :     bool        have_anycompatible_range_result = false;
     840             :     polymorphic_actuals poly_actuals;
     841             :     polymorphic_actuals anyc_actuals;
     842             :     int         inargno;
     843             :     int         i;
     844             : 
     845             :     /*
     846             :      * First pass: resolve polymorphic inputs, check for outputs.  As in
     847             :      * resolve_polymorphic_tupdesc, we rely on the parser to have enforced
     848             :      * type consistency and coerced ANYCOMPATIBLE args to a common supertype.
     849             :      */
     850        6354 :     memset(&poly_actuals, 0, sizeof(poly_actuals));
     851        6354 :     memset(&anyc_actuals, 0, sizeof(anyc_actuals));
     852        6354 :     inargno = 0;
     853       16028 :     for (i = 0; i < numargs; i++)
     854             :     {
     855        9674 :         char        argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
     856             : 
     857        9674 :         switch (argtypes[i])
     858             :         {
     859        1340 :             case ANYELEMENTOID:
     860             :             case ANYNONARRAYOID:
     861             :             case ANYENUMOID:
     862        1340 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     863             :                 {
     864           4 :                     have_polymorphic_result = true;
     865           4 :                     have_anyelement_result = true;
     866             :                 }
     867             :                 else
     868             :                 {
     869        1336 :                     if (!OidIsValid(poly_actuals.anyelement_type))
     870             :                     {
     871         768 :                         poly_actuals.anyelement_type =
     872         768 :                             get_call_expr_argtype(call_expr, inargno);
     873         768 :                         if (!OidIsValid(poly_actuals.anyelement_type))
     874           0 :                             return false;
     875             :                     }
     876        1336 :                     argtypes[i] = poly_actuals.anyelement_type;
     877             :                 }
     878        1340 :                 break;
     879         144 :             case ANYARRAYOID:
     880         144 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     881             :                 {
     882          12 :                     have_polymorphic_result = true;
     883          12 :                     have_anyarray_result = true;
     884             :                 }
     885             :                 else
     886             :                 {
     887         132 :                     if (!OidIsValid(poly_actuals.anyarray_type))
     888             :                     {
     889         132 :                         poly_actuals.anyarray_type =
     890         132 :                             get_call_expr_argtype(call_expr, inargno);
     891         132 :                         if (!OidIsValid(poly_actuals.anyarray_type))
     892           0 :                             return false;
     893             :                     }
     894         132 :                     argtypes[i] = poly_actuals.anyarray_type;
     895             :                 }
     896         144 :                 break;
     897          12 :             case ANYRANGEOID:
     898          12 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     899             :                 {
     900           0 :                     have_polymorphic_result = true;
     901           0 :                     have_anyrange_result = true;
     902             :                 }
     903             :                 else
     904             :                 {
     905          12 :                     if (!OidIsValid(poly_actuals.anyrange_type))
     906             :                     {
     907          12 :                         poly_actuals.anyrange_type =
     908          12 :                             get_call_expr_argtype(call_expr, inargno);
     909          12 :                         if (!OidIsValid(poly_actuals.anyrange_type))
     910           0 :                             return false;
     911             :                     }
     912          12 :                     argtypes[i] = poly_actuals.anyrange_type;
     913             :                 }
     914          12 :                 break;
     915          92 :             case ANYCOMPATIBLEOID:
     916             :             case ANYCOMPATIBLENONARRAYOID:
     917          92 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     918             :                 {
     919           4 :                     have_polymorphic_result = true;
     920           4 :                     have_anycompatible_result = true;
     921             :                 }
     922             :                 else
     923             :                 {
     924          88 :                     if (!OidIsValid(anyc_actuals.anyelement_type))
     925             :                     {
     926          44 :                         anyc_actuals.anyelement_type =
     927          44 :                             get_call_expr_argtype(call_expr, inargno);
     928          44 :                         if (!OidIsValid(anyc_actuals.anyelement_type))
     929           0 :                             return false;
     930             :                     }
     931          88 :                     argtypes[i] = anyc_actuals.anyelement_type;
     932             :                 }
     933          92 :                 break;
     934          24 :             case ANYCOMPATIBLEARRAYOID:
     935          24 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     936             :                 {
     937          12 :                     have_polymorphic_result = true;
     938          12 :                     have_anycompatible_array_result = true;
     939             :                 }
     940             :                 else
     941             :                 {
     942          12 :                     if (!OidIsValid(anyc_actuals.anyarray_type))
     943             :                     {
     944          12 :                         anyc_actuals.anyarray_type =
     945          12 :                             get_call_expr_argtype(call_expr, inargno);
     946          12 :                         if (!OidIsValid(anyc_actuals.anyarray_type))
     947           0 :                             return false;
     948             :                     }
     949          12 :                     argtypes[i] = anyc_actuals.anyarray_type;
     950             :                 }
     951          24 :                 break;
     952          36 :             case ANYCOMPATIBLERANGEOID:
     953          36 :                 if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
     954             :                 {
     955           0 :                     have_polymorphic_result = true;
     956           0 :                     have_anycompatible_range_result = true;
     957             :                 }
     958             :                 else
     959             :                 {
     960          36 :                     if (!OidIsValid(anyc_actuals.anyrange_type))
     961             :                     {
     962          36 :                         anyc_actuals.anyrange_type =
     963          36 :                             get_call_expr_argtype(call_expr, inargno);
     964          36 :                         if (!OidIsValid(anyc_actuals.anyrange_type))
     965           0 :                             return false;
     966             :                     }
     967          36 :                     argtypes[i] = anyc_actuals.anyrange_type;
     968             :                 }
     969          36 :                 break;
     970        8026 :             default:
     971        8026 :                 break;
     972             :         }
     973        9674 :         if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
     974        9618 :             inargno++;
     975             :     }
     976             : 
     977             :     /* Done? */
     978        6354 :     if (!have_polymorphic_result)
     979        6338 :         return true;
     980             : 
     981             :     /* If needed, deduce one polymorphic type from others */
     982          16 :     if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
     983           0 :         resolve_anyelement_from_others(&poly_actuals);
     984             : 
     985          16 :     if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
     986           4 :         resolve_anyarray_from_others(&poly_actuals);
     987             : 
     988          16 :     if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
     989           0 :         resolve_anyrange_from_others(&poly_actuals);
     990             : 
     991          16 :     if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
     992           4 :         resolve_anyelement_from_others(&anyc_actuals);
     993             : 
     994          16 :     if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
     995          12 :         resolve_anyarray_from_others(&anyc_actuals);
     996             : 
     997          16 :     if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
     998           0 :         resolve_anyrange_from_others(&anyc_actuals);
     999             : 
    1000             :     /* And finally replace the output column types as needed */
    1001          88 :     for (i = 0; i < numargs; i++)
    1002             :     {
    1003          72 :         switch (argtypes[i])
    1004             :         {
    1005           4 :             case ANYELEMENTOID:
    1006             :             case ANYNONARRAYOID:
    1007             :             case ANYENUMOID:
    1008           4 :                 argtypes[i] = poly_actuals.anyelement_type;
    1009           4 :                 break;
    1010          12 :             case ANYARRAYOID:
    1011          12 :                 argtypes[i] = poly_actuals.anyarray_type;
    1012          12 :                 break;
    1013           0 :             case ANYRANGEOID:
    1014           0 :                 argtypes[i] = poly_actuals.anyrange_type;
    1015           0 :                 break;
    1016           4 :             case ANYCOMPATIBLEOID:
    1017             :             case ANYCOMPATIBLENONARRAYOID:
    1018           4 :                 argtypes[i] = anyc_actuals.anyelement_type;
    1019           4 :                 break;
    1020          12 :             case ANYCOMPATIBLEARRAYOID:
    1021          12 :                 argtypes[i] = anyc_actuals.anyarray_type;
    1022          12 :                 break;
    1023           0 :             case ANYCOMPATIBLERANGEOID:
    1024           0 :                 argtypes[i] = anyc_actuals.anyrange_type;
    1025           0 :                 break;
    1026          40 :             default:
    1027          40 :                 break;
    1028             :         }
    1029             :     }
    1030             : 
    1031          16 :     return true;
    1032             : }
    1033             : 
    1034             : /*
    1035             :  * get_type_func_class
    1036             :  *      Given the type OID, obtain its TYPEFUNC classification.
    1037             :  *      Also, if it's a domain, return the base type OID.
    1038             :  *
    1039             :  * This is intended to centralize a bunch of formerly ad-hoc code for
    1040             :  * classifying types.  The categories used here are useful for deciding
    1041             :  * how to handle functions returning the datatype.
    1042             :  */
    1043             : static TypeFuncClass
    1044       86124 : get_type_func_class(Oid typid, Oid *base_typeid)
    1045             : {
    1046       86124 :     *base_typeid = typid;
    1047             : 
    1048       86124 :     switch (get_typtype(typid))
    1049             :     {
    1050        6494 :         case TYPTYPE_COMPOSITE:
    1051        6494 :             return TYPEFUNC_COMPOSITE;
    1052       76672 :         case TYPTYPE_BASE:
    1053             :         case TYPTYPE_ENUM:
    1054             :         case TYPTYPE_RANGE:
    1055       76672 :             return TYPEFUNC_SCALAR;
    1056         738 :         case TYPTYPE_DOMAIN:
    1057         738 :             *base_typeid = typid = getBaseType(typid);
    1058         738 :             if (get_typtype(typid) == TYPTYPE_COMPOSITE)
    1059         174 :                 return TYPEFUNC_COMPOSITE_DOMAIN;
    1060             :             else                /* domain base type can't be a pseudotype */
    1061         564 :                 return TYPEFUNC_SCALAR;
    1062        2220 :         case TYPTYPE_PSEUDO:
    1063        2220 :             if (typid == RECORDOID)
    1064        1672 :                 return TYPEFUNC_RECORD;
    1065             : 
    1066             :             /*
    1067             :              * We treat VOID and CSTRING as legitimate scalar datatypes,
    1068             :              * mostly for the convenience of the JDBC driver (which wants to
    1069             :              * be able to do "SELECT * FROM foo()" for all legitimately
    1070             :              * user-callable functions).
    1071             :              */
    1072         548 :             if (typid == VOIDOID || typid == CSTRINGOID)
    1073         540 :                 return TYPEFUNC_SCALAR;
    1074           8 :             return TYPEFUNC_OTHER;
    1075             :     }
    1076             :     /* shouldn't get here, probably */
    1077           0 :     return TYPEFUNC_OTHER;
    1078             : }
    1079             : 
    1080             : 
    1081             : /*
    1082             :  * get_func_arg_info
    1083             :  *
    1084             :  * Fetch info about the argument types, names, and IN/OUT modes from the
    1085             :  * pg_proc tuple.  Return value is the total number of arguments.
    1086             :  * Other results are palloc'd.  *p_argtypes is always filled in, but
    1087             :  * *p_argnames and *p_argmodes will be set NULL in the default cases
    1088             :  * (no names, and all IN arguments, respectively).
    1089             :  *
    1090             :  * Note that this function simply fetches what is in the pg_proc tuple;
    1091             :  * it doesn't do any interpretation of polymorphic types.
    1092             :  */
    1093             : int
    1094       10476 : get_func_arg_info(HeapTuple procTup,
    1095             :                   Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
    1096             : {
    1097       10476 :     Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
    1098             :     Datum       proallargtypes;
    1099             :     Datum       proargmodes;
    1100             :     Datum       proargnames;
    1101             :     bool        isNull;
    1102             :     ArrayType  *arr;
    1103             :     int         numargs;
    1104             :     Datum      *elems;
    1105             :     int         nelems;
    1106             :     int         i;
    1107             : 
    1108             :     /* First discover the total number of parameters and get their types */
    1109       10476 :     proallargtypes = SysCacheGetAttr(PROCOID, procTup,
    1110             :                                      Anum_pg_proc_proallargtypes,
    1111             :                                      &isNull);
    1112       10476 :     if (!isNull)
    1113             :     {
    1114             :         /*
    1115             :          * We expect the arrays to be 1-D arrays of the right types; verify
    1116             :          * that.  For the OID and char arrays, we don't need to use
    1117             :          * deconstruct_array() since the array data is just going to look like
    1118             :          * a C array of values.
    1119             :          */
    1120         720 :         arr = DatumGetArrayTypeP(proallargtypes);   /* ensure not toasted */
    1121         720 :         numargs = ARR_DIMS(arr)[0];
    1122         720 :         if (ARR_NDIM(arr) != 1 ||
    1123         720 :             numargs < 0 ||
    1124         720 :             ARR_HASNULL(arr) ||
    1125         720 :             ARR_ELEMTYPE(arr) != OIDOID)
    1126           0 :             elog(ERROR, "proallargtypes is not a 1-D Oid array");
    1127             :         Assert(numargs >= procStruct->pronargs);
    1128         720 :         *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
    1129         720 :         memcpy(*p_argtypes, ARR_DATA_PTR(arr),
    1130             :                numargs * sizeof(Oid));
    1131             :     }
    1132             :     else
    1133             :     {
    1134             :         /* If no proallargtypes, use proargtypes */
    1135        9756 :         numargs = procStruct->proargtypes.dim1;
    1136             :         Assert(numargs == procStruct->pronargs);
    1137        9756 :         *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
    1138        9756 :         memcpy(*p_argtypes, procStruct->proargtypes.values,
    1139             :                numargs * sizeof(Oid));
    1140             :     }
    1141             : 
    1142             :     /* Get argument names, if available */
    1143       10476 :     proargnames = SysCacheGetAttr(PROCOID, procTup,
    1144             :                                   Anum_pg_proc_proargnames,
    1145             :                                   &isNull);
    1146       10476 :     if (isNull)
    1147        7388 :         *p_argnames = NULL;
    1148             :     else
    1149             :     {
    1150        3088 :         deconstruct_array(DatumGetArrayTypeP(proargnames),
    1151             :                           TEXTOID, -1, false, TYPALIGN_INT,
    1152             :                           &elems, NULL, &nelems);
    1153        3088 :         if (nelems != numargs)  /* should not happen */
    1154           0 :             elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
    1155        3088 :         *p_argnames = (char **) palloc(sizeof(char *) * numargs);
    1156       10790 :         for (i = 0; i < numargs; i++)
    1157        7702 :             (*p_argnames)[i] = TextDatumGetCString(elems[i]);
    1158             :     }
    1159             : 
    1160             :     /* Get argument modes, if available */
    1161       10476 :     proargmodes = SysCacheGetAttr(PROCOID, procTup,
    1162             :                                   Anum_pg_proc_proargmodes,
    1163             :                                   &isNull);
    1164       10476 :     if (isNull)
    1165        9756 :         *p_argmodes = NULL;
    1166             :     else
    1167             :     {
    1168         720 :         arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
    1169         720 :         if (ARR_NDIM(arr) != 1 ||
    1170         720 :             ARR_DIMS(arr)[0] != numargs ||
    1171         720 :             ARR_HASNULL(arr) ||
    1172         720 :             ARR_ELEMTYPE(arr) != CHAROID)
    1173           0 :             elog(ERROR, "proargmodes is not a 1-D char array");
    1174         720 :         *p_argmodes = (char *) palloc(numargs * sizeof(char));
    1175         720 :         memcpy(*p_argmodes, ARR_DATA_PTR(arr),
    1176             :                numargs * sizeof(char));
    1177             :     }
    1178             : 
    1179       10476 :     return numargs;
    1180             : }
    1181             : 
    1182             : /*
    1183             :  * get_func_trftypes
    1184             :  *
    1185             :  * Returns the number of transformed types used by function.
    1186             :  */
    1187             : int
    1188          24 : get_func_trftypes(HeapTuple procTup,
    1189             :                   Oid **p_trftypes)
    1190             : {
    1191             :     Datum       protrftypes;
    1192             :     ArrayType  *arr;
    1193             :     int         nelems;
    1194             :     bool        isNull;
    1195             : 
    1196          24 :     protrftypes = SysCacheGetAttr(PROCOID, procTup,
    1197             :                                   Anum_pg_proc_protrftypes,
    1198             :                                   &isNull);
    1199          24 :     if (!isNull)
    1200             :     {
    1201             :         /*
    1202             :          * We expect the arrays to be 1-D arrays of the right types; verify
    1203             :          * that.  For the OID and char arrays, we don't need to use
    1204             :          * deconstruct_array() since the array data is just going to look like
    1205             :          * a C array of values.
    1206             :          */
    1207           0 :         arr = DatumGetArrayTypeP(protrftypes);  /* ensure not toasted */
    1208           0 :         nelems = ARR_DIMS(arr)[0];
    1209           0 :         if (ARR_NDIM(arr) != 1 ||
    1210           0 :             nelems < 0 ||
    1211           0 :             ARR_HASNULL(arr) ||
    1212           0 :             ARR_ELEMTYPE(arr) != OIDOID)
    1213           0 :             elog(ERROR, "protrftypes is not a 1-D Oid array");
    1214             :         Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
    1215           0 :         *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
    1216           0 :         memcpy(*p_trftypes, ARR_DATA_PTR(arr),
    1217             :                nelems * sizeof(Oid));
    1218             : 
    1219           0 :         return nelems;
    1220             :     }
    1221             :     else
    1222          24 :         return 0;
    1223             : }
    1224             : 
    1225             : /*
    1226             :  * get_func_input_arg_names
    1227             :  *
    1228             :  * Extract the names of input arguments only, given a function's
    1229             :  * proargnames and proargmodes entries in Datum form.
    1230             :  *
    1231             :  * Returns the number of input arguments, which is the length of the
    1232             :  * palloc'd array returned to *arg_names.  Entries for unnamed args
    1233             :  * are set to NULL.  You don't get anything if proargnames is NULL.
    1234             :  */
    1235             : int
    1236       18646 : get_func_input_arg_names(Datum proargnames, Datum proargmodes,
    1237             :                          char ***arg_names)
    1238             : {
    1239             :     ArrayType  *arr;
    1240             :     int         numargs;
    1241             :     Datum      *argnames;
    1242             :     char       *argmodes;
    1243             :     char      **inargnames;
    1244             :     int         numinargs;
    1245             :     int         i;
    1246             : 
    1247             :     /* Do nothing if null proargnames */
    1248       18646 :     if (proargnames == PointerGetDatum(NULL))
    1249             :     {
    1250        6858 :         *arg_names = NULL;
    1251        6858 :         return 0;
    1252             :     }
    1253             : 
    1254             :     /*
    1255             :      * We expect the arrays to be 1-D arrays of the right types; verify that.
    1256             :      * For proargmodes, we don't need to use deconstruct_array() since the
    1257             :      * array data is just going to look like a C array of values.
    1258             :      */
    1259       11788 :     arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
    1260       11788 :     if (ARR_NDIM(arr) != 1 ||
    1261       11788 :         ARR_HASNULL(arr) ||
    1262       11788 :         ARR_ELEMTYPE(arr) != TEXTOID)
    1263           0 :         elog(ERROR, "proargnames is not a 1-D text array");
    1264       11788 :     deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
    1265             :                       &argnames, NULL, &numargs);
    1266       11788 :     if (proargmodes != PointerGetDatum(NULL))
    1267             :     {
    1268        6204 :         arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
    1269        6204 :         if (ARR_NDIM(arr) != 1 ||
    1270        6204 :             ARR_DIMS(arr)[0] != numargs ||
    1271        6204 :             ARR_HASNULL(arr) ||
    1272        6204 :             ARR_ELEMTYPE(arr) != CHAROID)
    1273           0 :             elog(ERROR, "proargmodes is not a 1-D char array");
    1274        6204 :         argmodes = (char *) ARR_DATA_PTR(arr);
    1275             :     }
    1276             :     else
    1277        5584 :         argmodes = NULL;
    1278             : 
    1279             :     /* zero elements probably shouldn't happen, but handle it gracefully */
    1280       11788 :     if (numargs <= 0)
    1281             :     {
    1282           0 :         *arg_names = NULL;
    1283           0 :         return 0;
    1284             :     }
    1285             : 
    1286             :     /* extract input-argument names */
    1287       11788 :     inargnames = (char **) palloc(numargs * sizeof(char *));
    1288       11788 :     numinargs = 0;
    1289       64424 :     for (i = 0; i < numargs; i++)
    1290             :     {
    1291       52636 :         if (argmodes == NULL ||
    1292       38168 :             argmodes[i] == PROARGMODE_IN ||
    1293       22064 :             argmodes[i] == PROARGMODE_INOUT ||
    1294       22008 :             argmodes[i] == PROARGMODE_VARIADIC)
    1295             :         {
    1296       33536 :             char       *pname = TextDatumGetCString(argnames[i]);
    1297             : 
    1298       33536 :             if (pname[0] != '\0')
    1299       33484 :                 inargnames[numinargs] = pname;
    1300             :             else
    1301          52 :                 inargnames[numinargs] = NULL;
    1302       33536 :             numinargs++;
    1303             :         }
    1304             :     }
    1305             : 
    1306       11788 :     *arg_names = inargnames;
    1307       11788 :     return numinargs;
    1308             : }
    1309             : 
    1310             : 
    1311             : /*
    1312             :  * get_func_result_name
    1313             :  *
    1314             :  * If the function has exactly one output parameter, and that parameter
    1315             :  * is named, return the name (as a palloc'd string).  Else return NULL.
    1316             :  *
    1317             :  * This is used to determine the default output column name for functions
    1318             :  * returning scalar types.
    1319             :  */
    1320             : char *
    1321       20444 : get_func_result_name(Oid functionId)
    1322             : {
    1323             :     char       *result;
    1324             :     HeapTuple   procTuple;
    1325             :     Datum       proargmodes;
    1326             :     Datum       proargnames;
    1327             :     bool        isnull;
    1328             :     ArrayType  *arr;
    1329             :     int         numargs;
    1330             :     char       *argmodes;
    1331             :     Datum      *argnames;
    1332             :     int         numoutargs;
    1333             :     int         nargnames;
    1334             :     int         i;
    1335             : 
    1336             :     /* First fetch the function's pg_proc row */
    1337       20444 :     procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
    1338       20444 :     if (!HeapTupleIsValid(procTuple))
    1339           0 :         elog(ERROR, "cache lookup failed for function %u", functionId);
    1340             : 
    1341             :     /* If there are no named OUT parameters, return NULL */
    1342       21232 :     if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
    1343         788 :         heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
    1344       19676 :         result = NULL;
    1345             :     else
    1346             :     {
    1347             :         /* Get the data out of the tuple */
    1348         768 :         proargmodes = SysCacheGetAttr(PROCOID, procTuple,
    1349             :                                       Anum_pg_proc_proargmodes,
    1350             :                                       &isnull);
    1351             :         Assert(!isnull);
    1352         768 :         proargnames = SysCacheGetAttr(PROCOID, procTuple,
    1353             :                                       Anum_pg_proc_proargnames,
    1354             :                                       &isnull);
    1355             :         Assert(!isnull);
    1356             : 
    1357             :         /*
    1358             :          * We expect the arrays to be 1-D arrays of the right types; verify
    1359             :          * that.  For the char array, we don't need to use deconstruct_array()
    1360             :          * since the array data is just going to look like a C array of
    1361             :          * values.
    1362             :          */
    1363         768 :         arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
    1364         768 :         numargs = ARR_DIMS(arr)[0];
    1365         768 :         if (ARR_NDIM(arr) != 1 ||
    1366         768 :             numargs < 0 ||
    1367         768 :             ARR_HASNULL(arr) ||
    1368         768 :             ARR_ELEMTYPE(arr) != CHAROID)
    1369           0 :             elog(ERROR, "proargmodes is not a 1-D char array");
    1370         768 :         argmodes = (char *) ARR_DATA_PTR(arr);
    1371         768 :         arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
    1372         768 :         if (ARR_NDIM(arr) != 1 ||
    1373         768 :             ARR_DIMS(arr)[0] != numargs ||
    1374         768 :             ARR_HASNULL(arr) ||
    1375         768 :             ARR_ELEMTYPE(arr) != TEXTOID)
    1376           0 :             elog(ERROR, "proargnames is not a 1-D text array");
    1377         768 :         deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
    1378             :                           &argnames, NULL, &nargnames);
    1379             :         Assert(nargnames == numargs);
    1380             : 
    1381             :         /* scan for output argument(s) */
    1382         768 :         result = NULL;
    1383         768 :         numoutargs = 0;
    1384        2290 :         for (i = 0; i < numargs; i++)
    1385             :         {
    1386        1522 :             if (argmodes[i] == PROARGMODE_IN ||
    1387         768 :                 argmodes[i] == PROARGMODE_VARIADIC)
    1388         754 :                 continue;
    1389             :             Assert(argmodes[i] == PROARGMODE_OUT ||
    1390             :                    argmodes[i] == PROARGMODE_INOUT ||
    1391             :                    argmodes[i] == PROARGMODE_TABLE);
    1392         768 :             if (++numoutargs > 1)
    1393             :             {
    1394             :                 /* multiple out args, so forget it */
    1395           0 :                 result = NULL;
    1396           0 :                 break;
    1397             :             }
    1398         768 :             result = TextDatumGetCString(argnames[i]);
    1399         768 :             if (result == NULL || result[0] == '\0')
    1400             :             {
    1401             :                 /* Parameter is not named, so forget it */
    1402           0 :                 result = NULL;
    1403           0 :                 break;
    1404             :             }
    1405             :         }
    1406             :     }
    1407             : 
    1408       20444 :     ReleaseSysCache(procTuple);
    1409             : 
    1410       20444 :     return result;
    1411             : }
    1412             : 
    1413             : 
    1414             : /*
    1415             :  * build_function_result_tupdesc_t
    1416             :  *
    1417             :  * Given a pg_proc row for a function, return a tuple descriptor for the
    1418             :  * result rowtype, or NULL if the function does not have OUT parameters.
    1419             :  *
    1420             :  * Note that this does not handle resolution of polymorphic types;
    1421             :  * that is deliberate.
    1422             :  */
    1423             : TupleDesc
    1424      178242 : build_function_result_tupdesc_t(HeapTuple procTuple)
    1425             : {
    1426      178242 :     Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
    1427             :     Datum       proallargtypes;
    1428             :     Datum       proargmodes;
    1429             :     Datum       proargnames;
    1430             :     bool        isnull;
    1431             : 
    1432             :     /* Return NULL if the function isn't declared to return RECORD */
    1433      178242 :     if (procform->prorettype != RECORDOID)
    1434       81496 :         return NULL;
    1435             : 
    1436             :     /* If there are no OUT parameters, return NULL */
    1437      192006 :     if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
    1438       95260 :         heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
    1439        1486 :         return NULL;
    1440             : 
    1441             :     /* Get the data out of the tuple */
    1442       95260 :     proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
    1443             :                                      Anum_pg_proc_proallargtypes,
    1444             :                                      &isnull);
    1445             :     Assert(!isnull);
    1446       95260 :     proargmodes = SysCacheGetAttr(PROCOID, procTuple,
    1447             :                                   Anum_pg_proc_proargmodes,
    1448             :                                   &isnull);
    1449             :     Assert(!isnull);
    1450       95260 :     proargnames = SysCacheGetAttr(PROCOID, procTuple,
    1451             :                                   Anum_pg_proc_proargnames,
    1452             :                                   &isnull);
    1453       95260 :     if (isnull)
    1454          70 :         proargnames = PointerGetDatum(NULL);    /* just to be sure */
    1455             : 
    1456       95260 :     return build_function_result_tupdesc_d(procform->prokind,
    1457             :                                            proallargtypes,
    1458             :                                            proargmodes,
    1459             :                                            proargnames);
    1460             : }
    1461             : 
    1462             : /*
    1463             :  * build_function_result_tupdesc_d
    1464             :  *
    1465             :  * Build a RECORD function's tupledesc from the pg_proc proallargtypes,
    1466             :  * proargmodes, and proargnames arrays.  This is split out for the
    1467             :  * convenience of ProcedureCreate, which needs to be able to compute the
    1468             :  * tupledesc before actually creating the function.
    1469             :  *
    1470             :  * For functions (but not for procedures), returns NULL if there are not at
    1471             :  * least two OUT or INOUT arguments.
    1472             :  */
    1473             : TupleDesc
    1474       97824 : build_function_result_tupdesc_d(char prokind,
    1475             :                                 Datum proallargtypes,
    1476             :                                 Datum proargmodes,
    1477             :                                 Datum proargnames)
    1478             : {
    1479             :     TupleDesc   desc;
    1480             :     ArrayType  *arr;
    1481             :     int         numargs;
    1482             :     Oid        *argtypes;
    1483             :     char       *argmodes;
    1484       97824 :     Datum      *argnames = NULL;
    1485             :     Oid        *outargtypes;
    1486             :     char      **outargnames;
    1487             :     int         numoutargs;
    1488             :     int         nargnames;
    1489             :     int         i;
    1490             : 
    1491             :     /* Can't have output args if columns are null */
    1492       97824 :     if (proallargtypes == PointerGetDatum(NULL) ||
    1493             :         proargmodes == PointerGetDatum(NULL))
    1494          22 :         return NULL;
    1495             : 
    1496             :     /*
    1497             :      * We expect the arrays to be 1-D arrays of the right types; verify that.
    1498             :      * For the OID and char arrays, we don't need to use deconstruct_array()
    1499             :      * since the array data is just going to look like a C array of values.
    1500             :      */
    1501       97802 :     arr = DatumGetArrayTypeP(proallargtypes);   /* ensure not toasted */
    1502       97802 :     numargs = ARR_DIMS(arr)[0];
    1503       97802 :     if (ARR_NDIM(arr) != 1 ||
    1504       97802 :         numargs < 0 ||
    1505       97802 :         ARR_HASNULL(arr) ||
    1506       97802 :         ARR_ELEMTYPE(arr) != OIDOID)
    1507           0 :         elog(ERROR, "proallargtypes is not a 1-D Oid array");
    1508       97802 :     argtypes = (Oid *) ARR_DATA_PTR(arr);
    1509       97802 :     arr = DatumGetArrayTypeP(proargmodes);  /* ensure not toasted */
    1510       97802 :     if (ARR_NDIM(arr) != 1 ||
    1511       97802 :         ARR_DIMS(arr)[0] != numargs ||
    1512       97802 :         ARR_HASNULL(arr) ||
    1513       97802 :         ARR_ELEMTYPE(arr) != CHAROID)
    1514           0 :         elog(ERROR, "proargmodes is not a 1-D char array");
    1515       97802 :     argmodes = (char *) ARR_DATA_PTR(arr);
    1516       97802 :     if (proargnames != PointerGetDatum(NULL))
    1517             :     {
    1518       97724 :         arr = DatumGetArrayTypeP(proargnames);  /* ensure not toasted */
    1519       97724 :         if (ARR_NDIM(arr) != 1 ||
    1520       97724 :             ARR_DIMS(arr)[0] != numargs ||
    1521       97724 :             ARR_HASNULL(arr) ||
    1522       97724 :             ARR_ELEMTYPE(arr) != TEXTOID)
    1523           0 :             elog(ERROR, "proargnames is not a 1-D text array");
    1524       97724 :         deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
    1525             :                           &argnames, NULL, &nargnames);
    1526             :         Assert(nargnames == numargs);
    1527             :     }
    1528             : 
    1529             :     /* zero elements probably shouldn't happen, but handle it gracefully */
    1530       97802 :     if (numargs <= 0)
    1531           0 :         return NULL;
    1532             : 
    1533             :     /* extract output-argument types and names */
    1534       97802 :     outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
    1535       97802 :     outargnames = (char **) palloc(numargs * sizeof(char *));
    1536       97802 :     numoutargs = 0;
    1537     1309264 :     for (i = 0; i < numargs; i++)
    1538             :     {
    1539             :         char       *pname;
    1540             : 
    1541     1211462 :         if (argmodes[i] == PROARGMODE_IN ||
    1542     1120900 :             argmodes[i] == PROARGMODE_VARIADIC)
    1543       94196 :             continue;
    1544             :         Assert(argmodes[i] == PROARGMODE_OUT ||
    1545             :                argmodes[i] == PROARGMODE_INOUT ||
    1546             :                argmodes[i] == PROARGMODE_TABLE);
    1547     1117266 :         outargtypes[numoutargs] = argtypes[i];
    1548     1117266 :         if (argnames)
    1549     1117110 :             pname = TextDatumGetCString(argnames[i]);
    1550             :         else
    1551         156 :             pname = NULL;
    1552     1117266 :         if (pname == NULL || pname[0] == '\0')
    1553             :         {
    1554             :             /* Parameter is not named, so gin up a column name */
    1555         348 :             pname = psprintf("column%d", numoutargs + 1);
    1556             :         }
    1557     1117266 :         outargnames[numoutargs] = pname;
    1558     1117266 :         numoutargs++;
    1559             :     }
    1560             : 
    1561             :     /*
    1562             :      * If there is no output argument, or only one, the function does not
    1563             :      * return tuples.
    1564             :      */
    1565       97802 :     if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
    1566           0 :         return NULL;
    1567             : 
    1568       97802 :     desc = CreateTemplateTupleDesc(numoutargs);
    1569     1215068 :     for (i = 0; i < numoutargs; i++)
    1570             :     {
    1571     2234532 :         TupleDescInitEntry(desc, i + 1,
    1572     1117266 :                            outargnames[i],
    1573     1117266 :                            outargtypes[i],
    1574             :                            -1,
    1575             :                            0);
    1576             :     }
    1577             : 
    1578       97802 :     return desc;
    1579             : }
    1580             : 
    1581             : 
    1582             : /*
    1583             :  * RelationNameGetTupleDesc
    1584             :  *
    1585             :  * Given a (possibly qualified) relation name, build a TupleDesc.
    1586             :  *
    1587             :  * Note: while this works as advertised, it's seldom the best way to
    1588             :  * build a tupdesc for a function's result type.  It's kept around
    1589             :  * only for backwards compatibility with existing user-written code.
    1590             :  */
    1591             : TupleDesc
    1592           0 : RelationNameGetTupleDesc(const char *relname)
    1593             : {
    1594             :     RangeVar   *relvar;
    1595             :     Relation    rel;
    1596             :     TupleDesc   tupdesc;
    1597             :     List       *relname_list;
    1598             : 
    1599             :     /* Open relation and copy the tuple description */
    1600           0 :     relname_list = stringToQualifiedNameList(relname);
    1601           0 :     relvar = makeRangeVarFromNameList(relname_list);
    1602           0 :     rel = relation_openrv(relvar, AccessShareLock);
    1603           0 :     tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
    1604           0 :     relation_close(rel, AccessShareLock);
    1605             : 
    1606           0 :     return tupdesc;
    1607             : }
    1608             : 
    1609             : /*
    1610             :  * TypeGetTupleDesc
    1611             :  *
    1612             :  * Given a type Oid, build a TupleDesc.  (In most cases you should be
    1613             :  * using get_call_result_type or one of its siblings instead of this
    1614             :  * routine, so that you can handle OUT parameters, RECORD result type,
    1615             :  * and polymorphic results.)
    1616             :  *
    1617             :  * If the type is composite, *and* a colaliases List is provided, *and*
    1618             :  * the List is of natts length, use the aliases instead of the relation
    1619             :  * attnames.  (NB: this usage is deprecated since it may result in
    1620             :  * creation of unnecessary transient record types.)
    1621             :  *
    1622             :  * If the type is a base type, a single item alias List is required.
    1623             :  */
    1624             : TupleDesc
    1625           0 : TypeGetTupleDesc(Oid typeoid, List *colaliases)
    1626             : {
    1627             :     Oid         base_typeoid;
    1628           0 :     TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
    1629           0 :     TupleDesc   tupdesc = NULL;
    1630             : 
    1631             :     /*
    1632             :      * Build a suitable tupledesc representing the output rows.  We
    1633             :      * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
    1634             :      * unlikely that legacy callers of this obsolete function would be
    1635             :      * prepared to apply domain constraints.
    1636             :      */
    1637           0 :     if (functypclass == TYPEFUNC_COMPOSITE)
    1638             :     {
    1639             :         /* Composite data type, e.g. a table's row type */
    1640           0 :         tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
    1641             : 
    1642           0 :         if (colaliases != NIL)
    1643             :         {
    1644           0 :             int         natts = tupdesc->natts;
    1645             :             int         varattno;
    1646             : 
    1647             :             /* does the list length match the number of attributes? */
    1648           0 :             if (list_length(colaliases) != natts)
    1649           0 :                 ereport(ERROR,
    1650             :                         (errcode(ERRCODE_DATATYPE_MISMATCH),
    1651             :                          errmsg("number of aliases does not match number of columns")));
    1652             : 
    1653             :             /* OK, use the aliases instead */
    1654           0 :             for (varattno = 0; varattno < natts; varattno++)
    1655             :             {
    1656           0 :                 char       *label = strVal(list_nth(colaliases, varattno));
    1657           0 :                 Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
    1658             : 
    1659           0 :                 if (label != NULL)
    1660           0 :                     namestrcpy(&(attr->attname), label);
    1661             :             }
    1662             : 
    1663             :             /* The tuple type is now an anonymous record type */
    1664           0 :             tupdesc->tdtypeid = RECORDOID;
    1665           0 :             tupdesc->tdtypmod = -1;
    1666             :         }
    1667             :     }
    1668           0 :     else if (functypclass == TYPEFUNC_SCALAR)
    1669             :     {
    1670             :         /* Base data type, i.e. scalar */
    1671             :         char       *attname;
    1672             : 
    1673             :         /* the alias list is required for base types */
    1674           0 :         if (colaliases == NIL)
    1675           0 :             ereport(ERROR,
    1676             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    1677             :                      errmsg("no column alias was provided")));
    1678             : 
    1679             :         /* the alias list length must be 1 */
    1680           0 :         if (list_length(colaliases) != 1)
    1681           0 :             ereport(ERROR,
    1682             :                     (errcode(ERRCODE_DATATYPE_MISMATCH),
    1683             :                      errmsg("number of aliases does not match number of columns")));
    1684             : 
    1685             :         /* OK, get the column alias */
    1686           0 :         attname = strVal(linitial(colaliases));
    1687             : 
    1688           0 :         tupdesc = CreateTemplateTupleDesc(1);
    1689           0 :         TupleDescInitEntry(tupdesc,
    1690             :                            (AttrNumber) 1,
    1691             :                            attname,
    1692             :                            typeoid,
    1693             :                            -1,
    1694             :                            0);
    1695             :     }
    1696           0 :     else if (functypclass == TYPEFUNC_RECORD)
    1697             :     {
    1698             :         /* XXX can't support this because typmod wasn't passed in ... */
    1699           0 :         ereport(ERROR,
    1700             :                 (errcode(ERRCODE_DATATYPE_MISMATCH),
    1701             :                  errmsg("could not determine row description for function returning record")));
    1702             :     }
    1703             :     else
    1704             :     {
    1705             :         /* crummy error message, but parser should have caught this */
    1706           0 :         elog(ERROR, "function in FROM has unsupported return type");
    1707             :     }
    1708             : 
    1709           0 :     return tupdesc;
    1710             : }
    1711             : 
    1712             : /*
    1713             :  * extract_variadic_args
    1714             :  *
    1715             :  * Extract a set of argument values, types and NULL markers for a given
    1716             :  * input function which makes use of a VARIADIC input whose argument list
    1717             :  * depends on the caller context. When doing a VARIADIC call, the caller
    1718             :  * has provided one argument made of an array of values, so deconstruct the
    1719             :  * array data before using it for the next processing. If no VARIADIC call
    1720             :  * is used, just fill in the status data based on all the arguments given
    1721             :  * by the caller.
    1722             :  *
    1723             :  * This function returns the number of arguments generated, or -1 in the
    1724             :  * case of "VARIADIC NULL".
    1725             :  */
    1726             : int
    1727         464 : extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start,
    1728             :                       bool convert_unknown, Datum **args, Oid **types,
    1729             :                       bool **nulls)
    1730             : {
    1731         464 :     bool        variadic = get_fn_expr_variadic(fcinfo->flinfo);
    1732             :     Datum      *args_res;
    1733             :     bool       *nulls_res;
    1734             :     Oid        *types_res;
    1735             :     int         nargs,
    1736             :                 i;
    1737             : 
    1738         464 :     *args = NULL;
    1739         464 :     *types = NULL;
    1740         464 :     *nulls = NULL;
    1741             : 
    1742         464 :     if (variadic)
    1743             :     {
    1744             :         ArrayType  *array_in;
    1745             :         Oid         element_type;
    1746             :         bool        typbyval;
    1747             :         char        typalign;
    1748             :         int16       typlen;
    1749             : 
    1750             :         Assert(PG_NARGS() == variadic_start + 1);
    1751             : 
    1752         120 :         if (PG_ARGISNULL(variadic_start))
    1753          16 :             return -1;
    1754             : 
    1755         104 :         array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
    1756         104 :         element_type = ARR_ELEMTYPE(array_in);
    1757             : 
    1758         104 :         get_typlenbyvalalign(element_type,
    1759             :                              &typlen, &typbyval, &typalign);
    1760         104 :         deconstruct_array(array_in, element_type, typlen, typbyval,
    1761             :                           typalign, &args_res, &nulls_res,
    1762             :                           &nargs);
    1763             : 
    1764             :         /* All the elements of the array have the same type */
    1765         104 :         types_res = (Oid *) palloc0(nargs * sizeof(Oid));
    1766         424 :         for (i = 0; i < nargs; i++)
    1767         320 :             types_res[i] = element_type;
    1768             :     }
    1769             :     else
    1770             :     {
    1771         344 :         nargs = PG_NARGS() - variadic_start;
    1772             :         Assert(nargs > 0);
    1773         344 :         nulls_res = (bool *) palloc0(nargs * sizeof(bool));
    1774         344 :         args_res = (Datum *) palloc0(nargs * sizeof(Datum));
    1775         344 :         types_res = (Oid *) palloc0(nargs * sizeof(Oid));
    1776             : 
    1777        1336 :         for (i = 0; i < nargs; i++)
    1778             :         {
    1779         992 :             nulls_res[i] = PG_ARGISNULL(i + variadic_start);
    1780         992 :             types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
    1781             :                                                i + variadic_start);
    1782             : 
    1783             :             /*
    1784             :              * Turn a constant (more or less literal) value that's of unknown
    1785             :              * type into text if required. Unknowns come in as a cstring
    1786             :              * pointer. Note: for functions declared as taking type "any", the
    1787             :              * parser will not do any type conversion on unknown-type literals
    1788             :              * (that is, undecorated strings or NULLs).
    1789             :              */
    1790         992 :             if (convert_unknown &&
    1791        1052 :                 types_res[i] == UNKNOWNOID &&
    1792         332 :                 get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
    1793             :             {
    1794         332 :                 types_res[i] = TEXTOID;
    1795             : 
    1796         664 :                 if (PG_ARGISNULL(i + variadic_start))
    1797          24 :                     args_res[i] = (Datum) 0;
    1798             :                 else
    1799         616 :                     args_res[i] =
    1800         308 :                         CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
    1801             :             }
    1802             :             else
    1803             :             {
    1804             :                 /* no conversion needed, just take the datum as given */
    1805         660 :                 args_res[i] = PG_GETARG_DATUM(i + variadic_start);
    1806             :             }
    1807             : 
    1808         992 :             if (!OidIsValid(types_res[i]) ||
    1809         720 :                 (convert_unknown && types_res[i] == UNKNOWNOID))
    1810           0 :                 ereport(ERROR,
    1811             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1812             :                          errmsg("could not determine data type for argument %d",
    1813             :                                 i + 1)));
    1814             :         }
    1815             :     }
    1816             : 
    1817             :     /* Fill in results */
    1818         448 :     *args = args_res;
    1819         448 :     *nulls = nulls_res;
    1820         448 :     *types = types_res;
    1821             : 
    1822         448 :     return nargs;
    1823             : }

Generated by: LCOV version 1.13