PostgreSQL Source Code  git master
funcapi.h File Reference
#include "access/tupdesc.h"
#include "executor/executor.h"
#include "executor/tuptable.h"
#include "fmgr.h"
Include dependency graph for funcapi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AttInMetadata
 
struct  FuncCallContext
 

Macros

#define HeapTupleGetDatum(tuple)   HeapTupleHeaderGetDatum((tuple)->t_data)
 
#define TupleGetDatum(_slot, _tuple)   HeapTupleGetDatum(_tuple)
 
#define SRF_IS_FIRSTCALL()   (fcinfo->flinfo->fn_extra == NULL)
 
#define SRF_FIRSTCALL_INIT()   init_MultiFuncCall(fcinfo)
 
#define SRF_PERCALL_SETUP()   per_MultiFuncCall(fcinfo)
 
#define SRF_RETURN_NEXT(_funcctx, _result)
 
#define SRF_RETURN_NEXT_NULL(_funcctx)
 
#define SRF_RETURN_DONE(_funcctx)
 

Typedefs

typedef struct AttInMetadata AttInMetadata
 
typedef struct FuncCallContext FuncCallContext
 
typedef enum TypeFuncClass TypeFuncClass
 

Enumerations

enum  TypeFuncClass {
  TYPEFUNC_SCALAR, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD,
  TYPEFUNC_OTHER
}
 

Functions

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_input_arg_names (char prokind, Datum proargnames, Datum proargmodes, char ***arg_names)
 
int get_func_trftypes (HeapTuple procTup, Oid **p_trftypes)
 
char * get_func_result_name (Oid functionId)
 
