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 "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/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)
 
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 (char prokind, 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 1654 of file funcapi.c.

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().

1658 {
1659  TupleDesc desc;
1660  ArrayType *arr;
1661  int numargs;
1662  Oid *argtypes;
1663  char *argmodes;
1664  Datum *argnames = NULL;
1665  Oid *outargtypes;
1666  char **outargnames;
1667  int numoutargs;
1668  int nargnames;
1669  int i;
1670 
1671  /* Can't have output args if columns are null */
1672  if (proallargtypes == PointerGetDatum(NULL) ||
1673  proargmodes == PointerGetDatum(NULL))
1674  return NULL;
1675 
1676  /*
1677  * We expect the arrays to be 1-D arrays of the right types; verify that.
1678  * For the OID and char arrays, we don't need to use deconstruct_array()
1679  * since the array data is just going to look like a C array of values.
1680  */
1681  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1682  numargs = ARR_DIMS(arr)[0];
1683  if (ARR_NDIM(arr) != 1 ||
1684  numargs < 0 ||
1685  ARR_HASNULL(arr) ||
1686  ARR_ELEMTYPE(arr) != OIDOID)
1687  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1688  argtypes = (Oid *) ARR_DATA_PTR(arr);
1689  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1690  if (ARR_NDIM(arr) != 1 ||
1691  ARR_DIMS(arr)[0] != numargs ||
1692  ARR_HASNULL(arr) ||
1693  ARR_ELEMTYPE(arr) != CHAROID)
1694  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1695  numargs);
1696  argmodes = (char *) ARR_DATA_PTR(arr);
1697  if (proargnames != PointerGetDatum(NULL))
1698  {
1699  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1700  if (ARR_NDIM(arr) != 1 ||
1701  ARR_DIMS(arr)[0] != numargs ||
1702  ARR_HASNULL(arr) ||
1703  ARR_ELEMTYPE(arr) != TEXTOID)
1704  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1705  numargs);
1706  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1707  &argnames, NULL, &nargnames);
1708  Assert(nargnames == numargs);
1709  }
1710 
1711  /* zero elements probably shouldn't happen, but handle it gracefully */
1712  if (numargs <= 0)
1713  return NULL;
1714 
1715  /* extract output-argument types and names */
1716  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1717  outargnames = (char **) palloc(numargs * sizeof(char *));
1718  numoutargs = 0;
1719  for (i = 0; i < numargs; i++)
1720  {
1721  char *pname;
1722 
1723  if (argmodes[i] == PROARGMODE_IN ||
1724  argmodes[i] == PROARGMODE_VARIADIC)
1725  continue;
1726  Assert(argmodes[i] == PROARGMODE_OUT ||
1727  argmodes[i] == PROARGMODE_INOUT ||
1728  argmodes[i] == PROARGMODE_TABLE);
1729  outargtypes[numoutargs] = argtypes[i];
1730  if (argnames)
1731  pname = TextDatumGetCString(argnames[i]);
1732  else
1733  pname = NULL;
1734  if (pname == NULL || pname[0] == '\0')
1735  {
1736  /* Parameter is not named, so gin up a column name */
1737  pname = psprintf("column%d", numoutargs + 1);
1738  }
1739  outargnames[numoutargs] = pname;
1740  numoutargs++;
1741  }
1742 
1743  /*
1744  * If there is no output argument, or only one, the function does not
1745  * return tuples.
1746  */
1747  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1748  return NULL;
1749 
1750  desc = CreateTemplateTupleDesc(numoutargs);
1751  for (i = 0; i < numoutargs; i++)
1752  {
1753  TupleDescInitEntry(desc, i + 1,
1754  outargnames[i],
1755  outargtypes[i],
1756  -1,
1757  0);
1758  }
1759 
1760  return desc;
1761 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
#define PointerGetDatum(X)
Definition: postgres.h:600
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
#define Assert(condition)
Definition: c.h:804
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1604 of file funcapi.c.

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

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

1605 {
1606  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1607  Datum proallargtypes;
1608  Datum proargmodes;
1609  Datum proargnames;
1610  bool isnull;
1611 
1612  /* Return NULL if the function isn't declared to return RECORD */
1613  if (procform->prorettype != RECORDOID)
1614  return NULL;
1615 
1616  /* If there are no OUT parameters, return NULL */
1617  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1618  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1619  return NULL;
1620 
1621  /* Get the data out of the tuple */
1622  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1623  Anum_pg_proc_proallargtypes,
1624  &isnull);
1625  Assert(!isnull);
1626  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1627  Anum_pg_proc_proargmodes,
1628  &isnull);
1629  Assert(!isnull);
1630  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1631  Anum_pg_proc_proargnames,
1632  &isnull);
1633  if (isnull)
1634  proargnames = PointerGetDatum(NULL); /* just to be sure */
1635 
1636  return build_function_result_tupdesc_d(procform->prokind,
1637  proallargtypes,
1638  proargmodes,
1639  proargnames);
1640 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define PointerGetDatum(X)
Definition: postgres.h:600
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1654
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
uintptr_t Datum
Definition: postgres.h:411
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define Assert(condition)
Definition: c.h:804

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 151 of file funcapi.c.

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

152 {
153  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
154 
155  /* Deregister the shutdown callback */
158  PointerGetDatum(fcinfo->flinfo));
159 
160  /* But use it to do the real work */
161  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
162 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:951
#define PointerGetDatum(X)
Definition: postgres.h:600
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
ExprContext * econtext
Definition: execnodes.h:303

◆ extract_variadic_args()

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

Definition at line 1909 of file funcapi.c.

References 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, and typalign.

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

1912 {
1913  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1914  Datum *args_res;
1915  bool *nulls_res;
1916  Oid *types_res;
1917  int nargs,
1918  i;
1919 
1920  *args = NULL;
1921  *types = NULL;
1922  *nulls = NULL;
1923 
1924  if (variadic)
1925  {
1927  Oid element_type;
1928  bool typbyval;
1929  char typalign;
1930  int16 typlen;
1931 
1932  Assert(PG_NARGS() == variadic_start + 1);
1933 
1934  if (PG_ARGISNULL(variadic_start))
1935  return -1;
1936 
1937  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1938  element_type = ARR_ELEMTYPE(array_in);
1939 
1940  get_typlenbyvalalign(element_type,
1941  &typlen, &typbyval, &typalign);
1942  deconstruct_array(array_in, element_type, typlen, typbyval,
1943  typalign, &args_res, &nulls_res,
1944  &nargs);
1945 
1946  /* All the elements of the array have the same type */
1947  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1948  for (i = 0; i < nargs; i++)
1949  types_res[i] = element_type;
1950  }
1951  else
1952  {
1953  nargs = PG_NARGS() - variadic_start;
1954  Assert(nargs > 0);
1955  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1956  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1957  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1958 
1959  for (i = 0; i < nargs; i++)
1960  {
1961  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1962  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1963  i + variadic_start);
1964 
1965  /*
1966  * Turn a constant (more or less literal) value that's of unknown
1967  * type into text if required. Unknowns come in as a cstring
1968  * pointer. Note: for functions declared as taking type "any", the
1969  * parser will not do any type conversion on unknown-type literals
1970  * (that is, undecorated strings or NULLs).
1971  */
1972  if (convert_unknown &&
1973  types_res[i] == UNKNOWNOID &&
1974  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1975  {
1976  types_res[i] = TEXTOID;
1977 
1978  if (PG_ARGISNULL(i + variadic_start))
1979  args_res[i] = (Datum) 0;
1980  else
1981  args_res[i] =
1982  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1983  }
1984  else
1985  {
1986  /* no conversion needed, just take the datum as given */
1987  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1988  }
1989 
1990  if (!OidIsValid(types_res[i]) ||
1991  (convert_unknown && types_res[i] == UNKNOWNOID))
1992  ereport(ERROR,
1993  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1994  errmsg("could not determine data type for argument %d",
1995  i + 1)));
1996  }
1997  }
1998 
1999  /* Fill in results */
2000  *args = args_res;
2001  *nulls = nulls_res;
2002  *types = types_res;
2003 
2004  return nargs;
2005 }
signed short int16
Definition: c.h:428
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1865
struct typedefs * types
Definition: ecpg.c:29
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2218
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1934
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
char typalign
Definition: pg_type.h:176
#define ERROR
Definition: elog.h:46
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1800
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
FmgrInfo * flinfo
Definition: fmgr.h:87
#define ereport(elevel,...)
Definition: elog.h:157
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:804
#define PG_NARGS()
Definition: fmgr.h:203
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:82
#define ARR_ELEMTYPE(a)
Definition: array.h:285
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:173

