PostgreSQL Source Code  git master
funcapi.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/regproc.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tuplestore.h"
#include "utils/typcache.h"
Include dependency graph for funcapi.c:

Go to the source code of this file.

Data Structures

struct  polymorphic_actuals
 

Typedefs

typedef struct polymorphic_actuals polymorphic_actuals
 

Functions

static void shutdown_MultiFuncCall (Datum arg)
 
static TypeFuncClass internal_get_result_type (Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
static void resolve_anyelement_from_others (polymorphic_actuals *actuals)
 
static void resolve_anyarray_from_others (polymorphic_actuals *actuals)
 
static void resolve_anyrange_from_others (polymorphic_actuals *actuals)
 
static void resolve_anymultirange_from_others (polymorphic_actuals *actuals)
 
static bool resolve_polymorphic_tupdesc (TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
 
static TypeFuncClass get_type_func_class (Oid typid, Oid *base_typeid)
 
void SetSingleFuncCall (FunctionCallInfo fcinfo, bits32 flags)
 
FuncCallContextinit_MultiFuncCall (PG_FUNCTION_ARGS)
 
FuncCallContextper_MultiFuncCall (PG_FUNCTION_ARGS)
 
void end_MultiFuncCall (PG_FUNCTION_ARGS, FuncCallContext *funcctx)
 
TypeFuncClass get_call_result_type (FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_expr_result_type (Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_func_result_type (Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TupleDesc get_expr_result_tupdesc (Node *expr, bool noError)
 
bool resolve_polymorphic_argtypes (int numargs, Oid *argtypes, char *argmodes, Node *call_expr)
 
int get_func_arg_info (HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
 
int get_func_trftypes (HeapTuple procTup, Oid **p_trftypes)
 
int get_func_input_arg_names (Datum proargnames, Datum proargmodes, char ***arg_names)
 
char * get_func_result_name (Oid functionId)
 
TupleDesc build_function_result_tupdesc_t (HeapTuple procTuple)
 
TupleDesc build_function_result_tupdesc_d (char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
 
TupleDesc RelationNameGetTupleDesc (const char *relname)
 
TupleDesc TypeGetTupleDesc (Oid typeoid, List *colaliases)
 
int extract_variadic_args (FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
 

Typedef Documentation

◆ polymorphic_actuals

Function Documentation

◆ build_function_result_tupdesc_d()

TupleDesc build_function_result_tupdesc_d ( char  prokind,
Datum  proallargtypes,
Datum  proargmodes,
Datum  proargnames 
)

Definition at line 1721 of file funcapi.c.

1725 {
1726  TupleDesc desc;
1727  ArrayType *arr;
1728  int numargs;
1729  Oid *argtypes;
1730  char *argmodes;
1731  Datum *argnames = NULL;
1732  Oid *outargtypes;
1733  char **outargnames;
1734  int numoutargs;
1735  int nargnames;
1736  int i;
1737 
1738  /* Can't have output args if columns are null */
1739  if (proallargtypes == PointerGetDatum(NULL) ||
1740  proargmodes == PointerGetDatum(NULL))
1741  return NULL;
1742 
1743  /*
1744  * We expect the arrays to be 1-D arrays of the right types; verify that.
1745  * For the OID and char arrays, we don't need to use deconstruct_array()
1746  * since the array data is just going to look like a C array of values.
1747  */
1748  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1749  numargs = ARR_DIMS(arr)[0];
1750  if (ARR_NDIM(arr) != 1 ||
1751  numargs < 0 ||
1752  ARR_HASNULL(arr) ||
1753  ARR_ELEMTYPE(arr) != OIDOID)
1754  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1755  argtypes = (Oid *) ARR_DATA_PTR(arr);
1756  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1757  if (ARR_NDIM(arr) != 1 ||
1758  ARR_DIMS(arr)[0] != numargs ||
1759  ARR_HASNULL(arr) ||
1760  ARR_ELEMTYPE(arr) != CHAROID)
1761  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1762  numargs);
1763  argmodes = (char *) ARR_DATA_PTR(arr);
1764  if (proargnames != PointerGetDatum(NULL))
1765  {
1766  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1767  if (ARR_NDIM(arr) != 1 ||
1768  ARR_DIMS(arr)[0] != numargs ||
1769  ARR_HASNULL(arr) ||
1770  ARR_ELEMTYPE(arr) != TEXTOID)
1771  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1772  numargs);
1773  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1774  &argnames, NULL, &nargnames);
1775  Assert(nargnames == numargs);
1776  }
1777 
1778  /* zero elements probably shouldn't happen, but handle it gracefully */
1779  if (numargs <= 0)
1780  return NULL;
1781 
1782  /* extract output-argument types and names */
1783  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1784  outargnames = (char **) palloc(numargs * sizeof(char *));
1785  numoutargs = 0;
1786  for (i = 0; i < numargs; i++)
1787  {
1788  char *pname;
1789 
1790  if (argmodes[i] == PROARGMODE_IN ||
1791  argmodes[i] == PROARGMODE_VARIADIC)
1792  continue;
1793  Assert(argmodes[i] == PROARGMODE_OUT ||
1794  argmodes[i] == PROARGMODE_INOUT ||
1795  argmodes[i] == PROARGMODE_TABLE);
1796  outargtypes[numoutargs] = argtypes[i];
1797  if (argnames)
1798  pname = TextDatumGetCString(argnames[i]);
1799  else
1800  pname = NULL;
1801  if (pname == NULL || pname[0] == '\0')
1802  {
1803  /* Parameter is not named, so gin up a column name */
1804  pname = psprintf("column%d", numoutargs + 1);
1805  }
1806  outargnames[numoutargs] = pname;
1807  numoutargs++;
1808  }
1809 
1810  /*
1811  * If there is no output argument, or only one, the function does not
1812  * return tuples.
1813  */
1814  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1815  return NULL;
1816 
1817  desc = CreateTemplateTupleDesc(numoutargs);
1818  for (i = 0; i < numoutargs; i++)
1819  {
1820  TupleDescInitEntry(desc, i + 1,
1821  outargnames[i],
1822  outargtypes[i],
1823  -1,
1824  0);
1825  }
1826 
1827  return desc;
1828 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_HASNULL(a)
Definition: array.h:284
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3491
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1068
uintptr_t Datum
Definition: postgres.h:411
#define PointerGetDatum(X)
Definition: postgres.h:600
unsigned int Oid
Definition: postgres_ext.h:31
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:583

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert(), CreateTemplateTupleDesc(), DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, i, palloc(), PointerGetDatum, psprintf(), TextDatumGetCString, and TupleDescInitEntry().

Referenced by build_function_result_tupdesc_t(), and ProcedureCreate().

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1671 of file funcapi.c.

1672 {
1673  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1674  Datum proallargtypes;
1675  Datum proargmodes;
1676  Datum proargnames;
1677  bool isnull;
1678 
1679  /* Return NULL if the function isn't declared to return RECORD */
1680  if (procform->prorettype != RECORDOID)
1681  return NULL;
1682 
1683  /* If there are no OUT parameters, return NULL */
1684  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1685  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1686  return NULL;
1687 
1688  /* Get the data out of the tuple */
1689  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1690  Anum_pg_proc_proallargtypes,
1691  &isnull);
1692  Assert(!isnull);
1693  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1694  Anum_pg_proc_proargmodes,
1695  &isnull);
1696  Assert(!isnull);
1697  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1698  Anum_pg_proc_proargnames,
1699  &isnull);
1700  if (isnull)
1701  proargnames = PointerGetDatum(NULL); /* just to be sure */
1702 
1703  return build_function_result_tupdesc_d(procform->prokind,
1704  proallargtypes,
1705  proargmodes,
1706  proargnames);
1707 }
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1721
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ PROCOID
Definition: syscache.h:79

References Assert(), build_function_result_tupdesc_d(), GETSTRUCT, heap_attisnull(), PointerGetDatum, PROCOID, and SysCacheGetAttr().

Referenced by CallStmtResultDesc(), internal_get_result_type(), and ProcedureCreate().

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 220 of file funcapi.c.

221 {
222  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
223 
224  /* Deregister the shutdown callback */
227  PointerGetDatum(fcinfo->flinfo));
228 
229  /* But use it to do the real work */
230  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
231 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:951
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:238
ExprContext * econtext
Definition: execnodes.h:309

References ReturnSetInfo::econtext, PointerGetDatum, shutdown_MultiFuncCall(), and UnregisterExprContextCallback().

◆ extract_variadic_args()

int extract_variadic_args ( FunctionCallInfo  fcinfo,
int  variadic_start,
bool  convert_unknown,
Datum **  args,
Oid **  types,
bool **  nulls 
)

Definition at line 1976 of file funcapi.c.

1979 {
1980  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1981  Datum *args_res;
1982  bool *nulls_res;
1983  Oid *types_res;
1984  int nargs,
1985  i;
1986 
1987  *args = NULL;
1988  *types = NULL;
1989  *nulls = NULL;
1990 
1991  if (variadic)
1992  {
1994  Oid element_type;
1995  bool typbyval;
1996  char typalign;
1997  int16 typlen;
1998 
1999  Assert(PG_NARGS() == variadic_start + 1);
2000 
2001  if (PG_ARGISNULL(variadic_start))
2002  return -1;
2003 
2004  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
2005  element_type = ARR_ELEMTYPE(array_in);
2006 
2007  get_typlenbyvalalign(element_type,
2008  &typlen, &typbyval, &typalign);
2009  deconstruct_array(array_in, element_type, typlen, typbyval,
2010  typalign, &args_res, &nulls_res,
2011  &nargs);
2012 
2013  /* All the elements of the array have the same type */
2014  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
2015  for (i = 0; i < nargs; i++)
2016  types_res[i] = element_type;
2017  }
2018  else
2019  {
2020  nargs = PG_NARGS() - variadic_start;
2021  Assert(nargs > 0);
2022  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
2023  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
2024  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
2025 
2026  for (i = 0; i < nargs; i++)
2027  {
2028  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
2029  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
2030  i + variadic_start);
2031 
2032  /*
2033  * Turn a constant (more or less literal) value that's of unknown
2034  * type into text if required. Unknowns come in as a cstring
2035  * pointer. Note: for functions declared as taking type "any", the
2036  * parser will not do any type conversion on unknown-type literals
2037  * (that is, undecorated strings or NULLs).
2038  */
2039  if (convert_unknown &&
2040  types_res[i] == UNKNOWNOID &&
2041  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
2042  {
2043  types_res[i] = TEXTOID;
2044 
2045  if (PG_ARGISNULL(i + variadic_start))
2046  args_res[i] = (Datum) 0;
2047  else
2048  args_res[i] =
2049  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
2050  }
2051  else
2052  {
2053  /* no conversion needed, just take the datum as given */
2054  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
2055  }
2056 
2057  if (!OidIsValid(types_res[i]) ||
2058  (convert_unknown && types_res[i] == UNKNOWNOID))
2059  ereport(ERROR,
2060  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2061  errmsg("could not determine data type for argument %d",
2062  i + 1)));
2063  }
2064  }
2065 
2066  /* Fill in results */
2067  *args = args_res;
2068  *nulls = nulls_res;
2069  *types = types_res;
2070 
2071  return nargs;
2072 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:174
#define CStringGetTextDatum(s)
Definition: builtins.h:85
signed short int16
Definition: c.h:428
#define OidIsValid(objectId)
Definition: c.h:710
struct typedefs * types
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1851
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1920
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1786
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_NARGS()
Definition: fmgr.h:203
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2228
void * palloc0(Size size)
Definition: mcxt.c:1099
char typalign
Definition: pg_type.h:176
FmgrInfo * flinfo
Definition: fmgr.h:87

References generate_unaccent_rules::args, ARR_ELEMTYPE, array_in(), Assert(), CStringGetTextDatum, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, FunctionCallInfoBaseData::flinfo, get_fn_expr_arg_stable(), get_fn_expr_argtype(), get_fn_expr_variadic(), get_typlenbyvalalign(), i, OidIsValid, palloc0(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, typalign, and types.

Referenced by json_build_array(), json_build_object(), jsonb_build_array(), and jsonb_build_object().

◆ get_call_result_type()

TypeFuncClass get_call_result_type ( FunctionCallInfo  fcinfo,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 276 of file funcapi.c.

279 {
280  return internal_get_result_type(fcinfo->flinfo->fn_oid,
281  fcinfo->flinfo->fn_expr,
282  (ReturnSetInfo *) fcinfo->resultinfo,
283  resultTypeId,
284  resultTupleDesc);
285 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:389
fmNodePtr fn_expr
Definition: fmgr.h:66
Oid fn_oid
Definition: fmgr.h:59
fmNodePtr resultinfo
Definition: fmgr.h:89

References FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_expr, FmgrInfo::fn_oid, internal_get_result_type(), and FunctionCallInfoBaseData::resultinfo.

Referenced by brin_metapage_info(), bt_metap(), bt_page_items_bytea(), bt_page_items_internal(), bt_page_stats_internal(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), init_sql_fcache(), materializeResult(), page_header(), pg_backup_stop(), pg_buffercache_pages(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_wal_record_info(), pg_replication_slot_advance(), pg_stat_get_wal_receiver(), pg_stat_statements_info(), pg_stats_ext_mcvlist_items(), pgp_armor_headers(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_func_handler(), PLy_function_build_args(), SetSingleFuncCall(), setup_firstcall(), ssl_extension_info(), storeRow(), and test_enc_conversion().

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 509 of file funcapi.c.

510 {
511  TupleDesc tupleDesc;
512  TypeFuncClass functypclass;
513 
514  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
515 
516  if (functypclass == TYPEFUNC_COMPOSITE ||
517  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
518  return tupleDesc;
519 
520  if (!noError)
521  {
522  Oid exprTypeId = exprType(expr);
523 
524  if (exprTypeId != RECORDOID)
525  ereport(ERROR,
526  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
527  errmsg("type %s is not composite",
528  format_type_be(exprTypeId))));
529  else
530  ereport(ERROR,
531  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
532  errmsg("record type has not been registered")));
533  }
534 
535  return NULL;
536 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41

References ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_expr_result_type(), TYPEFUNC_COMPOSITE, and TYPEFUNC_COMPOSITE_DOMAIN.

Referenced by expandRecordVariable(), ExpandRowReference(), get_name_for_var_field(), get_rte_attribute_is_dropped(), and ParseComplexProjection().

◆ get_expr_result_type()

TypeFuncClass get_expr_result_type ( Node expr,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 292 of file funcapi.c.

295 {
296  TypeFuncClass result;
297 
298  if (expr && IsA(expr, FuncExpr))
299  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
300  expr,
301  NULL,
302  resultTypeId,
303  resultTupleDesc);
304  else if (expr && IsA(expr, OpExpr))
305  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
306  expr,
307  NULL,
308  resultTypeId,
309  resultTupleDesc);
310  else if (expr && IsA(expr, RowExpr) &&
311  ((RowExpr *) expr)->row_typeid == RECORDOID)
312  {
313  /* We can resolve the record type by generating the tupdesc directly */
314  RowExpr *rexpr = (RowExpr *) expr;
315  TupleDesc tupdesc;
316  AttrNumber i = 1;
317  ListCell *lcc,
318  *lcn;
319 
320  tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
321  Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
322  forboth(lcc, rexpr->args, lcn, rexpr->colnames)
323  {
324  Node *col = (Node *) lfirst(lcc);
325  char *colname = strVal(lfirst(lcn));
326 
327  TupleDescInitEntry(tupdesc, i,
328  colname,
329  exprType(col),
330  exprTypmod(col),
331  0);
333  exprCollation(col));
334  i++;
335  }
336  if (resultTypeId)
337  *resultTypeId = rexpr->row_typeid;
338  if (resultTupleDesc)
339  *resultTupleDesc = BlessTupleDesc(tupdesc);
340  return TYPEFUNC_COMPOSITE;
341  }
342  else
343  {
344  /* handle as a generic expression; no chance to resolve RECORD */
345  Oid typid = exprType(expr);
346  Oid base_typid;
347 
348  if (resultTypeId)
349  *resultTypeId = typid;
350  if (resultTupleDesc)
351  *resultTupleDesc = NULL;
352  result = get_type_func_class(typid, &base_typid);
353  if ((result == TYPEFUNC_COMPOSITE ||
354  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
355  resultTupleDesc)
356  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
357  }
358 
359  return result;
360 }
int16 AttrNumber
Definition: attnum.h:21
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2071
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1286
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1266
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:286
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:788
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Definition: nodes.h:574
Oid row_typeid
Definition: primnodes.h:1076
List * args
Definition: primnodes.h:1075
List * colnames
Definition: primnodes.h:1091
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:754
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1864
#define strVal(v)
Definition: value.h:72

References RowExpr::args, Assert(), BlessTupleDesc(), RowExpr::colnames, CreateTemplateTupleDesc(), exprCollation(), exprType(), exprTypmod(), forboth, get_opcode(), get_type_func_class(), i, internal_get_result_type(), IsA, lfirst, list_length(), lookup_rowtype_tupdesc_copy(), RowExpr::row_typeid, strVal, TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, and TYPEFUNC_COMPOSITE_DOMAIN.

Referenced by addRangeTableEntryForFunction(), ExecInitFunctionScan(), expandRTE(), get_expr_result_tupdesc(), init_sexpr(), inline_function(), inline_set_returning_function(), and pull_up_constant_function().

◆ get_func_arg_info()

int get_func_arg_info ( HeapTuple  procTup,
Oid **  p_argtypes,
char ***  p_argnames,
char **  p_argmodes 
)

Definition at line 1337 of file funcapi.c.

1339 {
1340  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1341  Datum proallargtypes;
1342  Datum proargmodes;
1343  Datum proargnames;
1344  bool isNull;
1345  ArrayType *arr;
1346  int numargs;
1347  Datum *elems;
1348  int nelems;
1349  int i;
1350 
1351  /* First discover the total number of parameters and get their types */
1352  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
1353  Anum_pg_proc_proallargtypes,
1354  &isNull);
1355  if (!isNull)
1356  {
1357  /*
1358  * We expect the arrays to be 1-D arrays of the right types; verify
1359  * that. For the OID and char arrays, we don't need to use
1360  * deconstruct_array() since the array data is just going to look like
1361  * a C array of values.
1362  */
1363  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1364  numargs = ARR_DIMS(arr)[0];
1365  if (ARR_NDIM(arr) != 1 ||
1366  numargs < 0 ||
1367  ARR_HASNULL(arr) ||
1368  ARR_ELEMTYPE(arr) != OIDOID)
1369  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1370  Assert(numargs >= procStruct->pronargs);
1371  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1372  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
1373  numargs * sizeof(Oid));
1374  }
1375  else
1376  {
1377  /* If no proallargtypes, use proargtypes */
1378  numargs = procStruct->proargtypes.dim1;
1379  Assert(numargs == procStruct->pronargs);
1380  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1381  memcpy(*p_argtypes, procStruct->proargtypes.values,
1382  numargs * sizeof(Oid));
1383  }
1384 
1385  /* Get argument names, if available */
1386  proargnames = SysCacheGetAttr(PROCOID, procTup,
1387  Anum_pg_proc_proargnames,
1388  &isNull);
1389  if (isNull)
1390  *p_argnames = NULL;
1391  else
1392  {
1394  TEXTOID, -1, false, TYPALIGN_INT,
1395  &elems, NULL, &nelems);
1396  if (nelems != numargs) /* should not happen */
1397  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
1398  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
1399  for (i = 0; i < numargs; i++)
1400  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
1401  }
1402 
1403  /* Get argument modes, if available */
1404  proargmodes = SysCacheGetAttr(PROCOID, procTup,
1405  Anum_pg_proc_proargmodes,
1406  &isNull);
1407  if (isNull)
1408  *p_argmodes = NULL;
1409  else
1410  {
1411  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1412  if (ARR_NDIM(arr) != 1 ||
1413  ARR_DIMS(arr)[0] != numargs ||
1414  ARR_HASNULL(arr) ||
1415  ARR_ELEMTYPE(arr) != CHAROID)
1416  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1417  numargs);
1418  *p_argmodes = (char *) palloc(numargs * sizeof(char));
1419  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
1420  numargs * sizeof(char));
1421  }
1422 
1423  return numargs;
1424 }

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert(), DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, GETSTRUCT, i, palloc(), PROCOID, SysCacheGetAttr(), and TextDatumGetCString.

Referenced by do_compile(), make_callstmt_target(), MatchNamedCall(), pg_get_function_arg_default(), plperl_validator(), plpgsql_validator(), plsample_func_handler(), PLy_procedure_create(), print_function_arguments(), and print_function_sqlbody().

◆ get_func_input_arg_names()

int get_func_input_arg_names ( Datum  proargnames,
Datum  proargmodes,
char ***  arg_names 
)

Definition at line 1481 of file funcapi.c.

1483 {
1484  ArrayType *arr;
1485  int numargs;
1486  Datum *argnames;
1487  char *argmodes;
1488  char **inargnames;
1489  int numinargs;
1490  int i;
1491 
1492  /* Do nothing if null proargnames */
1493  if (proargnames == PointerGetDatum(NULL))
1494  {
1495  *arg_names = NULL;
1496  return 0;
1497  }
1498 
1499  /*
1500  * We expect the arrays to be 1-D arrays of the right types; verify that.
1501  * For proargmodes, we don't need to use deconstruct_array() since the
1502  * array data is just going to look like a C array of values.
1503  */
1504  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1505  if (ARR_NDIM(arr) != 1 ||
1506  ARR_HASNULL(arr) ||
1507  ARR_ELEMTYPE(arr) != TEXTOID)
1508  elog(ERROR, "proargnames is not a 1-D text array or it contains nulls");
1509  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1510  &argnames, NULL, &numargs);
1511  if (proargmodes != PointerGetDatum(NULL))
1512  {
1513  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1514  if (ARR_NDIM(arr) != 1 ||
1515  ARR_DIMS(arr)[0] != numargs ||
1516  ARR_HASNULL(arr) ||
1517  ARR_ELEMTYPE(arr) != CHAROID)
1518  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1519  numargs);
1520  argmodes = (char *) ARR_DATA_PTR(arr);
1521  }
1522  else
1523  argmodes = NULL;
1524 
1525  /* zero elements probably shouldn't happen, but handle it gracefully */
1526  if (numargs <= 0)
1527  {
1528  *arg_names = NULL;
1529  return 0;
1530  }
1531 
1532  /* extract input-argument names */
1533  inargnames = (char **) palloc(numargs * sizeof(char *));
1534  numinargs = 0;
1535  for (i = 0; i < numargs; i++)
1536  {
1537  if (argmodes == NULL ||
1538  argmodes[i] == PROARGMODE_IN ||
1539  argmodes[i] == PROARGMODE_INOUT ||
1540  argmodes[i] == PROARGMODE_VARIADIC)
1541  {
1542  char *pname = TextDatumGetCString(argnames[i]);
1543 
1544  if (pname[0] != '\0')
1545  inargnames[numinargs] = pname;
1546  else
1547  inargnames[numinargs] = NULL;
1548  numinargs++;
1549  }
1550  }
1551 
1552  *arg_names = inargnames;
1553  return numinargs;
1554 }

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, i, palloc(), PointerGetDatum, and TextDatumGetCString.

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1567 of file funcapi.c.

