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

1480 {
1481  TupleDesc desc;
1482  ArrayType *arr;
1483  int numargs;
1484  Oid *argtypes;
1485  char *argmodes;
1486  Datum *argnames = NULL;
1487  Oid *outargtypes;
1488  char **outargnames;
1489  int numoutargs;
1490  int nargnames;
1491  int i;
1492 
1493  /* Can't have output args if columns are null */
1494  if (proallargtypes == PointerGetDatum(NULL) ||
1495  proargmodes == PointerGetDatum(NULL))
1496  return NULL;
1497 
1498  /*
1499  * We expect the arrays to be 1-D arrays of the right types; verify that.
1500  * For the OID and char arrays, we don't need to use deconstruct_array()
1501  * since the array data is just going to look like a C array of values.
1502  */
1503  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1504  numargs = ARR_DIMS(arr)[0];
1505  if (ARR_NDIM(arr) != 1 ||
1506  numargs < 0 ||
1507  ARR_HASNULL(arr) ||
1508  ARR_ELEMTYPE(arr) != OIDOID)
1509  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1510  argtypes = (Oid *) ARR_DATA_PTR(arr);
1511  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1512  if (ARR_NDIM(arr) != 1 ||
1513  ARR_DIMS(arr)[0] != numargs ||
1514  ARR_HASNULL(arr) ||
1515  ARR_ELEMTYPE(arr) != CHAROID)
1516  elog(ERROR, "proargmodes is not a 1-D char array");
1517  argmodes = (char *) ARR_DATA_PTR(arr);
1518  if (proargnames != PointerGetDatum(NULL))
1519  {
1520  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1521  if (ARR_NDIM(arr) != 1 ||
1522  ARR_DIMS(arr)[0] != numargs ||
1523  ARR_HASNULL(arr) ||
1524  ARR_ELEMTYPE(arr) != TEXTOID)
1525  elog(ERROR, "proargnames is not a 1-D text array");
1526  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1527  &argnames, NULL, &nargnames);
1528  Assert(nargnames == numargs);
1529  }
1530 
1531  /* zero elements probably shouldn't happen, but handle it gracefully */
1532  if (numargs <= 0)
1533  return NULL;
1534 
1535  /* extract output-argument types and names */
1536  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1537  outargnames = (char **) palloc(numargs * sizeof(char *));
1538  numoutargs = 0;
1539  for (i = 0; i < numargs; i++)
1540  {
1541  char *pname;
1542 
1543  if (argmodes[i] == PROARGMODE_IN ||
1544  argmodes[i] == PROARGMODE_VARIADIC)
1545  continue;
1546  Assert(argmodes[i] == PROARGMODE_OUT ||
1547  argmodes[i] == PROARGMODE_INOUT ||
1548  argmodes[i] == PROARGMODE_TABLE);
1549  outargtypes[numoutargs] = argtypes[i];
1550  if (argnames)
1551  pname = TextDatumGetCString(argnames[i]);
1552  else
1553  pname = NULL;
1554  if (pname == NULL || pname[0] == '\0')
1555  {
1556  /* Parameter is not named, so gin up a column name */
1557  pname = psprintf("column%d", numoutargs + 1);
1558  }
1559  outargnames[numoutargs] = pname;
1560  numoutargs++;
1561  }
1562 
1563  /*
1564  * If there is no output argument, or only one, the function does not
1565  * return tuples.
1566  */
1567  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1568  return NULL;
1569 
1570  desc = CreateTemplateTupleDesc(numoutargs);
1571  for (i = 0; i < numoutargs; i++)
1572  {
1573  TupleDescInitEntry(desc, i + 1,
1574  outargnames[i],
1575  outargtypes[i],
1576  -1,
1577  0);
1578  }
1579 
1580  return desc;
1581 }
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define PointerGetDatum(X)
Definition: postgres.h:556
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:746
#define ARR_NDIM(a)
Definition: array.h:278
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1426 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().

1427 {
1428  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1429  Datum proallargtypes;
1430  Datum proargmodes;
1431  Datum proargnames;
1432  bool isnull;
1433 
1434  /* Return NULL if the function isn't declared to return RECORD */
1435  if (procform->prorettype != RECORDOID)
1436  return NULL;
1437 
1438  /* If there are no OUT parameters, return NULL */
1439  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1440  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1441  return NULL;
1442 
1443  /* Get the data out of the tuple */
1444  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1445  Anum_pg_proc_proallargtypes,
1446  &isnull);
1447  Assert(!isnull);
1448  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1449  Anum_pg_proc_proargmodes,
1450  &isnull);
1451  Assert(!isnull);
1452  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1453  Anum_pg_proc_proargnames,
1454  &isnull);
1455  if (isnull)
1456  proargnames = PointerGetDatum(NULL); /* just to be sure */
1457 
1458  return build_function_result_tupdesc_d(procform->prokind,
1459  proallargtypes,
1460  proargmodes,
1461  proargnames);
1462 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define PointerGetDatum(X)
Definition: postgres.h:556
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1476
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define Assert(condition)
Definition: c.h:746

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 149 of file funcapi.c.

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

150 {
151  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
152 
153  /* Deregister the shutdown callback */
156  PointerGetDatum(fcinfo->flinfo));
157 
158  /* But use it to do the real work */
159  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
160 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:950
#define PointerGetDatum(X)
Definition: postgres.h:556
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:167
ExprContext * econtext
Definition: execnodes.h:302

◆ extract_variadic_args()

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

Definition at line 1729 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().