◆ get_call_result_type()

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

Definition at line 207 of file funcapi.c.

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

Referenced by brin_metapage_info(), brin_page_items(), bt_metap(), bt_page_items_bytea(), bt_page_items_internal(), bt_page_stats_internal(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), each_worker(), each_worker_jsonb(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_items(), gist_page_items_bytea(), 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_available_extension_versions(), pg_available_extensions(), pg_buffercache_pages(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), 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_logical_slot_get_changes_guts(), pg_ls_dir_files(), pg_replication_slot_advance(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_prefetch_recovery(), pg_stat_get_progress_info(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), pg_stat_statements_info(), pg_stat_statements_internal(), pg_stats_ext_mcvlist_items(), pg_stop_backup_v2(), pg_timezone_names(), pgp_armor_headers(), pgrowlocks(), 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(), postgres_fdw_get_connections(), setup_firstcall(), ssl_extension_info(), storeRow(), and test_enc_conversion().

210 {
211  return internal_get_result_type(fcinfo->flinfo->fn_oid,
212  fcinfo->flinfo->fn_expr,
213  (ReturnSetInfo *) fcinfo->resultinfo,
214  resultTypeId,
215  resultTupleDesc);
216 }
fmNodePtr resultinfo
Definition: fmgr.h:89
FmgrInfo * flinfo
Definition: fmgr.h:87
Oid fn_oid
Definition: fmgr.h:59
fmNodePtr fn_expr
Definition: fmgr.h:66
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:320

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 440 of file funcapi.c.

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().

441 {
442  TupleDesc tupleDesc;
443  TypeFuncClass functypclass;
444 
445  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
446 
447  if (functypclass == TYPEFUNC_COMPOSITE ||
448  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
449  return tupleDesc;
450 
451  if (!noError)
452  {
453  Oid exprTypeId = exprType(expr);
454 
455  if (exprTypeId != RECORDOID)
456  ereport(ERROR,
457  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
458  errmsg("type %s is not composite",
459  format_type_be(exprTypeId))));
460  else
461  ereport(ERROR,
462  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
463  errmsg("record type has not been registered")));
464  }
465 
466  return NULL;
467 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:223
#define ERROR
Definition: elog.h:46
TypeFuncClass
Definition: funcapi.h:146
#define ereport(elevel,...)
Definition: elog.h:157
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ get_expr_result_type()

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

Definition at line 223 of file funcapi.c.

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().

226 {
227  TypeFuncClass result;
228 
229  if (expr && IsA(expr, FuncExpr))
230  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
231  expr,
232  NULL,
233  resultTypeId,
234  resultTupleDesc);
235  else if (expr && IsA(expr, OpExpr))
236  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
237  expr,
238  NULL,
239  resultTypeId,
240  resultTupleDesc);
241  else if (expr && IsA(expr, RowExpr) &&
242  ((RowExpr *) expr)->row_typeid == RECORDOID)
243  {
244  /* We can resolve the record type by generating the tupdesc directly */
245  RowExpr *rexpr = (RowExpr *) expr;
246  TupleDesc tupdesc;
247  AttrNumber i = 1;
248  ListCell *lcc,
249  *lcn;
250 
251  tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
252  Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
253  forboth(lcc, rexpr->args, lcn, rexpr->colnames)
254  {
255  Node *col = (Node *) lfirst(lcc);
256  char *colname = strVal(lfirst(lcn));
257 
258  TupleDescInitEntry(tupdesc, i,
259  colname,
260  exprType(col),
261  exprTypmod(col),
262  0);
263  TupleDescInitEntryCollation(tupdesc, i,
264  exprCollation(col));
265  i++;
266  }
267  if (resultTypeId)
268  *resultTypeId = rexpr->row_typeid;
269  if (resultTupleDesc)
270  *resultTupleDesc = BlessTupleDesc(tupdesc);
271  return TYPEFUNC_COMPOSITE;
272  }
273  else
274  {
275  /* handle as a generic expression; no chance to resolve RECORD */
276  Oid typid = exprType(expr);
277  Oid base_typid;
278 
279  if (resultTypeId)
280  *resultTypeId = typid;
281  if (resultTupleDesc)
282  *resultTupleDesc = NULL;
283  result = get_type_func_class(typid, &base_typid);
284  if ((result == TYPEFUNC_COMPOSITE ||
285  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
286  resultTupleDesc)
287  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
288  }
289 
290  return result;
291 }
List * args
Definition: primnodes.h:1058
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:539
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
List * colnames
Definition: primnodes.h:1074
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:751
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1217
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid row_typeid
Definition: primnodes.h:1059
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
int i
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:320
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1860

◆ get_func_arg_info()

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

Definition at line 1268 of file funcapi.c.

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 check_sql_fn_statements(), do_compile(), ExecuteCallStmt(), 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().

1270 {
1271  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1272  Datum proallargtypes;
1273  Datum proargmodes;
1274  Datum proargnames;
1275  bool isNull;
1276  ArrayType *arr;
1277  int numargs;
1278  Datum *elems;
1279  int nelems;
1280  int i;
1281 
1282  /* First discover the total number of parameters and get their types */
1283  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
1284  Anum_pg_proc_proallargtypes,
1285  &isNull);
1286  if (!isNull)
1287  {
1288  /*
1289  * We expect the arrays to be 1-D arrays of the right types; verify
1290  * that. For the OID and char arrays, we don't need to use
1291  * deconstruct_array() since the array data is just going to look like
1292  * a C array of values.
1293  */
1294  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1295  numargs = ARR_DIMS(arr)[0];
1296  if (ARR_NDIM(arr) != 1 ||
1297  numargs < 0 ||
1298  ARR_HASNULL(arr) ||
1299  ARR_ELEMTYPE(arr) != OIDOID)
1300  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1301  Assert(numargs >= procStruct->pronargs);
1302  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1303  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
1304  numargs * sizeof(Oid));
1305  }
1306  else
1307  {
1308  /* If no proallargtypes, use proargtypes */
1309  numargs = procStruct->proargtypes.dim1;
1310  Assert(numargs == procStruct->pronargs);
1311  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1312  memcpy(*p_argtypes, procStruct->proargtypes.values,
1313  numargs * sizeof(Oid));
1314  }
1315 
1316  /* Get argument names, if available */
1317  proargnames = SysCacheGetAttr(PROCOID, procTup,
1318  Anum_pg_proc_proargnames,
1319  &isNull);
1320  if (isNull)
1321  *p_argnames = NULL;
1322  else
1323  {
1325  TEXTOID, -1, false, TYPALIGN_INT,
1326  &elems, NULL, &nelems);
1327  if (nelems != numargs) /* should not happen */
1328  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
1329  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
1330  for (i = 0; i < numargs; i++)
1331  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
1332  }
1333 
1334  /* Get argument modes, if available */
1335  proargmodes = SysCacheGetAttr(PROCOID, procTup,
1336  Anum_pg_proc_proargmodes,
1337  &isNull);
1338  if (isNull)
1339  *p_argmodes = NULL;
1340  else
1341  {
1342  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1343  if (ARR_NDIM(arr) != 1 ||
1344  ARR_DIMS(arr)[0] != numargs ||
1345  ARR_HASNULL(arr) ||
1346  ARR_ELEMTYPE(arr) != CHAROID)
1347  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1348  numargs);
1349  *p_argmodes = (char *) palloc(numargs * sizeof(char));
1350  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
1351  numargs * sizeof(char));
1352  }
1353 
1354  return numargs;
1355 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define Assert(condition)
Definition: c.h:804
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ get_func_input_arg_names()

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