TupleDesc build_function_result_tupdesc_d (char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
 
TupleDesc build_function_result_tupdesc_t (HeapTuple procTuple)
 
TupleDesc RelationNameGetTupleDesc (const char *relname)
 
TupleDesc TypeGetTupleDesc (Oid typeoid, List *colaliases)
 
TupleDesc BlessTupleDesc (TupleDesc tupdesc)
 
AttInMetadataTupleDescGetAttInMetadata (TupleDesc tupdesc)
 
HeapTuple BuildTupleFromCStrings (AttInMetadata *attinmeta, char **values)
 
Datum HeapTupleHeaderGetDatum (HeapTupleHeader tuple)
 
FuncCallContextinit_MultiFuncCall (PG_FUNCTION_ARGS)
 
FuncCallContextper_MultiFuncCall (PG_FUNCTION_ARGS)
 
void end_MultiFuncCall (PG_FUNCTION_ARGS, FuncCallContext *funcctx)
 
int extract_variadic_args (FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **values, Oid **types, bool **nulls)
 

Macro Definition Documentation

◆ HeapTupleGetDatum

#define HeapTupleGetDatum (   tuple)    HeapTupleHeaderGetDatum((tuple)->t_data)

Definition at line 221 of file funcapi.h.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), bt_page_print_tuples(), bt_page_stats(), build_pgstattuple_type(), copy_replication_slot(), dblink_get_pkey(), exec_eval_datum(), ExecEvalConvertRowtype(), ExecEvalFieldStoreForm(), ExecEvalRow(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), hstore_each(), hstore_populate_record(), page_header(), pg_buffercache_pages(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_keywords(), pg_get_multixact_members(), pg_get_object_address(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_old_snapshot_time_mapping(), pg_partition_tree(), pg_prepared_xact(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_wal(), pg_stat_get_wal_receiver(), pg_stat_statements_info(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_walfile_name_offset(), pg_xact_commit_timestamp_origin(), pgp_armor_headers(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_hash_to_datum(), plperl_return_next_internal(), pltcl_build_tuple_result(), pltcl_func_handler(), prs_process_call(), show_all_settings(), ssl_extension_info(), test_predtest(), ts_process_call(), tsvector_unnest(), and tt_process_call().

◆ SRF_FIRSTCALL_INIT

◆ SRF_IS_FIRSTCALL

◆ SRF_PERCALL_SETUP

◆ SRF_RETURN_DONE

#define SRF_RETURN_DONE (   _funcctx)
Value:
do { \
ReturnSetInfo *rsi; \
end_MultiFuncCall(fcinfo, _funcctx); \
rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
rsi->isDone = ExprEndResult; \
} while (0)
#define PG_RETURN_NULL()
Definition: fmgr.h:345

Definition at line 318 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), bt_page_items_bytea(), dblink_get_pkey(), generate_series_step_int4(), generate_series_step_int8(), generate_series_step_numeric(), generate_series_timestamp(), generate_series_timestamptz(), generate_subscripts(), gin_leafpage_items(), hash_page_items(), heap_page_items(), hstore_each(), hstore_skeys(), hstore_svals(), json_object_keys(), jsonb_object_keys(), jsonb_path_query_internal(), normal_rand(), pg_buffercache_pages(), pg_check_frozen(), pg_check_visible(), pg_get_keywords(), pg_get_multixact_members(), pg_get_publication_tables(), pg_listening_channels(), pg_lock_status(), pg_old_snapshot_time_mapping(), pg_partition_ancestors(), pg_partition_tree(), pg_prepared_xact(), pg_snapshot_xip(), pg_stat_get_backend_idset(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility_map_rel(), pg_visibility_rel(), pgp_armor_headers(), PLy_exec_function(), regexp_matches(), regexp_split_to_table(), show_all_settings(), ssl_extension_info(), test_regex(), ts_parse_byid(), ts_parse_byname(), ts_stat1(), ts_stat2(), ts_token_type_byid(), ts_token_type_byname(), and tsvector_unnest().

◆ SRF_RETURN_NEXT

#define SRF_RETURN_NEXT (   _funcctx,
  _result 
)
Value:
do { \
ReturnSetInfo *rsi; \
(_funcctx)->call_cntr++; \
rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
rsi->isDone = ExprMultipleResult; \
PG_RETURN_DATUM(_result); \
} while (0)
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

Definition at line 300 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), bt_page_items_bytea(), dblink_get_pkey(), generate_series_step_int4(), generate_series_step_int8(), generate_series_step_numeric(), generate_series_timestamp(), generate_series_timestamptz(), generate_subscripts(), gin_leafpage_items(), hash_page_items(), heap_page_items(), hstore_each(), hstore_skeys(), hstore_svals(), json_object_keys(), jsonb_object_keys(), jsonb_path_query_internal(), normal_rand(), pg_buffercache_pages(), pg_check_frozen(), pg_check_visible(), pg_get_keywords(), pg_get_multixact_members(), pg_get_publication_tables(), pg_listening_channels(), pg_lock_status(), pg_old_snapshot_time_mapping(), pg_partition_ancestors(), pg_partition_tree(), pg_prepared_xact(), pg_snapshot_xip(), pg_stat_get_backend_idset(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility_map_rel(), pg_visibility_rel(), pgp_armor_headers(), PLy_exec_function(), regexp_matches(), regexp_split_to_table(), show_all_settings(), ssl_extension_info(), test_regex(), ts_parse_byid(), ts_parse_byname(), ts_stat1(), ts_stat2(), ts_token_type_byid(), ts_token_type_byname(), and tsvector_unnest().

◆ SRF_RETURN_NEXT_NULL

#define SRF_RETURN_NEXT_NULL (   _funcctx)
Value:
do { \
ReturnSetInfo *rsi; \
(_funcctx)->call_cntr++; \
rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
rsi->isDone = ExprMultipleResult; \
} while (0)
#define PG_RETURN_NULL()
Definition: fmgr.h:345

Definition at line 309 of file funcapi.h.

Referenced by PLy_exec_function().

◆ TupleGetDatum

#define TupleGetDatum (   _slot,
  _tuple 
)    HeapTupleGetDatum(_tuple)

Definition at line 223 of file funcapi.h.

Typedef Documentation

◆ AttInMetadata

typedef struct AttInMetadata AttInMetadata

◆ FuncCallContext

◆ TypeFuncClass

Enumeration Type Documentation

◆ TypeFuncClass

Enumerator
TYPEFUNC_SCALAR 
TYPEFUNC_COMPOSITE 
TYPEFUNC_COMPOSITE_DOMAIN 
TYPEFUNC_RECORD 
TYPEFUNC_OTHER 

Definition at line 146 of file funcapi.h.

147 {
148  TYPEFUNC_SCALAR, /* scalar result type */
149  TYPEFUNC_COMPOSITE, /* determinable rowtype result */
150  TYPEFUNC_COMPOSITE_DOMAIN, /* domain over determinable rowtype result */
151  TYPEFUNC_RECORD, /* indeterminate rowtype result */
152  TYPEFUNC_OTHER /* bogus type, eg pseudotype */
153 } TypeFuncClass;
TypeFuncClass
Definition: funcapi.h:146

Function Documentation

◆ BlessTupleDesc()

TupleDesc BlessTupleDesc ( TupleDesc  tupdesc)

Definition at line 2052 of file execTuples.c.

References assign_record_type_typmod(), TupleDescData::tdtypeid, and TupleDescData::tdtypmod.

Referenced by aclexplode(), brin_metapage_info(), bt_page_items(), bt_page_items_bytea(), each_worker(), each_worker_jsonb(), elements_worker(), elements_worker_jsonb(), exec_eval_datum(), ExecEvalWholeRowVar(), ExecInitExprRec(), ExecInitFunctionScan(), get_expr_result_type(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), init_sql_fcache(), MakeOldSnapshotTimeMappingTupleDesc(), pg_buffercache_pages(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_get_object_address(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_partition_tree(), pg_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_wal(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility_map_summary(), pg_visibility_tupdesc(), pg_walfile_name_offset(), pg_xact_commit_timestamp_origin(), pgstathashindex(), PLy_output_setup_record(), setup_firstcall(), ssl_extension_info(), test_predtest(), ts_setup_firstcall(), tsvector_unnest(), TupleDescGetAttInMetadata(), and verify_heapam_tupdesc().

2053 {
2054  if (tupdesc->tdtypeid == RECORDOID &&
2055  tupdesc->tdtypmod < 0)
2056  assign_record_type_typmod(tupdesc);
2057 
2058  return tupdesc; /* just for notational convenience */
2059 }
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1946
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82

◆ build_function_result_tupdesc_d()

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

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

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

1604 {
1605  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1606  Datum proallargtypes;
1607  Datum proargmodes;
1608  Datum proargnames;
1609  bool isnull;
1610 
1611  /* Return NULL if the function isn't declared to return RECORD */
1612  if (procform->prorettype != RECORDOID)
1613  return NULL;
1614 
1615  /* If there are no OUT parameters, return NULL */
1616  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1617  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1618  return NULL;
1619 
1620  /* Get the data out of the tuple */
1621  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1622  Anum_pg_proc_proallargtypes,
1623  &isnull);
1624  Assert(!isnull);
1625  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1626  Anum_pg_proc_proargmodes,
1627  &isnull);
1628  Assert(!isnull);
1629  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1630  Anum_pg_proc_proargnames,
1631  &isnull);
1632  if (isnull)
1633  proargnames = PointerGetDatum(NULL); /* just to be sure */
1634 
1635  return build_function_result_tupdesc_d(procform->prokind,
1636  proallargtypes,
1637  proargmodes,
1638  proargnames);
1639 }
#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:1653
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:1388
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define Assert(condition)
Definition: c.h:792

◆ BuildTupleFromCStrings()

HeapTuple BuildTupleFromCStrings ( AttInMetadata attinmeta,
char **  values 
)

Definition at line 2116 of file execTuples.c.

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, AttInMetadata::atttypmods, heap_form_tuple(), i, InputFunctionCall(), TupleDescData::natts, palloc(), pfree(), AttInMetadata::tupdesc, and TupleDescAttr.

Referenced by bt_metap(), bt_page_stats(), build_pgstattuple_type(), build_tuplestore_recursively(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), libpqrcv_processTuples(), materializeResult(), pg_config(), pg_get_keywords(), pg_get_multixact_members(), pg_logdir_ls_internal(), pgp_armor_headers(), pgrowlocks(), pgstatindex_impl(), pltcl_build_tuple_result(), prs_process_call(), show_all_settings(), storeRow(), ts_process_call(), tt_process_call(), and xpath_table().

2117 {
2118  TupleDesc tupdesc = attinmeta->tupdesc;
2119  int natts = tupdesc->natts;
2120  Datum *dvalues;
2121  bool *nulls;
2122  int i;
2123  HeapTuple tuple;
2124 
2125  dvalues = (Datum *) palloc(natts * sizeof(Datum));
2126  nulls = (bool *) palloc(natts * sizeof(bool));
2127 
2128  /*
2129  * Call the "in" function for each non-dropped attribute, even for nulls,
2130  * to support domains.
2131  */
2132  for (i = 0; i < natts; i++)
2133  {
2134  if (!TupleDescAttr(tupdesc, i)->attisdropped)
2135  {
2136  /* Non-dropped attributes */
2137  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2138  values[i],
2139  attinmeta->attioparams[i],
2140  attinmeta->atttypmods[i]);
2141  if (values[i] != NULL)
2142  nulls[i] = false;
2143  else
2144  nulls[i] = true;
2145  }
2146  else
2147  {
2148  /* Handle dropped attributes by setting to NULL */
2149  dvalues[i] = (Datum) 0;
2150  nulls[i] = true;
2151  }
2152  }
2153 
2154  /*
2155  * Form a tuple
2156  */
2157  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2158 
2159  /*
2160  * Release locally palloc'd space. XXX would probably be good to pfree
2161  * values of pass-by-reference datums, as well.
2162  */
2163  pfree(dvalues);
2164  pfree(nulls);
2165 
2166  return tuple;
2167 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int32 * atttypmods
Definition: funcapi.h:47
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Oid * attioparams
Definition: funcapi.h:44
void pfree(void *pointer)
Definition: mcxt.c:1057
TupleDesc tupdesc
Definition: funcapi.h:38
uintptr_t Datum
Definition: postgres.h:367
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1532
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void * palloc(Size size)
Definition: mcxt.c:950
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41

◆ 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:950
#define PointerGetDatum(X)
Definition: postgres.h:556
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
ExprContext * econtext
Definition: execnodes.h:302

◆ extract_variadic_args()

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

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

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

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:704
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:45
TypeFuncClass
Definition: funcapi.h:146
#define ereport(elevel,...)
Definition: elog.h:155
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ 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:1046
#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:267
Definition: nodes.h:528
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
List * colnames
Definition: primnodes.h:1062
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:1217
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:1229
#define Assert(condition)
Definition: c.h:792
#define lfirst(lc)
Definition: pg_list.h:169
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid row_typeid
Definition: primnodes.h:1047
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(), exec_stmt_call(), ExecuteCallStmt(), MatchNamedCall(), pg_get_function_arg_default(), plperl_validator(), plpgsql_validator(), plsample_func_handler(), PLy_procedure_create(), and print_function_arguments().

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:655
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:45
#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:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define Assert(condition)
Definition: c.h:792
#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:950
#define elog(elevel,...)
Definition: elog.h:228
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 1411 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().

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

1500 {
1501  char *result;
1502  HeapTuple procTuple;
1503  Datum proargmodes;
1504  Datum proargnames;
1505  bool isnull;
1506  ArrayType *arr;
1507  int numargs;
1508  char *argmodes;
1509  Datum *argnames;
1510  int numoutargs;
1511  int nargnames;
1512  int i;
1513 
1514  /* First fetch the function's pg_proc row */
1515  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1516  if (!HeapTupleIsValid(procTuple))
1517  elog(ERROR, "cache lookup failed for function %u", functionId);
1518 
1519  /* If there are no named OUT parameters, return NULL */
1520  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1521  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1522  result = NULL;
1523  else
1524  {
1525  /* Get the data out of the tuple */
1526  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1527  Anum_pg_proc_proargmodes,
1528  &isnull);
1529  Assert(!isnull);
1530  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1531  Anum_pg_proc_proargnames,
1532  &isnull);
1533  Assert(!isnull);
1534 
1535  /*
1536  * We expect the arrays to be 1-D arrays of the right types; verify
1537  * that. For the char array, we don't need to use deconstruct_array()
1538  * since the array data is just going to look like a C array of
1539  * values.
1540  */
1541  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1542  numargs = ARR_DIMS(arr)[0];
1543  if (ARR_NDIM(arr) != 1 ||
1544  numargs < 0 ||
1545  ARR_HASNULL(arr) ||
1546  ARR_ELEMTYPE(arr) != CHAROID)
1547  elog(ERROR, "proargmodes is not a 1-D char array or it contains nulls");
1548  argmodes = (char *) ARR_DATA_PTR(arr);
1549  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1550  if (ARR_NDIM(arr) != 1 ||
1551  ARR_DIMS(arr)[0] != numargs ||
1552  ARR_HASNULL(arr) ||
1553  ARR_ELEMTYPE(arr) != TEXTOID)
1554  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1555  numargs);
1556  deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT,
1557  &argnames, NULL, &nargnames);
1558  Assert(nargnames == numargs);
1559 
1560  /* scan for output argument(s) */
1561  result = NULL;
1562  numoutargs = 0;
1563  for (i = 0; i < numargs; i++)
1564  {
1565  if (argmodes[i] == PROARGMODE_IN ||
1566  argmodes[i] == PROARGMODE_VARIADIC)
1567  continue;
1568  Assert(argmodes[i] == PROARGMODE_OUT ||
1569  argmodes[i] == PROARGMODE_INOUT ||
1570  argmodes[i] == PROARGMODE_TABLE);
1571  if (++numoutargs > 1)
1572  {
1573  /* multiple out args, so forget it */
1574  result = NULL;
1575  break;
1576  }
1577  result = TextDatumGetCString(argnames[i]);
1578  if (result == NULL || result[0] == '\0')
1579  {
1580  /* Parameter is not named, so forget it */
1581  result = NULL;
1582  break;
1583  }
1584  }
1585  }
1586 
1587  ReleaseSysCache(procTuple);
1588 
1589  return result;
1590 }
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:45
#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:367
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:792
#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:228
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 1363 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().

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

