PostgreSQL Source Code  git master
funcapi.h File Reference
#include "fmgr.h"
#include "access/tupdesc.h"
#include "executor/executor.h"
#include "executor/tuptable.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 221 of file funcapi.h.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), bt_page_print_tuples(), bt_page_stats(), build_pgstattuple_type(), copy_replication_slot(), dblink_get_pkey(), exec_eval_datum(), ExecEvalConvertRowtype(), ExecEvalFieldStoreForm(), ExecEvalRow(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), 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_logdir_ls_internal(), pg_ls_dir_files(), 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_timezone_names(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_walfile_name_offset(), pgp_armor_headers(), pgrowlocks(), 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:335

Definition at line 307 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_logdir_ls_internal(), pg_ls_dir(), pg_ls_dir_files(), pg_partition_ancestors(), pg_partition_tree(), pg_prepared_xact(), pg_stat_get_backend_idset(), pg_stats_ext_mcvlist_items(), pg_tablespace_databases(), pg_timezone_abbrevs(), pg_timezone_names(), pg_visibility_map_rel(), pg_visibility_rel(), pgp_armor_headers(), pgrowlocks(), 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(), tsvector_unnest(), and txid_snapshot_xip().

◆ 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:343

Definition at line 289 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_logdir_ls_internal(), pg_ls_dir(), pg_ls_dir_files(), pg_partition_ancestors(), pg_partition_tree(), pg_prepared_xact(), pg_stat_get_backend_idset(), pg_stats_ext_mcvlist_items(), pg_tablespace_databases(), pg_timezone_abbrevs(), pg_timezone_names(), pg_visibility_map_rel(), pg_visibility_rel(), pgp_armor_headers(), pgrowlocks(), 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(), tsvector_unnest(), and txid_snapshot_xip().

◆ 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:335

Definition at line 298 of file funcapi.h.

Referenced by PLy_exec_function().

◆ TupleGetDatum

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

Definition at line 223 of file funcapi.h.

Typedef Documentation

◆ AttInMetadata

typedef struct AttInMetadata AttInMetadata

◆ FuncCallContext

◆ TypeFuncClass

Enumeration Type Documentation

◆ TypeFuncClass

Enumerator
TYPEFUNC_SCALAR 
TYPEFUNC_COMPOSITE 
TYPEFUNC_COMPOSITE_DOMAIN 
TYPEFUNC_RECORD 
TYPEFUNC_OTHER 

Definition at line 147 of file funcapi.h.

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

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(), 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_ls_dir_files(), 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_timezone_names(), pg_visibility_map_summary(), pg_visibility_tupdesc(), pg_walfile_name_offset(), 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:1772
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 1243 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().