1568 {
1569  char *result;
1570  HeapTuple procTuple;
1571  Datum proargmodes;
1572  Datum proargnames;
1573  bool isnull;
1574  ArrayType *arr;
1575  int numargs;
1576  char *argmodes;
1577  Datum *argnames;
1578  int numoutargs;
1579  int nargnames;
1580  int i;
1581 
1582  /* First fetch the function's pg_proc row */
1583  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1584  if (!HeapTupleIsValid(procTuple))
1585  elog(ERROR, "cache lookup failed for function %u", functionId);
1586 
1587  /* If there are no named OUT parameters, return NULL */
1588  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1589  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1590  result = NULL;
1591  else
1592  {
1593  /* Get the data out of the tuple */
1594  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1595  Anum_pg_proc_proargmodes,
1596  &isnull);
1597  Assert(!isnull);
1598  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1599  Anum_pg_proc_proargnames,
1600  &isnull);
1601  Assert(!isnull);
1602 
1603  /*
1604  * We expect the arrays to be 1-D arrays of the right types; verify
1605  * that. For the char array, we don't need to use deconstruct_array()
1606  * since the array data is just going to look like a C array of
1607  * values.
1608  */
1609  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1610  numargs = ARR_DIMS(arr)[0];
1611  if (ARR_NDIM(arr) != 1 ||
1612  numargs < 0 ||
1613  ARR_HASNULL(arr) ||
1614  ARR_ELEMTYPE(arr) != CHAROID)
1615  elog(ERROR, "proargmodes is not a 1-D char array or it contains nulls");
1616  argmodes = (char *) ARR_DATA_PTR(arr);
1617  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1618  if (ARR_NDIM(arr) != 1 ||
1619  ARR_DIMS(arr)[0] != numargs ||
1620  ARR_HASNULL(arr) ||
1621  ARR_ELEMTYPE(arr) != TEXTOID)
1622  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1623  numargs);
1624  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1625  &argnames, NULL, &nargnames);
1626  Assert(nargnames == numargs);
1627 
1628  /* scan for output argument(s) */
1629  result = NULL;
1630  numoutargs = 0;
1631  for (i = 0; i < numargs; i++)
1632  {
1633  if (argmodes[i] == PROARGMODE_IN ||
1634  argmodes[i] == PROARGMODE_VARIADIC)
1635  continue;
1636  Assert(argmodes[i] == PROARGMODE_OUT ||
1637  argmodes[i] == PROARGMODE_INOUT ||
1638  argmodes[i] == PROARGMODE_TABLE);
1639  if (++numoutargs > 1)
1640  {
1641  /* multiple out args, so forget it */
1642  result = NULL;
1643  break;
1644  }
1645  result = TextDatumGetCString(argnames[i]);
1646  if (result == NULL || result[0] == '\0')
1647  {
1648  /* Parameter is not named, so forget it */
1649  result = NULL;
1650  break;
1651  }
1652  }
1653  }
1654 
1655  ReleaseSysCache(procTuple);
1656 
1657  return result;
1658 }
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert(), DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, heap_attisnull(), HeapTupleIsValid, i, ObjectIdGetDatum, PROCOID, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by chooseScalarFunctionAlias().

