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:289

Definition at line 309 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), 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_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:297
struct ReturnSetInfo ReturnSetInfo
ExprDoneCond isDone
Definition: execnodes.h:202

Definition at line 291 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), 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_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:202
#define PG_RETURN_NULL()
Definition: fmgr.h:289

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 1031 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(), ExecInitExpr(), 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_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().

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

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

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

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

Definition at line 1115 of file execTuples.c.

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, tupleDesc::attrs, AttInMetadata::atttypmods, heap_form_tuple(), i, InputFunctionCall(), tupleDesc::natts, NULL, palloc(), pfree(), and AttInMetadata::tupdesc.

Referenced by bt_metap(), bt_page_items(), bt_page_stats(), build_pgstattuple_type(), build_tuplestore_recursively(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), 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().

1116 {
1117  TupleDesc tupdesc = attinmeta->tupdesc;
1118  int natts = tupdesc->natts;
1119  Datum *dvalues;
1120  bool *nulls;
1121  int i;
1122  HeapTuple tuple;
1123 
1124  dvalues = (Datum *) palloc(natts * sizeof(Datum));
1125  nulls = (bool *) palloc(natts * sizeof(bool));
1126 
1127  /* Call the "in" function for each non-dropped attribute */
1128  for (i = 0; i < natts; i++)
1129  {
1130  if (!tupdesc->attrs[i]->attisdropped)
1131  {
1132  /* Non-dropped attributes */
1133  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
1134  values[i],
1135  attinmeta->attioparams[i],
1136  attinmeta->atttypmods[i]);
1137  if (values[i] != NULL)
1138  nulls[i] = false;
1139  else
1140  nulls[i] = true;
1141  }
1142  else
1143  {
1144  /* Handle dropped attributes by setting to NULL */
1145  dvalues[i] = (Datum) 0;
1146  nulls[i] = true;
1147  }
1148  }
1149 
1150  /*
1151  * Form a tuple
1152  */
1153  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
1154 
1155  /*
1156  * Release locally palloc'd space. XXX would probably be good to pfree
1157  * values of pass-by-reference datums, as well.
1158  */
1159  pfree(dvalues);
1160  pfree(nulls);
1161 
1162  return tuple;
1163 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int32 * atttypmods
Definition: funcapi.h:47
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
Oid * attioparams
Definition: funcapi.h:44
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:992
TupleDesc tupdesc
Definition: funcapi.h:38
uintptr_t Datum
Definition: postgres.h:374
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1882
#define NULL
Definition: c.h:226
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void * palloc(Size size)
Definition: mcxt.c:891
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:897
#define PointerGetDatum(X)
Definition: postgres.h:564
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:173
ExprContext * econtext
Definition: execnodes.h:197
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_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(), 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:71
fmNodePtr resultinfo
Definition: fmgr.h:73
Oid fn_oid
Definition: fmgr.h:56
fmNodePtr fn_expr
Definition: fmgr.h:63
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, 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_fcache(), and ParseComplexProjection().