Definition at line 1412 of file funcapi.c.

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().

1415 {
1416  ArrayType *arr;
1417  int numargs;
1418  Datum *argnames;
1419  char *argmodes;
1420  char **inargnames;
1421  int numinargs;
1422  int i;
1423 
1424  /* Do nothing if null proargnames */
1425  if (proargnames == PointerGetDatum(NULL))
1426  {
1427  *arg_names = NULL;
1428  return 0;
1429  }
1430 
1431  /*
1432  * We expect the arrays to be 1-D arrays of the right types; verify that.
1433  * For proargmodes, we don't need to use deconstruct_array() since the
1434  * array data is just going to look like a C array of values.
1435  */
1436  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1437  if (ARR_NDIM(arr) != 1 ||
1438  ARR_HASNULL(arr) ||
1439  ARR_ELEMTYPE(arr) != TEXTOID)
1440  elog(ERROR, "proargnames is not a 1-D text array or it contains nulls");
1441  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1442  &argnames, NULL, &numargs);
1443  if (proargmodes != PointerGetDatum(NULL))
1444  {
1445  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1446  if (ARR_NDIM(arr) != 1 ||
1447  ARR_DIMS(arr)[0] != numargs ||
1448  ARR_HASNULL(arr) ||
1449  ARR_ELEMTYPE(arr) != CHAROID)
1450  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1451  numargs);
1452  argmodes = (char *) ARR_DATA_PTR(arr);
1453  }
1454  else
1455  argmodes = NULL;
1456 
1457  /* zero elements probably shouldn't happen, but handle it gracefully */
1458  if (numargs <= 0)
1459  {
1460  *arg_names = NULL;
1461  return 0;
1462  }
1463 
1464  /* extract input-argument names */
1465  inargnames = (char **) palloc(numargs * sizeof(char *));
1466  numinargs = 0;
1467  for (i = 0; i < numargs; i++)
1468  {
1469  if (argmodes == NULL ||
1470  argmodes[i] == PROARGMODE_IN ||
1471  argmodes[i] == PROARGMODE_INOUT ||
1472  (argmodes[i] == PROARGMODE_OUT && prokind == PROKIND_PROCEDURE) ||
1473  argmodes[i] == PROARGMODE_VARIADIC)
1474  {
1475  char *pname = TextDatumGetCString(argnames[i]);
1476 
1477  if (pname[0] != '\0')
1478  inargnames[numinargs] = pname;
1479  else
1480  inargnames[numinargs] = NULL;
1481  numinargs++;
1482  }
1483  }
1484 
1485  *arg_names = inargnames;
1486  return numinargs;
1487 }
#define PointerGetDatum(X)
Definition: postgres.h:600
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1500 of file funcapi.c.

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().

1501 {
1502  char *result;
1503  HeapTuple procTuple;
1504  Datum proargmodes;
1505  Datum proargnames;
1506  bool isnull;
1507  ArrayType *arr;
1508  int numargs;
1509  char *argmodes;
1510  Datum *argnames;
1511  int numoutargs;
1512  int nargnames;
1513  int i;
1514 
1515  /* First fetch the function's pg_proc row */
1516  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1517  if (!HeapTupleIsValid(procTuple))
1518  elog(ERROR, "cache lookup failed for function %u", functionId);
1519 
1520  /* If there are no named OUT parameters, return NULL */
1521  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1522  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1523  result = NULL;
1524  else
1525  {
1526  /* Get the data out of the tuple */
1527  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1528  Anum_pg_proc_proargmodes,
1529  &isnull);
1530  Assert(!isnull);
1531  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1532  Anum_pg_proc_proargnames,
1533  &isnull);
1534  Assert(!isnull);
1535 
1536  /*
1537  * We expect the arrays to be 1-D arrays of the right types; verify
1538  * that. For the char array, we don't need to use deconstruct_array()
1539  * since the array data is just going to look like a C array of
1540  * values.
1541  */
1542  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1543  numargs = ARR_DIMS(arr)[0];
1544  if (ARR_NDIM(arr) != 1 ||
1545  numargs < 0 ||
1546  ARR_HASNULL(arr) ||
1547  ARR_ELEMTYPE(arr) != CHAROID)
1548  elog(ERROR, "proargmodes is not a 1-D char array or it contains nulls");
1549  argmodes = (char *) ARR_DATA_PTR(arr);
1550  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1551  if (ARR_NDIM(arr) != 1 ||
1552  ARR_DIMS(arr)[0] != numargs ||
1553  ARR_HASNULL(arr) ||
1554  ARR_ELEMTYPE(arr) != TEXTOID)
1555  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1556  numargs);
1557  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1558  &argnames, NULL, &nargnames);
1559  Assert(nargnames == numargs);
1560 
1561  /* scan for output argument(s) */
1562  result = NULL;
1563  numoutargs = 0;
1564  for (i = 0; i < numargs; i++)
1565  {
1566  if (argmodes[i] == PROARGMODE_IN ||
1567  argmodes[i] == PROARGMODE_VARIADIC)
1568  continue;
1569  Assert(argmodes[i] == PROARGMODE_OUT ||
1570  argmodes[i] == PROARGMODE_INOUT ||
1571  argmodes[i] == PROARGMODE_TABLE);
1572  if (++numoutargs > 1)
1573  {
1574  /* multiple out args, so forget it */
1575  result = NULL;
1576  break;
1577  }
1578  result = TextDatumGetCString(argnames[i]);
1579  if (result == NULL || result[0] == '\0')
1580  {
1581  /* Parameter is not named, so forget it */
1582  result = NULL;
1583  break;
1584  }
1585  }
1586  }
1587 
1588  ReleaseSysCache(procTuple);
1589 
1590  return result;
1591 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3488
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ get_func_result_type()

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

Definition at line 300 of file funcapi.c.

References internal_get_result_type().

Referenced by fmgr_sql_validator().

303 {
304  return internal_get_result_type(functionId,
305  NULL,
306  NULL,
307  resultTypeId,
308  resultTupleDesc);
309 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:320

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1365 of file funcapi.c.

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

Referenced by print_function_trftypes().

1367 {
1368  Datum protrftypes;
1369  ArrayType *arr;
1370  int nelems;
1371  bool isNull;
1372 
1373  protrftypes = SysCacheGetAttr(PROCOID, procTup,
1374  Anum_pg_proc_protrftypes,
1375  &isNull);
1376  if (!isNull)
1377  {
1378  /*
1379  * We expect the arrays to be 1-D arrays of the right types; verify
1380  * that. For the OID and char arrays, we don't need to use
1381  * deconstruct_array() since the array data is just going to look like
1382  * a C array of values.
1383  */
1384  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
1385  nelems = ARR_DIMS(arr)[0];
1386  if (ARR_NDIM(arr) != 1 ||
1387  nelems < 0 ||
1388  ARR_HASNULL(arr) ||
1389  ARR_ELEMTYPE(arr) != OIDOID)
1390  elog(ERROR, "protrftypes is not a 1-D Oid array or it contains nulls");
1391  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
1392  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
1393  nelems * sizeof(Oid));
1394 
1395  return nelems;
1396  }
1397  else
1398  return 0;
1399 }
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
uintptr_t Datum
Definition: postgres.h:411
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ARR_NDIM(a)
Definition: array.h:283
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ get_type_func_class()

