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

Generated by: LCOV version 1.13