PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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_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)
 
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 (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)
 
TupleTableSlotTupleDescGetSlot (TupleDesc tupdesc)
 
FuncCallContextinit_MultiFuncCall (PG_FUNCTION_ARGS)
 
FuncCallContextper_MultiFuncCall (PG_FUNCTION_ARGS)
 
void end_MultiFuncCall (PG_FUNCTION_ARGS, FuncCallContext *funcctx)
 

Macro Definition Documentation

#define HeapTupleGetDatum (   tuple)    HeapTupleHeaderGetDatum((tuple)->t_data)
#define SRF_RETURN_DONE (   _funcctx)
Value:
do { \
end_MultiFuncCall(fcinfo, _funcctx); \
rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
rsi->isDone = ExprEndResult; \
} while (0)
void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
Definition: funcapi.c:155
struct ReturnSetInfo ReturnSetInfo
#define PG_RETURN_NULL()
Definition: fmgr.h:305

Definition at line 309 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(), 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(), pg_ls_dir(), pg_ls_dir_files(), pg_prepared_xact(), pg_stat_get_backend_idset(), 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().

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

Definition at line 291 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(), 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(), pg_ls_dir(), pg_ls_dir_files(), pg_prepared_xact(), pg_stat_get_backend_idset(), 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().

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

Definition at line 300 of file funcapi.h.

Referenced by PLy_exec_function().

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

Definition at line 224 of file funcapi.h.

Typedef Documentation

Enumeration Type Documentation

Enumerator
TYPEFUNC_SCALAR 
TYPEFUNC_COMPOSITE 
TYPEFUNC_RECORD 
TYPEFUNC_OTHER 

Definition at line 150 of file funcapi.h.

151 {
152  TYPEFUNC_SCALAR, /* scalar result type */
153  TYPEFUNC_COMPOSITE, /* determinable rowtype result */
154  TYPEFUNC_RECORD, /* indeterminate rowtype result */
155  TYPEFUNC_OTHER /* bogus type, eg pseudotype */
156 } TypeFuncClass;
TypeFuncClass
Definition: funcapi.h:150

Function Documentation

TupleDesc BlessTupleDesc ( TupleDesc  tupdesc)

Definition at line 1032 of file execTuples.c.

References assign_record_type_typmod(), RECORDOID, tupleDesc::tdtypeid, and tupleDesc::tdtypmod.

Referenced by aclexplode(), brin_metapage_info(), each_worker(), each_worker_jsonb(), elements_worker(), elements_worker_jsonb(), exec_eval_datum(), ExecEvalWholeRowVar(), ExecInitExprRec(), ExecInitFunctionScan(), 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_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_timezone_abbrevs(), pg_timezone_names(), pg_visibility_map_summary(), pg_visibility_tupdesc(), pg_walfile_name_offset(), pgstathashindex(), plpgsql_exec_get_datum_type(), plpgsql_exec_get_datum_type_info(), PLy_output_record_funcs(), populate_recordset_worker(), setup_firstcall(), ssl_extension_info(), ts_setup_firstcall(), tsvector_unnest(), TupleDescGetAttInMetadata(), TupleDescGetSlot(), and TupleQueueHandleControlMessage().

1033 {
1034  if (tupdesc->tdtypeid == RECORDOID &&
1035  tupdesc->tdtypmod < 0)
1036  assign_record_type_typmod(tupdesc);
1037 
1038  return tupdesc; /* just for notational convenience */
1039 }
Oid tdtypeid
Definition: tupdesc.h:74
int32 tdtypmod
Definition: tupdesc.h:75
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1309
#define RECORDOID
Definition: pg_type.h:680
TupleDesc build_function_result_tupdesc_d ( Datum  proallargtypes,
Datum  proargmodes,
Datum  proargnames 
)

Definition at line 1171 of file funcapi.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, CHAROID, CreateTemplateTupleDesc(), DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, i, NULL, OIDOID, palloc(), PointerGetDatum, PROARGMODE_IN, PROARGMODE_INOUT, PROARGMODE_OUT, PROARGMODE_TABLE, PROARGMODE_VARIADIC, psprintf(), TextDatumGetCString, TEXTOID, and TupleDescInitEntry().

Referenced by build_function_result_tupdesc_t(), and ProcedureCreate().