static TypeFuncClass get_type_func_class ( Oid  typid,
Oid base_typeid 
)
static

Definition at line 1217 of file funcapi.c.

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().

1218 {
1219  *base_typeid = typid;
1220 
1221  switch (get_typtype(typid))
1222  {
1223  case TYPTYPE_COMPOSITE:
1224  return TYPEFUNC_COMPOSITE;
1225  case TYPTYPE_BASE:
1226  case TYPTYPE_ENUM:
1227  case TYPTYPE_RANGE:
1228  case TYPTYPE_MULTIRANGE:
1229  return TYPEFUNC_SCALAR;
1230  case TYPTYPE_DOMAIN:
1231  *base_typeid = typid = getBaseType(typid);
1232  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
1234  else /* domain base type can't be a pseudotype */
1235  return TYPEFUNC_SCALAR;
1236  case TYPTYPE_PSEUDO:
1237  if (typid == RECORDOID)
1238  return TYPEFUNC_RECORD;
1239 
1240  /*
1241  * We treat VOID and CSTRING as legitimate scalar datatypes,
1242  * mostly for the convenience of the JDBC driver (which wants to
1243  * be able to do "SELECT * FROM foo()" for all legitimately
1244  * user-callable functions).
1245  */
1246  if (typid == VOIDOID || typid == CSTRINGOID)
1247  return TYPEFUNC_SCALAR;
1248  return TYPEFUNC_OTHER;
1249  }
1250  /* shouldn't get here, probably */
1251  return TYPEFUNC_OTHER;
1252 }
char get_typtype(Oid typid)
Definition: lsyscache.c:2576
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2468

◆ init_MultiFuncCall()

FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 64 of file funcapi.c.

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.

65 {
66  FuncCallContext *retval;
67 
68  /*
69  * Bail if we're called in the wrong context
70  */
71  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
72  ereport(ERROR,
73  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
74  errmsg("set-valued function called in context that cannot accept a set")));
75 
76  if (fcinfo->flinfo->fn_extra == NULL)
77  {
78  /*
79  * First call
80  */
81  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
82  MemoryContext multi_call_ctx;
83 
84  /*
85  * Create a suitably long-lived context to hold cross-call data
86  */
87  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
88  "SRF multi-call context",
90 
91  /*
92  * Allocate suitably long-lived space and zero it
93  */
94  retval = (FuncCallContext *)
95  MemoryContextAllocZero(multi_call_ctx,
96  sizeof(FuncCallContext));
97 
98  /*
99  * initialize the elements
100  */
101  retval->call_cntr = 0;
102  retval->max_calls = 0;
103  retval->user_fctx = NULL;
104  retval->attinmeta = NULL;
105  retval->tuple_desc = NULL;
106  retval->multi_call_memory_ctx = multi_call_ctx;
107 
108  /*
109  * save the pointer for cross-call use
110  */
111  fcinfo->flinfo->fn_extra = retval;
112 
113  /*
114  * Ensure we will get shut down cleanly if the exprcontext is not run
115  * to completion.
116  */
119  PointerGetDatum(fcinfo->flinfo));
120  }
121  else
122  {
123  /* second and subsequent calls */
124  elog(ERROR, "init_MultiFuncCall cannot be called more than once");
125 
126  /* never reached, but keep compiler happy */
127  retval = NULL;
128  }
129 
130  return retval;
131 }
uint64 call_cntr
Definition: funcapi.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define AllocSetContextCreate
Definition: memutils.h:173
#define PointerGetDatum(X)
Definition: postgres.h:600
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
int errcode(int sqlerrcode)
Definition: elog.c:698
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
TupleDesc tuple_desc
Definition: funcapi.h:112
#define ERROR
Definition: elog.h:46
AttInMetadata * attinmeta
Definition: funcapi.h:91
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define ereport(elevel,...)
Definition: elog.h:157
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:925
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
ExprContext * econtext
Definition: execnodes.h:303
void * user_fctx
Definition: funcapi.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
uint64 max_calls
Definition: funcapi.h:74

◆ 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 320 of file funcapi.c.

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().

325 {
326  TypeFuncClass result;
327  HeapTuple tp;
328  Form_pg_proc procform;
329  Oid rettype;
330  Oid base_rettype;
331  TupleDesc tupdesc;
332 
333  /* First fetch the function's pg_proc row to inspect its rettype */
335  if (!HeapTupleIsValid(tp))
336  elog(ERROR, "cache lookup failed for function %u", funcid);
337  procform = (Form_pg_proc) GETSTRUCT(tp);
338 
339  rettype = procform->prorettype;
340 
341  /* Check for OUT parameters defining a RECORD result */
342  tupdesc = build_function_result_tupdesc_t(tp);
343  if (tupdesc)
344  {
345  /*
346  * It has OUT parameters, so it's basically like a regular composite
347  * type, except we have to be able to resolve any polymorphic OUT
348  * parameters.
349  */
350  if (resultTypeId)
351  *resultTypeId = rettype;
352 
353  if (resolve_polymorphic_tupdesc(tupdesc,
354  &procform->proargtypes,
355  call_expr))
356  {
357  if (tupdesc->tdtypeid == RECORDOID &&
358  tupdesc->tdtypmod < 0)
359  assign_record_type_typmod(tupdesc);
360  if (resultTupleDesc)
361  *resultTupleDesc = tupdesc;
362  result = TYPEFUNC_COMPOSITE;
363  }
364  else
365  {
366  if (resultTupleDesc)
367  *resultTupleDesc = NULL;
368  result = TYPEFUNC_RECORD;
369  }
370 
371  ReleaseSysCache(tp);
372 
373  return result;
374  }
375 
376  /*
377  * If scalar polymorphic result, try to resolve it.
378  */
379  if (IsPolymorphicType(rettype))
380  {
381  Oid newrettype = exprType(call_expr);
382 
383  if (newrettype == InvalidOid) /* this probably should not happen */
384  ereport(ERROR,
385  (errcode(ERRCODE_DATATYPE_MISMATCH),
386  errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
387  NameStr(procform->proname),
388  format_type_be(rettype))));
389  rettype = newrettype;
390  }
391 
392  if (resultTypeId)
393  *resultTypeId = rettype;
394  if (resultTupleDesc)
395  *resultTupleDesc = NULL; /* default result */
396 
397  /* Classify the result type */
398  result = get_type_func_class(rettype, &base_rettype);
399  switch (result)
400  {
401  case TYPEFUNC_COMPOSITE:
403  if (resultTupleDesc)
404  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
405  /* Named composite types can't have any polymorphic columns */
406  break;
407  case TYPEFUNC_SCALAR:
408  break;
409  case TYPEFUNC_RECORD:
410  /* We must get the tupledesc from call context */
411  if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
412  rsinfo->expectedDesc != NULL)
413  {
414  result = TYPEFUNC_COMPOSITE;
415  if (resultTupleDesc)
416  *resultTupleDesc = rsinfo->expectedDesc;
417  /* Assume no polymorphic columns here, either */
418  }
419  break;
420  default:
421  break;
422  }
423 
424  ReleaseSysCache(tp);
425 
426  return result;
427 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1946
TupleDesc expectedDesc
Definition: execnodes.h:304
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1604
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
int32 tdtypmod
Definition: tupdesc.h:83
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1217
TypeFuncClass
Definition: funcapi.h:146
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
Definition: funcapi.c:633
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1860

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 139 of file funcapi.c.