1732 {
1733  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1734  Datum *args_res;
1735  bool *nulls_res;
1736  Oid *types_res;
1737  int nargs,
1738  i;
1739 
1740  *args = NULL;
1741  *types = NULL;
1742  *nulls = NULL;
1743 
1744  if (variadic)
1745  {
1747  Oid element_type;
1748  bool typbyval;
1749  char typalign;
1750  int16 typlen;
1751 
1752  Assert(PG_NARGS() == variadic_start + 1);
1753 
1754  if (PG_ARGISNULL(variadic_start))
1755  return -1;
1756 
1757  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1758  element_type = ARR_ELEMTYPE(array_in);
1759 
1760  get_typlenbyvalalign(element_type,
1761  &typlen, &typbyval, &typalign);
1762  deconstruct_array(array_in, element_type, typlen, typbyval,
1763  typalign, &args_res, &nulls_res,
1764  &nargs);
1765 
1766  /* All the elements of the array have the same type */
1767  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1768  for (i = 0; i < nargs; i++)
1769  types_res[i] = element_type;
1770  }
1771  else
1772  {
1773  nargs = PG_NARGS() - variadic_start;
1774  Assert(nargs > 0);
1775  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1776  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1777  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1778 
1779  for (i = 0; i < nargs; i++)
1780  {
1781  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1782  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1783  i + variadic_start);
1784 
1785  /*
1786  * Turn a constant (more or less literal) value that's of unknown
1787  * type into text if required. Unknowns come in as a cstring
1788  * pointer. Note: for functions declared as taking type "any", the
1789  * parser will not do any type conversion on unknown-type literals
1790  * (that is, undecorated strings or NULLs).
1791  */
1792  if (convert_unknown &&
1793  types_res[i] == UNKNOWNOID &&
1794  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1795  {
1796  types_res[i] = TEXTOID;
1797 
1798  if (PG_ARGISNULL(i + variadic_start))
1799  args_res[i] = (Datum) 0;
1800  else
1801  args_res[i] =
1802  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1803  }
1804  else
1805  {
1806  /* no conversion needed, just take the datum as given */
1807  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1808  }
1809 
1810  if (!OidIsValid(types_res[i]) ||
1811  (convert_unknown && types_res[i] == UNKNOWNOID))
1812  ereport(ERROR,
1813  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1814  errmsg("could not determine data type for argument %d",
1815  i + 1)));
1816  }
1817  }
1818 
1819  /* Fill in results */
1820  *args = args_res;
1821  *nulls = nulls_res;
1822  *types = types_res;
1823 
1824  return nargs;
1825 }
signed short int16
Definition: c.h:362
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1869
struct typedefs * types
Definition: ecpg.c:29
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2159
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1938
int errcode(int sqlerrcode)
Definition: elog.c:610
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:251
char typalign
Definition: pg_type.h:170
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1804
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define Assert(condition)
Definition: c.h:746
#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:3483
int errmsg(const char *fmt,...)
Definition: elog.c:821
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:86
#define ARR_ELEMTYPE(a)
Definition: array.h:280
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 205 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(), bt_page_items_bytea(), bt_page_stats(), 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(), 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_progress_info(), pg_stat_get_replication_slots(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), 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(), setup_firstcall(), ssl_extension_info(), and storeRow().

208 {
209  return internal_get_result_type(fcinfo->flinfo->fn_oid,
210  fcinfo->flinfo->fn_expr,
211  (ReturnSetInfo *) fcinfo->resultinfo,
212  resultTypeId,
213  resultTupleDesc);
214 }
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:318

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 438 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().

439 {
440  TupleDesc tupleDesc;
441  TypeFuncClass functypclass;
442 
443  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
444 
445  if (functypclass == TYPEFUNC_COMPOSITE ||
446  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
447  return tupleDesc;
448 
449  if (!noError)
450  {
451  Oid exprTypeId = exprType(expr);
452 
453  if (exprTypeId != RECORDOID)
454  ereport(ERROR,
455  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
456  errmsg("type %s is not composite",
457  format_type_be(exprTypeId))));
458  else
459  ereport(ERROR,
460  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
461  errmsg("record type has not been registered")));
462  }
463 
464  return NULL;
465 }
int errcode(int sqlerrcode)
Definition: elog.c:610
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:221
#define ERROR
Definition: elog.h:43
TypeFuncClass
Definition: funcapi.h:146
#define ereport(elevel,...)
Definition: elog.h:144
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:821

◆ get_expr_result_type()

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

Definition at line 221 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().

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

◆ get_func_arg_info()

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

Definition at line 1094 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(), exec_stmt_call(), ExecuteCallStmt(), MatchNamedCall(), pg_get_function_arg_default(), plperl_validator(), plpgsql_validator(), plsample_func_handler(), PLy_procedure_create(), and print_function_arguments().