1174 {
1175  TupleDesc desc;
1176  ArrayType *arr;
1177  int numargs;
1178  Oid *argtypes;
1179  char *argmodes;
1180  Datum *argnames = NULL;
1181  Oid *outargtypes;
1182  char **outargnames;
1183  int numoutargs;
1184  int nargnames;
1185  int i;
1186 
1187  /* Can't have output args if columns are null */
1188  if (proallargtypes == PointerGetDatum(NULL) ||
1189  proargmodes == PointerGetDatum(NULL))
1190  return NULL;
1191 
1192  /*
1193  * We expect the arrays to be 1-D arrays of the right types; verify that.
1194  * For the OID and char arrays, we don't need to use deconstruct_array()
1195  * since the array data is just going to look like a C array of values.
1196  */
1197  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1198  numargs = ARR_DIMS(arr)[0];
1199  if (ARR_NDIM(arr) != 1 ||
1200  numargs < 0 ||
1201  ARR_HASNULL(arr) ||
1202  ARR_ELEMTYPE(arr) != OIDOID)
1203  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1204  argtypes = (Oid *) ARR_DATA_PTR(arr);
1205  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1206  if (ARR_NDIM(arr) != 1 ||
1207  ARR_DIMS(arr)[0] != numargs ||
1208  ARR_HASNULL(arr) ||
1209  ARR_ELEMTYPE(arr) != CHAROID)
1210  elog(ERROR, "proargmodes is not a 1-D char array");
1211  argmodes = (char *) ARR_DATA_PTR(arr);
1212  if (proargnames != PointerGetDatum(NULL))
1213  {
1214  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1215  if (ARR_NDIM(arr) != 1 ||
1216  ARR_DIMS(arr)[0] != numargs ||
1217  ARR_HASNULL(arr) ||
1218  ARR_ELEMTYPE(arr) != TEXTOID)
1219  elog(ERROR, "proargnames is not a 1-D text array");
1220  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1221  &argnames, NULL, &nargnames);
1222  Assert(nargnames == numargs);
1223  }
1224 
1225  /* zero elements probably shouldn't happen, but handle it gracefully */
1226  if (numargs <= 0)
1227  return NULL;
1228 
1229  /* extract output-argument types and names */
1230  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1231  outargnames = (char **) palloc(numargs * sizeof(char *));
1232  numoutargs = 0;
1233  for (i = 0; i < numargs; i++)
1234  {
1235  char *pname;
1236 
1237  if (argmodes[i] == PROARGMODE_IN ||
1238  argmodes[i] == PROARGMODE_VARIADIC)
1239  continue;
1240  Assert(argmodes[i] == PROARGMODE_OUT ||
1241  argmodes[i] == PROARGMODE_INOUT ||
1242  argmodes[i] == PROARGMODE_TABLE);
1243  outargtypes[numoutargs] = argtypes[i];
1244  if (argnames)
1245  pname = TextDatumGetCString(argnames[i]);
1246  else
1247  pname = NULL;
1248  if (pname == NULL || pname[0] == '\0')
1249  {
1250  /* Parameter is not named, so gin up a column name */
1251  pname = psprintf("column%d", numoutargs + 1);
1252  }
1253  outargnames[numoutargs] = pname;
1254  numoutargs++;
1255  }
1256 
1257  /*
1258  * If there is no output argument, or only one, the function does not
1259  * return tuples.
1260  */
1261  if (numoutargs < 2)
1262  return NULL;
1263 
1264  desc = CreateTemplateTupleDesc(numoutargs, false);
1265  for (i = 0; i < numoutargs; i++)
1266  {
1267  TupleDescInitEntry(desc, i + 1,
1268  outargnames[i],
1269  outargtypes[i],
1270  -1,
1271  0);
1272  }
1273 
1274  return desc;
1275 }
#define OIDOID
Definition: pg_type.h:328
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5507
unsigned int Oid
Definition: postgres_ext.h:31
#define PROARGMODE_INOUT
Definition: pg_proc.h:5506
#define PROARGMODE_OUT
Definition: pg_proc.h:5505
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define PROARGMODE_TABLE
Definition: pg_proc.h:5508
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:462
#define PROARGMODE_IN
Definition: pg_proc.h:5504
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define CHAROID
Definition: pg_type.h:296
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1123 of file funcapi.c.

References Anum_pg_proc_proallargtypes, Anum_pg_proc_proargmodes, Anum_pg_proc_proargnames, Assert, build_function_result_tupdesc_d(), GETSTRUCT, heap_attisnull(), NULL, PointerGetDatum, PROCOID, RECORDOID, and SysCacheGetAttr().

Referenced by internal_get_result_type(), and ProcedureCreate().