1247 {
1248  TupleDesc desc;
1249  ArrayType *arr;
1250  int numargs;
1251  Oid *argtypes;
1252  char *argmodes;
1253  Datum *argnames = NULL;
1254  Oid *outargtypes;
1255  char **outargnames;
1256  int numoutargs;
1257  int nargnames;
1258  int i;
1259 
1260  /* Can't have output args if columns are null */
1261  if (proallargtypes == PointerGetDatum(NULL) ||
1262  proargmodes == PointerGetDatum(NULL))
1263  return NULL;
1264 
1265  /*
1266  * We expect the arrays to be 1-D arrays of the right types; verify that.
1267  * For the OID and char arrays, we don't need to use deconstruct_array()
1268  * since the array data is just going to look like a C array of values.
1269  */
1270  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1271  numargs = ARR_DIMS(arr)[0];
1272  if (ARR_NDIM(arr) != 1 ||
1273  numargs < 0 ||
1274  ARR_HASNULL(arr) ||
1275  ARR_ELEMTYPE(arr) != OIDOID)
1276  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1277  argtypes = (Oid *) ARR_DATA_PTR(arr);
1278  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1279  if (ARR_NDIM(arr) != 1 ||
1280  ARR_DIMS(arr)[0] != numargs ||
1281  ARR_HASNULL(arr) ||
1282  ARR_ELEMTYPE(arr) != CHAROID)
1283  elog(ERROR, "proargmodes is not a 1-D char array");
1284  argmodes = (char *) ARR_DATA_PTR(arr);
1285  if (proargnames != PointerGetDatum(NULL))
1286  {
1287  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1288  if (ARR_NDIM(arr) != 1 ||
1289  ARR_DIMS(arr)[0] != numargs ||
1290  ARR_HASNULL(arr) ||
1291  ARR_ELEMTYPE(arr) != TEXTOID)
1292  elog(ERROR, "proargnames is not a 1-D text array");
1293  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1294  &argnames, NULL, &nargnames);
1295  Assert(nargnames == numargs);
1296  }
1297 
1298  /* zero elements probably shouldn't happen, but handle it gracefully */
1299  if (numargs <= 0)
1300  return NULL;
1301 
1302  /* extract output-argument types and names */
1303  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1304  outargnames = (char **) palloc(numargs * sizeof(char *));
1305  numoutargs = 0;
1306  for (i = 0; i < numargs; i++)
1307  {
1308  char *pname;
1309 
1310  if (argmodes[i] == PROARGMODE_IN ||
1311  argmodes[i] == PROARGMODE_VARIADIC)
1312  continue;
1313  Assert(argmodes[i] == PROARGMODE_OUT ||
1314  argmodes[i] == PROARGMODE_INOUT ||
1315  argmodes[i] == PROARGMODE_TABLE);
1316  outargtypes[numoutargs] = argtypes[i];
1317  if (argnames)
1318  pname = TextDatumGetCString(argnames[i]);
1319  else
1320  pname = NULL;
1321  if (pname == NULL || pname[0] == '\0')
1322  {
1323  /* Parameter is not named, so gin up a column name */
1324  pname = psprintf("column%d", numoutargs + 1);
1325  }
1326  outargnames[numoutargs] = pname;
1327  numoutargs++;
1328  }
1329 
1330  /*
1331  * If there is no output argument, or only one, the function does not
1332  * return tuples.
1333  */
1334  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1335  return NULL;
1336 
1337  desc = CreateTemplateTupleDesc(numoutargs);
1338  for (i = 0; i < numoutargs; i++)
1339  {
1340  TupleDescInitEntry(desc, i + 1,
1341  outargnames[i],
1342  outargtypes[i],
1343  -1,
1344  0);
1345  }
1346 
1347  return desc;
1348 }
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:84
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:733
#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:3461
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
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 1193 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().

1194 {
1195  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1196  Datum proallargtypes;
1197  Datum proargmodes;
1198  Datum proargnames;
1199  bool isnull;
1200 
1201  /* Return NULL if the function isn't declared to return RECORD */
1202  if (procform->prorettype != RECORDOID)
1203  return NULL;
1204 
1205  /* If there are no OUT parameters, return NULL */
1206  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1207  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1208  return NULL;
1209 
1210  /* Get the data out of the tuple */
1211  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1212  Anum_pg_proc_proallargtypes,
1213  &isnull);
1214  Assert(!isnull);
1215  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1216  Anum_pg_proc_proargmodes,
1217  &isnull);
1218  Assert(!isnull);
1219  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1220  Anum_pg_proc_proargnames,
1221  &isnull);
1222  if (isnull)
1223  proargnames = PointerGetDatum(NULL); /* just to be sure */
1224 
1225  return build_function_result_tupdesc_d(procform->prokind,
1226  proallargtypes,
1227  proargmodes,
1228  proargnames);
1229 }
#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:1243
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:134
#define Assert(condition)
Definition: c.h:733

◆ 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_print_tuples(), 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:48
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
Oid * attioparams
Definition: funcapi.h:45
void pfree(void *pointer)
Definition: mcxt.c:1056
TupleDesc tupdesc
Definition: funcapi.h:39
uintptr_t Datum
Definition: postgres.h:367
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1531
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:42

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 140 of file funcapi.c.

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

141 {
142  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
143 
144  /* Deregister the shutdown callback */
147  PointerGetDatum(fcinfo->flinfo));
148 
149  /* But use it to do the real work */
150  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
151 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:889
#define PointerGetDatum(X)
Definition: postgres.h:556
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:158
ExprContext * econtext
Definition: execnodes.h:301

◆ extract_variadic_args()

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

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