◆ get_func_result_type()

TypeFuncClass get_func_result_type ( Oid  functionId,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 369 of file funcapi.c.

372 {
373  return internal_get_result_type(functionId,
374  NULL,
375  NULL,
376  resultTypeId,
377  resultTupleDesc);
378 }

References internal_get_result_type().

Referenced by fmgr_sql_validator().

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1434 of file funcapi.c.

1436 {
1437  Datum protrftypes;
1438  ArrayType *arr;
1439  int nelems;
1440  bool isNull;
1441 
1442  protrftypes = SysCacheGetAttr(PROCOID, procTup,
1443  Anum_pg_proc_protrftypes,
1444  &isNull);
1445  if (!isNull)
1446  {
1447  /*
1448  * We expect the arrays to be 1-D arrays of the right types; verify
1449  * that. For the OID and char arrays, we don't need to use
1450  * deconstruct_array() since the array data is just going to look like
1451  * a C array of values.
1452  */
1453  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
1454  nelems = ARR_DIMS(arr)[0];
1455  if (ARR_NDIM(arr) != 1 ||
1456  nelems < 0 ||
1457  ARR_HASNULL(arr) ||
1458  ARR_ELEMTYPE(arr) != OIDOID)
1459  elog(ERROR, "protrftypes is not a 1-D Oid array or it contains nulls");
1460  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
1461  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
1462  nelems * sizeof(Oid));
1463 
1464  return nelems;
1465  }
1466  else
1467  return 0;
1468 }

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, DatumGetArrayTypeP, elog, ERROR, palloc(), PROCOID, and SysCacheGetAttr().