1124 {
1125  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1126  Datum proallargtypes;
1127  Datum proargmodes;
1128  Datum proargnames;
1129  bool isnull;
1130 
1131  /* Return NULL if the function isn't declared to return RECORD */
1132  if (procform->prorettype != RECORDOID)
1133  return NULL;
1134 
1135  /* If there are no OUT parameters, return NULL */
1136  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
1138  return NULL;
1139 
1140  /* Get the data out of the tuple */
1141  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1143  &isnull);
1144  Assert(!isnull);
1145  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1147  &isnull);
1148  Assert(!isnull);
1149  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1151  &isnull);
1152  if (isnull)
1153  proargnames = PointerGetDatum(NULL); /* just to be sure */
1154 
1155  return build_function_result_tupdesc_d(proallargtypes,
1156  proargmodes,
1157  proargnames);
1158 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define PointerGetDatum(X)
Definition: postgres.h:562
TupleDesc build_function_result_tupdesc_d(Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1171
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:296
#define Anum_pg_proc_proargmodes
Definition: pg_proc.h:111
#define RECORDOID
Definition: pg_type.h:680
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
uintptr_t Datum
Definition: postgres.h:372
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define Anum_pg_proc_proallargtypes
Definition: pg_proc.h:110
HeapTuple BuildTupleFromCStrings ( AttInMetadata attinmeta,
char **  values 
)

Definition at line 1118 of file execTuples.c.

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, AttInMetadata::atttypmods, heap_form_tuple(), i, InputFunctionCall(), tupleDesc::natts, NULL, 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(), materializeQueryResult(), materializeResult(), pg_config(), pg_get_keywords(), pg_get_multixact_members(), pg_logdir_ls(), 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().

1119 {
1120  TupleDesc tupdesc = attinmeta->tupdesc;
1121  int natts = tupdesc->natts;
1122  Datum *dvalues;
1123  bool *nulls;
1124  int i;
1125  HeapTuple tuple;
1126 
1127  dvalues = (Datum *) palloc(natts * sizeof(Datum));
1128  nulls = (bool *) palloc(natts * sizeof(bool));
1129 
1130  /* Call the "in" function for each non-dropped attribute */
1131  for (i = 0; i < natts; i++)
1132  {
1133  if (!TupleDescAttr(tupdesc, i)->attisdropped)
1134  {
1135  /* Non-dropped attributes */
1136  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
1137  values[i],
1138  attinmeta->attioparams[i],
1139  attinmeta->atttypmods[i]);
1140  if (values[i] != NULL)
1141  nulls[i] = false;
1142  else
1143  nulls[i] = true;
1144  }
1145  else
1146  {
1147  /* Handle dropped attributes by setting to NULL */
1148  dvalues[i] = (Datum) 0;
1149  nulls[i] = true;
1150  }
1151  }
1152 
1153  /*
1154  * Form a tuple
1155  */
1156  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
1157 
1158  /*
1159  * Release locally palloc'd space. XXX would probably be good to pfree
1160  * values of pass-by-reference datums, as well.
1161  */
1162  pfree(dvalues);
1163  pfree(nulls);
1164 
1165  return tuple;
1166 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int32 * atttypmods
Definition: funcapi.h:47
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
Oid * attioparams
Definition: funcapi.h:44
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:950
TupleDesc tupdesc
Definition: funcapi.h:38
uintptr_t Datum
Definition: postgres.h:372
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1623
#define NULL
Definition: c.h:229
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void * palloc(Size size)
Definition: mcxt.c:849
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41
void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 155 of file funcapi.c.

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

156 {
157  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
158 
159  /* Deregister the shutdown callback */
162  PointerGetDatum(fcinfo->flinfo));
163 
164  /* But use it to do the real work */
165  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
166 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:763
#define PointerGetDatum(X)
Definition: postgres.h:562
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:173
ExprContext * econtext
Definition: execnodes.h:266
TypeFuncClass get_call_result_type ( FunctionCallInfo  fcinfo,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 211 of file funcapi.c.

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

Referenced by brin_metapage_info(), brin_page_items(), bt_metap(), bt_page_items(), bt_page_items_bytea(), bt_page_stats(), build_pgstattuple_type(), crosstab(), each_worker(), each_worker_jsonb(), 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(), 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_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_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(), populate_record_worker(), populate_recordset_worker(), setup_firstcall(), ssl_extension_info(), and storeRow().

214 {
215  return internal_get_result_type(fcinfo->flinfo->fn_oid,
216  fcinfo->flinfo->fn_expr,
217  (ReturnSetInfo *) fcinfo->resultinfo,
218  resultTypeId,
219  resultTupleDesc);
220 }
FmgrInfo * flinfo
Definition: fmgr.h:79
fmNodePtr resultinfo
Definition: fmgr.h:81
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:289
TypeFuncClass get_expr_result_type ( Node expr,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 227 of file funcapi.c.

References exprType(), get_opcode(), get_type_func_class(), internal_get_result_type(), IsA, lookup_rowtype_tupdesc_copy(), NULL, result, and TYPEFUNC_COMPOSITE.

Referenced by addRangeTableEntryForFunction(), ExecInitFunctionScan(), expandRecordVariable(), ExpandRowReference(), expandRTE(), get_name_for_var_field(), get_rte_attribute_is_dropped(), get_rte_attribute_type(), init_sexpr(), and ParseComplexProjection().

230 {
232 
233  if (expr && IsA(expr, FuncExpr))
234  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
235  expr,
236  NULL,
237  resultTypeId,
238  resultTupleDesc);
239  else if (expr && IsA(expr, OpExpr))
240  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
241  expr,
242  NULL,
243  resultTypeId,
244  resultTupleDesc);
245  else
246  {
247  /* handle as a generic expression; no chance to resolve RECORD */
248  Oid typid = exprType(expr);
249 
250  if (resultTypeId)
251  *resultTypeId = typid;
252  if (resultTupleDesc)
253  *resultTupleDesc = NULL;
254  result = get_type_func_class(typid);
255  if (result == TYPEFUNC_COMPOSITE && resultTupleDesc)
256  *resultTupleDesc = lookup_rowtype_tupdesc_copy(typid, -1);
257  }
258 
259  return result;
260 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
return result
Definition: formatting.c:1633
unsigned int Oid
Definition: postgres_ext.h:31
static TypeFuncClass get_type_func_class(Oid typid)
Definition: funcapi.c:750
TypeFuncClass
Definition: funcapi.h:150
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
#define NULL
Definition: c.h:229
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:289
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1292
int get_func_arg_info ( HeapTuple  procTup,
Oid **  p_argtypes,
char ***  p_argnames,
char **  p_argmodes 
)

Definition at line 793 of file funcapi.c.

References Anum_pg_proc_proallargtypes, Anum_pg_proc_proargmodes, Anum_pg_proc_proargnames, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, CHAROID, DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, GETSTRUCT, i, NULL, OIDOID, palloc(), PROCOID, SysCacheGetAttr(), TextDatumGetCString, and TEXTOID.

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

795 {
796  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
797  Datum proallargtypes;
798  Datum proargmodes;
799  Datum proargnames;
800  bool isNull;
801  ArrayType *arr;
802  int numargs;
803  Datum *elems;
804  int nelems;
805  int i;
806 
807  /* First discover the total number of parameters and get their types */
808  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
810  &isNull);
811  if (!isNull)
812  {
813  /*
814  * We expect the arrays to be 1-D arrays of the right types; verify
815  * that. For the OID and char arrays, we don't need to use
816  * deconstruct_array() since the array data is just going to look like
817  * a C array of values.
818  */
819  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
820  numargs = ARR_DIMS(arr)[0];
821  if (ARR_NDIM(arr) != 1 ||
822  numargs < 0 ||
823  ARR_HASNULL(arr) ||
824  ARR_ELEMTYPE(arr) != OIDOID)
825  elog(ERROR, "proallargtypes is not a 1-D Oid array");
826  Assert(numargs >= procStruct->pronargs);
827  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
828  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
829  numargs * sizeof(Oid));
830  }
831  else
832  {
833  /* If no proallargtypes, use proargtypes */
834  numargs = procStruct->proargtypes.dim1;
835  Assert(numargs == procStruct->pronargs);
836  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
837  memcpy(*p_argtypes, procStruct->proargtypes.values,
838  numargs * sizeof(Oid));
839  }
840 
841  /* Get argument names, if available */
842  proargnames = SysCacheGetAttr(PROCOID, procTup,
844  &isNull);
845  if (isNull)
846  *p_argnames = NULL;
847  else
848  {
850  TEXTOID, -1, false, 'i',
851  &elems, NULL, &nelems);
852  if (nelems != numargs) /* should not happen */
853  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
854  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
855  for (i = 0; i < numargs; i++)
856  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
857  }
858 
859  /* Get argument modes, if available */
860  proargmodes = SysCacheGetAttr(PROCOID, procTup,
862  &isNull);
863  if (isNull)
864  *p_argmodes = NULL;
865  else
866  {
867  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
868  if (ARR_NDIM(arr) != 1 ||
869  ARR_DIMS(arr)[0] != numargs ||
870  ARR_HASNULL(arr) ||
871  ARR_ELEMTYPE(arr) != CHAROID)
872  elog(ERROR, "proargmodes is not a 1-D char array");
873  *p_argmodes = (char *) palloc(numargs * sizeof(char));
874  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
875  numargs * sizeof(char));
876  }
877 
878  return numargs;
879 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define OIDOID
Definition: pg_type.h:328
#define TEXTOID
Definition: pg_type.h:324
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define Anum_pg_proc_proargmodes
Definition: pg_proc.h:111
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
#define CHAROID
Definition: pg_type.h:296
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
#define Anum_pg_proc_proallargtypes
Definition: pg_proc.h:110
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
int get_func_input_arg_names ( Datum  proargnames,
Datum  proargmodes,
char ***  arg_names 
)