1499 {
1500  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1501  Datum *args_res;
1502  bool *nulls_res;
1503  Oid *types_res;
1504  int nargs,
1505  i;
1506 
1507  *args = NULL;
1508  *types = NULL;
1509  *nulls = NULL;
1510 
1511  if (variadic)
1512  {
1514  Oid element_type;
1515  bool typbyval;
1516  char typalign;
1517  int16 typlen;
1518 
1519  Assert(PG_NARGS() == variadic_start + 1);
1520 
1521  if (PG_ARGISNULL(variadic_start))
1522  return -1;
1523 
1524  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1525  element_type = ARR_ELEMTYPE(array_in);
1526 
1527  get_typlenbyvalalign(element_type,
1528  &typlen, &typbyval, &typalign);
1529  deconstruct_array(array_in, element_type, typlen, typbyval,
1530  typalign, &args_res, &nulls_res,
1531  &nargs);
1532 
1533  /* All the elements of the array have the same type */
1534  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1535  for (i = 0; i < nargs; i++)
1536  types_res[i] = element_type;
1537  }
1538  else
1539  {
1540  nargs = PG_NARGS() - variadic_start;
1541  Assert(nargs > 0);
1542  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1543  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1544  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1545 
1546  for (i = 0; i < nargs; i++)
1547  {
1548  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1549  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1550  i + variadic_start);
1551 
1552  /*
1553  * Turn a constant (more or less literal) value that's of unknown
1554  * type into text if required. Unknowns come in as a cstring
1555  * pointer. Note: for functions declared as taking type "any", the
1556  * parser will not do any type conversion on unknown-type literals
1557  * (that is, undecorated strings or NULLs).
1558  */
1559  if (convert_unknown &&
1560  types_res[i] == UNKNOWNOID &&
1561  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1562  {
1563  types_res[i] = TEXTOID;
1564 
1565  if (PG_ARGISNULL(i + variadic_start))
1566  args_res[i] = (Datum) 0;
1567  else
1568  args_res[i] =
1569  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1570  }
1571  else
1572  {
1573  /* no conversion needed, just take the datum as given */
1574  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1575  }
1576 
1577  if (!OidIsValid(types_res[i]) ||
1578  (convert_unknown && types_res[i] == UNKNOWNOID))
1579  ereport(ERROR,
1580  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1581  errmsg("could not determine data type for argument %d",
1582  i + 1)));
1583  }
1584  }
1585 
1586  /* Fill in results */
1587  *args = args_res;
1588  *nulls = nulls_res;
1589  *types = types_res;
1590 
1591  return nargs;
1592 }
signed short int16
Definition: c.h:346
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1882
struct typedefs * types
Definition: ecpg.c:29
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2049
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:263
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:1951
int errcode(int sqlerrcode)
Definition: elog.c:608
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:639
#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:1817
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * flinfo
Definition: fmgr.h:87
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define Assert(condition)
Definition: c.h:733
#define PG_NARGS()
Definition: fmgr.h:198
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3461
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:83
#define ARR_ELEMTYPE(a)
Definition: array.h:280
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:172

◆ get_call_result_type()

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

Definition at line 196 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(), 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_hba_file_rules(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_progress_info(), 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(), 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().

199 {
200  return internal_get_result_type(fcinfo->flinfo->fn_oid,
201  fcinfo->flinfo->fn_expr,
202  (ReturnSetInfo *) fcinfo->resultinfo,
203  resultTypeId,
204  resultTupleDesc);
205 }
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:309

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

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

430 {
431  TupleDesc tupleDesc;
432  TypeFuncClass functypclass;
433 
434  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
435 
436  if (functypclass == TYPEFUNC_COMPOSITE ||
437  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
438  return tupleDesc;
439 
440  if (!noError)
441  {
442  Oid exprTypeId = exprType(expr);
443 
444  if (exprTypeId != RECORDOID)
445  ereport(ERROR,
446  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
447  errmsg("type %s is not composite",
448  format_type_be(exprTypeId))));
449  else
450  ereport(ERROR,
451  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
452  errmsg("record type has not been registered")));
453  }
454 
455  return NULL;
456 }
int errcode(int sqlerrcode)
Definition: elog.c:608
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define ERROR
Definition: elog.h:43
TypeFuncClass
Definition: funcapi.h:147
#define ereport(elevel, rest)
Definition: elog.h:141
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ get_expr_result_type()

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

Definition at line 212 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(), get_rte_attribute_type(), init_sexpr(), and pull_up_constant_function().