1096 {
1097  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1098  Datum proallargtypes;
1099  Datum proargmodes;
1100  Datum proargnames;
1101  bool isNull;
1102  ArrayType *arr;
1103  int numargs;
1104  Datum *elems;
1105  int nelems;
1106  int i;
1107 
1108  /* First discover the total number of parameters and get their types */
1109  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
1110  Anum_pg_proc_proallargtypes,
1111  &isNull);
1112  if (!isNull)
1113  {
1114  /*
1115  * We expect the arrays to be 1-D arrays of the right types; verify
1116  * that. For the OID and char arrays, we don't need to use
1117  * deconstruct_array() since the array data is just going to look like
1118  * a C array of values.
1119  */
1120  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1121  numargs = ARR_DIMS(arr)[0];
1122  if (ARR_NDIM(arr) != 1 ||
1123  numargs < 0 ||
1124  ARR_HASNULL(arr) ||
1125  ARR_ELEMTYPE(arr) != OIDOID)
1126  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1127  Assert(numargs >= procStruct->pronargs);
1128  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1129  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
1130  numargs * sizeof(Oid));
1131  }
1132  else
1133  {
1134  /* If no proallargtypes, use proargtypes */
1135  numargs = procStruct->proargtypes.dim1;
1136  Assert(numargs == procStruct->pronargs);
1137  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1138  memcpy(*p_argtypes, procStruct->proargtypes.values,
1139  numargs * sizeof(Oid));
1140  }
1141 
1142  /* Get argument names, if available */
1143  proargnames = SysCacheGetAttr(PROCOID, procTup,
1144  Anum_pg_proc_proargnames,
1145  &isNull);
1146  if (isNull)
1147  *p_argnames = NULL;
1148  else
1149  {
1151  TEXTOID, -1, false, TYPALIGN_INT,
1152  &elems, NULL, &nelems);
1153  if (nelems != numargs) /* should not happen */
1154  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
1155  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
1156  for (i = 0; i < numargs; i++)
1157  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
1158  }
1159 
1160  /* Get argument modes, if available */
1161  proargmodes = SysCacheGetAttr(PROCOID, procTup,
1162  Anum_pg_proc_proargmodes,
1163  &isNull);
1164  if (isNull)
1165  *p_argmodes = NULL;
1166  else
1167  {
1168  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1169  if (ARR_NDIM(arr) != 1 ||
1170  ARR_DIMS(arr)[0] != numargs ||
1171  ARR_HASNULL(arr) ||
1172  ARR_ELEMTYPE(arr) != CHAROID)
1173  elog(ERROR, "proargmodes is not a 1-D char array");
1174  *p_argmodes = (char *) palloc(numargs * sizeof(char));
1175  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
1176  numargs * sizeof(char));
1177  }
1178 
1179  return numargs;
1180 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define Assert(condition)
Definition: c.h:746
#define ARR_NDIM(a)
Definition: array.h:278
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ get_func_input_arg_names()

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

Definition at line 1236 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().

1239 {
1240  ArrayType *arr;
1241  int numargs;
1242  Datum *argnames;
1243  char *argmodes;
1244  char **inargnames;
1245  int numinargs;
1246  int i;
1247 
1248  /* Do nothing if null proargnames */
1249  if (proargnames == PointerGetDatum(NULL))
1250  {
1251  *arg_names = NULL;
1252  return 0;
1253  }
1254 
1255  /*
1256  * We expect the arrays to be 1-D arrays of the right types; verify that.
1257  * For proargmodes, we don't need to use deconstruct_array() since the
1258  * array data is just going to look like a C array of values.
1259  */
1260  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1261  if (ARR_NDIM(arr) != 1 ||
1262  ARR_HASNULL(arr) ||
1263  ARR_ELEMTYPE(arr) != TEXTOID)
1264  elog(ERROR, "proargnames is not a 1-D text array");
1265  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1266  &argnames, NULL, &numargs);
1267  if (proargmodes != PointerGetDatum(NULL))
1268  {
1269  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1270  if (ARR_NDIM(arr) != 1 ||
1271  ARR_DIMS(arr)[0] != numargs ||
1272  ARR_HASNULL(arr) ||
1273  ARR_ELEMTYPE(arr) != CHAROID)
1274  elog(ERROR, "proargmodes is not a 1-D char array");
1275  argmodes = (char *) ARR_DATA_PTR(arr);
1276  }
1277  else
1278  argmodes = NULL;
1279 
1280  /* zero elements probably shouldn't happen, but handle it gracefully */
1281  if (numargs <= 0)
1282  {
1283  *arg_names = NULL;
1284  return 0;
1285  }
1286 
1287  /* extract input-argument names */
1288  inargnames = (char **) palloc(numargs * sizeof(char *));
1289  numinargs = 0;
1290  for (i = 0; i < numargs; i++)
1291  {
1292  if (argmodes == NULL ||
1293  argmodes[i] == PROARGMODE_IN ||
1294  argmodes[i] == PROARGMODE_INOUT ||
1295  (argmodes[i] == PROARGMODE_OUT && prokind == PROKIND_PROCEDURE) ||
1296  argmodes[i] == PROARGMODE_VARIADIC)
1297  {
1298  char *pname = TextDatumGetCString(argnames[i]);
1299 
1300  if (pname[0] != '\0')
1301  inargnames[numinargs] = pname;
1302  else
1303  inargnames[numinargs] = NULL;
1304  numinargs++;
1305  }
1306  }
1307 
1308  *arg_names = inargnames;
1309  return numinargs;
1310 }
#define PointerGetDatum(X)
Definition: postgres.h:556
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
#define ARR_NDIM(a)
Definition: array.h:278
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1323 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().