Definition at line 935 of file funcapi.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, CHAROID, DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, i, NULL, palloc(), PointerGetDatum, PROARGMODE_IN, PROARGMODE_INOUT, PROARGMODE_VARIADIC, TextDatumGetCString, and TEXTOID.

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

937 {
938  ArrayType *arr;
939  int numargs;
940  Datum *argnames;
941  char *argmodes;
942  char **inargnames;
943  int numinargs;
944  int i;
945 
946  /* Do nothing if null proargnames */
947  if (proargnames == PointerGetDatum(NULL))
948  {
949  *arg_names = NULL;
950  return 0;
951  }
952 
953  /*
954  * We expect the arrays to be 1-D arrays of the right types; verify that.
955  * For proargmodes, we don't need to use deconstruct_array() since the
956  * array data is just going to look like a C array of values.
957  */
958  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
959  if (ARR_NDIM(arr) != 1 ||
960  ARR_HASNULL(arr) ||
961  ARR_ELEMTYPE(arr) != TEXTOID)
962  elog(ERROR, "proargnames is not a 1-D text array");
963  deconstruct_array(arr, TEXTOID, -1, false, 'i',
964  &argnames, NULL, &numargs);
965  if (proargmodes != PointerGetDatum(NULL))
966  {
967  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
968  if (ARR_NDIM(arr) != 1 ||
969  ARR_DIMS(arr)[0] != numargs ||
970  ARR_HASNULL(arr) ||
971  ARR_ELEMTYPE(arr) != CHAROID)
972  elog(ERROR, "proargmodes is not a 1-D char array");
973  argmodes = (char *) ARR_DATA_PTR(arr);
974  }
975  else
976  argmodes = NULL;
977 
978  /* zero elements probably shouldn't happen, but handle it gracefully */
979  if (numargs <= 0)
980  {
981  *arg_names = NULL;
982  return 0;
983  }
984 
985  /* extract input-argument names */
986  inargnames = (char **) palloc(numargs * sizeof(char *));
987  numinargs = 0;
988  for (i = 0; i < numargs; i++)
989  {
990  if (argmodes == NULL ||
991  argmodes[i] == PROARGMODE_IN ||
992  argmodes[i] == PROARGMODE_INOUT ||
993  argmodes[i] == PROARGMODE_VARIADIC)
994  {
995  char *pname = TextDatumGetCString(argnames[i]);
996 
997  if (pname[0] != '\0')
998  inargnames[numinargs] = pname;
999  else
1000  inargnames[numinargs] = NULL;
1001  numinargs++;
1002  }
1003  }
1004 
1005  *arg_names = inargnames;
1006  return numinargs;
1007 }
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5507
#define PROARGMODE_INOUT
Definition: pg_proc.h:5506
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
#define PROARGMODE_IN
Definition: pg_proc.h:5504
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define CHAROID
Definition: pg_type.h:296
#define NULL
Definition: c.h:229
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
char* get_func_result_name ( Oid  functionId)

Definition at line 1020 of file funcapi.c.

References Anum_pg_proc_proargmodes, Anum_pg_proc_proargnames, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, CHAROID, DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, heap_attisnull(), HeapTupleIsValid, i, NULL, ObjectIdGetDatum, PROARGMODE_IN, PROARGMODE_INOUT, PROARGMODE_OUT, PROARGMODE_TABLE, PROARGMODE_VARIADIC, PROCOID, ReleaseSysCache(), result, SearchSysCache1, SysCacheGetAttr(), TextDatumGetCString, and TEXTOID.