Referenced by print_function_trftypes().

◆ get_type_func_class()

static TypeFuncClass get_type_func_class ( Oid  typid,
Oid base_typeid 
)
static

Definition at line 1286 of file funcapi.c.

1287 {
1288  *base_typeid = typid;
1289 
1290  switch (get_typtype(typid))
1291  {
1292  case TYPTYPE_COMPOSITE:
1293  return TYPEFUNC_COMPOSITE;
1294  case TYPTYPE_BASE:
1295  case TYPTYPE_ENUM:
1296  case TYPTYPE_RANGE:
1297  case TYPTYPE_MULTIRANGE:
1298  return TYPEFUNC_SCALAR;
1299  case TYPTYPE_DOMAIN:
1300  *base_typeid = typid = getBaseType(typid);
1301  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
1303  else /* domain base type can't be a pseudotype */
1304  return TYPEFUNC_SCALAR;
1305  case TYPTYPE_PSEUDO:
1306  if (typid == RECORDOID)
1307  return TYPEFUNC_RECORD;
1308 
1309  /*
1310  * We treat VOID and CSTRING as legitimate scalar datatypes,
1311  * mostly for the convenience of the JDBC driver (which wants to
1312  * be able to do "SELECT * FROM foo()" for all legitimately
1313  * user-callable functions).
1314  */
1315  if (typid == VOIDOID || typid == CSTRINGOID)
1316  return TYPEFUNC_SCALAR;
1317  return TYPEFUNC_OTHER;
1318  }
1319  /* shouldn't get here, probably */
1320  return TYPEFUNC_OTHER;
1321 }
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_OTHER
Definition: funcapi.h:152
char get_typtype(Oid typid)
Definition: lsyscache.c:2586
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2478

References get_typtype(), getBaseType(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_OTHER, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by get_expr_result_type(), internal_get_result_type(), and TypeGetTupleDesc().

◆ init_MultiFuncCall()

FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 133 of file funcapi.c.

134 {
135  FuncCallContext *retval;
136 
137  /*
138  * Bail if we're called in the wrong context
139  */
140  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
141  ereport(ERROR,
142  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143  errmsg("set-valued function called in context that cannot accept a set")));
144 
145  if (fcinfo->flinfo->fn_extra == NULL)
146  {
147  /*
148  * First call
149  */
150  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
151  MemoryContext multi_call_ctx;
152 
153  /*
154  * Create a suitably long-lived context to hold cross-call data
155  */
156  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
157  "SRF multi-call context",
159 
160  /*
161  * Allocate suitably long-lived space and zero it
162  */
163  retval = (FuncCallContext *)
164  MemoryContextAllocZero(multi_call_ctx,
165  sizeof(FuncCallContext));
166 
167  /*
168  * initialize the elements
169  */
170  retval->call_cntr = 0;
171  retval->max_calls = 0;
172  retval->user_fctx = NULL;
173  retval->attinmeta = NULL;
174  retval->tuple_desc = NULL;
175  retval->multi_call_memory_ctx = multi_call_ctx;
176 
177  /*
178  * save the pointer for cross-call use
179  */
180  fcinfo->flinfo->fn_extra = retval;
181 
182  /*
183  * Ensure we will get shut down cleanly if the exprcontext is not run
184  * to completion.
185  */
188  PointerGetDatum(fcinfo->flinfo));
189  }
190  else
191  {
192  /* second and subsequent calls */
193  elog(ERROR, "init_MultiFuncCall cannot be called more than once");
194 
195  /* never reached, but keep compiler happy */
196  retval = NULL;
197  }
198 
199  return retval;
200 }
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:925
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:207
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, FuncCallContext::attinmeta, FuncCallContext::call_cntr, ReturnSetInfo::econtext, elog, ereport, errcode(), errmsg(), ERROR, IsA, FuncCallContext::max_calls, MemoryContextAllocZero(), FuncCallContext::multi_call_memory_ctx, PointerGetDatum, RegisterExprContextCallback(), shutdown_MultiFuncCall(), FuncCallContext::tuple_desc, and FuncCallContext::user_fctx.

◆ internal_get_result_type()

static TypeFuncClass internal_get_result_type ( Oid  funcid,
Node call_expr,
ReturnSetInfo rsinfo,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)
static

Definition at line 389 of file funcapi.c.

394 {
395  TypeFuncClass result;
396  HeapTuple tp;
397  Form_pg_proc procform;
398  Oid rettype;
399  Oid base_rettype;
400  TupleDesc tupdesc;
401 
402  /* First fetch the function's pg_proc row to inspect its rettype */
404  if (!HeapTupleIsValid(tp))
405  elog(ERROR, "cache lookup failed for function %u", funcid);
406  procform = (Form_pg_proc) GETSTRUCT(tp);
407 
408  rettype = procform->prorettype;
409 
410  /* Check for OUT parameters defining a RECORD result */
411  tupdesc = build_function_result_tupdesc_t(tp);
412  if (tupdesc)
413  {
414  /*
415  * It has OUT parameters, so it's basically like a regular composite
416  * type, except we have to be able to resolve any polymorphic OUT
417  * parameters.
418  */
419  if (resultTypeId)
420  *resultTypeId = rettype;
421 
422  if (resolve_polymorphic_tupdesc(tupdesc,
423  &procform->proargtypes,
424  call_expr))
425  {
426  if (tupdesc->tdtypeid == RECORDOID &&
427  tupdesc->tdtypmod < 0)
428  assign_record_type_typmod(tupdesc);
429  if (resultTupleDesc)
430  *resultTupleDesc = tupdesc;
431  result = TYPEFUNC_COMPOSITE;
432  }
433  else
434  {
435  if (resultTupleDesc)
436  *resultTupleDesc = NULL;
437  result = TYPEFUNC_RECORD;
438  }
439 
440  ReleaseSysCache(tp);
441 
442  return result;
443  }
444 
445  /*
446  * If scalar polymorphic result, try to resolve it.
447  */
448  if (IsPolymorphicType(rettype))
449  {
450  Oid newrettype = exprType(call_expr);
451 
452  if (newrettype == InvalidOid) /* this probably should not happen */
453  ereport(ERROR,
454  (errcode(ERRCODE_DATATYPE_MISMATCH),
455  errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
456  NameStr(procform->proname),
457  format_type_be(rettype))));
458  rettype = newrettype;
459  }
460 
461  if (resultTypeId)
462  *resultTypeId = rettype;
463  if (resultTupleDesc)
464  *resultTupleDesc = NULL; /* default result */
465 
466  /* Classify the result type */
467  result = get_type_func_class(rettype, &base_rettype);
468  switch (result)
469  {
470  case TYPEFUNC_COMPOSITE:
472  if (resultTupleDesc)
473  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
474  /* Named composite types can't have any polymorphic columns */
475  break;
476  case TYPEFUNC_SCALAR:
477  break;
478  case TYPEFUNC_RECORD:
479  /* We must get the tupledesc from call context */
480  if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
481  rsinfo->expectedDesc != NULL)
482  {
483  result = TYPEFUNC_COMPOSITE;
484  if (resultTupleDesc)
485  *resultTupleDesc = rsinfo->expectedDesc;
486  /* Assume no polymorphic columns here, either */
487  }
488  break;
489  default:
490  break;
491  }
492 
493  ReleaseSysCache(tp);
494 
495  return result;
496 }
#define NameStr(name)
Definition: c.h:681
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1671
static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
Definition: funcapi.c:702
#define InvalidOid
Definition: postgres_ext.h:36
TupleDesc expectedDesc
Definition: execnodes.h:310
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1950

