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 (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 220 of file funcapi.h.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), bt_page_print_tuples(), bt_page_stats_internal(), 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_catalog_foreign_keys(), 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_replication_slot(), 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_enc_conversion(), 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 317 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items_bytea(), bt_page_items_internal(), 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(), multirange_unnest(), normal_rand(), pg_buffercache_pages(), pg_check_frozen(), pg_check_visible(), pg_get_catalog_foreign_keys(), 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 299 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items_bytea(), bt_page_items_internal(), 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(), multirange_unnest(), normal_rand(), pg_buffercache_pages(), pg_check_frozen(), pg_check_visible(), pg_get_catalog_foreign_keys(), 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 308 of file funcapi.h.

Referenced by PLy_exec_function().

◆ TupleGetDatum

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

Definition at line 222 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 2082 of file execTuples.c.

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

Referenced by aclexplode(), brin_metapage_info(), bt_page_items_bytea(), bt_page_items_internal(), 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_catalog_foreign_keys(), 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_replication_slot(), 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_enc_conversion(), test_predtest(), ts_setup_firstcall(), tsvector_unnest(), TupleDescGetAttInMetadata(), and verify_heapam_tupdesc().

2083 {
2084  if (tupdesc->tdtypeid == RECORDOID &&
2085  tupdesc->tdtypmod < 0)
2086  assign_record_type_typmod(tupdesc);
2087 
2088  return tupdesc; /* just for notational convenience */
2089 }
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1947
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 1652 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().

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

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

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

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

◆ BuildTupleFromCStrings()

HeapTuple BuildTupleFromCStrings ( AttInMetadata attinmeta,
char **  values 
)

Definition at line 2146 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_internal(), 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().

2147 {
2148  TupleDesc tupdesc = attinmeta->tupdesc;
2149  int natts = tupdesc->natts;
2150  Datum *dvalues;
2151  bool *nulls;
2152  int i;
2153  HeapTuple tuple;
2154 
2155  dvalues = (Datum *) palloc(natts * sizeof(Datum));
2156  nulls = (bool *) palloc(natts * sizeof(bool));
2157 
2158  /*
2159  * Call the "in" function for each non-dropped attribute, even for nulls,
2160  * to support domains.
2161  */
2162  for (i = 0; i < natts; i++)
2163  {
2164  if (!TupleDescAttr(tupdesc, i)->attisdropped)
2165  {
2166  /* Non-dropped attributes */
2167  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2168  values[i],
2169  attinmeta->attioparams[i],
2170  attinmeta->atttypmods[i]);
2171  if (values[i] != NULL)
2172  nulls[i] = false;
2173  else
2174  nulls[i] = true;
2175  }
2176  else
2177  {
2178  /* Handle dropped attributes by setting to NULL */
2179  dvalues[i] = (Datum) 0;
2180  nulls[i] = true;
2181  }
2182  }
2183 
2184  /*
2185  * Form a tuple
2186  */
2187  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2188 
2189  /*
2190  * Release locally palloc'd space. XXX would probably be good to pfree
2191  * values of pass-by-reference datums, as well.
2192  */
2193  pfree(dvalues);
2194  pfree(nulls);
2195 
2196  return tuple;
2197 }
#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:1169
TupleDesc tupdesc
Definition: funcapi.h:38
uintptr_t Datum
Definition: postgres.h:411
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1529
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void * palloc(Size size)
Definition: mcxt.c:1062
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:951
#define PointerGetDatum(X)
Definition: postgres.h:600
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
ExprContext * econtext
Definition: execnodes.h:303

◆ extract_variadic_args()

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

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

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

◆ get_call_result_type()

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

Definition at line 207 of file funcapi.c.

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