Referenced by chooseScalarFunctionAlias().

1021 {
1022  char *result;
1023  HeapTuple procTuple;
1024  Datum proargmodes;
1025  Datum proargnames;
1026  bool isnull;
1027  ArrayType *arr;
1028  int numargs;
1029  char *argmodes;
1030  Datum *argnames;
1031  int numoutargs;
1032  int nargnames;
1033  int i;
1034 
1035  /* First fetch the function's pg_proc row */
1036  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1037  if (!HeapTupleIsValid(procTuple))
1038  elog(ERROR, "cache lookup failed for function %u", functionId);
1039 
1040  /* If there are no named OUT parameters, return NULL */
1041  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
1043  result = NULL;
1044  else
1045  {
1046  /* Get the data out of the tuple */
1047  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1049  &isnull);
1050  Assert(!isnull);
1051  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1053  &isnull);
1054  Assert(!isnull);
1055 
1056  /*
1057  * We expect the arrays to be 1-D arrays of the right types; verify
1058  * that. For the char array, we don't need to use deconstruct_array()
1059  * since the array data is just going to look like a C array of
1060  * values.
1061  */
1062  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1063  numargs = ARR_DIMS(arr)[0];
1064  if (ARR_NDIM(arr) != 1 ||
1065  numargs < 0 ||
1066  ARR_HASNULL(arr) ||
1067  ARR_ELEMTYPE(arr) != CHAROID)
1068  elog(ERROR, "proargmodes is not a 1-D char array");
1069  argmodes = (char *) ARR_DATA_PTR(arr);
1070  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1071  if (ARR_NDIM(arr) != 1 ||
1072  ARR_DIMS(arr)[0] != numargs ||
1073  ARR_HASNULL(arr) ||
1074  ARR_ELEMTYPE(arr) != TEXTOID)
1075  elog(ERROR, "proargnames is not a 1-D text array");
1076  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1077  &argnames, NULL, &nargnames);
1078  Assert(nargnames == numargs);
1079 
1080  /* scan for output argument(s) */
1081  result = NULL;
1082  numoutargs = 0;
1083  for (i = 0; i < numargs; i++)
1084  {
1085  if (argmodes[i] == PROARGMODE_IN ||
1086  argmodes[i] == PROARGMODE_VARIADIC)
1087  continue;
1088  Assert(argmodes[i] == PROARGMODE_OUT ||
1089  argmodes[i] == PROARGMODE_INOUT ||
1090  argmodes[i] == PROARGMODE_TABLE);
1091  if (++numoutargs > 1)
1092  {
1093  /* multiple out args, so forget it */
1094  result = NULL;
1095  break;
1096  }
1097  result = TextDatumGetCString(argnames[i]);
1098  if (result == NULL || result[0] == '\0')
1099  {
1100  /* Parameter is not named, so forget it */
1101  result = NULL;
1102  break;
1103  }
1104  }
1105  }
1106 
1107  ReleaseSysCache(procTuple);
1108 
1109  return result;
1110 }
#define TEXTOID
Definition: pg_type.h:324
return result
Definition: formatting.c:1633
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5507
#define PROARGMODE_INOUT
Definition: pg_proc.h:5506
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define PROARGMODE_OUT
Definition: pg_proc.h:5505
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define PROARGMODE_TABLE
Definition: pg_proc.h:5508
#define ARR_DATA_PTR(a)
Definition: array.h:303
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:296
#define ARR_HASNULL(a)
Definition: array.h:272
#define Anum_pg_proc_proargmodes
Definition: pg_proc.h:111
#define PROARGMODE_IN
Definition: pg_proc.h:5504
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
#define CHAROID
Definition: pg_type.h:296
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define ARR_NDIM(a)
Definition: array.h:271
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
TypeFuncClass get_func_result_type ( Oid  functionId,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)

Definition at line 269 of file funcapi.c.

References internal_get_result_type(), and NULL.

Referenced by check_sql_fn_retval(), and inline_set_returning_function().

272 {
273  return internal_get_result_type(functionId,
274  NULL,
275  NULL,
276  resultTypeId,
277  resultTupleDesc);
278 }
#define NULL
Definition: c.h:229
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:289
int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 887 of file funcapi.c.

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

Referenced by print_function_trftypes().

889 {
890  Datum protrftypes;
891  ArrayType *arr;
892  int nelems;
893  bool isNull;
894 
895  protrftypes = SysCacheGetAttr(PROCOID, procTup,
897  &isNull);
898  if (!isNull)
899  {
900  /*
901  * We expect the arrays to be 1-D arrays of the right types; verify
902  * that. For the OID and char arrays, we don't need to use
903  * deconstruct_array() since the array data is just going to look like
904  * a C array of values.
905  */
906  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
907  nelems = ARR_DIMS(arr)[0];
908  if (ARR_NDIM(arr) != 1 ||
909  nelems < 0 ||
910  ARR_HASNULL(arr) ||
911  ARR_ELEMTYPE(arr) != OIDOID)
912  elog(ERROR, "protrftypes is not a 1-D Oid array");
913  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
914  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
915  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
916  nelems * sizeof(Oid));
917 
918  return nelems;
919  }
920  else
921  return 0;
922 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define OIDOID
Definition: pg_type.h:328
#define Anum_pg_proc_protrftypes
Definition: pg_proc.h:114
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define ARR_DATA_PTR(a)
Definition: array.h:303
#define ARR_HASNULL(a)
Definition: array.h:272
uintptr_t Datum
Definition: postgres.h:372
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1279
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define Assert(condition)
Definition: c.h:676
#define ARR_NDIM(a)
Definition: array.h:271
void * palloc(Size size)
Definition: mcxt.c:849
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
Datum HeapTupleHeaderGetDatum ( HeapTupleHeader  tuple)