References assign_record_type_typmod(), build_function_result_tupdesc_t(), elog, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, exprType(), format_type_be(), get_type_func_class(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IsA, lookup_rowtype_tupdesc_copy(), NameStr, ObjectIdGetDatum, PROCOID, ReleaseSysCache(), resolve_polymorphic_tupdesc(), SearchSysCache1(), TupleDescData::tdtypeid, TupleDescData::tdtypmod, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by get_call_result_type(), get_expr_result_type(), and get_func_result_type().

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 208 of file funcapi.c.

209 {
210  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
211 
212  return retval;
213 }

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1841 of file funcapi.c.

1842 {
1843  RangeVar *relvar;
1844  Relation rel;
1845  TupleDesc tupdesc;
1846  List *relname_list;
1847 
1848  /* Open relation and copy the tuple description */
1849  relname_list = stringToQualifiedNameList(relname);
1850  relvar = makeRangeVarFromNameList(relname_list);
1851  rel = relation_openrv(relvar, AccessShareLock);
1852  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1854 
1855  return tupdesc;
1856 }
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3108
NameData relname
Definition: pg_class.h:38
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1877
#define RelationGetDescr(relation)
Definition: rel.h:515
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Definition: pg_list.h:51
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111

References AccessShareLock, CreateTupleDescCopy(), makeRangeVarFromNameList(), relation_close(), relation_openrv(), RelationGetDescr, relname, and stringToQualifiedNameList().

◆ resolve_anyarray_from_others()

static void resolve_anyarray_from_others ( polymorphic_actuals actuals)
static

Definition at line 613 of file funcapi.c.

614 {
615  /* If we don't know ANYELEMENT, resolve that first */
616  if (!OidIsValid(actuals->anyelement_type))
618 
619  if (OidIsValid(actuals->anyelement_type))
620  {
621  /* Use the array type corresponding to actual type */
622  Oid array_typeid = get_array_type(actuals->anyelement_type);
623 
624  if (!OidIsValid(array_typeid))
625  ereport(ERROR,
626  (errcode(ERRCODE_UNDEFINED_OBJECT),
627  errmsg("could not find array type for data type %s",
628  format_type_be(actuals->anyelement_type))));
629  actuals->anyarray_type = array_typeid;
630  }
631  else
632  elog(ERROR, "could not determine polymorphic type");
633 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:547
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2744

References polymorphic_actuals::anyarray_type, polymorphic_actuals::anyelement_type, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_array_type(), OidIsValid, and resolve_anyelement_from_others().

Referenced by resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

◆ resolve_anyelement_from_others()

static void resolve_anyelement_from_others ( polymorphic_actuals actuals)
static

Definition at line 547 of file funcapi.c.

548 {
549  if (OidIsValid(actuals->anyarray_type))
550  {
551  /* Use the element type corresponding to actual type */
552  Oid array_base_type = getBaseType(actuals->anyarray_type);
553  Oid array_typelem = get_element_type(array_base_type);
554 
555  if (!OidIsValid(array_typelem))
556  ereport(ERROR,
557  (errcode(ERRCODE_DATATYPE_MISMATCH),
558  errmsg("argument declared %s is not an array but type %s",
559  "anyarray",
560  format_type_be(array_base_type))));
561  actuals->anyelement_type = array_typelem;
562  }
563  else if (OidIsValid(actuals->anyrange_type))
564  {
565  /* Use the element type corresponding to actual type */
566  Oid range_base_type = getBaseType(actuals->anyrange_type);
567  Oid range_typelem = get_range_subtype(range_base_type);
568 
569  if (!OidIsValid(range_typelem))
570  ereport(ERROR,
571  (errcode(ERRCODE_DATATYPE_MISMATCH),
572  errmsg("argument declared %s is not a range type but type %s",
573  "anyrange",
574  format_type_be(range_base_type))));
575  actuals->anyelement_type = range_typelem;
576  }
577  else if (OidIsValid(actuals->anymultirange_type))
578  {
579  /* Use the element type based on the multirange type */
580  Oid multirange_base_type;
581  Oid multirange_typelem;
582  Oid range_base_type;
583  Oid range_typelem;
584 
585  multirange_base_type = getBaseType(actuals->anymultirange_type);
586  multirange_typelem = get_multirange_range(multirange_base_type);
587  if (!OidIsValid(multirange_typelem))
588  ereport(ERROR,
589  (errcode(ERRCODE_DATATYPE_MISMATCH),
590  errmsg("argument declared %s is not a multirange type but type %s",
591  "anymultirange",
592  format_type_be(multirange_base_type))));
593 
594  range_base_type = getBaseType(multirange_typelem);
595  range_typelem = get_range_subtype(range_base_type);
596 
597  if (!OidIsValid(range_typelem))
598  ereport(ERROR,
599  (errcode(ERRCODE_DATATYPE_MISMATCH),
600  errmsg("argument declared %s does not contain a range type but type %s",
601  "anymultirange",
602  format_type_be(range_base_type))));
603  actuals->anyelement_type = range_typelem;
604  }
605  else
606  elog(ERROR, "could not determine polymorphic type");
607 }
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3367
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2716
Oid get_multirange_range(Oid multirangeOid)
Definition: lsyscache.c:3443
Oid anymultirange_type
Definition: funcapi.c:40

References polymorphic_actuals::anyarray_type, polymorphic_actuals::anyelement_type, polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_element_type(), get_multirange_range(), get_range_subtype(), getBaseType(), and OidIsValid.

Referenced by resolve_anyarray_from_others(), resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

◆ resolve_anymultirange_from_others()

static void resolve_anymultirange_from_others ( polymorphic_actuals actuals)
static

Definition at line 668 of file funcapi.c.

669 {
670  /*
671  * We can't deduce a multirange type from polymorphic array or base types,
672  * because there may be multiple range types with the same subtype, but we
673  * can deduce it from a polymorphic range type.
674  */
675  if (OidIsValid(actuals->anyrange_type))
676  {
677  Oid range_base_type = getBaseType(actuals->anyrange_type);
678  Oid multirange_typeid = get_range_multirange(range_base_type);
679 
680  if (!OidIsValid(multirange_typeid))
681  ereport(ERROR,
682  (errcode(ERRCODE_UNDEFINED_OBJECT),
683  errmsg("could not find multirange type for data type %s",
684  format_type_be(actuals->anyrange_type))));
685  actuals->anymultirange_type = multirange_typeid;
686  }
687  else
688  elog(ERROR, "could not determine polymorphic type");
689 }
Oid get_range_multirange(Oid rangeOid)
Definition: lsyscache.c:3418

References polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_range_multirange(), getBaseType(), and OidIsValid.

Referenced by resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

◆ resolve_anyrange_from_others()

static void resolve_anyrange_from_others ( polymorphic_actuals actuals)
static

Definition at line 639 of file funcapi.c.

640 {
641  /*
642  * We can't deduce a range type from other polymorphic array or base
643  * types, because there may be multiple range types with the same subtype,
644  * but we can deduce it from a polymorphic multirange type.
645  */
646  if (OidIsValid(actuals->anymultirange_type))
647  {
648  /* Use the element type based on the multirange type */
649  Oid multirange_base_type = getBaseType(actuals->anymultirange_type);
650  Oid multirange_typelem = get_multirange_range(multirange_base_type);
651 
652  if (!OidIsValid(multirange_typelem))
653  ereport(ERROR,
654  (errcode(ERRCODE_DATATYPE_MISMATCH),
655  errmsg("argument declared %s is not a multirange type but type %s",
656  "anymultirange",
657  format_type_be(multirange_base_type))));
658  actuals->anyrange_type = multirange_typelem;
659  }
660  else
661  elog(ERROR, "could not determine polymorphic type");
662 }

References polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_multirange_range(), getBaseType(), and OidIsValid.

Referenced by resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

◆ resolve_polymorphic_argtypes()

bool resolve_polymorphic_argtypes ( int  numargs,
Oid argtypes,
char *  argmodes,
Node call_expr 
)

Definition at line 1022 of file funcapi.c.

1024 {
1025  bool have_polymorphic_result = false;
1026  bool have_anyelement_result = false;
1027  bool have_anyarray_result = false;
1028  bool have_anyrange_result = false;
1029  bool have_anymultirange_result = false;
1030  bool have_anycompatible_result = false;
1031  bool have_anycompatible_array_result = false;
1032  bool have_anycompatible_range_result = false;
1033  bool have_anycompatible_multirange_result = false;
1034  polymorphic_actuals poly_actuals;
1035  polymorphic_actuals anyc_actuals;
1036  int inargno;
1037  int i;
1038 
1039  /*
1040  * First pass: resolve polymorphic inputs, check for outputs. As in
1041  * resolve_polymorphic_tupdesc, we rely on the parser to have enforced
1042  * type consistency and coerced ANYCOMPATIBLE args to a common supertype.
1043  */
1044  memset(&poly_actuals, 0, sizeof(poly_actuals));
1045  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
1046  inargno = 0;
1047  for (i = 0; i < numargs; i++)
1048  {
1049  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
1050 
1051  switch (argtypes[i])
1052  {
1053  case ANYELEMENTOID:
1054  case ANYNONARRAYOID:
1055  case ANYENUMOID:
1056  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1057  {
1058  have_polymorphic_result = true;
1059  have_anyelement_result = true;
1060  }
1061  else
1062  {
1063  if (!OidIsValid(poly_actuals.anyelement_type))
1064  {
1065  poly_actuals.anyelement_type =
1066  get_call_expr_argtype(call_expr, inargno);
1067  if (!OidIsValid(poly_actuals.anyelement_type))
1068  return false;
1069  }
1070  argtypes[i] = poly_actuals.anyelement_type;
1071  }
1072  break;
1073  case ANYARRAYOID:
1074  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1075  {
1076  have_polymorphic_result = true;
1077  have_anyarray_result = true;
1078  }
1079  else
1080  {
1081  if (!OidIsValid(poly_actuals.anyarray_type))
1082  {
1083  poly_actuals.anyarray_type =
1084  get_call_expr_argtype(call_expr, inargno);
1085  if (!OidIsValid(poly_actuals.anyarray_type))
1086  return false;
1087  }
1088  argtypes[i] = poly_actuals.anyarray_type;
1089  }
1090  break;
1091  case ANYRANGEOID:
1092  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1093  {
1094  have_polymorphic_result = true;
1095  have_anyrange_result = true;
1096  }
1097  else
1098  {
1099  if (!OidIsValid(poly_actuals.anyrange_type))
1100  {
1101  poly_actuals.anyrange_type =
1102  get_call_expr_argtype(call_expr, inargno);
1103  if (!OidIsValid(poly_actuals.anyrange_type))
1104  return false;
1105  }
1106  argtypes[i] = poly_actuals.anyrange_type;
1107  }
1108  break;
1109  case ANYMULTIRANGEOID:
1110  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1111  {
1112  have_polymorphic_result = true;
1113  have_anymultirange_result = true;
1114  }
1115  else
1116  {
1117  if (!OidIsValid(poly_actuals.anymultirange_type))
1118  {
1119  poly_actuals.anymultirange_type =
1120  get_call_expr_argtype(call_expr, inargno);
1121  if (!OidIsValid(poly_actuals.anymultirange_type))
1122  return false;
1123  }
1124  argtypes[i] = poly_actuals.anymultirange_type;
1125  }
1126  break;
1127  case ANYCOMPATIBLEOID:
1128  case ANYCOMPATIBLENONARRAYOID:
1129  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1130  {
1131  have_polymorphic_result = true;
1132  have_anycompatible_result = true;
1133  }
1134  else
1135  {
1136  if (!OidIsValid(anyc_actuals.anyelement_type))
1137  {
1138  anyc_actuals.anyelement_type =
1139  get_call_expr_argtype(call_expr, inargno);
1140  if (!OidIsValid(anyc_actuals.anyelement_type))
1141  return false;
1142  }
1143  argtypes[i] = anyc_actuals.anyelement_type;
1144  }
1145  break;
1146  case ANYCOMPATIBLEARRAYOID:
1147  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1148  {
1149  have_polymorphic_result = true;
1150  have_anycompatible_array_result = true;
1151  }
1152  else
1153  {
1154  if (!OidIsValid(anyc_actuals.anyarray_type))
1155  {
1156  anyc_actuals.anyarray_type =
1157  get_call_expr_argtype(call_expr, inargno);
1158  if (!OidIsValid(anyc_actuals.anyarray_type))
1159  return false;
1160  }
1161  argtypes[i] = anyc_actuals.anyarray_type;
1162  }
1163  break;
1164  case ANYCOMPATIBLERANGEOID:
1165  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1166  {
1167  have_polymorphic_result = true;
1168  have_anycompatible_range_result = true;
1169  }
1170  else
1171  {
1172  if (!OidIsValid(anyc_actuals.anyrange_type))
1173  {
1174  anyc_actuals.anyrange_type =
1175  get_call_expr_argtype(call_expr, inargno);
1176  if (!OidIsValid(anyc_actuals.anyrange_type))
1177  return false;
1178  }
1179  argtypes[i] = anyc_actuals.anyrange_type;
1180  }
1181  break;
1182  case ANYCOMPATIBLEMULTIRANGEOID:
1183  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1184  {
1185  have_polymorphic_result = true;
1186  have_anycompatible_multirange_result = true;
1187  }
1188  else
1189  {
1190  if (!OidIsValid(anyc_actuals.anymultirange_type))
1191  {
1192  anyc_actuals.anymultirange_type =
1193  get_call_expr_argtype(call_expr, inargno);
1194  if (!OidIsValid(anyc_actuals.anymultirange_type))
1195  return false;
1196  }
1197  argtypes[i] = anyc_actuals.anymultirange_type;
1198  }
1199  break;
1200  default:
1201  break;
1202  }
1203  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
1204  inargno++;
1205  }
1206 
1207  /* Done? */
1208  if (!have_polymorphic_result)
1209  return true;
1210 
1211  /* If needed, deduce one polymorphic type from others */
1212  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
1213  resolve_anyelement_from_others(&poly_actuals);
1214 
1215  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
1216  resolve_anyarray_from_others(&poly_actuals);
1217 
1218  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
1219  resolve_anyrange_from_others(&poly_actuals);
1220 
1221  if (have_anymultirange_result && !OidIsValid(poly_actuals.anymultirange_type))
1222  resolve_anymultirange_from_others(&poly_actuals);
1223 
1224  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
1225  resolve_anyelement_from_others(&anyc_actuals);
1226 
1227  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
1228  resolve_anyarray_from_others(&anyc_actuals);
1229 
1230  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
1231  resolve_anyrange_from_others(&anyc_actuals);
1232 
1233  if (have_anycompatible_multirange_result && !OidIsValid(anyc_actuals.anymultirange_type))
1234  resolve_anymultirange_from_others(&anyc_actuals);
1235 
1236  /* And finally replace the output column types as needed */
1237  for (i = 0; i < numargs; i++)
1238  {
1239  switch (argtypes[i])
1240  {
1241  case ANYELEMENTOID:
1242  case ANYNONARRAYOID:
1243  case ANYENUMOID:
1244  argtypes[i] = poly_actuals.anyelement_type;
1245  break;
1246  case ANYARRAYOID:
1247  argtypes[i] = poly_actuals.anyarray_type;
1248  break;
1249  case ANYRANGEOID:
1250  argtypes[i] = poly_actuals.anyrange_type;
1251  break;
1252  case ANYMULTIRANGEOID:
1253  argtypes[i] = poly_actuals.anymultirange_type;
1254  break;
1255  case ANYCOMPATIBLEOID:
1256  case ANYCOMPATIBLENONARRAYOID:
1257  argtypes[i] = anyc_actuals.anyelement_type;
1258  break;
1259  case ANYCOMPATIBLEARRAYOID:
1260  argtypes[i] = anyc_actuals.anyarray_type;
1261  break;
1262  case ANYCOMPATIBLERANGEOID:
1263  argtypes[i] = anyc_actuals.anyrange_type;
1264  break;
1265  case ANYCOMPATIBLEMULTIRANGEOID:
1266  argtypes[i] = anyc_actuals.anymultirange_type;
1267  break;
1268  default:
1269  break;
1270  }
1271  }
1272 
1273  return true;
1274 }
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1805
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:639
static void resolve_anymultirange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:668
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:613

References polymorphic_actuals::anyarray_type, polymorphic_actuals::anyelement_type, polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, get_call_expr_argtype(), i, OidIsValid, resolve_anyarray_from_others(), resolve_anyelement_from_others(), resolve_anymultirange_from_others(), and resolve_anyrange_from_others().

Referenced by plpgsql_resolve_polymorphic_argtypes().

◆ resolve_polymorphic_tupdesc()

static bool resolve_polymorphic_tupdesc ( TupleDesc  tupdesc,
oidvector declared_args,
Node call_expr 
)
static

Definition at line 702 of file funcapi.c.

704 {
705  int natts = tupdesc->natts;
706  int nargs = declared_args->dim1;
707  bool have_polymorphic_result = false;
708  bool have_anyelement_result = false;
709  bool have_anyarray_result = false;
710  bool have_anyrange_result = false;
711  bool have_anymultirange_result = false;
712  bool have_anycompatible_result = false;
713  bool have_anycompatible_array_result = false;
714  bool have_anycompatible_range_result = false;
715  bool have_anycompatible_multirange_result = false;
716  polymorphic_actuals poly_actuals;
717  polymorphic_actuals anyc_actuals;
718  Oid anycollation = InvalidOid;
719  Oid anycompatcollation = InvalidOid;
720  int i;
721 
722  /* See if there are any polymorphic outputs; quick out if not */
723  for (i = 0; i < natts; i++)
724  {
725  switch (TupleDescAttr(tupdesc, i)->atttypid)
726  {
727  case ANYELEMENTOID:
728  case ANYNONARRAYOID:
729  case ANYENUMOID:
730  have_polymorphic_result = true;
731  have_anyelement_result = true;
732  break;
733  case ANYARRAYOID:
734  have_polymorphic_result = true;
735  have_anyarray_result = true;
736  break;
737  case ANYRANGEOID:
738  have_polymorphic_result = true;
739  have_anyrange_result = true;
740  break;
741  case ANYMULTIRANGEOID:
742  have_polymorphic_result = true;
743  have_anymultirange_result = true;
744  break;
745  case ANYCOMPATIBLEOID:
746  case ANYCOMPATIBLENONARRAYOID:
747  have_polymorphic_result = true;
748  have_anycompatible_result = true;
749  break;
750  case ANYCOMPATIBLEARRAYOID:
751  have_polymorphic_result = true;
752  have_anycompatible_array_result = true;
753  break;
754  case ANYCOMPATIBLERANGEOID:
755  have_polymorphic_result = true;
756  have_anycompatible_range_result = true;
757  break;
758  case ANYCOMPATIBLEMULTIRANGEOID:
759  have_polymorphic_result = true;
760  have_anycompatible_multirange_result = true;
761  break;
762  default:
763  break;
764  }
765  }
766  if (!have_polymorphic_result)
767  return true;
768 
769  /*
770  * Otherwise, extract actual datatype(s) from input arguments. (We assume
771  * the parser already validated consistency of the arguments. Also, for
772  * the ANYCOMPATIBLE pseudotype family, we expect that all matching
773  * arguments were coerced to the selected common supertype, so that it
774  * doesn't matter which one's exposed type we look at.)
775  */
776  if (!call_expr)
777  return false; /* no hope */
778 
779  memset(&poly_actuals, 0, sizeof(poly_actuals));
780  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
781 
782  for (i = 0; i < nargs; i++)
783  {
784  switch (declared_args->values[i])
785  {
786  case ANYELEMENTOID:
787  case ANYNONARRAYOID:
788  case ANYENUMOID:
789  if (!OidIsValid(poly_actuals.anyelement_type))
790  {
791  poly_actuals.anyelement_type =
792  get_call_expr_argtype(call_expr, i);
793  if (!OidIsValid(poly_actuals.anyelement_type))
794  return false;
795  }
796  break;
797  case ANYARRAYOID:
798  if (!OidIsValid(poly_actuals.anyarray_type))
799  {
800  poly_actuals.anyarray_type =
801  get_call_expr_argtype(call_expr, i);
802  if (!OidIsValid(poly_actuals.anyarray_type))
803  return false;
804  }
805  break;
806  case ANYRANGEOID:
807  if (!OidIsValid(poly_actuals.anyrange_type))
808  {
809  poly_actuals.anyrange_type =
810  get_call_expr_argtype(call_expr, i);
811  if (!OidIsValid(poly_actuals.anyrange_type))
812  return false;
813  }
814  break;
815  case ANYMULTIRANGEOID:
816  if (!OidIsValid(poly_actuals.anymultirange_type))
817  {
818  poly_actuals.anymultirange_type =
819  get_call_expr_argtype(call_expr, i);
820  if (!OidIsValid(poly_actuals.anymultirange_type))
821  return false;
822  }
823  break;
824  case ANYCOMPATIBLEOID:
825  case ANYCOMPATIBLENONARRAYOID:
826  if (!OidIsValid(anyc_actuals.anyelement_type))
827  {
828  anyc_actuals.anyelement_type =
829  get_call_expr_argtype(call_expr, i);
830  if (!OidIsValid(anyc_actuals.anyelement_type))
831  return false;
832  }
833  break;
834  case ANYCOMPATIBLEARRAYOID:
835  if (!OidIsValid(anyc_actuals.anyarray_type))
836  {
837  anyc_actuals.anyarray_type =
838  get_call_expr_argtype(call_expr, i);
839  if (!OidIsValid(anyc_actuals.anyarray_type))
840  return false;
841  }
842  break;
843  case ANYCOMPATIBLERANGEOID:
844  if (!OidIsValid(anyc_actuals.anyrange_type))
845  {
846  anyc_actuals.anyrange_type =
847  get_call_expr_argtype(call_expr, i);
848  if (!OidIsValid(anyc_actuals.anyrange_type))
849  return false;
850  }
851  break;
852  case ANYCOMPATIBLEMULTIRANGEOID:
853  if (!OidIsValid(anyc_actuals.anymultirange_type))
854  {
855  anyc_actuals.anymultirange_type =
856  get_call_expr_argtype(call_expr, i);
857  if (!OidIsValid(anyc_actuals.anymultirange_type))
858  return false;
859  }
860  break;
861  default:
862  break;
863  }
864  }
865 
866  /* If needed, deduce one polymorphic type from others */
867  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
868  resolve_anyelement_from_others(&poly_actuals);
869 
870  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
871  resolve_anyarray_from_others(&poly_actuals);
872 
873  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
874  resolve_anyrange_from_others(&poly_actuals);
875 
876  if (have_anymultirange_result && !OidIsValid(poly_actuals.anymultirange_type))
877  resolve_anymultirange_from_others(&poly_actuals);
878 
879  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
880  resolve_anyelement_from_others(&anyc_actuals);
881 
882  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
883  resolve_anyarray_from_others(&anyc_actuals);
884 
885  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
886  resolve_anyrange_from_others(&anyc_actuals);
887 
888  if (have_anycompatible_multirange_result && !OidIsValid(anyc_actuals.anymultirange_type))
889  resolve_anymultirange_from_others(&anyc_actuals);
890 
891  /*
892  * Identify the collation to use for polymorphic OUT parameters. (It'll
893  * necessarily be the same for both anyelement and anyarray, likewise for
894  * anycompatible and anycompatiblearray.) Note that range types are not
895  * collatable, so any possible internal collation of a range type is not
896  * considered here.
897  */
898  if (OidIsValid(poly_actuals.anyelement_type))
899  anycollation = get_typcollation(poly_actuals.anyelement_type);
900  else if (OidIsValid(poly_actuals.anyarray_type))
901  anycollation = get_typcollation(poly_actuals.anyarray_type);
902 
903  if (OidIsValid(anyc_actuals.anyelement_type))
904  anycompatcollation = get_typcollation(anyc_actuals.anyelement_type);
905  else if (OidIsValid(anyc_actuals.anyarray_type))
906  anycompatcollation = get_typcollation(anyc_actuals.anyarray_type);
907 
908  if (OidIsValid(anycollation) || OidIsValid(anycompatcollation))
909  {
910  /*
911  * The types are collatable, so consider whether to use a nondefault
912  * collation. We do so if we can identify the input collation used
913  * for the function.
914  */
915  Oid inputcollation = exprInputCollation(call_expr);
916 
917  if (OidIsValid(inputcollation))
918  {
919  if (OidIsValid(anycollation))
920  anycollation = inputcollation;
921  if (OidIsValid(anycompatcollation))
922  anycompatcollation = inputcollation;
923  }
924  }
925 
926  /* And finally replace the tuple column types as needed */
927  for (i = 0; i < natts; i++)
928  {
929  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
930 
931  switch (att->atttypid)
932  {
933  case ANYELEMENTOID:
934  case ANYNONARRAYOID:
935  case ANYENUMOID:
936  TupleDescInitEntry(tupdesc, i + 1,
937  NameStr(att->attname),
938  poly_actuals.anyelement_type,
939  -1,
940  0);
941  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
942  break;
943  case ANYARRAYOID:
944  TupleDescInitEntry(tupdesc, i + 1,
945  NameStr(att->attname),
946  poly_actuals.anyarray_type,
947  -1,
948  0);
949  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
950  break;
951  case ANYRANGEOID:
952  TupleDescInitEntry(tupdesc, i + 1,
953  NameStr(att->attname),
954  poly_actuals.anyrange_type,
955  -1,
956  0);
957  /* no collation should be attached to a range type */
958  break;
959  case ANYMULTIRANGEOID:
960  TupleDescInitEntry(tupdesc, i + 1,
961  NameStr(att->attname),
962  poly_actuals.anymultirange_type,
963  -1,
964  0);
965  /* no collation should be attached to a multirange type */
966  break;
967  case ANYCOMPATIBLEOID:
968  case ANYCOMPATIBLENONARRAYOID:
969  TupleDescInitEntry(tupdesc, i + 1,
970  NameStr(att->attname),
971  anyc_actuals.anyelement_type,
972  -1,
973  0);
974  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
975  break;
976  case ANYCOMPATIBLEARRAYOID:
977  TupleDescInitEntry(tupdesc, i + 1,
978  NameStr(att->attname),
979  anyc_actuals.anyarray_type,
980  -1,
981  0);
982  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
983  break;
984  case ANYCOMPATIBLERANGEOID:
985  TupleDescInitEntry(tupdesc, i + 1,
986  NameStr(att->attname),
987  anyc_actuals.anyrange_type,
988  -1,
989  0);
990  /* no collation should be attached to a range type */
991  break;
992  case ANYCOMPATIBLEMULTIRANGEOID:
993  TupleDescInitEntry(tupdesc, i + 1,
994  NameStr(att->attname),
995  anyc_actuals.anymultirange_type,
996  -1,
997  0);
998  /* no collation should be attached to a multirange type */
999  break;
1000  default:
1001  break;
1002  }
1003  }
1004 
1005  return true;
1006 }
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3013
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:1034
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
int dim1
Definition: c.h:666
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:668
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References polymorphic_actuals::anyarray_type, polymorphic_actuals::anyelement_type, polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, oidvector::dim1, exprInputCollation(), get_call_expr_argtype(), get_typcollation(), i, InvalidOid, NameStr, TupleDescData::natts, OidIsValid, resolve_anyarray_from_others(), resolve_anyelement_from_others(), resolve_anymultirange_from_others(), resolve_anyrange_from_others(), TupleDescAttr, TupleDescInitEntry(), TupleDescInitEntryCollation(), and oidvector::values.

Referenced by internal_get_result_type().

◆ SetSingleFuncCall()

void SetSingleFuncCall ( FunctionCallInfo  fcinfo,
bits32  flags 
)

Definition at line 76 of file funcapi.c.

77 {
78  bool random_access;
79  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
80  Tuplestorestate *tupstore;
81  MemoryContext old_context,
82  per_query_ctx;
83  TupleDesc stored_tupdesc;
84 
85  /* check to see if caller supports returning a tuplestore */
86  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
87  ereport(ERROR,
88  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
89  errmsg("set-valued function called in context that cannot accept a set")));
90  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
91  ((flags & SRF_SINGLE_USE_EXPECTED) != 0 && rsinfo->expectedDesc == NULL))
92  ereport(ERROR,
93  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94  errmsg("materialize mode required, but it is not allowed in this context")));
95 
96  /*
97  * Store the tuplestore and the tuple descriptor in ReturnSetInfo. This
98  * must be done in the per-query memory context.
99  */
100  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
101  old_context = MemoryContextSwitchTo(per_query_ctx);
102 
103  /* build a tuple descriptor for our result type */
104  if ((flags & SRF_SINGLE_USE_EXPECTED) != 0)
105  stored_tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
106  else
107  {
108  if (get_call_result_type(fcinfo, NULL, &stored_tupdesc) != TYPEFUNC_COMPOSITE)
109  elog(ERROR, "return type must be a row type");
110  }
111 
112  /* If requested, bless the tuple descriptor */
113  if ((flags & SRF_SINGLE_BLESS) != 0)
114  BlessTupleDesc(stored_tupdesc);
115 
116  random_access = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
117 
118  tupstore = tuplestore_begin_heap(random_access, false, work_mem);
119  rsinfo->returnMode = SFRM_Materialize;
120  rsinfo->setResult = tupstore;
121  rsinfo->setDesc = stored_tupdesc;
122  MemoryContextSwitchTo(old_context);
123 }
@ SFRM_Materialize_Random
Definition: execnodes.h:295
@ SFRM_Materialize
Definition: execnodes.h:294
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_SINGLE_BLESS
Definition: funcapi.h:292
#define SRF_SINGLE_USE_EXPECTED
Definition: funcapi.h:291
int work_mem
Definition: globals.c:125
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:239
SetFunctionReturnMode returnMode
Definition: execnodes.h:313
TupleDesc setDesc
Definition: execnodes.h:317
Tuplestorestate * setResult
Definition: execnodes.h:316
int allowedModes
Definition: execnodes.h:311
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318