◆ HeapTupleHeaderGetDatum()

Datum HeapTupleHeaderGetDatum ( HeapTupleHeader  tuple)

Definition at line 2205 of file execTuples.c.

References HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, HeapTupleHeaderHasExternal, lookup_rowtype_tupdesc(), PointerGetDatum, ReleaseTupleDesc, and toast_flatten_tuple_to_datum().

Referenced by populate_composite(), and populate_recordset_record().

2206 {
2207  Datum result;
2208  TupleDesc tupDesc;
2209 
2210  /* No work if there are no external TOAST pointers in the tuple */
2211  if (!HeapTupleHeaderHasExternal(tuple))
2212  return PointerGetDatum(tuple);
2213 
2214  /* Use the type data saved by heap_form_tuple to look up the rowtype */
2216  HeapTupleHeaderGetTypMod(tuple));
2217 
2218  /* And do the flattening */
2219  result = toast_flatten_tuple_to_datum(tuple,
2221  tupDesc);
2222 
2223  ReleaseTupleDesc(tupDesc);
2224 
2225  return result;
2226 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1826
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
uintptr_t Datum
Definition: postgres.h:367
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:539
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: heaptoast.c:449
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ 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: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:704
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
TupleDesc tuple_desc
Definition: funcapi.h:112
#define ERROR
Definition: elog.h:45
AttInMetadata * attinmeta
Definition: funcapi.h:91
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:840
#define ereport(elevel,...)
Definition: elog.h:155
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:915
#define elog(elevel,...)
Definition: elog.h:228
uint64 max_calls
Definition: funcapi.h:74

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

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