Referenced by brin_metapage_info(), brin_page_items(), bt_metap(), bt_page_items_bytea(), bt_page_items_internal(), bt_page_stats_internal(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), each_worker(), each_worker_jsonb(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_items(), gist_page_items_bytea(), gist_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), init_sql_fcache(), materializeResult(), page_header(), pg_available_extension_versions(), pg_available_extensions(), pg_buffercache_pages(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_event_trigger_ddl_commands(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_get_backend_memory_contexts(), pg_get_replication_slots(), pg_get_shmem_allocations(), pg_hba_file_rules(), pg_logical_slot_get_changes_guts(), pg_ls_dir_files(), pg_replication_slot_advance(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_progress_info(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), pg_stat_statements_info(), pg_stat_statements_internal(), pg_stats_ext_mcvlist_items(), pg_stop_backup_v2(), pg_timezone_names(), pgp_armor_headers(), pgrowlocks(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_func_handler(), PLy_function_build_args(), postgres_fdw_get_connections(), setup_firstcall(), ssl_extension_info(), storeRow(), and test_enc_conversion().

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

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 440 of file funcapi.c.

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

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

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

◆ get_expr_result_type()

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

Definition at line 223 of file funcapi.c.

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

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

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

◆ 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 do_compile(), make_callstmt_target(), MatchNamedCall(), pg_get_function_arg_default(), plperl_validator(), plpgsql_validator(), plsample_func_handler(), PLy_procedure_create(), print_function_arguments(), and print_function_sqlbody().

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

◆ get_func_input_arg_names()

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

Definition at line 1412 of file funcapi.c.

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

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

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_VARIADIC)
1472  {
1473  char *pname = TextDatumGetCString(argnames[i]);
1474 
1475  if (pname[0] != '\0')
1476  inargnames[numinargs] = pname;
1477  else
1478  inargnames[numinargs] = NULL;
1479  numinargs++;
1480  }
1481  }
1482 
1483  *arg_names = inargnames;
1484  return numinargs;
1485 }
#define PointerGetDatum(X)
Definition: postgres.h:600
#define ERROR
Definition: elog.h:46
#define ARR_DIMS(a)
Definition: array.h:287
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define ARR_HASNULL(a)
Definition: array.h:284
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:411
#define ARR_NDIM(a)
Definition: array.h:283
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3491
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define DatumGetArrayTypeP(X)
Definition: array.h:254

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

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

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

◆ get_func_result_type()

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

Definition at line 300 of file funcapi.c.

References internal_get_result_type().

Referenced by fmgr_sql_validator().

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

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1365 of file funcapi.c.

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

Referenced by print_function_trftypes().

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

◆ HeapTupleHeaderGetDatum()

Datum HeapTupleHeaderGetDatum ( HeapTupleHeader  tuple)

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

2236 {
2237  Datum result;
2238  TupleDesc tupDesc;
2239 
2240  /* No work if there are no external TOAST pointers in the tuple */
2241  if (!HeapTupleHeaderHasExternal(tuple))
2242  return PointerGetDatum(tuple);
2243 
2244  /* Use the type data saved by heap_form_tuple to look up the rowtype */
2246  HeapTupleHeaderGetTypMod(tuple));
2247 
2248  /* And do the flattening */
2249  result = toast_flatten_tuple_to_datum(tuple,
2251  tupDesc);
2252 
2253  ReleaseTupleDesc(tupDesc);
2254 
2255  return result;
2256 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1827
#define PointerGetDatum(X)
Definition: postgres.h:600
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:467
uintptr_t Datum
Definition: postgres.h:411
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:538
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:451

◆ 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:587
#define AllocSetContextCreate
Definition: memutils.h:173
#define PointerGetDatum(X)
Definition: postgres.h:600
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
int errcode(int sqlerrcode)
Definition: elog.c:698
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:169
TupleDesc tuple_desc
Definition: funcapi.h:112
#define ERROR
Definition: elog.h:46
AttInMetadata * attinmeta
Definition: funcapi.h:91
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define ereport(elevel,...)
Definition: elog.h:157
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:925
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
ExprContext * econtext
Definition: execnodes.h:303
void * user_fctx
Definition: funcapi.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
uint64 max_calls
Definition: funcapi.h:74

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

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

1773 {
1774  RangeVar *relvar;
1775  Relation rel;
1776  TupleDesc tupdesc;
1777  List *relname_list;
1778 
1779  /* Open relation and copy the tuple description */
1780  relname_list = stringToQualifiedNameList(relname);
1781  relvar = makeRangeVarFromNameList(relname_list);
1782  rel = relation_openrv(relvar, AccessShareLock);
1783  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1785 
1786  return tupdesc;
1787 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define RelationGetDescr(relation)
Definition: rel.h:503
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3107
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:1877
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:1819
#define OidIsValid(objectId)
Definition: c.h:710
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:570
Oid anymultirange_type
Definition: funcapi.c:38
static void resolve_anymultirange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:599
int i
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:544

◆ TupleDescGetAttInMetadata()

AttInMetadata* TupleDescGetAttInMetadata ( TupleDesc  tupdesc)

Definition at line 2097 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_internal(), 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().

2098 {
2099  int natts = tupdesc->natts;
2100  int i;
2101  Oid atttypeid;
2102  Oid attinfuncid;
2103  FmgrInfo *attinfuncinfo;
2104  Oid *attioparams;
2105  int32 *atttypmods;
2106  AttInMetadata *attinmeta;
2107 
2108  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
2109 
2110  /* "Bless" the tupledesc so that we can make rowtype datums with it */
2111  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2112 
2113  /*
2114  * Gather info needed later to call the "in" function for each attribute
2115  */
2116  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2117  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2118  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2119 
2120  for (i = 0; i < natts; i++)
2121  {
2122  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2123 
2124  /* Ignore dropped attributes */
2125  if (!att->attisdropped)
2126  {
2127  atttypeid = att->atttypid;
2128  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2129  fmgr_info(attinfuncid, &attinfuncinfo[i]);
2130  atttypmods[i] = att->atttypmod;
2131  }
2132  }
2133  attinmeta->attinfuncs = attinfuncinfo;
2134  attinmeta->attioparams = attioparams;
2135  attinmeta->atttypmods = atttypmods;
2136 
2137  return attinmeta;
2138 }
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:429
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2082
TupleDesc tupdesc
Definition: funcapi.h:38
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2821
void * palloc0(Size size)
Definition: mcxt.c:1093
void * palloc(Size size)
Definition: mcxt.c:1062
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

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

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