230 {
231  TypeFuncClass result;
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:559
unsigned int Oid
Definition: postgres_ext.h:31
static TypeFuncClass get_type_func_class(Oid typid)
Definition: funcapi.c:748
TypeFuncClass
Definition: funcapi.h:150
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1062
#define NULL
Definition: c.h:226
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:1279
int get_func_arg_info ( HeapTuple  procTup,
Oid **  p_argtypes,
char ***  p_argnames,
char **  p_argmodes 
)

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

793 {
794  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
795  Datum proallargtypes;
796  Datum proargmodes;
797  Datum proargnames;
798  bool isNull;
799  ArrayType *arr;
800  int numargs;
801  Datum *elems;
802  int nelems;
803  int i;
804 
805  /* First discover the total number of parameters and get their types */
806  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
808  &isNull);
809  if (!isNull)
810  {
811  /*
812  * We expect the arrays to be 1-D arrays of the right types; verify
813  * that. For the OID and char arrays, we don't need to use
814  * deconstruct_array() since the array data is just going to look like
815  * a C array of values.
816  */
817  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
818  numargs = ARR_DIMS(arr)[0];
819  if (ARR_NDIM(arr) != 1 ||
820  numargs < 0 ||
821  ARR_HASNULL(arr) ||
822  ARR_ELEMTYPE(arr) != OIDOID)
823  elog(ERROR, "proallargtypes is not a 1-D Oid array");
824  Assert(numargs >= procStruct->pronargs);
825  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
826  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
827  numargs * sizeof(Oid));
828  }
829  else
830  {
831  /* If no proallargtypes, use proargtypes */
832  numargs = procStruct->proargtypes.dim1;
833  Assert(numargs == procStruct->pronargs);
834  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
835  memcpy(*p_argtypes, procStruct->proargtypes.values,
836  numargs * sizeof(Oid));
837  }
838 
839  /* Get argument names, if available */
840  proargnames = SysCacheGetAttr(PROCOID, procTup,
842  &isNull);
843  if (isNull)
844  *p_argnames = NULL;
845  else
846  {
848  TEXTOID, -1, false, 'i',
849  &elems, NULL, &nelems);
850  if (nelems != numargs) /* should not happen */
851  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
852  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
853  for (i = 0; i < numargs; i++)
854  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
855  }
856 
857  /* Get argument modes, if available */
858  proargmodes = SysCacheGetAttr(PROCOID, procTup,
860  &isNull);
861  if (isNull)
862  *p_argmodes = NULL;
863  else
864  {
865  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
866  if (ARR_NDIM(arr) != 1 ||
867  ARR_DIMS(arr)[0] != numargs ||
868  ARR_HASNULL(arr) ||
869  ARR_ELEMTYPE(arr) != CHAROID)
870  elog(ERROR, "proargmodes is not a 1-D char array");
871  *p_argmodes = (char *) palloc(numargs * sizeof(char));
872  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
873  numargs * sizeof(char));
874  }
875 
876  return numargs;
877 }
#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:91
uintptr_t Datum
Definition: postgres.h:374
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
#define CHAROID
Definition: pg_type.h:296
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#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:891
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 933 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().

935 {
936  ArrayType *arr;
937  int numargs;
938  Datum *argnames;
939  char *argmodes;
940  char **inargnames;
941  int numinargs;
942  int i;
943 
944  /* Do nothing if null proargnames */
945  if (proargnames == PointerGetDatum(NULL))
946  {
947  *arg_names = NULL;
948  return 0;
949  }
950 
951  /*
952  * We expect the arrays to be 1-D arrays of the right types; verify that.
953  * For proargmodes, we don't need to use deconstruct_array() since the
954  * array data is just going to look like a C array of values.
955  */
956  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
957  if (ARR_NDIM(arr) != 1 ||
958  ARR_HASNULL(arr) ||
959  ARR_ELEMTYPE(arr) != TEXTOID)
960  elog(ERROR, "proargnames is not a 1-D text array");
961  deconstruct_array(arr, TEXTOID, -1, false, 'i',
962  &argnames, NULL, &numargs);
963  if (proargmodes != PointerGetDatum(NULL))
964  {
965  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
966  if (ARR_NDIM(arr) != 1 ||
967  ARR_DIMS(arr)[0] != numargs ||
968  ARR_HASNULL(arr) ||
969  ARR_ELEMTYPE(arr) != CHAROID)
970  elog(ERROR, "proargmodes is not a 1-D char array");
971  argmodes = (char *) ARR_DATA_PTR(arr);
972  }
973  else
974  argmodes = NULL;
975 
976  /* zero elements probably shouldn't happen, but handle it gracefully */
977  if (numargs <= 0)
978  {
979  *arg_names = NULL;
980  return 0;
981  }
982 
983  /* extract input-argument names */
984  inargnames = (char **) palloc(numargs * sizeof(char *));
985  numinargs = 0;
986  for (i = 0; i < numargs; i++)
987  {
988  if (argmodes == NULL ||
989  argmodes[i] == PROARGMODE_IN ||
990  argmodes[i] == PROARGMODE_INOUT ||
991  argmodes[i] == PROARGMODE_VARIADIC)
992  {
993  char *pname = TextDatumGetCString(argnames[i]);
994 
995  if (pname[0] != '\0')
996  inargnames[numinargs] = pname;
997  else
998  inargnames[numinargs] = NULL;
999  numinargs++;
1000  }
1001  }
1002 
1003  *arg_names = inargnames;
1004  return numinargs;
1005 }
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5393
#define PROARGMODE_INOUT
Definition: pg_proc.h:5392
#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:5390
#define TextDatumGetCString(d)
Definition: builtins.h:91
uintptr_t Datum
Definition: postgres.h:374
#define CHAROID
Definition: pg_type.h:296
#define NULL
Definition: c.h:226
#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:891
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 1018 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(), SearchSysCache1, SysCacheGetAttr(), TextDatumGetCString, and TEXTOID.