215 {
216  TypeFuncClass result;
217 
218  if (expr && IsA(expr, FuncExpr))
219  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
220  expr,
221  NULL,
222  resultTypeId,
223  resultTupleDesc);
224  else if (expr && IsA(expr, OpExpr))
225  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
226  expr,
227  NULL,
228  resultTypeId,
229  resultTupleDesc);
230  else if (expr && IsA(expr, RowExpr) &&
231  ((RowExpr *) expr)->row_typeid == RECORDOID)
232  {
233  /* We can resolve the record type by generating the tupdesc directly */
234  RowExpr *rexpr = (RowExpr *) expr;
235  TupleDesc tupdesc;
236  AttrNumber i = 1;
237  ListCell *lcc,
238  *lcn;
239 
240  tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
241  Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
242  forboth(lcc, rexpr->args, lcn, rexpr->colnames)
243  {
244  Node *col = (Node *) lfirst(lcc);
245  char *colname = strVal(lfirst(lcn));
246 
247  TupleDescInitEntry(tupdesc, i,
248  colname,
249  exprType(col),
250  exprTypmod(col),
251  0);
252  TupleDescInitEntryCollation(tupdesc, i,
253  exprCollation(col));
254  i++;
255  }
256  if (resultTypeId)
257  *resultTypeId = rexpr->row_typeid;
258  if (resultTupleDesc)
259  *resultTupleDesc = BlessTupleDesc(tupdesc);
260  return TYPEFUNC_COMPOSITE;
261  }
262  else
263  {
264  /* handle as a generic expression; no chance to resolve RECORD */
265  Oid typid = exprType(expr);
266  Oid base_typid;
267 
268  if (resultTypeId)
269  *resultTypeId = typid;
270  if (resultTupleDesc)
271  *resultTupleDesc = NULL;
272  result = get_type_func_class(typid, &base_typid);
273  if ((result == TYPEFUNC_COMPOSITE ||
274  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
275  resultTupleDesc)
276  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
277  }
278 
279  return result;
280 }
List * args
Definition: primnodes.h:1008
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#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:525
#define strVal(v)
Definition: value.h:54
unsigned int Oid
Definition: postgres_ext.h:31
List * colnames
Definition: primnodes.h:1024
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:813
TypeFuncClass
Definition: funcapi.h:147
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:1092
#define Assert(condition)
Definition: c.h:733
#define lfirst(lc)
Definition: pg_list.h:190
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid row_typeid
Definition: primnodes.h:1009
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:309
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1686

◆ get_func_arg_info()

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

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

865 {
866  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
867  Datum proallargtypes;
868  Datum proargmodes;
869  Datum proargnames;
870  bool isNull;
871  ArrayType *arr;
872  int numargs;
873  Datum *elems;
874  int nelems;
875  int i;
876 
877  /* First discover the total number of parameters and get their types */
878  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
879  Anum_pg_proc_proallargtypes,
880  &isNull);
881  if (!isNull)
882  {
883  /*
884  * We expect the arrays to be 1-D arrays of the right types; verify
885  * that. For the OID and char arrays, we don't need to use
886  * deconstruct_array() since the array data is just going to look like
887  * a C array of values.
888  */
889  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
890  numargs = ARR_DIMS(arr)[0];
891  if (ARR_NDIM(arr) != 1 ||
892  numargs < 0 ||
893  ARR_HASNULL(arr) ||
894  ARR_ELEMTYPE(arr) != OIDOID)
895  elog(ERROR, "proallargtypes is not a 1-D Oid array");
896  Assert(numargs >= procStruct->pronargs);
897  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
898  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
899  numargs * sizeof(Oid));
900  }
901  else
902  {
903  /* If no proallargtypes, use proargtypes */
904  numargs = procStruct->proargtypes.dim1;
905  Assert(numargs == procStruct->pronargs);
906  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
907  memcpy(*p_argtypes, procStruct->proargtypes.values,
908  numargs * sizeof(Oid));
909  }
910 
911  /* Get argument names, if available */
912  proargnames = SysCacheGetAttr(PROCOID, procTup,
913  Anum_pg_proc_proargnames,
914  &isNull);
915  if (isNull)
916  *p_argnames = NULL;
917  else
918  {
920  TEXTOID, -1, false, 'i',
921  &elems, NULL, &nelems);
922  if (nelems != numargs) /* should not happen */
923  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
924  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
925  for (i = 0; i < numargs; i++)
926  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
927  }
928 
929  /* Get argument modes, if available */
930  proargmodes = SysCacheGetAttr(PROCOID, procTup,
931  Anum_pg_proc_proargmodes,
932  &isNull);
933  if (isNull)
934  *p_argmodes = NULL;
935  else
936  {
937  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
938  if (ARR_NDIM(arr) != 1 ||
939  ARR_DIMS(arr)[0] != numargs ||
940  ARR_HASNULL(arr) ||
941  ARR_ELEMTYPE(arr) != CHAROID)
942  elog(ERROR, "proargmodes is not a 1-D char array");
943  *p_argmodes = (char *) palloc(numargs * sizeof(char));
944  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
945  numargs * sizeof(char));
946  }
947 
948  return numargs;
949 }
#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:84
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:134
#define Assert(condition)
Definition: c.h:733
#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:3461
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
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 1005 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().