140 {
141  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
142 
143  return retval;
144 }

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1774 of file funcapi.c.

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

1775 {
1776  RangeVar *relvar;
1777  Relation rel;
1778  TupleDesc tupdesc;
1779  List *relname_list;
1780 
1781  /* Open relation and copy the tuple description */
1782  relname_list = stringToQualifiedNameList(relname);
1783  relvar = makeRangeVarFromNameList(relname_list);
1784  rel = relation_openrv(relvar, AccessShareLock);
1785  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1787 
1788  return tupdesc;
1789 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define RelationGetDescr(relation)
Definition: rel.h:498
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3061
NameData relname
Definition: pg_class.h:38
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1876
Definition: pg_list.h:50

◆ resolve_anyarray_from_others()

static void resolve_anyarray_from_others ( polymorphic_actuals actuals)
static

Definition at line 544 of file funcapi.c.

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().

545 {
546  /* If we don't know ANYELEMENT, resolve that first */
547  if (!OidIsValid(actuals->anyelement_type))
549 
550  if (OidIsValid(actuals->anyelement_type))
551  {
552  /* Use the array type corresponding to actual type */
553  Oid array_typeid = get_array_type(actuals->anyelement_type);
554 
555  if (!OidIsValid(array_typeid))
556  ereport(ERROR,
557  (errcode(ERRCODE_UNDEFINED_OBJECT),
558  errmsg("could not find array type for data type %s",
559  format_type_be(actuals->anyelement_type))));
560  actuals->anyarray_type = array_typeid;
561  }
562  else
563  elog(ERROR, "could not determine polymorphic type");
564 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:478
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2734
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ resolve_anyelement_from_others()

static void resolve_anyelement_from_others ( polymorphic_actuals actuals)
static

Definition at line 478 of file funcapi.c.

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().

479 {
480  if (OidIsValid(actuals->anyarray_type))
481  {
482  /* Use the element type corresponding to actual type */
483  Oid array_base_type = getBaseType(actuals->anyarray_type);
484  Oid array_typelem = get_element_type(array_base_type);
485 
486  if (!OidIsValid(array_typelem))
487  ereport(ERROR,
488  (errcode(ERRCODE_DATATYPE_MISMATCH),
489  errmsg("argument declared %s is not an array but type %s",
490  "anyarray",
491  format_type_be(array_base_type))));
492  actuals->anyelement_type = array_typelem;
493  }
494  else if (OidIsValid(actuals->anyrange_type))
495  {
496  /* Use the element type corresponding to actual type */
497  Oid range_base_type = getBaseType(actuals->anyrange_type);
498  Oid range_typelem = get_range_subtype(range_base_type);
499 
500  if (!OidIsValid(range_typelem))
501  ereport(ERROR,
502  (errcode(ERRCODE_DATATYPE_MISMATCH),
503  errmsg("argument declared %s is not a range type but type %s",
504  "anyrange",
505  format_type_be(range_base_type))));
506  actuals->anyelement_type = range_typelem;
507  }
508  else if (OidIsValid(actuals->anymultirange_type))
509  {
510  /* Use the element type based on the multirange type */
511  Oid multirange_base_type;
512  Oid multirange_typelem;
513  Oid range_base_type;
514  Oid range_typelem;
515 
516  multirange_base_type = getBaseType(actuals->anymultirange_type);
517  multirange_typelem = get_multirange_range(multirange_base_type);
518  if (!OidIsValid(multirange_typelem))
519  ereport(ERROR,
520  (errcode(ERRCODE_DATATYPE_MISMATCH),
521  errmsg("argument declared %s is not a multirange type but type %s",
522  "anymultirange",
523  format_type_be(multirange_base_type))));
524 
525  range_base_type = getBaseType(multirange_typelem);
526  range_typelem = get_range_subtype(range_base_type);
527 
528  if (!OidIsValid(range_typelem))
529  ereport(ERROR,
530  (errcode(ERRCODE_DATATYPE_MISMATCH),
531  errmsg("argument declared %s does not contain a range type but type %s",
532  "anymultirange",
533  format_type_be(range_base_type))));
534  actuals->anyelement_type = range_typelem;
535  }
536  else
537  elog(ERROR, "could not determine polymorphic type");
538 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
Oid get_multirange_range(Oid multirangeOid)
Definition: lsyscache.c:3433
#define ERROR
Definition: elog.h:46
Oid anymultirange_type
Definition: funcapi.c:38
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2468
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3357

◆ resolve_anymultirange_from_others()

static void resolve_anymultirange_from_others ( polymorphic_actuals actuals)
static

Definition at line 599 of file funcapi.c.

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().

600 {
601  /*
602  * We can't deduce a multirange type from polymorphic array or base types,
603  * because there may be multiple range types with the same subtype, but we
604  * can deduce it from a polymorphic range type.
605  */
606  if (OidIsValid(actuals->anyrange_type))
607  {
608  Oid range_base_type = getBaseType(actuals->anyrange_type);
609  Oid multirange_typeid = get_range_multirange(range_base_type);
610 
611  if (!OidIsValid(multirange_typeid))
612  ereport(ERROR,
613  (errcode(ERRCODE_UNDEFINED_OBJECT),
614  errmsg("could not find multirange type for data type %s",
615  format_type_be(actuals->anyrange_type))));
616  actuals->anymultirange_type = multirange_typeid;
617  }
618  else
619  elog(ERROR, "could not determine polymorphic type");
620 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
Oid get_range_multirange(Oid rangeOid)
Definition: lsyscache.c:3408
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
Oid anymultirange_type
Definition: funcapi.c:38
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2468

◆ resolve_anyrange_from_others()

static void resolve_anyrange_from_others ( polymorphic_actuals actuals)
static

Definition at line 570 of file funcapi.c.

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().

571 {
572  /*
573  * We can't deduce a range type from other polymorphic array or base
574  * types, because there may be multiple range types with the same subtype,
575  * but we can deduce it from a polymorphic multirange type.
576  */
577  if (OidIsValid(actuals->anymultirange_type))
578  {
579  /* Use the element type based on the multirange type */
580  Oid multirange_base_type = getBaseType(actuals->anymultirange_type);
581  Oid multirange_typelem = get_multirange_range(multirange_base_type);
582 
583  if (!OidIsValid(multirange_typelem))
584  ereport(ERROR,
585  (errcode(ERRCODE_DATATYPE_MISMATCH),
586  errmsg("argument declared %s is not a multirange type but type %s",
587  "anymultirange",
588  format_type_be(multirange_base_type))));
589  actuals->anyrange_type = multirange_typelem;
590  }
591  else
592  elog(ERROR, "could not determine polymorphic type");
593 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
Oid get_multirange_range(Oid multirangeOid)
Definition: lsyscache.c:3433
#define ERROR
Definition: elog.h:46
Oid anymultirange_type
Definition: funcapi.c:38
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2468

◆ resolve_polymorphic_argtypes()

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

Definition at line 953 of file funcapi.c.

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().

955 {
956  bool have_polymorphic_result = false;
957  bool have_anyelement_result = false;
958  bool have_anyarray_result = false;
959  bool have_anyrange_result = false;
960  bool have_anymultirange_result = false;
961  bool have_anycompatible_result = false;
962  bool have_anycompatible_array_result = false;
963  bool have_anycompatible_range_result = false;
964  bool have_anycompatible_multirange_result = false;
965  polymorphic_actuals poly_actuals;
966  polymorphic_actuals anyc_actuals;
967  int inargno;
968  int i;
969 
970  /*
971  * First pass: resolve polymorphic inputs, check for outputs. As in
972  * resolve_polymorphic_tupdesc, we rely on the parser to have enforced
973  * type consistency and coerced ANYCOMPATIBLE args to a common supertype.
974  */
975  memset(&poly_actuals, 0, sizeof(poly_actuals));
976  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
977  inargno = 0;
978  for (i = 0; i < numargs; i++)
979  {
980  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
981 
982  switch (argtypes[i])
983  {
984  case ANYELEMENTOID:
985  case ANYNONARRAYOID:
986  case ANYENUMOID:
987  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
988  {
989  have_polymorphic_result = true;
990  have_anyelement_result = true;
991  }
992  else
993  {
994  if (!OidIsValid(poly_actuals.anyelement_type))
995  {
996  poly_actuals.anyelement_type =
997  get_call_expr_argtype(call_expr, inargno);
998  if (!OidIsValid(poly_actuals.anyelement_type))
999  return false;
1000  }
1001  argtypes[i] = poly_actuals.anyelement_type;
1002  }
1003  break;
1004  case ANYARRAYOID:
1005  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1006  {
1007  have_polymorphic_result = true;
1008  have_anyarray_result = true;
1009  }
1010  else
1011  {
1012  if (!OidIsValid(poly_actuals.anyarray_type))
1013  {
1014  poly_actuals.anyarray_type =
1015  get_call_expr_argtype(call_expr, inargno);
1016  if (!OidIsValid(poly_actuals.anyarray_type))
1017  return false;
1018  }
1019  argtypes[i] = poly_actuals.anyarray_type;
1020  }
1021  break;
1022  case ANYRANGEOID:
1023  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1024  {
1025  have_polymorphic_result = true;
1026  have_anyrange_result = true;
1027  }
1028  else
1029  {
1030  if (!OidIsValid(poly_actuals.anyrange_type))
1031  {
1032  poly_actuals.anyrange_type =
1033  get_call_expr_argtype(call_expr, inargno);
1034  if (!OidIsValid(poly_actuals.anyrange_type))
1035  return false;
1036  }
1037  argtypes[i] = poly_actuals.anyrange_type;
1038  }
1039  break;
1040  case ANYMULTIRANGEOID:
1041  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1042  {
1043  have_polymorphic_result = true;
1044  have_anymultirange_result = true;
1045  }
1046  else
1047  {
1048  if (!OidIsValid(poly_actuals.anymultirange_type))
1049  {
1050  poly_actuals.anymultirange_type =
1051  get_call_expr_argtype(call_expr, inargno);
1052  if (!OidIsValid(poly_actuals.anymultirange_type))
1053  return false;
1054  }
1055  argtypes[i] = poly_actuals.anymultirange_type;
1056  }
1057  break;
1058  case ANYCOMPATIBLEOID:
1059  case ANYCOMPATIBLENONARRAYOID:
1060  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1061  {
1062  have_polymorphic_result = true;
1063  have_anycompatible_result = true;
1064  }
1065  else
1066  {
1067  if (!OidIsValid(anyc_actuals.anyelement_type))
1068  {
1069  anyc_actuals.anyelement_type =
1070  get_call_expr_argtype(call_expr, inargno);
1071  if (!OidIsValid(anyc_actuals.anyelement_type))
1072  return false;
1073  }
1074  argtypes[i] = anyc_actuals.anyelement_type;
1075  }
1076  break;
1077  case ANYCOMPATIBLEARRAYOID:
1078  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1079  {
1080  have_polymorphic_result = true;
1081  have_anycompatible_array_result = true;
1082  }
1083  else
1084  {
1085  if (!OidIsValid(anyc_actuals.anyarray_type))
1086  {
1087  anyc_actuals.anyarray_type =
1088  get_call_expr_argtype(call_expr, inargno);
1089  if (!OidIsValid(anyc_actuals.anyarray_type))
1090  return false;
1091  }
1092  argtypes[i] = anyc_actuals.anyarray_type;
1093  }
1094  break;
1095  case ANYCOMPATIBLERANGEOID:
1096  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1097  {
1098  have_polymorphic_result = true;
1099  have_anycompatible_range_result = true;
1100  }
1101  else
1102  {
1103  if (!OidIsValid(anyc_actuals.anyrange_type))
1104  {
1105  anyc_actuals.anyrange_type =
1106  get_call_expr_argtype(call_expr, inargno);
1107  if (!OidIsValid(anyc_actuals.anyrange_type))
1108  return false;
1109  }
1110  argtypes[i] = anyc_actuals.anyrange_type;
1111  }
1112  break;
1113  case ANYCOMPATIBLEMULTIRANGEOID:
1114  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1115  {
1116  have_polymorphic_result = true;
1117  have_anycompatible_multirange_result = true;
1118  }
1119  else
1120  {
1121  if (!OidIsValid(anyc_actuals.anymultirange_type))
1122  {
1123  anyc_actuals.anymultirange_type =
1124  get_call_expr_argtype(call_expr, inargno);
1125  if (!OidIsValid(anyc_actuals.anymultirange_type))
1126  return false;
1127  }
1128  argtypes[i] = anyc_actuals.anymultirange_type;
1129  }
1130  break;
1131  default:
1132  break;
1133  }
1134  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
1135  inargno++;
1136  }
1137 
1138  /* Done? */
1139  if (!have_polymorphic_result)
1140  return true;
1141 
1142  /* If needed, deduce one polymorphic type from others */
1143  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
1144  resolve_anyelement_from_others(&poly_actuals);
1145 
1146  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
1147  resolve_anyarray_from_others(&poly_actuals);
1148 
1149  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
1150  resolve_anyrange_from_others(&poly_actuals);
1151 
1152  if (have_anymultirange_result && !OidIsValid(poly_actuals.anymultirange_type))
1153  resolve_anymultirange_from_others(&poly_actuals);
1154 
1155  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
1156  resolve_anyelement_from_others(&anyc_actuals);
1157 
1158  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
1159  resolve_anyarray_from_others(&anyc_actuals);
1160 
1161  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
1162  resolve_anyrange_from_others(&anyc_actuals);
1163 
1164  if (have_anycompatible_multirange_result && !OidIsValid(anyc_actuals.anymultirange_type))
1165  resolve_anymultirange_from_others(&anyc_actuals);
1166 
1167  /* And finally replace the output column types as needed */
1168  for (i = 0; i < numargs; i++)
1169  {
1170  switch (argtypes[i])
1171  {
1172  case ANYELEMENTOID:
1173  case ANYNONARRAYOID:
1174  case ANYENUMOID:
1175  argtypes[i] = poly_actuals.anyelement_type;
1176  break;
1177  case ANYARRAYOID:
1178  argtypes[i] = poly_actuals.anyarray_type;
1179  break;
1180  case ANYRANGEOID:
1181  argtypes[i] = poly_actuals.anyrange_type;
1182  break;
1183  case ANYMULTIRANGEOID:
1184  argtypes[i] = poly_actuals.anymultirange_type;
1185  break;
1186  case ANYCOMPATIBLEOID:
1187  case ANYCOMPATIBLENONARRAYOID:
1188  argtypes[i] = anyc_actuals.anyelement_type;
1189  break;
1190  case ANYCOMPATIBLEARRAYOID:
1191  argtypes[i] = anyc_actuals.anyarray_type;
1192  break;
1193  case ANYCOMPATIBLERANGEOID:
1194  argtypes[i] = anyc_actuals.anyrange_type;
1195  break;
1196  case ANYCOMPATIBLEMULTIRANGEOID:
1197  argtypes[i] = anyc_actuals.anymultirange_type;
1198  break;
1199  default:
1200  break;
1201  }
1202  }
1203 
1204  return true;
1205 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:478
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1819
#define OidIsValid(objectId)
Definition: c.h:710
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:570
Oid anymultirange_type
Definition: funcapi.c:38
static void resolve_anymultirange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:599
int i
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:544

◆ resolve_polymorphic_tupdesc()

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

Definition at line 633 of file funcapi.c.

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().

635 {
636  int natts = tupdesc->natts;
637  int nargs = declared_args->dim1;
638  bool have_polymorphic_result = false;
639  bool have_anyelement_result = false;
640  bool have_anyarray_result = false;
641  bool have_anyrange_result = false;
642  bool have_anymultirange_result = false;
643  bool have_anycompatible_result = false;
644  bool have_anycompatible_array_result = false;
645  bool have_anycompatible_range_result = false;
646  bool have_anycompatible_multirange_result = false;
647  polymorphic_actuals poly_actuals;
648  polymorphic_actuals anyc_actuals;
649  Oid anycollation = InvalidOid;
650  Oid anycompatcollation = InvalidOid;
651  int i;
652 
653  /* See if there are any polymorphic outputs; quick out if not */
654  for (i = 0; i < natts; i++)
655  {
656  switch (TupleDescAttr(tupdesc, i)->atttypid)
657  {
658  case ANYELEMENTOID:
659  case ANYNONARRAYOID:
660  case ANYENUMOID:
661  have_polymorphic_result = true;
662  have_anyelement_result = true;
663  break;
664  case ANYARRAYOID:
665  have_polymorphic_result = true;
666  have_anyarray_result = true;
667  break;
668  case ANYRANGEOID:
669  have_polymorphic_result = true;
670  have_anyrange_result = true;
671  break;
672  case ANYMULTIRANGEOID:
673  have_polymorphic_result = true;
674  have_anymultirange_result = true;
675  break;
676  case ANYCOMPATIBLEOID:
677  case ANYCOMPATIBLENONARRAYOID:
678  have_polymorphic_result = true;
679  have_anycompatible_result = true;
680  break;
681  case ANYCOMPATIBLEARRAYOID:
682  have_polymorphic_result = true;
683  have_anycompatible_array_result = true;
684  break;
685  case ANYCOMPATIBLERANGEOID:
686  have_polymorphic_result = true;
687  have_anycompatible_range_result = true;
688  break;
689  case ANYCOMPATIBLEMULTIRANGEOID:
690  have_polymorphic_result = true;
691  have_anycompatible_multirange_result = true;
692  break;
693  default:
694  break;
695  }
696  }
697  if (!have_polymorphic_result)
698  return true;
699 
700  /*
701  * Otherwise, extract actual datatype(s) from input arguments. (We assume
702  * the parser already validated consistency of the arguments. Also, for
703  * the ANYCOMPATIBLE pseudotype family, we expect that all matching
704  * arguments were coerced to the selected common supertype, so that it
705  * doesn't matter which one's exposed type we look at.)
706  */
707  if (!call_expr)
708  return false; /* no hope */
709 
710  memset(&poly_actuals, 0, sizeof(poly_actuals));
711  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
712 
713  for (i = 0; i < nargs; i++)
714  {
715  switch (declared_args->values[i])
716  {
717  case ANYELEMENTOID:
718  case ANYNONARRAYOID:
719  case ANYENUMOID:
720  if (!OidIsValid(poly_actuals.anyelement_type))
721  {
722  poly_actuals.anyelement_type =
723  get_call_expr_argtype(call_expr, i);
724  if (!OidIsValid(poly_actuals.anyelement_type))
725  return false;
726  }
727  break;
728  case ANYARRAYOID:
729  if (!OidIsValid(poly_actuals.anyarray_type))
730  {
731  poly_actuals.anyarray_type =
732  get_call_expr_argtype(call_expr, i);
733  if (!OidIsValid(poly_actuals.anyarray_type))
734  return false;
735  }
736  break;
737  case ANYRANGEOID:
738  if (!OidIsValid(poly_actuals.anyrange_type))
739  {
740  poly_actuals.anyrange_type =
741  get_call_expr_argtype(call_expr, i);
742  if (!OidIsValid(poly_actuals.anyrange_type))
743  return false;
744  }
745  break;
746  case ANYMULTIRANGEOID:
747  if (!OidIsValid(poly_actuals.anymultirange_type))
748  {
749  poly_actuals.anymultirange_type =
750  get_call_expr_argtype(call_expr, i);
751  if (!OidIsValid(poly_actuals.anymultirange_type))
752  return false;
753  }
754  break;
755  case ANYCOMPATIBLEOID:
756  case ANYCOMPATIBLENONARRAYOID:
757  if (!OidIsValid(anyc_actuals.anyelement_type))
758  {
759  anyc_actuals.anyelement_type =
760  get_call_expr_argtype(call_expr, i);
761  if (!OidIsValid(anyc_actuals.anyelement_type))
762  return false;
763  }
764  break;
765  case ANYCOMPATIBLEARRAYOID:
766  if (!OidIsValid(anyc_actuals.anyarray_type))
767  {
768  anyc_actuals.anyarray_type =
769  get_call_expr_argtype(call_expr, i);
770  if (!OidIsValid(anyc_actuals.anyarray_type))
771  return false;
772  }
773  break;
774  case ANYCOMPATIBLERANGEOID:
775  if (!OidIsValid(anyc_actuals.anyrange_type))
776  {
777  anyc_actuals.anyrange_type =
778  get_call_expr_argtype(call_expr, i);
779  if (!OidIsValid(anyc_actuals.anyrange_type))
780  return false;
781  }
782  break;
783  case ANYCOMPATIBLEMULTIRANGEOID:
784  if (!OidIsValid(anyc_actuals.anymultirange_type))
785  {
786  anyc_actuals.anymultirange_type =
787  get_call_expr_argtype(call_expr, i);
788  if (!OidIsValid(anyc_actuals.anymultirange_type))
789  return false;
790  }
791  break;
792  default:
793  break;
794  }
795  }
796 
797  /* If needed, deduce one polymorphic type from others */
798  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
799  resolve_anyelement_from_others(&poly_actuals);
800 
801  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
802  resolve_anyarray_from_others(&poly_actuals);
803 
804  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
805  resolve_anyrange_from_others(&poly_actuals);
806 
807  if (have_anymultirange_result && !OidIsValid(poly_actuals.anymultirange_type))
808  resolve_anymultirange_from_others(&poly_actuals);
809 
810  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
811  resolve_anyelement_from_others(&anyc_actuals);
812 
813  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
814  resolve_anyarray_from_others(&anyc_actuals);
815 
816  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
817  resolve_anyrange_from_others(&anyc_actuals);
818 
819  if (have_anycompatible_multirange_result && !OidIsValid(anyc_actuals.anymultirange_type))
820  resolve_anymultirange_from_others(&anyc_actuals);
821 
822  /*
823  * Identify the collation to use for polymorphic OUT parameters. (It'll
824  * necessarily be the same for both anyelement and anyarray, likewise for
825  * anycompatible and anycompatiblearray.) Note that range types are not
826  * collatable, so any possible internal collation of a range type is not
827  * considered here.
828  */
829  if (OidIsValid(poly_actuals.anyelement_type))
830  anycollation = get_typcollation(poly_actuals.anyelement_type);
831  else if (OidIsValid(poly_actuals.anyarray_type))
832  anycollation = get_typcollation(poly_actuals.anyarray_type);
833 
834  if (OidIsValid(anyc_actuals.anyelement_type))
835  anycompatcollation = get_typcollation(anyc_actuals.anyelement_type);
836  else if (OidIsValid(anyc_actuals.anyarray_type))
837  anycompatcollation = get_typcollation(anyc_actuals.anyarray_type);
838 
839  if (OidIsValid(anycollation) || OidIsValid(anycompatcollation))
840  {
841  /*
842  * The types are collatable, so consider whether to use a nondefault
843  * collation. We do so if we can identify the input collation used
844  * for the function.
845  */
846  Oid inputcollation = exprInputCollation(call_expr);
847 
848  if (OidIsValid(inputcollation))
849  {
850  if (OidIsValid(anycollation))
851  anycollation = inputcollation;
852  if (OidIsValid(anycompatcollation))
853  anycompatcollation = inputcollation;
854  }
855  }
856 
857  /* And finally replace the tuple column types as needed */
858  for (i = 0; i < natts; i++)
859  {
860  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
861 
862  switch (att->atttypid)
863  {
864  case ANYELEMENTOID:
865  case ANYNONARRAYOID:
866  case ANYENUMOID:
867  TupleDescInitEntry(tupdesc, i + 1,
868  NameStr(att->attname),
869  poly_actuals.anyelement_type,
870  -1,
871  0);
872  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
873  break;
874  case ANYARRAYOID:
875  TupleDescInitEntry(tupdesc, i + 1,
876  NameStr(att->attname),
877  poly_actuals.anyarray_type,
878  -1,
879  0);
880  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
881  break;
882  case ANYRANGEOID:
883  TupleDescInitEntry(tupdesc, i + 1,
884  NameStr(att->attname),
885  poly_actuals.anyrange_type,
886  -1,
887  0);
888  /* no collation should be attached to a range type */
889  break;
890  case ANYMULTIRANGEOID:
891  TupleDescInitEntry(tupdesc, i + 1,
892  NameStr(att->attname),
893  poly_actuals.anymultirange_type,
894  -1,
895  0);
896  /* no collation should be attached to a multirange type */
897  break;
898  case ANYCOMPATIBLEOID:
899  case ANYCOMPATIBLENONARRAYOID:
900  TupleDescInitEntry(tupdesc, i + 1,
901  NameStr(att->attname),
902  anyc_actuals.anyelement_type,
903  -1,
904  0);
905  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
906  break;
907  case ANYCOMPATIBLEARRAYOID:
908  TupleDescInitEntry(tupdesc, i + 1,
909  NameStr(att->attname),
910  anyc_actuals.anyarray_type,
911  -1,
912  0);
913  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
914  break;
915  case ANYCOMPATIBLERANGEOID:
916  TupleDescInitEntry(tupdesc, i + 1,
917  NameStr(att->attname),
918  anyc_actuals.anyrange_type,
919  -1,
920  0);
921  /* no collation should be attached to a range type */
922  break;
923  case ANYCOMPATIBLEMULTIRANGEOID:
924  TupleDescInitEntry(tupdesc, i + 1,
925  NameStr(att->attname),
926  anyc_actuals.anymultirange_type,
927  -1,
928  0);
929  /* no collation should be attached to a multirange type */
930  break;
931  default:
932  break;
933  }
934  }
935 
936  return true;
937 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:478
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1819
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:570
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:751
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:668
int dim1
Definition: c.h:666
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
Oid anymultirange_type
Definition: funcapi.c:38
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3003
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:974
static void resolve_anymultirange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:599
int i
#define NameStr(name)
Definition: c.h:681
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:544

◆ shutdown_MultiFuncCall()

static void shutdown_MultiFuncCall ( Datum  arg)
static

Definition at line 169 of file funcapi.c.

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

Referenced by end_MultiFuncCall(), and init_MultiFuncCall().

170 {
171  FmgrInfo *flinfo = (FmgrInfo *) DatumGetPointer(arg);
172  FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
173 
174  /* unbind from flinfo */
175  flinfo->fn_extra = NULL;
176 
177  /*
178  * Delete context that holds all multi-call data, including the
179  * FuncCallContext itself
180  */
182 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
void * fn_extra
Definition: fmgr.h:64
#define DatumGetPointer(X)
Definition: postgres.h:593
void * arg

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1807 of file funcapi.c.

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.

1808 {
1809  Oid base_typeoid;
1810  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1811  TupleDesc tupdesc = NULL;
1812 
1813  /*
1814  * Build a suitable tupledesc representing the output rows. We
1815  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1816  * unlikely that legacy callers of this obsolete function would be
1817  * prepared to apply domain constraints.
1818  */
1819  if (functypclass == TYPEFUNC_COMPOSITE)
1820  {
1821  /* Composite data type, e.g. a table's row type */
1822  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1823 
1824  if (colaliases != NIL)
1825  {
1826  int natts = tupdesc->natts;
1827  int varattno;
1828 
1829  /* does the list length match the number of attributes? */
1830  if (list_length(colaliases) != natts)
1831  ereport(ERROR,
1832  (errcode(ERRCODE_DATATYPE_MISMATCH),
1833  errmsg("number of aliases does not match number of columns")));
1834 
1835  /* OK, use the aliases instead */
1836  for (varattno = 0; varattno < natts; varattno++)
1837  {
1838  char *label = strVal(list_nth(colaliases, varattno));
1839  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1840 
1841  if (label != NULL)
1842  namestrcpy(&(attr->attname), label);
1843  }
1844 
1845  /* The tuple type is now an anonymous record type */
1846  tupdesc->tdtypeid = RECORDOID;
1847  tupdesc->tdtypmod = -1;
1848  }
1849  }
1850  else if (functypclass == TYPEFUNC_SCALAR)
1851  {
1852  /* Base data type, i.e. scalar */
1853  char *attname;
1854 
1855  /* the alias list is required for base types */
1856  if (colaliases == NIL)
1857  ereport(ERROR,
1858  (errcode(ERRCODE_DATATYPE_MISMATCH),
1859  errmsg("no column alias was provided")));
1860 
1861  /* the alias list length must be 1 */
1862  if (list_length(colaliases) != 1)
1863  ereport(ERROR,
1864  (errcode(ERRCODE_DATATYPE_MISMATCH),
1865  errmsg("number of aliases does not match number of columns")));
1866 
1867  /* OK, get the column alias */
1868  attname = strVal(linitial(colaliases));
1869 
1870  tupdesc = CreateTemplateTupleDesc(1);
1871  TupleDescInitEntry(tupdesc,
1872  (AttrNumber) 1,
1873  attname,
1874  typeoid,
1875  -1,
1876  0);
1877  }
1878  else if (functypclass == TYPEFUNC_RECORD)
1879  {
1880  /* XXX can't support this because typmod wasn't passed in ... */
1881  ereport(ERROR,
1882  (errcode(ERRCODE_DATATYPE_MISMATCH),
1883  errmsg("could not determine row description for function returning record")));
1884  }
1885  else
1886  {
1887  /* crummy error message, but parser should have caught this */
1888  elog(ERROR, "function in FROM has unsupported return type");
1889  }
1890 
1891  return tupdesc;
1892 }
#define NIL
Definition: pg_list.h:65
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:45
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
NameData attname
Definition: pg_attribute.h:41
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int32 tdtypmod
Definition: tupdesc.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1217
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:580
static char * label
#define ereport(elevel,...)
Definition: elog.h:157
static int list_length(const List *l)
Definition: pg_list.h:149
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1860