References ReturnSetInfo::allowedModes, BlessTupleDesc(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, get_call_result_type(), if(), IsA, MemoryContextSwitchTo(), FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, SRF_SINGLE_BLESS, SRF_SINGLE_USE_EXPECTED, tuplestore_begin_heap(), TYPEFUNC_COMPOSITE, and work_mem.

Referenced by brin_page_items(), dblink_get_notify(), each_worker(), each_worker_jsonb(), elements_worker(), elements_worker_jsonb(), GetWALRecordsInfo(), GetWalStats(), gist_page_items(), gist_page_items_bytea(), pg_available_extension_versions(), pg_available_extensions(), pg_config(), pg_cursor(), pg_event_trigger_ddl_commands(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_get_backend_memory_contexts(), pg_get_replication_slots(), pg_get_shmem_allocations(), pg_hba_file_rules(), pg_ident_file_mappings(), pg_logical_slot_get_changes_guts(), pg_ls_dir(), pg_ls_dir_files(), pg_prepared_statement(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_progress_info(), pg_stat_get_recovery_prefetch(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pg_tablespace_databases(), pg_timezone_names(), pgrowlocks(), postgres_fdw_get_connections(), show_all_file_settings(), text_to_table(), verify_heapam(), and xpath_table().

◆ shutdown_MultiFuncCall()

static void shutdown_MultiFuncCall ( Datum  arg)
static

Definition at line 238 of file funcapi.c.

239 {
240  FmgrInfo *flinfo = (FmgrInfo *) DatumGetPointer(arg);
241  FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
242 
243  /* unbind from flinfo */
244  flinfo->fn_extra = NULL;
245 
246  /*
247  * Delete context that holds all multi-call data, including the
248  * FuncCallContext itself
249  */
251 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
void * arg
#define DatumGetPointer(X)
Definition: postgres.h:593
Definition: fmgr.h:57
void * fn_extra
Definition: fmgr.h:64

References arg, DatumGetPointer, FmgrInfo::fn_extra, MemoryContextDelete(), and FuncCallContext::multi_call_memory_ctx.

Referenced by end_MultiFuncCall(), and init_MultiFuncCall().

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1874 of file funcapi.c.

1875 {
1876  Oid base_typeoid;
1877  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1878  TupleDesc tupdesc = NULL;
1879 
1880  /*
1881  * Build a suitable tupledesc representing the output rows. We
1882  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1883  * unlikely that legacy callers of this obsolete function would be
1884  * prepared to apply domain constraints.
1885  */
1886  if (functypclass == TYPEFUNC_COMPOSITE)
1887  {
1888  /* Composite data type, e.g. a table's row type */
1889  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1890 
1891  if (colaliases != NIL)
1892  {
1893  int natts = tupdesc->natts;
1894  int varattno;
1895 
1896  /* does the list length match the number of attributes? */
1897  if (list_length(colaliases) != natts)
1898  ereport(ERROR,
1899  (errcode(ERRCODE_DATATYPE_MISMATCH),
1900  errmsg("number of aliases does not match number of columns")));
1901 
1902  /* OK, use the aliases instead */
1903  for (varattno = 0; varattno < natts; varattno++)
1904  {
1905  char *label = strVal(list_nth(colaliases, varattno));
1906  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1907 
1908  if (label != NULL)
1909  namestrcpy(&(attr->attname), label);
1910  }
1911 
1912  /* The tuple type is now an anonymous record type */
1913  tupdesc->tdtypeid = RECORDOID;
1914  tupdesc->tdtypmod = -1;
1915  }
1916  }
1917  else if (functypclass == TYPEFUNC_SCALAR)
1918  {
1919  /* Base data type, i.e. scalar */
1920  char *attname;
1921 
1922  /* the alias list is required for base types */
1923  if (colaliases == NIL)
1924  ereport(ERROR,
1925  (errcode(ERRCODE_DATATYPE_MISMATCH),
1926  errmsg("no column alias was provided")));
1927 
1928  /* the alias list length must be 1 */
1929  if (list_length(colaliases) != 1)
1930  ereport(ERROR,
1931  (errcode(ERRCODE_DATATYPE_MISMATCH),
1932  errmsg("number of aliases does not match number of columns")));
1933 
1934  /* OK, get the column alias */
1935  attname = strVal(linitial(colaliases));
1936 
1937  tupdesc = CreateTemplateTupleDesc(1);
1938  TupleDescInitEntry(tupdesc,
1939  (AttrNumber) 1,
1940  attname,
1941  typeoid,
1942  -1,
1943  0);
1944  }
1945  else if (functypclass == TYPEFUNC_RECORD)
1946  {
1947  /* XXX can't support this because typmod wasn't passed in ... */
1948  ereport(ERROR,
1949  (errcode(ERRCODE_DATATYPE_MISMATCH),
1950  errmsg("could not determine row description for function returning record")));
1951  }
1952  else
1953  {
1954  /* crummy error message, but parser should have caught this */
1955  elog(ERROR, "function in FROM has unsupported return type");
1956  }
1957 
1958  return tupdesc;
1959 }
void namestrcpy(Name name, const char *str)
Definition: name.c:233
NameData attname
Definition: pg_attribute.h:41
static char * label
#define NIL
Definition: pg_list.h:65
#define linitial(l)
Definition: pg_list.h:174
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278

References attname, CreateTemplateTupleDesc(), elog, ereport, errcode(), errmsg(), ERROR, get_type_func_class(), label, linitial, list_length(), list_nth(), lookup_rowtype_tupdesc_copy(), namestrcpy(), TupleDescData::natts, NIL, strVal, TupleDescData::tdtypeid, TupleDescData::tdtypmod, TupleDescAttr, TupleDescInitEntry(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.