Referenced by chooseScalarFunctionAlias().

1019 {
1020  char *result;
1021  HeapTuple procTuple;
1022  Datum proargmodes;
1023  Datum proargnames;
1024  bool isnull;
1025  ArrayType *arr;
1026  int numargs;
1027  char *argmodes;
1028  Datum *argnames;
1029  int numoutargs;
1030  int nargnames;
1031  int i;
1032 
1033  /* First fetch the function's pg_proc row */
1034  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1035  if (!HeapTupleIsValid(procTuple))
1036  elog(ERROR, "cache lookup failed for function %u", functionId);
1037 
1038  /* If there are no named OUT parameters, return NULL */
1039  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
1041  result = NULL;
1042  else
1043  {
1044  /* Get the data out of the tuple */
1045  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1047  &isnull);
1048  Assert(!isnull);
1049  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1051  &isnull);
1052  Assert(!isnull);
1053 
1054  /*
1055  * We expect the arrays to be 1-D arrays of the right types; verify
1056  * that. For the char array, we don't need to use deconstruct_array()
1057  * since the array data is just going to look like a C array of
1058  * values.
1059  */
1060  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1061  numargs = ARR_DIMS(arr)[0];
1062  if (ARR_NDIM(arr) != 1 ||
1063  numargs < 0 ||
1064  ARR_HASNULL(arr) ||
1065  ARR_ELEMTYPE(arr) != CHAROID)
1066  elog(ERROR, "proargmodes is not a 1-D char array");
1067  argmodes = (char *) ARR_DATA_PTR(arr);
1068  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1069  if (ARR_NDIM(arr) != 1 ||
1070  ARR_DIMS(arr)[0] != numargs ||
1071  ARR_HASNULL(arr) ||
1072  ARR_ELEMTYPE(arr) != TEXTOID)
1073  elog(ERROR, "proargnames is not a 1-D text array");
1074  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1075  &argnames, NULL, &nargnames);
1076  Assert(nargnames == numargs);
1077 
1078  /* scan for output argument(s) */
1079  result = NULL;
1080  numoutargs = 0;
1081  for (i = 0; i < numargs; i++)
1082  {
1083  if (argmodes[i] == PROARGMODE_IN ||
1084  argmodes[i] == PROARGMODE_VARIADIC)
1085  continue;
1086  Assert(argmodes[i] == PROARGMODE_OUT ||
1087  argmodes[i] == PROARGMODE_INOUT ||
1088  argmodes[i] == PROARGMODE_TABLE);
1089  if (++numoutargs > 1)
1090  {
1091  /* multiple out args, so forget it */
1092  result = NULL;
1093  break;
1094  }
1095  result = TextDatumGetCString(argnames[i]);
1096  if (result == NULL || result[0] == '\0')
1097  {
1098  /* Parameter is not named, so forget it */
1099  result = NULL;
1100  break;
1101  }
1102  }
1103  }
1104 
1105  ReleaseSysCache(procTuple);
1106 
1107  return result;
1108 }
#define TEXTOID
Definition: pg_type.h:324
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5393
#define PROARGMODE_INOUT
Definition: pg_proc.h:5392
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define PROARGMODE_OUT
Definition: pg_proc.h:5391
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:275
#define PROARGMODE_TABLE
Definition: pg_proc.h:5394
#define ARR_DATA_PTR(a)
Definition: array.h:303
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:297
#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:5390
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
#define TextDatumGetCString(d)
Definition: builtins.h:91
uintptr_t Datum
Definition: postgres.h:374
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
#define CHAROID
Definition: pg_type.h:296
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#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:226
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 885 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().

