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(), 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(), 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_partition_tree(), pg_prepared_xact(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_wal_receiver(), 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:344

Definition at line 317 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_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(), 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:352

Definition at line 299 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_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(), 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:344

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 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(), 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_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(), and TupleDescGetAttInMetadata().

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:1830
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 1474 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().

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

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

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

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

◆ 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:1056
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:1533
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 149 of file funcapi.c.

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

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

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

◆ get_call_result_type()

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

Definition at line 205 of file funcapi.c.

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

Referenced by brin_metapage_info(), brin_page_items(), bt_metap(), bt_page_items(), bt_page_items_bytea(), bt_page_stats(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), each_worker(), each_worker_jsonb(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), init_sql_fcache(), materializeResult(), page_header(), pg_available_extension_versions(), pg_available_extensions(), pg_buffercache_pages(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_event_trigger_ddl_commands(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_get_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_internal(), pg_stats_ext_mcvlist_items(), pg_stop_backup_v2(), pg_timezone_names(), pgp_armor_headers(), pgrowlocks(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_func_handler(), PLy_function_build_args(), setup_firstcall(), ssl_extension_info(), and storeRow().

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

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 438 of file funcapi.c.

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

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

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

◆ get_expr_result_type()

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

Definition at line 221 of file funcapi.c.

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

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

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

◆ get_func_arg_info()

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

Definition at line 1094 of file funcapi.c.

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

Referenced by check_sql_fn_statements(), do_compile(), exec_stmt_call(), MatchNamedCall(), pg_get_function_arg_default(), plperl_validator(), plpgsql_validator(), PLy_procedure_create(), and print_function_arguments().

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

◆ get_func_input_arg_names()

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

Definition at line 1236 of file funcapi.c.

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

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

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

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

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

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

◆ get_func_result_type()

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

Definition at line 298 of file funcapi.c.

References internal_get_result_type().

Referenced by fmgr_sql_validator().

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

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1188 of file funcapi.c.

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

Referenced by print_function_trftypes().

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

◆ 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:1710
#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 62 of file funcapi.c.

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

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

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 137 of file funcapi.c.

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

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1592 of file funcapi.c.

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

1593 {
1594  RangeVar *relvar;
1595  Relation rel;
1596  TupleDesc tupdesc;
1597  List *relname_list;
1598 
1599  /* Open relation and copy the tuple description */
1600  relname_list = stringToQualifiedNameList(relname);
1601  relvar = makeRangeVarFromNameList(relname_list);
1602  rel = relation_openrv(relvar, AccessShareLock);
1603  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1605 
1606  return tupdesc;
1607 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:482
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3062
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 830 of file funcapi.c.

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

Referenced by plpgsql_resolve_polymorphic_argtypes().

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

◆ 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:362
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:2751
void * palloc0(Size size)
Definition: mcxt.c:980
void * palloc(Size size)
Definition: mcxt.c:949
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

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

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