Definition at line 1204 of file execTuples.c.

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

Referenced by populate_composite(), and TQRemapRecord().

1205 {
1206  Datum result;
1207  TupleDesc tupDesc;
1208 
1209  /* No work if there are no external TOAST pointers in the tuple */
1210  if (!HeapTupleHeaderHasExternal(tuple))
1211  return PointerGetDatum(tuple);
1212 
1213  /* Use the type data saved by heap_form_tuple to look up the rowtype */
1215  HeapTupleHeaderGetTypMod(tuple));
1216 
1217  /* And do the flattening */
1218  result = toast_flatten_tuple_to_datum(tuple,
1220  tupDesc);
1221 
1222  ReleaseTupleDesc(tupDesc);
1223 
1224  return result;
1225 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1258
#define PointerGetDatum(X)
Definition: postgres.h:562
return result
Definition: formatting.c:1633
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: tuptoaster.c:1187
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:540
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:109
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 52 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, NULL, PointerGetDatum, RegisterExprContextCallback(), shutdown_MultiFuncCall(), FuncCallContext::slot, FuncCallContext::tuple_desc, and FuncCallContext::user_fctx.

53 {
54  FuncCallContext *retval;
55 
56  /*
57  * Bail if we're called in the wrong context
58  */
59  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
60  ereport(ERROR,
61  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
62  errmsg("set-valued function called in context that cannot accept a set")));
63 
64  if (fcinfo->flinfo->fn_extra == NULL)
65  {
66  /*
67  * First call
68  */
69  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
70  MemoryContext multi_call_ctx;
71 
72  /*
73  * Create a suitably long-lived context to hold cross-call data
74  */
75  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
76  "SRF multi-call context",
78 
79  /*
80  * Allocate suitably long-lived space and zero it
81  */
82  retval = (FuncCallContext *)
83  MemoryContextAllocZero(multi_call_ctx,
84  sizeof(FuncCallContext));
85 
86  /*
87  * initialize the elements
88  */
89  retval->call_cntr = 0;
90  retval->max_calls = 0;
91  retval->slot = NULL;
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:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
TupleTableSlot * slot
Definition: funcapi.h:82
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
int errcode(int sqlerrcode)
Definition: elog.c:575
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:173
TupleDesc tuple_desc
Definition: funcapi.h:120
#define ERROR
Definition: elog.h:43
AttInMetadata * attinmeta
Definition: funcapi.h:99
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:742
#define NULL
Definition: c.h:229
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:737
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
ExprContext * econtext
Definition: execnodes.h:266
void * user_fctx
Definition: funcapi.h:90
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
uint64 max_calls
Definition: funcapi.h:74
FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 128 of file funcapi.c.

References ExecClearTuple(), NULL, and FuncCallContext::slot.

129 {
130  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
131 
132  /*
133  * Clear the TupleTableSlot, if present. This is for safety's sake: the
134  * Slot will be in a long-lived context (it better be, if the
135  * FuncCallContext is pointing to it), but in most usage patterns the
136  * tuples stored in it will be in the function's per-tuple context. So at
137  * the beginning of each call, the Slot will hold a dangling pointer to an
138  * already-recycled tuple. We clear it out here.
139  *
140  * Note: use of retval->slot is obsolete as of 8.0, and we expect that it
141  * will always be NULL. This is just here for backwards compatibility in
142  * case someone creates a slot anyway.
143  */
144  if (retval->slot != NULL)
145  ExecClearTuple(retval->slot);
146 
147  return retval;
148 }
TupleTableSlot * slot
Definition: funcapi.h:82
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
#define NULL
Definition: c.h:229
TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1288 of file funcapi.c.

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

1289 {
1290  RangeVar *relvar;
1291  Relation rel;
1292  TupleDesc tupdesc;
1293  List *relname_list;
1294 
1295  /* Open relation and copy the tuple description */
1296  relname_list = stringToQualifiedNameList(relname);
1297  relvar = makeRangeVarFromNameList(relname_list);
1298  rel = relation_openrv(relvar, AccessShareLock);
1299  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1301 
1302  return tupdesc;
1303 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:100
#define RelationGetDescr(relation)
Definition: rel.h:428
#define AccessShareLock
Definition: lockdefs.h:36
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
Definition: pg_list.h:45
bool resolve_polymorphic_argtypes ( int  numargs,
Oid argtypes,
char *  argmodes,
Node call_expr 
)

Definition at line 599 of file funcapi.c.

References ANYARRAYOID, ANYELEMENTOID, ANYENUMOID, ANYNONARRAYOID, ANYRANGEOID, get_call_expr_argtype(), i, InvalidOid, OidIsValid, PROARGMODE_IN, PROARGMODE_OUT, PROARGMODE_TABLE, and resolve_generic_type().

Referenced by plpgsql_resolve_polymorphic_argtypes().

601 {
602  bool have_anyelement_result = false;
603  bool have_anyarray_result = false;
604  bool have_anyrange_result = false;
605  Oid anyelement_type = InvalidOid;
606  Oid anyarray_type = InvalidOid;
607  Oid anyrange_type = InvalidOid;
608  int inargno;
609  int i;
610 
611  /* First pass: resolve polymorphic inputs, check for outputs */
612  inargno = 0;
613  for (i = 0; i < numargs; i++)
614  {
615  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
616 
617  switch (argtypes[i])
618  {
619  case ANYELEMENTOID:
620  case ANYNONARRAYOID:
621  case ANYENUMOID:
622  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
623  have_anyelement_result = true;
624  else
625  {
626  if (!OidIsValid(anyelement_type))
627  {
628  anyelement_type = get_call_expr_argtype(call_expr,
629  inargno);
630  if (!OidIsValid(anyelement_type))
631  return false;
632  }
633  argtypes[i] = anyelement_type;
634  }
635  break;
636  case ANYARRAYOID:
637  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
638  have_anyarray_result = true;
639  else
640  {
641  if (!OidIsValid(anyarray_type))
642  {
643  anyarray_type = get_call_expr_argtype(call_expr,
644  inargno);
645  if (!OidIsValid(anyarray_type))
646  return false;
647  }
648  argtypes[i] = anyarray_type;
649  }
650  break;
651  case ANYRANGEOID:
652  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
653  have_anyrange_result = true;
654  else
655  {
656  if (!OidIsValid(anyrange_type))
657  {
658  anyrange_type = get_call_expr_argtype(call_expr,
659  inargno);
660  if (!OidIsValid(anyrange_type))
661  return false;
662  }
663  argtypes[i] = anyrange_type;
664  }
665  break;
666  default:
667  break;
668  }
669  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
670  inargno++;
671  }
672 
673  /* Done? */
674  if (!have_anyelement_result && !have_anyarray_result &&
675  !have_anyrange_result)
676  return true;
677 
678  /* If no input polymorphics, parser messed up */
679  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
680  !OidIsValid(anyrange_type))
681  return false;
682 
683  /* If needed, deduce one polymorphic type from others */
684  if (have_anyelement_result && !OidIsValid(anyelement_type))
685  {
686  if (OidIsValid(anyarray_type))
687  anyelement_type = resolve_generic_type(ANYELEMENTOID,
688  anyarray_type,
689  ANYARRAYOID);
690  if (OidIsValid(anyrange_type))
691  {
693  anyrange_type,
694  ANYRANGEOID);
695 
696  /* check for inconsistent array and range results */
697  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
698  return false;
699  anyelement_type = subtype;
700  }
701  }
702 
703  if (have_anyarray_result && !OidIsValid(anyarray_type))
704  anyarray_type = resolve_generic_type(ANYARRAYOID,
705  anyelement_type,
706  ANYELEMENTOID);
707 
708  /*
709  * We can't deduce a range type from other polymorphic inputs, because
710  * there may be multiple range types for the same subtype.
711  */
712  if (have_anyrange_result && !OidIsValid(anyrange_type))
713  return false;
714 
715  /* XXX do we need to enforce ANYNONARRAY or ANYENUM here? I think not */
716 
717  /* And finally replace the output column types as needed */
718  for (i = 0; i < numargs; i++)
719  {
720  switch (argtypes[i])
721  {
722  case ANYELEMENTOID:
723  case ANYNONARRAYOID:
724  case ANYENUMOID:
725  argtypes[i] = anyelement_type;
726  break;
727  case ANYARRAYOID:
728  argtypes[i] = anyarray_type;
729  break;
730  case ANYRANGEOID:
731  argtypes[i] = anyrange_type;
732  break;
733  default:
734  break;
735  }
736  }
737 
738  return true;
739 }
#define ANYNONARRAYOID
Definition: pg_type.h:704
Oid resolve_generic_type(Oid declared_type, Oid context_actual_type, Oid context_declared_type)
#define ANYELEMENTOID
Definition: pg_type.h:702
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1928
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define PROARGMODE_OUT
Definition: pg_proc.h:5505
#define ANYRANGEOID
Definition: pg_type.h:714
#define PROARGMODE_TABLE
Definition: pg_proc.h:5508
#define ANYENUMOID
Definition: pg_type.h:706
#define PROARGMODE_IN
Definition: pg_proc.h:5504
#define ANYARRAYOID
Definition: pg_type.h:688
#define InvalidOid
Definition: postgres_ext.h:36
int i
AttInMetadata* TupleDescGetAttInMetadata ( TupleDesc  tupdesc)