1324 {
1325  char *result;
1326  HeapTuple procTuple;
1327  Datum proargmodes;
1328  Datum proargnames;
1329  bool isnull;
1330  ArrayType *arr;
1331  int numargs;
1332  char *argmodes;
1333  Datum *argnames;
1334  int numoutargs;
1335  int nargnames;
1336  int i;
1337 
1338  /* First fetch the function's pg_proc row */
1339  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1340  if (!HeapTupleIsValid(procTuple))
1341  elog(ERROR, "cache lookup failed for function %u", functionId);
1342 
1343  /* If there are no named OUT parameters, return NULL */
1344  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1345  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1346  result = NULL;
1347  else
1348  {
1349  /* Get the data out of the tuple */
1350  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1351  Anum_pg_proc_proargmodes,
1352  &isnull);
1353  Assert(!isnull);
1354  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1355  Anum_pg_proc_proargnames,
1356  &isnull);
1357  Assert(!isnull);
1358 
1359  /*
1360  * We expect the arrays to be 1-D arrays of the right types; verify
1361  * that. For the char array, we don't need to use deconstruct_array()
1362  * since the array data is just going to look like a C array of
1363  * values.
1364  */
1365  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1366  numargs = ARR_DIMS(arr)[0];
1367  if (ARR_NDIM(arr) != 1 ||
1368  numargs < 0 ||
1369  ARR_HASNULL(arr) ||
1370  ARR_ELEMTYPE(arr) != CHAROID)
1371  elog(ERROR, "proargmodes is not a 1-D char array");
1372  argmodes = (char *) ARR_DATA_PTR(arr);
1373  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1374  if (ARR_NDIM(arr) != 1 ||
1375  ARR_DIMS(arr)[0] != numargs ||
1376  ARR_HASNULL(arr) ||
1377  ARR_ELEMTYPE(arr) != TEXTOID)
1378  elog(ERROR, "proargnames is not a 1-D text array");
1379  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1380  &argnames, NULL, &nargnames);
1381  Assert(nargnames == numargs);
1382 
1383  /* scan for output argument(s) */
1384  result = NULL;
1385  numoutargs = 0;
1386  for (i = 0; i < numargs; i++)
1387  {
1388  if (argmodes[i] == PROARGMODE_IN ||
1389  argmodes[i] == PROARGMODE_VARIADIC)
1390  continue;
1391  Assert(argmodes[i] == PROARGMODE_OUT ||
1392  argmodes[i] == PROARGMODE_INOUT ||
1393  argmodes[i] == PROARGMODE_TABLE);
1394  if (++numoutargs > 1)
1395  {
1396  /* multiple out args, so forget it */
1397  result = NULL;
1398  break;
1399  }
1400  result = TextDatumGetCString(argnames[i]);
1401  if (result == NULL || result[0] == '\0')
1402  {
1403  /* Parameter is not named, so forget it */
1404  result = NULL;
1405  break;
1406  }
1407  }
1408  }
1409 
1410  ReleaseSysCache(procTuple);
1411 
1412  return result;
1413 }
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
#define ARR_NDIM(a)
Definition: array.h:278
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
#define elog(elevel,...)
Definition: elog.h:214
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ get_func_result_type()

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

Definition at line 298 of file funcapi.c.

References internal_get_result_type().

Referenced by fmgr_sql_validator().

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

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1188 of file funcapi.c.

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

Referenced by print_function_trftypes().

1190 {
1191  Datum protrftypes;
1192  ArrayType *arr;
1193  int nelems;
1194  bool isNull;
1195 
1196  protrftypes = SysCacheGetAttr(PROCOID, procTup,
1197  Anum_pg_proc_protrftypes,
1198  &isNull);
1199  if (!isNull)
1200  {
1201  /*
1202  * We expect the arrays to be 1-D arrays of the right types; verify
1203  * that. For the OID and char arrays, we don't need to use
1204  * deconstruct_array() since the array data is just going to look like
1205  * a C array of values.
1206  */
1207  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
1208  nelems = ARR_DIMS(arr)[0];
1209  if (ARR_NDIM(arr) != 1 ||
1210  nelems < 0 ||
1211  ARR_HASNULL(arr) ||
1212  ARR_ELEMTYPE(arr) != OIDOID)
1213  elog(ERROR, "protrftypes is not a 1-D Oid array");
1214  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
1215  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
1216  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
1217  nelems * sizeof(Oid));
1218 
1219  return nelems;
1220  }
1221  else
1222  return 0;
1223 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int16 pronargs
Definition: pg_proc.h:81
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:282
#define ARR_DATA_PTR(a)
Definition: array.h:310
#define ARR_HASNULL(a)
Definition: array.h:279
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define Assert(condition)
Definition: c.h:746
#define ARR_NDIM(a)
Definition: array.h:278
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:214
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249

◆ get_type_func_class()

static TypeFuncClass get_type_func_class ( Oid  typid,
Oid base_typeid 
)
static

Definition at line 1044 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().

1045 {
1046  *base_typeid = typid;
1047 
1048  switch (get_typtype(typid))
1049  {
1050  case TYPTYPE_COMPOSITE:
1051  return TYPEFUNC_COMPOSITE;
1052  case TYPTYPE_BASE:
1053  case TYPTYPE_ENUM:
1054  case TYPTYPE_RANGE:
1055  return TYPEFUNC_SCALAR;
1056  case TYPTYPE_DOMAIN:
1057  *base_typeid = typid = getBaseType(typid);
1058  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
1060  else /* domain base type can't be a pseudotype */
1061  return TYPEFUNC_SCALAR;
1062  case TYPTYPE_PSEUDO:
1063  if (typid == RECORDOID)
1064  return TYPEFUNC_RECORD;
1065 
1066  /*
1067  * We treat VOID and CSTRING as legitimate scalar datatypes,
1068  * mostly for the convenience of the JDBC driver (which wants to
1069  * be able to do "SELECT * FROM foo()" for all legitimately
1070  * user-callable functions).
1071  */
1072  if (typid == VOIDOID || typid == CSTRINGOID)
1073  return TYPEFUNC_SCALAR;
1074  return TYPEFUNC_OTHER;
1075  }
1076  /* shouldn't get here, probably */
1077  return TYPEFUNC_OTHER;
1078 }
char get_typtype(Oid typid)
Definition: lsyscache.c:2517
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2409

◆ init_MultiFuncCall()

FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 62 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.

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

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

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 137 of file funcapi.c.

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

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1594 of file funcapi.c.

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

1595 {
1596  RangeVar *relvar;
1597  Relation rel;
1598  TupleDesc tupdesc;
1599  List *relname_list;
1600 
1601  /* Open relation and copy the tuple description */
1602  relname_list = stringToQualifiedNameList(relname);
1603  relvar = makeRangeVarFromNameList(relname_list);
1604  rel = relation_openrv(relvar, AccessShareLock);
1605  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1607 
1608  return tupdesc;
1609 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:482
#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 514 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().

515 {
516  /* If we don't know ANYELEMENT, resolve that first */
517  if (!OidIsValid(actuals->anyelement_type))
519 
520  if (OidIsValid(actuals->anyelement_type))
521  {
522  /* Use the array type corresponding to actual type */
523  Oid array_typeid = get_array_type(actuals->anyelement_type);
524 
525  if (!OidIsValid(array_typeid))
526  ereport(ERROR,
527  (errcode(ERRCODE_UNDEFINED_OBJECT),
528  errmsg("could not find array type for data type %s",
529  format_type_be(actuals->anyelement_type))));
530  actuals->anyarray_type = array_typeid;
531  }
532  else
533  elog(ERROR, "could not determine polymorphic type");
534 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:476
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2664
int errcode(int sqlerrcode)
Definition: elog.c:610
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:652
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214

◆ resolve_anyelement_from_others()

static void resolve_anyelement_from_others ( polymorphic_actuals actuals)
static

Definition at line 476 of file funcapi.c.

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

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

477 {
478  if (OidIsValid(actuals->anyarray_type))
479  {
480  /* Use the element type corresponding to actual type */
481  Oid array_base_type = getBaseType(actuals->anyarray_type);
482  Oid array_typelem = get_element_type(array_base_type);
483 
484  if (!OidIsValid(array_typelem))
485  ereport(ERROR,
486  (errcode(ERRCODE_DATATYPE_MISMATCH),
487  errmsg("argument declared %s is not an array but type %s",
488  "anyarray",
489  format_type_be(array_base_type))));
490  actuals->anyelement_type = array_typelem;
491  }
492  else if (OidIsValid(actuals->anyrange_type))
493  {
494  /* Use the element type corresponding to actual type */
495  Oid range_base_type = getBaseType(actuals->anyrange_type);
496  Oid range_typelem = get_range_subtype(range_base_type);
497 
498  if (!OidIsValid(range_typelem))
499  ereport(ERROR,
500  (errcode(ERRCODE_DATATYPE_MISMATCH),
501  errmsg("argument declared %s is not a range type but type %s",
502  "anyrange",
503  format_type_be(range_base_type))));
504  actuals->anyelement_type = range_typelem;
505  }
506  else
507  elog(ERROR, "could not determine polymorphic type");
508 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2636
int errcode(int sqlerrcode)
Definition: elog.c:610
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:652
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2409
Oid get_range_subtype(Oid rangeOid)
Definition: lsyscache.c:3232

◆ resolve_anyrange_from_others()

static void resolve_anyrange_from_others ( polymorphic_actuals actuals)
static

Definition at line 540 of file funcapi.c.

References elog, and ERROR.

Referenced by resolve_polymorphic_argtypes(), and resolve_polymorphic_tupdesc().

541 {
542  /*
543  * We can't deduce a range type from other polymorphic inputs, because
544  * there may be multiple range types with the same subtype.
545  */
546  elog(ERROR, "could not determine polymorphic type");
547 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:214

◆ resolve_polymorphic_argtypes()

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

Definition at line 830 of file funcapi.c.

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

Referenced by plpgsql_resolve_polymorphic_argtypes().

832 {
833  bool have_polymorphic_result = false;
834  bool have_anyelement_result = false;
835  bool have_anyarray_result = false;
836  bool have_anyrange_result = false;
837  bool have_anycompatible_result = false;
838  bool have_anycompatible_array_result = false;
839  bool have_anycompatible_range_result = false;
840  polymorphic_actuals poly_actuals;
841  polymorphic_actuals anyc_actuals;
842  int inargno;
843  int i;
844 
845  /*
846  * First pass: resolve polymorphic inputs, check for outputs. As in
847  * resolve_polymorphic_tupdesc, we rely on the parser to have enforced
848  * type consistency and coerced ANYCOMPATIBLE args to a common supertype.
849  */
850  memset(&poly_actuals, 0, sizeof(poly_actuals));
851  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
852  inargno = 0;
853  for (i = 0; i < numargs; i++)
854  {
855  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
856 
857  switch (argtypes[i])
858  {
859  case ANYELEMENTOID:
860  case ANYNONARRAYOID:
861  case ANYENUMOID:
862  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
863  {
864  have_polymorphic_result = true;
865  have_anyelement_result = true;
866  }
867  else
868  {
869  if (!OidIsValid(poly_actuals.anyelement_type))
870  {
871  poly_actuals.anyelement_type =
872  get_call_expr_argtype(call_expr, inargno);
873  if (!OidIsValid(poly_actuals.anyelement_type))
874  return false;
875  }
876  argtypes[i] = poly_actuals.anyelement_type;
877  }
878  break;
879  case ANYARRAYOID:
880  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
881  {
882  have_polymorphic_result = true;
883  have_anyarray_result = true;
884  }
885  else
886  {
887  if (!OidIsValid(poly_actuals.anyarray_type))
888  {
889  poly_actuals.anyarray_type =
890  get_call_expr_argtype(call_expr, inargno);
891  if (!OidIsValid(poly_actuals.anyarray_type))
892  return false;
893  }
894  argtypes[i] = poly_actuals.anyarray_type;
895  }
896  break;
897  case ANYRANGEOID:
898  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
899  {
900  have_polymorphic_result = true;
901  have_anyrange_result = true;
902  }
903  else
904  {
905  if (!OidIsValid(poly_actuals.anyrange_type))
906  {
907  poly_actuals.anyrange_type =
908  get_call_expr_argtype(call_expr, inargno);
909  if (!OidIsValid(poly_actuals.anyrange_type))
910  return false;
911  }
912  argtypes[i] = poly_actuals.anyrange_type;
913  }
914  break;
915  case ANYCOMPATIBLEOID:
916  case ANYCOMPATIBLENONARRAYOID:
917  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
918  {
919  have_polymorphic_result = true;
920  have_anycompatible_result = true;
921  }
922  else
923  {
924  if (!OidIsValid(anyc_actuals.anyelement_type))
925  {
926  anyc_actuals.anyelement_type =
927  get_call_expr_argtype(call_expr, inargno);
928  if (!OidIsValid(anyc_actuals.anyelement_type))
929  return false;
930  }
931  argtypes[i] = anyc_actuals.anyelement_type;
932  }
933  break;
934  case ANYCOMPATIBLEARRAYOID:
935  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
936  {
937  have_polymorphic_result = true;
938  have_anycompatible_array_result = true;
939  }
940  else
941  {
942  if (!OidIsValid(anyc_actuals.anyarray_type))
943  {
944  anyc_actuals.anyarray_type =
945  get_call_expr_argtype(call_expr, inargno);
946  if (!OidIsValid(anyc_actuals.anyarray_type))
947  return false;
948  }
949  argtypes[i] = anyc_actuals.anyarray_type;
950  }
951  break;
952  case ANYCOMPATIBLERANGEOID:
953  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
954  {
955  have_polymorphic_result = true;
956  have_anycompatible_range_result = true;
957  }
958  else
959  {
960  if (!OidIsValid(anyc_actuals.anyrange_type))
961  {
962  anyc_actuals.anyrange_type =
963  get_call_expr_argtype(call_expr, inargno);
964  if (!OidIsValid(anyc_actuals.anyrange_type))
965  return false;
966  }
967  argtypes[i] = anyc_actuals.anyrange_type;
968  }
969  break;
970  default:
971  break;
972  }
973  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
974  inargno++;
975  }
976 
977  /* Done? */
978  if (!have_polymorphic_result)
979  return true;
980 
981  /* If needed, deduce one polymorphic type from others */
982  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
983  resolve_anyelement_from_others(&poly_actuals);
984 
985  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
986  resolve_anyarray_from_others(&poly_actuals);
987 
988  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
989  resolve_anyrange_from_others(&poly_actuals);
990 
991  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
992  resolve_anyelement_from_others(&anyc_actuals);
993 
994  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
995  resolve_anyarray_from_others(&anyc_actuals);
996 
997  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
998  resolve_anyrange_from_others(&anyc_actuals);
999 
1000  /* And finally replace the output column types as needed */
1001  for (i = 0; i < numargs; i++)
1002  {
1003  switch (argtypes[i])
1004  {
1005  case ANYELEMENTOID:
1006  case ANYNONARRAYOID:
1007  case ANYENUMOID:
1008  argtypes[i] = poly_actuals.anyelement_type;
1009  break;
1010  case ANYARRAYOID:
1011  argtypes[i] = poly_actuals.anyarray_type;
1012  break;
1013  case ANYRANGEOID:
1014  argtypes[i] = poly_actuals.anyrange_type;
1015  break;
1016  case ANYCOMPATIBLEOID:
1017  case ANYCOMPATIBLENONARRAYOID:
1018  argtypes[i] = anyc_actuals.anyelement_type;
1019  break;
1020  case ANYCOMPATIBLEARRAYOID:
1021  argtypes[i] = anyc_actuals.anyarray_type;
1022  break;
1023  case ANYCOMPATIBLERANGEOID:
1024  argtypes[i] = anyc_actuals.anyrange_type;
1025  break;
1026  default:
1027  break;
1028  }
1029  }
1030 
1031  return true;
1032 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:476
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1823
#define OidIsValid(objectId)
Definition: c.h:652
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:540
int i
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:514

◆ resolve_polymorphic_tupdesc()

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

Definition at line 560 of file funcapi.c.

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

Referenced by internal_get_result_type().

562 {
563  int natts = tupdesc->natts;
564  int nargs = declared_args->dim1;
565  bool have_polymorphic_result = false;
566  bool have_anyelement_result = false;
567  bool have_anyarray_result = false;
568  bool have_anyrange_result = false;
569  bool have_anycompatible_result = false;
570  bool have_anycompatible_array_result = false;
571  bool have_anycompatible_range_result = false;
572  polymorphic_actuals poly_actuals;
573  polymorphic_actuals anyc_actuals;
574  Oid anycollation = InvalidOid;
575  Oid anycompatcollation = InvalidOid;
576  int i;
577 
578  /* See if there are any polymorphic outputs; quick out if not */
579  for (i = 0; i < natts; i++)
580  {
581  switch (TupleDescAttr(tupdesc, i)->atttypid)
582  {
583  case ANYELEMENTOID:
584  case ANYNONARRAYOID:
585  case ANYENUMOID:
586  have_polymorphic_result = true;
587  have_anyelement_result = true;
588  break;
589  case ANYARRAYOID:
590  have_polymorphic_result = true;
591  have_anyarray_result = true;
592  break;
593  case ANYRANGEOID:
594  have_polymorphic_result = true;
595  have_anyrange_result = true;
596  break;
597  case ANYCOMPATIBLEOID:
598  case ANYCOMPATIBLENONARRAYOID:
599  have_polymorphic_result = true;
600  have_anycompatible_result = true;
601  break;
602  case ANYCOMPATIBLEARRAYOID:
603  have_polymorphic_result = true;
604  have_anycompatible_array_result = true;
605  break;
606  case ANYCOMPATIBLERANGEOID:
607  have_polymorphic_result = true;
608  have_anycompatible_range_result = true;
609  break;
610  default:
611  break;
612  }
613  }
614  if (!have_polymorphic_result)
615  return true;
616 
617  /*
618  * Otherwise, extract actual datatype(s) from input arguments. (We assume
619  * the parser already validated consistency of the arguments. Also, for
620  * the ANYCOMPATIBLE pseudotype family, we expect that all matching
621  * arguments were coerced to the selected common supertype, so that it
622  * doesn't matter which one's exposed type we look at.)
623  */
624  if (!call_expr)
625  return false; /* no hope */
626 
627  memset(&poly_actuals, 0, sizeof(poly_actuals));
628  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
629 
630  for (i = 0; i < nargs; i++)
631  {
632  switch (declared_args->values[i])
633  {
634  case ANYELEMENTOID:
635  case ANYNONARRAYOID:
636  case ANYENUMOID:
637  if (!OidIsValid(poly_actuals.anyelement_type))
638  {
639  poly_actuals.anyelement_type =
640  get_call_expr_argtype(call_expr, i);
641  if (!OidIsValid(poly_actuals.anyelement_type))
642  return false;
643  }
644  break;
645  case ANYARRAYOID:
646  if (!OidIsValid(poly_actuals.anyarray_type))
647  {
648  poly_actuals.anyarray_type =
649  get_call_expr_argtype(call_expr, i);
650  if (!OidIsValid(poly_actuals.anyarray_type))
651  return false;
652  }
653  break;
654  case ANYRANGEOID:
655  if (!OidIsValid(poly_actuals.anyrange_type))
656  {
657  poly_actuals.anyrange_type =
658  get_call_expr_argtype(call_expr, i);
659  if (!OidIsValid(poly_actuals.anyrange_type))
660  return false;
661  }
662  break;
663  case ANYCOMPATIBLEOID:
664  case ANYCOMPATIBLENONARRAYOID:
665  if (!OidIsValid(anyc_actuals.anyelement_type))
666  {
667  anyc_actuals.anyelement_type =
668  get_call_expr_argtype(call_expr, i);
669  if (!OidIsValid(anyc_actuals.anyelement_type))
670  return false;
671  }
672  break;
673  case ANYCOMPATIBLEARRAYOID:
674  if (!OidIsValid(anyc_actuals.anyarray_type))
675  {
676  anyc_actuals.anyarray_type =
677  get_call_expr_argtype(call_expr, i);
678  if (!OidIsValid(anyc_actuals.anyarray_type))
679  return false;
680  }
681  break;
682  case ANYCOMPATIBLERANGEOID:
683  if (!OidIsValid(anyc_actuals.anyrange_type))
684  {
685  anyc_actuals.anyrange_type =
686  get_call_expr_argtype(call_expr, i);
687  if (!OidIsValid(anyc_actuals.anyrange_type))
688  return false;
689  }
690  break;
691  default:
692  break;
693  }
694  }
695 
696  /* If needed, deduce one polymorphic type from others */
697  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
698  resolve_anyelement_from_others(&poly_actuals);
699 
700  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
701  resolve_anyarray_from_others(&poly_actuals);
702 
703  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
704  resolve_anyrange_from_others(&poly_actuals);
705 
706  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
707  resolve_anyelement_from_others(&anyc_actuals);
708 
709  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
710  resolve_anyarray_from_others(&anyc_actuals);
711 
712  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
713  resolve_anyrange_from_others(&anyc_actuals);
714 
715  /*
716  * Identify the collation to use for polymorphic OUT parameters. (It'll
717  * necessarily be the same for both anyelement and anyarray, likewise for
718  * anycompatible and anycompatiblearray.) Note that range types are not
719  * collatable, so any possible internal collation of a range type is not
720  * considered here.
721  */
722  if (OidIsValid(poly_actuals.anyelement_type))
723  anycollation = get_typcollation(poly_actuals.anyelement_type);
724  else if (OidIsValid(poly_actuals.anyarray_type))
725  anycollation = get_typcollation(poly_actuals.anyarray_type);
726 
727  if (OidIsValid(anyc_actuals.anyelement_type))
728  anycompatcollation = get_typcollation(anyc_actuals.anyelement_type);
729  else if (OidIsValid(anyc_actuals.anyarray_type))
730  anycompatcollation = get_typcollation(anyc_actuals.anyarray_type);
731 
732  if (OidIsValid(anycollation) || OidIsValid(anycompatcollation))
733  {
734  /*
735  * The types are collatable, so consider whether to use a nondefault
736  * collation. We do so if we can identify the input collation used
737  * for the function.
738  */
739  Oid inputcollation = exprInputCollation(call_expr);
740 
741  if (OidIsValid(inputcollation))
742  {
743  if (OidIsValid(anycollation))
744  anycollation = inputcollation;
745  if (OidIsValid(anycompatcollation))
746  anycompatcollation = inputcollation;
747  }
748  }
749 
750  /* And finally replace the tuple column types as needed */
751  for (i = 0; i < natts; i++)
752  {
753  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
754 
755  switch (att->atttypid)
756  {
757  case ANYELEMENTOID:
758  case ANYNONARRAYOID:
759  case ANYENUMOID:
760  TupleDescInitEntry(tupdesc, i + 1,
761  NameStr(att->attname),
762  poly_actuals.anyelement_type,
763  -1,
764  0);
765  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
766  break;
767  case ANYARRAYOID:
768  TupleDescInitEntry(tupdesc, i + 1,
769  NameStr(att->attname),
770  poly_actuals.anyarray_type,
771  -1,
772  0);
773  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
774  break;
775  case ANYRANGEOID:
776  TupleDescInitEntry(tupdesc, i + 1,
777  NameStr(att->attname),
778  poly_actuals.anyrange_type,
779  -1,
780  0);
781  /* no collation should be attached to a range type */
782  break;
783  case ANYCOMPATIBLEOID:
784  case ANYCOMPATIBLENONARRAYOID:
785  TupleDescInitEntry(tupdesc, i + 1,
786  NameStr(att->attname),
787  anyc_actuals.anyelement_type,
788  -1,
789  0);
790  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
791  break;
792  case ANYCOMPATIBLEARRAYOID:
793  TupleDescInitEntry(tupdesc, i + 1,
794  NameStr(att->attname),
795  anyc_actuals.anyarray_type,
796  -1,
797  0);
798  TupleDescInitEntryCollation(tupdesc, i + 1, anycompatcollation);
799  break;
800  case ANYCOMPATIBLERANGEOID:
801  TupleDescInitEntry(tupdesc, i + 1,
802  NameStr(att->attname),
803  anyc_actuals.anyrange_type,
804  -1,
805  0);
806  /* no collation should be attached to a range type */
807  break;
808  default:
809  break;
810  }
811  }
812 
813  return true;
814 }
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:476
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1823
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:540
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:610
int dim1
Definition: c.h:608
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
Oid atttypid
Definition: pg_attribute.h:49
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2933
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:973
int i
#define NameStr(name)
Definition: c.h:623
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:514

◆ shutdown_MultiFuncCall()

static void shutdown_MultiFuncCall ( Datum  arg)
static

Definition at line 167 of file funcapi.c.

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

Referenced by end_MultiFuncCall(), and init_MultiFuncCall().

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

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1627 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.

1628 {
1629  Oid base_typeoid;
1630  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1631  TupleDesc tupdesc = NULL;
1632 
1633  /*
1634  * Build a suitable tupledesc representing the output rows. We
1635  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1636  * unlikely that legacy callers of this obsolete function would be
1637  * prepared to apply domain constraints.
1638  */
1639  if (functypclass == TYPEFUNC_COMPOSITE)
1640  {
1641  /* Composite data type, e.g. a table's row type */
1642  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1643 
1644  if (colaliases != NIL)
1645  {
1646  int natts = tupdesc->natts;
1647  int varattno;
1648 
1649  /* does the list length match the number of attributes? */
1650  if (list_length(colaliases) != natts)
1651  ereport(ERROR,
1652  (errcode(ERRCODE_DATATYPE_MISMATCH),
1653  errmsg("number of aliases does not match number of columns")));
1654 
1655  /* OK, use the aliases instead */
1656  for (varattno = 0; varattno < natts; varattno++)
1657  {
1658  char *label = strVal(list_nth(colaliases, varattno));
1659  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1660 
1661  if (label != NULL)
1662  namestrcpy(&(attr->attname), label);
1663  }
1664 
1665  /* The tuple type is now an anonymous record type */
1666  tupdesc->tdtypeid = RECORDOID;
1667  tupdesc->tdtypmod = -1;
1668  }
1669  }
1670  else if (functypclass == TYPEFUNC_SCALAR)
1671  {
1672  /* Base data type, i.e. scalar */
1673  char *attname;
1674 
1675  /* the alias list is required for base types */
1676  if (colaliases == NIL)
1677  ereport(ERROR,
1678  (errcode(ERRCODE_DATATYPE_MISMATCH),
1679  errmsg("no column alias was provided")));
1680 
1681  /* the alias list length must be 1 */
1682  if (list_length(colaliases) != 1)
1683  ereport(ERROR,
1684  (errcode(ERRCODE_DATATYPE_MISMATCH),
1685  errmsg("number of aliases does not match number of columns")));
1686 
1687  /* OK, get the column alias */
1688  attname = strVal(linitial(colaliases));
1689 
1690  tupdesc = CreateTemplateTupleDesc(1);
1691  TupleDescInitEntry(tupdesc,
1692  (AttrNumber) 1,
1693  attname,
1694  typeoid,
1695  -1,
1696  0);
1697  }
1698  else if (functypclass == TYPEFUNC_RECORD)
1699  {
1700  /* XXX can't support this because typmod wasn't passed in ... */
1701  ereport(ERROR,
1702  (errcode(ERRCODE_DATATYPE_MISMATCH),
1703  errmsg("could not determine row description for function returning record")));
1704  }
1705  else
1706  {
1707  /* crummy error message, but parser should have caught this */
1708  elog(ERROR, "function in FROM has unsupported return type");
1709  }
1710 
1711  return tupdesc;
1712 }
#define NIL
Definition: pg_list.h:65
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
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:610
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:43
NameData attname
Definition: pg_attribute.h:40
static void * list_nth(const List *list, int n)
Definition: pg_list.h:266
int32 tdtypmod
Definition: tupdesc.h:83
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1044
TypeFuncClass
Definition: funcapi.h:146
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
static char * label
#define ereport(elevel,...)
Definition: elog.h:144
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:821
#define elog(elevel,...)
Definition: elog.h:214
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1744