1007 {
1008  ArrayType *arr;
1009  int numargs;
1010  Datum *argnames;
1011  char *argmodes;
1012  char **inargnames;
1013  int numinargs;
1014  int i;
1015 
1016  /* Do nothing if null proargnames */
1017  if (proargnames == PointerGetDatum(NULL))
1018  {
1019  *arg_names = NULL;
1020  return 0;
1021  }
1022 
1023  /*
1024  * We expect the arrays to be 1-D arrays of the right types; verify that.
1025  * For proargmodes, we don't need to use deconstruct_array() since the
1026  * array data is just going to look like a C array of values.
1027  */
1028  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1029  if (ARR_NDIM(arr) != 1 ||
1030  ARR_HASNULL(arr) ||
1031  ARR_ELEMTYPE(arr) != TEXTOID)
1032  elog(ERROR, "proargnames is not a 1-D text array");
1033  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1034  &argnames, NULL, &numargs);
1035  if (proargmodes != PointerGetDatum(NULL))
1036  {
1037  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1038  if (ARR_NDIM(arr) != 1 ||
1039  ARR_DIMS(arr)[0] != numargs ||
1040  ARR_HASNULL(arr) ||
1041  ARR_ELEMTYPE(arr) != CHAROID)
1042  elog(ERROR, "proargmodes is not a 1-D char array");
1043  argmodes = (char *) ARR_DATA_PTR(arr);
1044  }
1045  else
1046  argmodes = NULL;
1047 
1048  /* zero elements probably shouldn't happen, but handle it gracefully */
1049  if (numargs <= 0)
1050  {
1051  *arg_names = NULL;
1052  return 0;
1053  }
1054 
1055  /* extract input-argument names */
1056  inargnames = (char **) palloc(numargs * sizeof(char *));
1057  numinargs = 0;
1058  for (i = 0; i < numargs; i++)
1059  {
1060  if (argmodes == NULL ||
1061  argmodes[i] == PROARGMODE_IN ||
1062  argmodes[i] == PROARGMODE_INOUT ||
1063  argmodes[i] == PROARGMODE_VARIADIC)
1064  {
1065  char *pname = TextDatumGetCString(argnames[i]);
1066 
1067  if (pname[0] != '\0')
1068  inargnames[numinargs] = pname;
1069  else
1070  inargnames[numinargs] = NULL;
1071  numinargs++;
1072  }
1073  }
1074 
1075  *arg_names = inargnames;
1076  return numinargs;
1077 }
#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:84
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:3461
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
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 1090 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().

1091 {
1092  char *result;
1093  HeapTuple procTuple;
1094  Datum proargmodes;
1095  Datum proargnames;
1096  bool isnull;
1097  ArrayType *arr;
1098  int numargs;
1099  char *argmodes;
1100  Datum *argnames;
1101  int numoutargs;
1102  int nargnames;
1103  int i;
1104 
1105  /* First fetch the function's pg_proc row */
1106  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1107  if (!HeapTupleIsValid(procTuple))
1108  elog(ERROR, "cache lookup failed for function %u", functionId);
1109 
1110  /* If there are no named OUT parameters, return NULL */
1111  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1112  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1113  result = NULL;
1114  else
1115  {
1116  /* Get the data out of the tuple */
1117  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1118  Anum_pg_proc_proargmodes,
1119  &isnull);
1120  Assert(!isnull);
1121  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1122  Anum_pg_proc_proargnames,
1123  &isnull);
1124  Assert(!isnull);
1125 
1126  /*
1127  * We expect the arrays to be 1-D arrays of the right types; verify
1128  * that. For the char array, we don't need to use deconstruct_array()
1129  * since the array data is just going to look like a C array of
1130  * values.
1131  */
1132  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1133  numargs = ARR_DIMS(arr)[0];
1134  if (ARR_NDIM(arr) != 1 ||
1135  numargs < 0 ||
1136  ARR_HASNULL(arr) ||
1137  ARR_ELEMTYPE(arr) != CHAROID)
1138  elog(ERROR, "proargmodes is not a 1-D char array");
1139  argmodes = (char *) ARR_DATA_PTR(arr);
1140  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1141  if (ARR_NDIM(arr) != 1 ||
1142  ARR_DIMS(arr)[0] != numargs ||
1143  ARR_HASNULL(arr) ||
1144  ARR_ELEMTYPE(arr) != TEXTOID)
1145  elog(ERROR, "proargnames is not a 1-D text array");
1146  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1147  &argnames, NULL, &nargnames);
1148  Assert(nargnames == numargs);
1149 
1150  /* scan for output argument(s) */
1151  result = NULL;
1152  numoutargs = 0;
1153  for (i = 0; i < numargs; i++)
1154  {
1155  if (argmodes[i] == PROARGMODE_IN ||
1156  argmodes[i] == PROARGMODE_VARIADIC)
1157  continue;
1158  Assert(argmodes[i] == PROARGMODE_OUT ||
1159  argmodes[i] == PROARGMODE_INOUT ||
1160  argmodes[i] == PROARGMODE_TABLE);
1161  if (++numoutargs > 1)
1162  {
1163  /* multiple out args, so forget it */
1164  result = NULL;
1165  break;
1166  }
1167  result = TextDatumGetCString(argnames[i]);
1168  if (result == NULL || result[0] == '\0')
1169  {
1170  /* Parameter is not named, so forget it */
1171  result = NULL;
1172  break;
1173  }
1174  }
1175  }
1176 
1177  ReleaseSysCache(procTuple);
1178 
1179  return result;
1180 }
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:84
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:733
#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:3461
#define elog(elevel,...)
Definition: elog.h:228
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 289 of file funcapi.c.