1774 {
1775  RangeVar *relvar;
1776  Relation rel;
1777  TupleDesc tupdesc;
1778  List *relname_list;
1779 
1780  /* Open relation and copy the tuple description */
1781  relname_list = stringToQualifiedNameList(relname);
1782  relvar = makeRangeVarFromNameList(relname_list);
1783  rel = relation_openrv(relvar, AccessShareLock);
1784  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1786 
1787  return tupdesc;
1788 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:483
#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_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:1822
#define OidIsValid(objectId)
Definition: c.h:698
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

◆ TupleDescGetAttInMetadata()

AttInMetadata* TupleDescGetAttInMetadata ( TupleDesc  tupdesc)

Definition at line 2067 of file execTuples.c.

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, AttInMetadata::atttypmods, BlessTupleDesc(), fmgr_info(), getTypeInputInfo(), i, TupleDescData::natts, palloc(), palloc0(), AttInMetadata::tupdesc, and TupleDescAttr.

Referenced by bt_metap(), bt_page_stats(), build_pgstattuple_type(), connectby_text(), connectby_text_serial(), create_foreign_modify(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), hash_page_items(), libpqrcv_processTuples(), materializeResult(), pg_config(), pg_get_keywords(), pg_get_multixact_members(), pg_logdir_ls_internal(), pg_stats_ext_mcvlist_items(), pgp_armor_headers(), pgrowlocks(), pgstatindex_impl(), pltcl_build_tuple_result(), pltcl_func_handler(), pltcl_init_tuple_store(), postgresAcquireSampleRowsFunc(), postgresBeginDirectModify(), postgresBeginForeignScan(), prs_setup_firstcall(), show_all_settings(), storeRow(), ts_setup_firstcall(), tt_setup_firstcall(), and xpath_table().

2068 {
2069  int natts = tupdesc->natts;
2070  int i;
2071  Oid atttypeid;
2072  Oid attinfuncid;
2073  FmgrInfo *attinfuncinfo;
2074  Oid *attioparams;
2075  int32 *atttypmods;
2076  AttInMetadata *attinmeta;
2077 
2078  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
2079 
2080  /* "Bless" the tupledesc so that we can make rowtype datums with it */
2081  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2082 
2083  /*
2084  * Gather info needed later to call the "in" function for each attribute
2085  */
2086  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2087  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2088  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2089 
2090  for (i = 0; i < natts; i++)
2091  {
2092  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2093 
2094  /* Ignore dropped attributes */
2095  if (!att->attisdropped)
2096  {
2097  atttypeid = att->atttypid;
2098  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2099  fmgr_info(attinfuncid, &attinfuncinfo[i]);
2100  atttypmods[i] = att->atttypmod;
2101  }
2102  }
2103  attinmeta->attinfuncs = attinfuncinfo;
2104  attinmeta->attioparams = attioparams;
2105  attinmeta->atttypmods = atttypmods;
2106 
2107  return attinmeta;
2108 }
Definition: fmgr.h:56
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int32 * atttypmods
Definition: funcapi.h:47
unsigned int Oid
Definition: postgres_ext.h:31
Oid * attioparams
Definition: funcapi.h:44
signed int int32
Definition: c.h:417
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
TupleDesc tupdesc
Definition: funcapi.h:38
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2794
void * palloc0(Size size)
Definition: mcxt.c:981
void * palloc(Size size)
Definition: mcxt.c:950
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

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

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