Definition at line 1069 of file execTuples.c.

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, AttInMetadata::atttypmods, BlessTupleDesc(), fmgr_info(), getTypeInputInfo(), i, tupleDesc::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(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), hash_page_items(), libpqrcv_processTuples(), materializeQueryResult(), materializeResult(), pg_config(), pg_get_keywords(), pg_get_multixact_members(), pg_logdir_ls(), pgp_armor_headers(), pgrowlocks(), pgstatindex_impl(), pltcl_build_tuple_result(), pltcl_func_handler(), pltcl_init_tuple_store(), postgresAcquireSampleRowsFunc(), postgresBeginDirectModify(), postgresBeginForeignModify(), postgresBeginForeignScan(), prs_setup_firstcall(), show_all_settings(), storeRow(), ts_setup_firstcall(), tt_setup_firstcall(), and xpath_table().

1070 {
1071  int natts = tupdesc->natts;
1072  int i;
1073  Oid atttypeid;
1074  Oid attinfuncid;
1075  FmgrInfo *attinfuncinfo;
1076  Oid *attioparams;
1077  int32 *atttypmods;
1078  AttInMetadata *attinmeta;
1079 
1080  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
1081 
1082  /* "Bless" the tupledesc so that we can make rowtype datums with it */
1083  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
1084 
1085  /*
1086  * Gather info needed later to call the "in" function for each attribute
1087  */
1088  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
1089  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
1090  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
1091 
1092  for (i = 0; i < natts; i++)
1093  {
1094  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1095 
1096  /* Ignore dropped attributes */
1097  if (!att->attisdropped)
1098  {
1099  atttypeid = att->atttypid;
1100  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
1101  fmgr_info(attinfuncid, &attinfuncinfo[i]);
1102  atttypmods[i] = att->atttypmod;
1103  }
1104  }
1105  attinmeta->attinfuncs = attinfuncinfo;
1106  attinmeta->attioparams = attioparams;
1107  attinmeta->atttypmods = atttypmods;
1108 
1109  return attinmeta;
1110 }
Definition: fmgr.h:56
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int32 * atttypmods
Definition: funcapi.h:47
unsigned int Oid
Definition: postgres_ext.h:31
Oid * attioparams
Definition: funcapi.h:44
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:256
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
TupleDesc tupdesc
Definition: funcapi.h:38
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2599
void * palloc0(Size size)
Definition: mcxt.c:878
void * palloc(Size size)
Definition: mcxt.c:849
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41
TupleTableSlot* TupleDescGetSlot ( TupleDesc  tupdesc)