887 {
888  Datum protrftypes;
889  ArrayType *arr;
890  int nelems;
891  bool isNull;
892 
893  protrftypes = SysCacheGetAttr(PROCOID, procTup,
895  &isNull);
896  if (!isNull)
897  {
898  /*
899  * We expect the arrays to be 1-D arrays of the right types; verify
900  * that. For the OID and char arrays, we don't need to use
901  * deconstruct_array() since the array data is just going to look like
902  * a C array of values.
903  */
904  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
905  nelems = ARR_DIMS(arr)[0];
906  if (ARR_NDIM(arr) != 1 ||
907  nelems < 0 ||
908  ARR_HASNULL(arr) ||
909  ARR_ELEMTYPE(arr) != OIDOID)
910  elog(ERROR, "protrftypes is not a 1-D Oid array");
911  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
912  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
913  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
914  nelems * sizeof(Oid));
915 
916  return nelems;
917  }
918  else
919  return 0;
920 }
#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:374
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define Assert(condition)
Definition: c.h:671
#define ARR_NDIM(a)
Definition: array.h:271
void * palloc(Size size)
Definition: mcxt.c:891
#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 1201 of file execTuples.c.

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

Referenced by TQRemapRecord().

1202 {
1203  Datum result;
1204  TupleDesc tupDesc;
1205 
1206  /* No work if there are no external TOAST pointers in the tuple */
1207  if (!HeapTupleHeaderHasExternal(tuple))
1208  return PointerGetDatum(tuple);
1209 
1210  /* Use the type data saved by heap_form_tuple to look up the rowtype */
1212  HeapTupleHeaderGetTypMod(tuple));
1213 
1214  /* And do the flattening */
1215  result = toast_flatten_tuple_to_datum(tuple,
1217  tupDesc);
1218 
1219  ReleaseTupleDesc(tupDesc);
1220 
1221  return result;
1222 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1245
#define PointerGetDatum(X)
Definition: postgres.h:564
#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:374
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:540
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
#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:559
TupleTableSlot * slot
Definition: funcapi.h:82
#define PointerGetDatum(X)
Definition: postgres.h:564
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:155
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:440
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:784
#define NULL
Definition: c.h:226
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:871
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
ExprContext * econtext
Definition: execnodes.h:197
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:226
TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1286 of file funcapi.c.

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

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

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

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

Definition at line 1068 of file execTuples.c.

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

Referenced by bt_metap(), bt_page_items(), bt_page_stats(), build_pgstattuple_type(), connectby_text(), connectby_text_serial(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), hash_page_items(), 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().

1069 {
1070  int natts = tupdesc->natts;
1071  int i;
1072  Oid atttypeid;
1073  Oid attinfuncid;
1074  FmgrInfo *attinfuncinfo;
1075  Oid *attioparams;
1076  int32 *atttypmods;
1077  AttInMetadata *attinmeta;
1078 
1079  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
1080 
1081  /* "Bless" the tupledesc so that we can make rowtype datums with it */
1082  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
1083 
1084  /*
1085  * Gather info needed later to call the "in" function for each attribute
1086  */
1087  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
1088  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
1089  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
1090 
1091  for (i = 0; i < natts; i++)
1092  {
1093  /* Ignore dropped attributes */
1094  if (!tupdesc->attrs[i]->attisdropped)
1095  {
1096  atttypeid = tupdesc->attrs[i]->atttypid;
1097  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
1098  fmgr_info(attinfuncid, &attinfuncinfo[i]);
1099  atttypmods[i] = tupdesc->attrs[i]->atttypmod;
1100  }
1101  }
1102  attinmeta->attinfuncs = attinfuncinfo;
1103  attinmeta->attioparams = attioparams;
1104  attinmeta->atttypmods = atttypmods;
1105 
1106  return attinmeta;
1107 }
Definition: fmgr.h:53
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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:253
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
TupleDesc tupdesc
Definition: funcapi.h:38
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
void * palloc0(Size size)
Definition: mcxt.c:920
void * palloc(Size size)
Definition: mcxt.c:891
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:41
TupleTableSlot* TupleDescGetSlot ( TupleDesc  tupdesc)

Definition at line 1048 of file execTuples.c.

References BlessTupleDesc(), and MakeSingleTupleTableSlot().

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

Definition at line 1319 of file funcapi.c.

References tupleDesc::attrs, 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, TupleDescInitEntry(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

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