References internal_get_result_type().

Referenced by check_sql_fn_retval(), and inline_set_returning_function().

292 {
293  return internal_get_result_type(functionId,
294  NULL,
295  NULL,
296  resultTypeId,
297  resultTupleDesc);
298 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:309

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

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

959 {
960  Datum protrftypes;
961  ArrayType *arr;
962  int nelems;
963  bool isNull;
964 
965  protrftypes = SysCacheGetAttr(PROCOID, procTup,
966  Anum_pg_proc_protrftypes,
967  &isNull);
968  if (!isNull)
969  {
970  /*
971  * We expect the arrays to be 1-D arrays of the right types; verify
972  * that. For the OID and char arrays, we don't need to use
973  * deconstruct_array() since the array data is just going to look like
974  * a C array of values.
975  */
976  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
977  nelems = ARR_DIMS(arr)[0];
978  if (ARR_NDIM(arr) != 1 ||
979  nelems < 0 ||
980  ARR_HASNULL(arr) ||
981  ARR_ELEMTYPE(arr) != OIDOID)
982  elog(ERROR, "protrftypes is not a 1-D Oid array");
983  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
984  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
985  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
986  nelems * sizeof(Oid));
987 
988  return nelems;
989  }
990  else
991  return 0;
992 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int16 pronargs
Definition: pg_proc.h:82
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:134
#define Assert(condition)
Definition: c.h:733
#define ARR_NDIM(a)
Definition: array.h:278
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
#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:1652
#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:442
#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 53 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.

54 {
55  FuncCallContext *retval;
56 
57  /*
58  * Bail if we're called in the wrong context
59  */
60  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
61  ereport(ERROR,
62  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
63  errmsg("set-valued function called in context that cannot accept a set")));
64 
65  if (fcinfo->flinfo->fn_extra == NULL)
66  {
67  /*
68  * First call
69  */
70  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
71  MemoryContext multi_call_ctx;
72 
73  /*
74  * Create a suitably long-lived context to hold cross-call data
75  */
76  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
77  "SRF multi-call context",
79 
80  /*
81  * Allocate suitably long-lived space and zero it
82  */
83  retval = (FuncCallContext *)
84  MemoryContextAllocZero(multi_call_ctx,
85  sizeof(FuncCallContext));
86 
87  /*
88  * initialize the elements
89  */
90  retval->call_cntr = 0;
91  retval->max_calls = 0;
92  retval->user_fctx = NULL;
93  retval->attinmeta = NULL;
94  retval->tuple_desc = NULL;
95  retval->multi_call_memory_ctx = multi_call_ctx;
96 
97  /*
98  * save the pointer for cross-call use
99  */
100  fcinfo->flinfo->fn_extra = retval;
101 
102  /*
103  * Ensure we will get shut down cleanly if the exprcontext is not run
104  * to completion.
105  */
108  PointerGetDatum(fcinfo->flinfo));
109  }
110  else
111  {
112  /* second and subsequent calls */
113  elog(ERROR, "init_MultiFuncCall cannot be called more than once");
114 
115  /* never reached, but keep compiler happy */
116  retval = NULL;
117  }
118 
119  return retval;
120 }
uint64 call_cntr
Definition: funcapi.h:66
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#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:608
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:158
TupleDesc tuple_desc
Definition: funcapi.h:113
#define ERROR
Definition: elog.h:43
AttInMetadata * attinmeta
Definition: funcapi.h:92
#define ereport(elevel, rest)
Definition: elog.h:141
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:863
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
ExprContext * econtext
Definition: execnodes.h:301
void * user_fctx
Definition: funcapi.h:83
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
uint64 max_calls
Definition: funcapi.h:75

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 128 of file funcapi.c.