Definition at line 1049 of file execTuples.c.

References BlessTupleDesc(), and MakeSingleTupleTableSlot().

1050 {
1051  TupleTableSlot *slot;
1052 
1053  /* The useful work is here */
1054  BlessTupleDesc(tupdesc);
1055 
1056  /* Make a standalone slot */
1057  slot = MakeSingleTupleTableSlot(tupdesc);
1058 
1059  /* Return the slot */
1060  return slot;
1061 }
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1321 of file funcapi.c.

References CreateTemplateTupleDesc(), elog, ereport, errcode(), errmsg(), ERROR, get_type_func_class(), label, linitial, list_length(), list_nth(), lookup_rowtype_tupdesc_copy(), namestrcpy(), tupleDesc::natts, NIL, NULL, RECORDOID, strVal, tupleDesc::tdtypeid, tupleDesc::tdtypmod, TupleDescAttr, TupleDescInitEntry(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

1322 {
1323  TypeFuncClass functypclass = get_type_func_class(typeoid);
1324  TupleDesc tupdesc = NULL;
1325 
1326  /*
1327  * Build a suitable tupledesc representing the output rows
1328  */
1329  if (functypclass == TYPEFUNC_COMPOSITE)
1330  {
1331  /* Composite data type, e.g. a table's row type */
1332  tupdesc = lookup_rowtype_tupdesc_copy(typeoid, -1);
1333 
1334  if (colaliases != NIL)
1335  {
1336  int natts = tupdesc->natts;
1337  int varattno;
1338 
1339  /* does the list length match the number of attributes? */
1340  if (list_length(colaliases) != natts)
1341  ereport(ERROR,
1342  (errcode(ERRCODE_DATATYPE_MISMATCH),
1343  errmsg("number of aliases does not match number of columns")));
1344 
1345  /* OK, use the aliases instead */
1346  for (varattno = 0; varattno < natts; varattno++)
1347  {
1348  char *label = strVal(list_nth(colaliases, varattno));
1349  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1350 
1351  if (label != NULL)
1352  namestrcpy(&(attr->attname), label);
1353  }
1354 
1355  /* The tuple type is now an anonymous record type */
1356  tupdesc->tdtypeid = RECORDOID;
1357  tupdesc->tdtypmod = -1;
1358  }
1359  }
1360  else if (functypclass == TYPEFUNC_SCALAR)
1361  {
1362  /* Base data type, i.e. scalar */
1363  char *attname;
1364 
1365  /* the alias list is required for base types */
1366  if (colaliases == NIL)
1367  ereport(ERROR,
1368  (errcode(ERRCODE_DATATYPE_MISMATCH),
1369  errmsg("no column alias was provided")));
1370 
1371  /* the alias list length must be 1 */
1372  if (list_length(colaliases) != 1)
1373  ereport(ERROR,
1374  (errcode(ERRCODE_DATATYPE_MISMATCH),
1375  errmsg("number of aliases does not match number of columns")));
1376 
1377  /* OK, get the column alias */
1378  attname = strVal(linitial(colaliases));
1379 
1380  tupdesc = CreateTemplateTupleDesc(1, false);
1381  TupleDescInitEntry(tupdesc,
1382  (AttrNumber) 1,
1383  attname,
1384  typeoid,
1385  -1,
1386  0);
1387  }
1388  else if (functypclass == TYPEFUNC_RECORD)
1389  {
1390  /* XXX can't support this because typmod wasn't passed in ... */
1391  ereport(ERROR,
1392  (errcode(ERRCODE_DATATYPE_MISMATCH),
1393  errmsg("could not determine row description for function returning record")));
1394  }
1395  else
1396  {
1397  /* crummy error message, but parser should have caught this */
1398  elog(ERROR, "function in FROM has unsupported return type");
1399  }
1400 
1401  return tupdesc;
1402 }
#define NIL
Definition: pg_list.h:69
Oid tdtypeid
Definition: tupdesc.h:74
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
int namestrcpy(Name name, const char *str)
Definition: name.c:216
int natts
Definition: tupdesc.h:73
int32 tdtypmod
Definition: tupdesc.h:75
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
static TypeFuncClass get_type_func_class(Oid typid)
Definition: funcapi.c:750
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define RECORDOID
Definition: pg_type.h:680
TypeFuncClass
Definition: funcapi.h:150
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:462
#define ereport(elevel, rest)
Definition: elog.h:122
static char * label
Definition: pg_basebackup.c:81
#define NULL
Definition: c.h:229
static int list_length(const List *l)
Definition: pg_list.h:89
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:41
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1292