129 {
130  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
131 
132  return retval;
133 }

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1361 of file funcapi.c.

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

1362 {
1363  RangeVar *relvar;
1364  Relation rel;
1365  TupleDesc tupdesc;
1366  List *relname_list;
1367 
1368  /* Open relation and copy the tuple description */
1369  relname_list = stringToQualifiedNameList(relname);
1370  relvar = makeRangeVarFromNameList(relname_list);
1371  rel = relation_openrv(relvar, AccessShareLock);
1372  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1374 
1375  return tupdesc;
1376 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:449
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
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:1687
Definition: pg_list.h:50

◆ resolve_polymorphic_argtypes()

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

Definition at line 661 of file funcapi.c.

References get_call_expr_argtype(), i, InvalidOid, OidIsValid, and resolve_generic_type().

Referenced by plpgsql_resolve_polymorphic_argtypes().

663 {
664  bool have_anyelement_result = false;
665  bool have_anyarray_result = false;
666  bool have_anyrange_result = false;
667  Oid anyelement_type = InvalidOid;
668  Oid anyarray_type = InvalidOid;
669  Oid anyrange_type = InvalidOid;
670  int inargno;
671  int i;
672 
673  /* First pass: resolve polymorphic inputs, check for outputs */
674  inargno = 0;
675  for (i = 0; i < numargs; i++)
676  {
677  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
678 
679  switch (argtypes[i])
680  {
681  case ANYELEMENTOID:
682  case ANYNONARRAYOID:
683  case ANYENUMOID:
684  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
685  have_anyelement_result = true;
686  else
687  {
688  if (!OidIsValid(anyelement_type))
689  {
690  anyelement_type = get_call_expr_argtype(call_expr,
691  inargno);
692  if (!OidIsValid(anyelement_type))
693  return false;
694  }
695  argtypes[i] = anyelement_type;
696  }
697  break;
698  case ANYARRAYOID:
699  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
700  have_anyarray_result = true;
701  else
702  {
703  if (!OidIsValid(anyarray_type))
704  {
705  anyarray_type = get_call_expr_argtype(call_expr,
706  inargno);
707  if (!OidIsValid(anyarray_type))
708  return false;
709  }
710  argtypes[i] = anyarray_type;
711  }
712  break;
713  case ANYRANGEOID:
714  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
715  have_anyrange_result = true;
716  else
717  {
718  if (!OidIsValid(anyrange_type))
719  {
720  anyrange_type = get_call_expr_argtype(call_expr,
721  inargno);
722  if (!OidIsValid(anyrange_type))
723  return false;
724  }
725  argtypes[i] = anyrange_type;
726  }
727  break;
728  default:
729  break;
730  }
731  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
732  inargno++;
733  }
734 
735  /* Done? */
736  if (!have_anyelement_result && !have_anyarray_result &&
737  !have_anyrange_result)
738  return true;
739 
740  /* If no input polymorphics, parser messed up */
741  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
742  !OidIsValid(anyrange_type))
743  return false;
744 
745  /* If needed, deduce one polymorphic type from others */
746  if (have_anyelement_result && !OidIsValid(anyelement_type))
747  {
748  if (OidIsValid(anyarray_type))
749  anyelement_type = resolve_generic_type(ANYELEMENTOID,
750  anyarray_type,
751  ANYARRAYOID);
752  if (OidIsValid(anyrange_type))
753  {
754  Oid subtype = resolve_generic_type(ANYELEMENTOID,
755  anyrange_type,
756  ANYRANGEOID);
757 
758  /* check for inconsistent array and range results */
759  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
760  return false;
761  anyelement_type = subtype;
762  }
763  }
764 
765  if (have_anyarray_result && !OidIsValid(anyarray_type))
766  anyarray_type = resolve_generic_type(ANYARRAYOID,
767  anyelement_type,
768  ANYELEMENTOID);
769 
770  /*
771  * We can't deduce a range type from other polymorphic inputs, because
772  * there may be multiple range types for the same subtype.
773  */
774  if (have_anyrange_result && !OidIsValid(anyrange_type))
775  return false;
776 
777  /* XXX do we need to enforce ANYNONARRAY or ANYENUM here? I think not */
778 
779  /* And finally replace the output column types as needed */
780  for (i = 0; i < numargs; i++)
781  {
782  switch (argtypes[i])
783  {
784  case ANYELEMENTOID:
785  case ANYNONARRAYOID:
786  case ANYENUMOID:
787  argtypes[i] = anyelement_type;
788  break;
789  case ANYARRAYOID:
790  argtypes[i] = anyarray_type;
791  break;
792  case ANYRANGEOID:
793  argtypes[i] = anyrange_type;
794  break;
795  default:
796  break;
797  }
798  }
799 
800  return true;
801 }
Oid resolve_generic_type(Oid declared_type, Oid context_actual_type, Oid context_declared_type)
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1836
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:639
#define InvalidOid
Definition: postgres_ext.h:36
int i

◆ 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_items(), bt_page_items_bytea(), 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:48
unsigned int Oid
Definition: postgres_ext.h:31
Oid * attioparams
Definition: funcapi.h:45
signed int int32
Definition: c.h:347
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2052
TupleDesc tupdesc
Definition: funcapi.h:39
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2641
void * palloc0(Size size)
Definition: mcxt.c:980
void * palloc(Size size)
Definition: mcxt.c:949
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:42

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

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

1395 {
1396  Oid base_typeoid;
1397  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1398  TupleDesc tupdesc = NULL;
1399 
1400  /*
1401  * Build a suitable tupledesc representing the output rows. We
1402  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1403  * unlikely that legacy callers of this obsolete function would be
1404  * prepared to apply domain constraints.
1405  */
1406  if (functypclass == TYPEFUNC_COMPOSITE)
1407  {
1408  /* Composite data type, e.g. a table's row type */
1409  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1410 
1411  if (colaliases != NIL)
1412  {
1413  int natts = tupdesc->natts;
1414  int varattno;
1415 
1416  /* does the list length match the number of attributes? */
1417  if (list_length(colaliases) != natts)
1418  ereport(ERROR,
1419  (errcode(ERRCODE_DATATYPE_MISMATCH),
1420  errmsg("number of aliases does not match number of columns")));
1421 
1422  /* OK, use the aliases instead */
1423  for (varattno = 0; varattno < natts; varattno++)
1424  {
1425  char *label = strVal(list_nth(colaliases, varattno));
1426  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1427 
1428  if (label != NULL)
1429  namestrcpy(&(attr->attname), label);
1430  }
1431 
1432  /* The tuple type is now an anonymous record type */
1433  tupdesc->tdtypeid = RECORDOID;
1434  tupdesc->tdtypmod = -1;
1435  }
1436  }
1437  else if (functypclass == TYPEFUNC_SCALAR)
1438  {
1439  /* Base data type, i.e. scalar */
1440  char *attname;
1441 
1442  /* the alias list is required for base types */
1443  if (colaliases == NIL)
1444  ereport(ERROR,
1445  (errcode(ERRCODE_DATATYPE_MISMATCH),
1446  errmsg("no column alias was provided")));
1447 
1448  /* the alias list length must be 1 */
1449  if (list_length(colaliases) != 1)
1450  ereport(ERROR,
1451  (errcode(ERRCODE_DATATYPE_MISMATCH),
1452  errmsg("number of aliases does not match number of columns")));
1453 
1454  /* OK, get the column alias */
1455  attname = strVal(linitial(colaliases));
1456 
1457  tupdesc = CreateTemplateTupleDesc(1);
1458  TupleDescInitEntry(tupdesc,
1459  (AttrNumber) 1,
1460  attname,
1461  typeoid,
1462  -1,
1463  0);
1464  }
1465  else if (functypclass == TYPEFUNC_RECORD)
1466  {
1467  /* XXX can't support this because typmod wasn't passed in ... */
1468  ereport(ERROR,
1469  (errcode(ERRCODE_DATATYPE_MISMATCH),
1470  errmsg("could not determine row description for function returning record")));
1471  }
1472  else
1473  {
1474  /* crummy error message, but parser should have caught this */
1475  elog(ERROR, "function in FROM has unsupported return type");
1476  }
1477 
1478  return tupdesc;
1479 }
#define NIL
Definition: pg_list.h:65
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:44
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:250
#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:200
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:813
TypeFuncClass
Definition: funcapi.h:147
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
#define ereport(elevel, rest)
Definition: elog.h:141
static char * label
Definition: pg_basebackup.c:86
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:822
#define elog(elevel,...)
Definition: elog.h:228
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1686