PostgreSQL Source Code  git master
funcapi.h File Reference
#include "fmgr.h"
#include "access/tupdesc.h"
#include "executor/executor.h"
#include "executor/tuptable.h"
Include dependency graph for funcapi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AttInMetadata
 
struct  FuncCallContext
 

Macros

#define HeapTupleGetDatum(tuple)   HeapTupleHeaderGetDatum((tuple)->t_data)
 
#define TupleGetDatum(_slot, _tuple)   HeapTupleGetDatum(_tuple)
 
#define SRF_IS_FIRSTCALL()   (fcinfo->flinfo->fn_extra == NULL)
 
#define SRF_FIRSTCALL_INIT()   init_MultiFuncCall(fcinfo)
 
#define SRF_PERCALL_SETUP()   per_MultiFuncCall(fcinfo)
 
#define SRF_RETURN_NEXT(_funcctx, _result)
 
#define SRF_RETURN_NEXT_NULL(_funcctx)
 
#define SRF_RETURN_DONE(_funcctx)
 

Typedefs

typedef struct AttInMetadata AttInMetadata
 
typedef struct FuncCallContext FuncCallContext
 
typedef enum TypeFuncClass TypeFuncClass
 

Enumerations

enum  TypeFuncClass {
  TYPEFUNC_SCALAR, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD,
  TYPEFUNC_OTHER
}
 

Functions

TypeFuncClass get_call_result_type (FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_expr_result_type (Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_func_result_type (Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TupleDesc get_expr_result_tupdesc (Node *expr, bool noError)
 
bool resolve_polymorphic_argtypes (int numargs, Oid *argtypes, char *argmodes, Node *call_expr)
 
int get_func_arg_info (HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
 
int get_func_input_arg_names (Datum proargnames, Datum proargmodes, char ***arg_names)
 
int get_func_trftypes (HeapTuple procTup, Oid **p_trftypes)
 
char * get_func_result_name (Oid functionId)
 
TupleDesc build_function_result_tupdesc_d (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)
 
int extract_variadic_args (FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **values, Oid **types, bool **nulls)
 

Macro Definition Documentation

◆ HeapTupleGetDatum

#define HeapTupleGetDatum (   tuple)    HeapTupleHeaderGetDatum((tuple)->t_data)

Definition at line 230 of file funcapi.h.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), bt_page_print_tuples(), bt_page_stats(), build_pgstattuple_type(), dblink_get_pkey(), exec_eval_datum(), ExecEvalConvertRowtype(), ExecEvalFieldStoreForm(), ExecEvalRow(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), hstore_each(), hstore_populate_record(), page_header(), pg_buffercache_pages(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_keywords(), pg_get_multixact_members(), pg_get_object_address(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_logdir_ls(), pg_ls_dir_files(), pg_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_wal_receiver(), pg_timezone_abbrevs(), pg_timezone_names(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_walfile_name_offset(), pgp_armor_headers(), pgrowlocks(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_hash_to_datum(), plperl_return_next_internal(), pltcl_build_tuple_result(), pltcl_func_handler(), prs_process_call(), show_all_settings(), ssl_extension_info(), ts_process_call(), tsvector_unnest(), and tt_process_call().

◆ SRF_FIRSTCALL_INIT

◆ SRF_IS_FIRSTCALL

◆ SRF_PERCALL_SETUP

◆ SRF_RETURN_DONE

#define SRF_RETURN_DONE (   _funcctx)
Value:
do { \
ReturnSetInfo *rsi; \
end_MultiFuncCall(fcinfo, _funcctx); \
rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
rsi->isDone = ExprEndResult; \
} while (0)
#define PG_RETURN_NULL()
Definition: fmgr.h:305

Definition at line 317 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), bt_page_items_bytea(), dblink_get_pkey(), generate_series_step_int4(), generate_series_step_int8(), generate_series_step_numeric(), generate_series_timestamp(), generate_series_timestamptz(), generate_subscripts(), gin_leafpage_items(), hash_page_items(), heap_page_items(), hstore_each(), hstore_skeys(), hstore_svals(), json_object_keys(), jsonb_object_keys(), 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().

◆ SRF_RETURN_NEXT

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

Definition at line 299 of file funcapi.h.

Referenced by aclexplode(), array_unnest(), brin_revmap_data(), bt_page_items(), bt_page_items_bytea(), dblink_get_pkey(), generate_series_step_int4(), generate_series_step_int8(), generate_series_step_numeric(), generate_series_timestamp(), generate_series_timestamptz(), generate_subscripts(), gin_leafpage_items(), hash_page_items(), heap_page_items(), hstore_each(), hstore_skeys(), hstore_svals(), json_object_keys(), jsonb_object_keys(), 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().

◆ SRF_RETURN_NEXT_NULL

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

Definition at line 308 of file funcapi.h.

Referenced by PLy_exec_function().

◆ TupleGetDatum

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

Definition at line 232 of file funcapi.h.

Typedef Documentation

◆ AttInMetadata

◆ FuncCallContext

◆ TypeFuncClass

Enumeration Type Documentation

◆ TypeFuncClass

Enumerator
TYPEFUNC_SCALAR 
TYPEFUNC_COMPOSITE 
TYPEFUNC_COMPOSITE_DOMAIN 
TYPEFUNC_RECORD 
TYPEFUNC_OTHER 

Definition at line 155 of file funcapi.h.

156 {
157  TYPEFUNC_SCALAR, /* scalar result type */
158  TYPEFUNC_COMPOSITE, /* determinable rowtype result */
159  TYPEFUNC_COMPOSITE_DOMAIN, /* domain over determinable rowtype result */
160  TYPEFUNC_RECORD, /* indeterminate rowtype result */
161  TYPEFUNC_OTHER /* bogus type, eg pseudotype */
162 } TypeFuncClass;
TypeFuncClass
Definition: funcapi.h:155

Function Documentation

◆ BlessTupleDesc()

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_setup_record(), setup_firstcall(), ssl_extension_info(), ts_setup_firstcall(), tsvector_unnest(), TupleDescGetAttInMetadata(), and TupleDescGetSlot().

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:80
int32 tdtypmod
Definition: tupdesc.h:81
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1738
#define RECORDOID
Definition: pg_type.h:680

◆ build_function_result_tupdesc_d()

TupleDesc build_function_result_tupdesc_d ( Datum  proallargtypes,
Datum  proargmodes,
Datum  proargnames 
)

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

1227 {
1228  TupleDesc desc;
1229  ArrayType *arr;
1230  int numargs;
1231  Oid *argtypes;
1232  char *argmodes;
1233  Datum *argnames = NULL;
1234  Oid *outargtypes;
1235  char **outargnames;
1236  int numoutargs;
1237  int nargnames;
1238  int i;
1239 
1240  /* Can't have output args if columns are null */
1241  if (proallargtypes == PointerGetDatum(NULL) ||
1242  proargmodes == PointerGetDatum(NULL))
1243  return NULL;
1244 
1245  /*
1246  * We expect the arrays to be 1-D arrays of the right types; verify that.
1247  * For the OID and char arrays, we don't need to use deconstruct_array()
1248  * since the array data is just going to look like a C array of values.
1249  */
1250  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1251  numargs = ARR_DIMS(arr)[0];
1252  if (ARR_NDIM(arr) != 1 ||
1253  numargs < 0 ||
1254  ARR_HASNULL(arr) ||
1255  ARR_ELEMTYPE(arr) != OIDOID)
1256  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1257  argtypes = (Oid *) ARR_DATA_PTR(arr);
1258  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1259  if (ARR_NDIM(arr) != 1 ||
1260  ARR_DIMS(arr)[0] != numargs ||
1261  ARR_HASNULL(arr) ||
1262  ARR_ELEMTYPE(arr) != CHAROID)
1263  elog(ERROR, "proargmodes is not a 1-D char array");
1264  argmodes = (char *) ARR_DATA_PTR(arr);
1265  if (proargnames != PointerGetDatum(NULL))
1266  {
1267  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1268  if (ARR_NDIM(arr) != 1 ||
1269  ARR_DIMS(arr)[0] != numargs ||
1270  ARR_HASNULL(arr) ||
1271  ARR_ELEMTYPE(arr) != TEXTOID)
1272  elog(ERROR, "proargnames is not a 1-D text array");
1273  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1274  &argnames, NULL, &nargnames);
1275  Assert(nargnames == numargs);
1276  }
1277 
1278  /* zero elements probably shouldn't happen, but handle it gracefully */
1279  if (numargs <= 0)
1280  return NULL;
1281 
1282  /* extract output-argument types and names */
1283  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1284  outargnames = (char **) palloc(numargs * sizeof(char *));
1285  numoutargs = 0;
1286  for (i = 0; i < numargs; i++)
1287  {
1288  char *pname;
1289 
1290  if (argmodes[i] == PROARGMODE_IN ||
1291  argmodes[i] == PROARGMODE_VARIADIC)
1292  continue;
1293  Assert(argmodes[i] == PROARGMODE_OUT ||
1294  argmodes[i] == PROARGMODE_INOUT ||
1295  argmodes[i] == PROARGMODE_TABLE);
1296  outargtypes[numoutargs] = argtypes[i];
1297  if (argnames)
1298  pname = TextDatumGetCString(argnames[i]);
1299  else
1300  pname = NULL;
1301  if (pname == NULL || pname[0] == '\0')
1302  {
1303  /* Parameter is not named, so gin up a column name */
1304  pname = psprintf("column%d", numoutargs + 1);
1305  }
1306  outargnames[numoutargs] = pname;
1307  numoutargs++;
1308  }
1309 
1310  /*
1311  * If there is no output argument, or only one, the function does not
1312  * return tuples.
1313  */
1314  if (numoutargs < 2)
1315  return NULL;
1316 
1317  desc = CreateTemplateTupleDesc(numoutargs, false);
1318  for (i = 0; i < numoutargs; i++)
1319  {
1320  TupleDescInitEntry(desc, i + 1,
1321  outargnames[i],
1322  outargtypes[i],
1323  -1,
1324  0);
1325  }
1326 
1327  return desc;
1328 }
#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:5558
unsigned int Oid
Definition: postgres_ext.h:31
#define PROARGMODE_INOUT
Definition: pg_proc.h:5557
#define PROARGMODE_OUT
Definition: pg_proc.h:5556
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
#define PROARGMODE_TABLE
Definition: pg_proc.h:5559
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ARR_HASNULL(a)
Definition: array.h:276
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define PROARGMODE_IN
Definition: pg_proc.h:5555
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define CHAROID
Definition: pg_type.h:296
#define Assert(condition)
Definition: c.h:670
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1176 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(), PointerGetDatum, PROCOID, RECORDOID, and SysCacheGetAttr().

Referenced by internal_get_result_type(), and ProcedureCreate().

1177 {
1178  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1179  Datum proallargtypes;
1180  Datum proargmodes;
1181  Datum proargnames;
1182  bool isnull;
1183 
1184  /* Return NULL if the function isn't declared to return RECORD */
1185  if (procform->prorettype != RECORDOID)
1186  return NULL;
1187 
1188  /* If there are no OUT parameters, return NULL */
1189  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes) ||
1191  return NULL;
1192 
1193  /* Get the data out of the tuple */
1194  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1196  &isnull);
1197  Assert(!isnull);
1198  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1200  &isnull);
1201  Assert(!isnull);
1202  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1204  &isnull);
1205  if (isnull)
1206  proargnames = PointerGetDatum(NULL); /* just to be sure */
1207 
1208  return build_function_result_tupdesc_d(proallargtypes,
1209  proargmodes,
1210  proargnames);
1211 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define PointerGetDatum(X)
Definition: postgres.h:562
TupleDesc build_function_result_tupdesc_d(Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1224
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:1368
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define Assert(condition)
Definition: c.h:670
#define Anum_pg_proc_proallargtypes
Definition: pg_proc.h:110

◆ BuildTupleFromCStrings()

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, 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:90
int32 * atttypmods
Definition: funcapi.h:48
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
Oid * attioparams
Definition: funcapi.h:45
int natts
Definition: tupdesc.h:79
void pfree(void *pointer)
Definition: mcxt.c:949
TupleDesc tupdesc
Definition: funcapi.h:39
uintptr_t Datum
Definition: postgres.h:372
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1618
static Datum values[MAXATTR]
Definition: bootstrap.c:164
void * palloc(Size size)
Definition: mcxt.c:848
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:42

◆ end_MultiFuncCall()

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:845
#define PointerGetDatum(X)
Definition: postgres.h:562
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:173
ExprContext * econtext
Definition: execnodes.h:267

◆ extract_variadic_args()

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

Definition at line 1476 of file funcapi.c.

References ARR_ELEMTYPE, array_in(), Assert, CStringGetTextDatum, deconstruct_array(), ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, get_fn_expr_arg_stable(), get_fn_expr_argtype(), get_fn_expr_variadic(), get_typlenbyvalalign(), i, OidIsValid, palloc0(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_NARGS, TEXTOID, and UNKNOWNOID.

Referenced by json_build_array(), json_build_object(), jsonb_build_array(), and jsonb_build_object().

1479 {
1480  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1481  Datum *args_res;
1482  bool *nulls_res;
1483  Oid *types_res;
1484  int nargs,
1485  i;
1486 
1487  *args = NULL;
1488  *types = NULL;
1489  *nulls = NULL;
1490 
1491  if (variadic)
1492  {
1494  Oid element_type;
1495  bool typbyval;
1496  char typalign;
1497  int16 typlen;
1498 
1499  Assert(PG_NARGS() == variadic_start + 1);
1500 
1501  if (PG_ARGISNULL(variadic_start))
1502  return -1;
1503 
1504  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1505  element_type = ARR_ELEMTYPE(array_in);
1506 
1507  get_typlenbyvalalign(element_type,
1508  &typlen, &typbyval, &typalign);
1509  deconstruct_array(array_in, element_type, typlen, typbyval,
1510  typalign, &args_res, &nulls_res,
1511  &nargs);
1512 
1513  /* All the elements of the array have the same type */
1514  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1515  for (i = 0; i < nargs; i++)
1516  types_res[i] = element_type;
1517  }
1518  else
1519  {
1520  nargs = PG_NARGS() - variadic_start;
1521  Assert(nargs > 0);
1522  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1523  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1524  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1525 
1526  for (i = 0; i < nargs; i++)
1527  {
1528  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1529  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1530  i + variadic_start);
1531 
1532  /*
1533  * Turn a constant (more or less literal) value that's of unknown
1534  * type into text if required. Unknowns come in as a cstring
1535  * pointer. Note: for functions declared as taking type "any", the
1536  * parser will not do any type conversion on unknown-type literals
1537  * (that is, undecorated strings or NULLs).
1538  */
1539  if (convert_unknown &&
1540  types_res[i] == UNKNOWNOID &&
1541  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1542  {
1543  types_res[i] = TEXTOID;
1544 
1545  if (PG_ARGISNULL(i + variadic_start))
1546  args_res[i] = (Datum) 0;
1547  else
1548  args_res[i] =
1549  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1550  }
1551  else
1552  {
1553  /* no conversion needed, just take the datum as given */
1554  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1555  }
1556 
1557  if (!OidIsValid(types_res[i]) ||
1558  (convert_unknown && types_res[i] == UNKNOWNOID))
1559  ereport(ERROR,
1560  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1561  errmsg("could not determine data type for argument %d",
1562  i + 1)));
1563  }
1564  }
1565 
1566  /* Fill in results */
1567  *args = args_res;
1568  *nulls = nulls_res;
1569  *types = types_res;
1570 
1571  return nargs;
1572 }
signed short int16
Definition: c.h:283
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1969
struct typedefs * types
Definition: ecpg.c:29
#define TEXTOID
Definition: pg_type.h:324
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2040
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2038
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
FmgrInfo * flinfo
Definition: fmgr.h:79
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define ERROR
Definition: elog.h:43
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:877
uintptr_t Datum
Definition: postgres.h:372
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define Assert(condition)
Definition: c.h:670
#define PG_NARGS()
Definition: fmgr.h:168
#define UNKNOWNOID
Definition: pg_type.h:431
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define ARR_ELEMTYPE(a)
Definition: array.h:277
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:172

◆ get_call_result_type()

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

Definition at line 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:292

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 412 of file funcapi.c.

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

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

413 {
415  TypeFuncClass functypclass;
416 
417  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
418 
419  if (functypclass == TYPEFUNC_COMPOSITE ||
420  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
421  return tupleDesc;
422 
423  if (!noError)
424  {
425  Oid exprTypeId = exprType(expr);
426 
427  if (exprTypeId != RECORDOID)
428  ereport(ERROR,
429  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
430  errmsg("type %s is not composite",
431  format_type_be(exprTypeId))));
432  else
433  ereport(ERROR,
434  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
435  errmsg("record type has not been registered")));
436  }
437 
438  return NULL;
439 }
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:227
#define ERROR
Definition: elog.h:43
#define RECORDOID
Definition: pg_type.h:680
TypeFuncClass
Definition: funcapi.h:155
#define ereport(elevel, rest)
Definition: elog.h:122
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ get_expr_result_type()

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(), TYPEFUNC_COMPOSITE, and TYPEFUNC_COMPOSITE_DOMAIN.

Referenced by addRangeTableEntryForFunction(), ExecInitFunctionScan(), expandRTE(), get_expr_result_tupdesc(), get_rte_attribute_type(), and init_sexpr().

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  Oid base_typid;
250 
251  if (resultTypeId)
252  *resultTypeId = typid;
253  if (resultTupleDesc)
254  *resultTupleDesc = NULL;
255  result = get_type_func_class(typid, &base_typid);
256  if ((result == TYPEFUNC_COMPOSITE ||
257  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
258  resultTupleDesc)
259  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
260  }
261 
262  return result;
263 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
unsigned int Oid
Definition: postgres_ext.h:31
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:796
TypeFuncClass
Definition: funcapi.h:155
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
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:292
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1652

◆ get_func_arg_info()

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

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

848 {
849  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
850  Datum proallargtypes;
851  Datum proargmodes;
852  Datum proargnames;
853  bool isNull;
854  ArrayType *arr;
855  int numargs;
856  Datum *elems;
857  int nelems;
858  int i;
859 
860  /* First discover the total number of parameters and get their types */
861  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
863  &isNull);
864  if (!isNull)
865  {
866  /*
867  * We expect the arrays to be 1-D arrays of the right types; verify
868  * that. For the OID and char arrays, we don't need to use
869  * deconstruct_array() since the array data is just going to look like
870  * a C array of values.
871  */
872  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
873  numargs = ARR_DIMS(arr)[0];
874  if (ARR_NDIM(arr) != 1 ||
875  numargs < 0 ||
876  ARR_HASNULL(arr) ||
877  ARR_ELEMTYPE(arr) != OIDOID)
878  elog(ERROR, "proallargtypes is not a 1-D Oid array");
879  Assert(numargs >= procStruct->pronargs);
880  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
881  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
882  numargs * sizeof(Oid));
883  }
884  else
885  {
886  /* If no proallargtypes, use proargtypes */
887  numargs = procStruct->proargtypes.dim1;
888  Assert(numargs == procStruct->pronargs);
889  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
890  memcpy(*p_argtypes, procStruct->proargtypes.values,
891  numargs * sizeof(Oid));
892  }
893 
894  /* Get argument names, if available */
895  proargnames = SysCacheGetAttr(PROCOID, procTup,
897  &isNull);
898  if (isNull)
899  *p_argnames = NULL;
900  else
901  {
903  TEXTOID, -1, false, 'i',
904  &elems, NULL, &nelems);
905  if (nelems != numargs) /* should not happen */
906  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
907  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
908  for (i = 0; i < numargs; i++)
909  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
910  }
911 
912  /* Get argument modes, if available */
913  proargmodes = SysCacheGetAttr(PROCOID, procTup,
915  &isNull);
916  if (isNull)
917  *p_argmodes = NULL;
918  else
919  {
920  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
921  if (ARR_NDIM(arr) != 1 ||
922  ARR_DIMS(arr)[0] != numargs ||
923  ARR_HASNULL(arr) ||
924  ARR_ELEMTYPE(arr) != CHAROID)
925  elog(ERROR, "proargmodes is not a 1-D char array");
926  *p_argmodes = (char *) palloc(numargs * sizeof(char));
927  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
928  numargs * sizeof(char));
929  }
930 
931  return numargs;
932 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#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:279
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ARR_HASNULL(a)
Definition: array.h:276
#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:1368
#define CHAROID
Definition: pg_type.h:296
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define Assert(condition)
Definition: c.h:670
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
#define Anum_pg_proc_proallargtypes
Definition: pg_proc.h:110
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ get_func_input_arg_names()

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

Definition at line 988 of file funcapi.c.

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

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

990 {
991  ArrayType *arr;
992  int numargs;
993  Datum *argnames;
994  char *argmodes;
995  char **inargnames;
996  int numinargs;
997  int i;
998 
999  /* Do nothing if null proargnames */
1000  if (proargnames == PointerGetDatum(NULL))
1001  {
1002  *arg_names = NULL;
1003  return 0;
1004  }
1005 
1006  /*
1007  * We expect the arrays to be 1-D arrays of the right types; verify that.
1008  * For proargmodes, we don't need to use deconstruct_array() since the
1009  * array data is just going to look like a C array of values.
1010  */
1011  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1012  if (ARR_NDIM(arr) != 1 ||
1013  ARR_HASNULL(arr) ||
1014  ARR_ELEMTYPE(arr) != TEXTOID)
1015  elog(ERROR, "proargnames is not a 1-D text array");
1016  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1017  &argnames, NULL, &numargs);
1018  if (proargmodes != PointerGetDatum(NULL))
1019  {
1020  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1021  if (ARR_NDIM(arr) != 1 ||
1022  ARR_DIMS(arr)[0] != numargs ||
1023  ARR_HASNULL(arr) ||
1024  ARR_ELEMTYPE(arr) != CHAROID)
1025  elog(ERROR, "proargmodes is not a 1-D char array");
1026  argmodes = (char *) ARR_DATA_PTR(arr);
1027  }
1028  else
1029  argmodes = NULL;
1030 
1031  /* zero elements probably shouldn't happen, but handle it gracefully */
1032  if (numargs <= 0)
1033  {
1034  *arg_names = NULL;
1035  return 0;
1036  }
1037 
1038  /* extract input-argument names */
1039  inargnames = (char **) palloc(numargs * sizeof(char *));
1040  numinargs = 0;
1041  for (i = 0; i < numargs; i++)
1042  {
1043  if (argmodes == NULL ||
1044  argmodes[i] == PROARGMODE_IN ||
1045  argmodes[i] == PROARGMODE_INOUT ||
1046  argmodes[i] == PROARGMODE_VARIADIC)
1047  {
1048  char *pname = TextDatumGetCString(argnames[i]);
1049 
1050  if (pname[0] != '\0')
1051  inargnames[numinargs] = pname;
1052  else
1053  inargnames[numinargs] = NULL;
1054  numinargs++;
1055  }
1056  }
1057 
1058  *arg_names = inargnames;
1059  return numinargs;
1060 }
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:562
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5558
#define PROARGMODE_INOUT
Definition: pg_proc.h:5557
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ARR_HASNULL(a)
Definition: array.h:276
#define PROARGMODE_IN
Definition: pg_proc.h:5555
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
#define CHAROID
Definition: pg_type.h:296
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1073 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, ObjectIdGetDatum, PROARGMODE_IN, PROARGMODE_INOUT, PROARGMODE_OUT, PROARGMODE_TABLE, PROARGMODE_VARIADIC, PROCOID, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), TextDatumGetCString, and TEXTOID.

Referenced by chooseScalarFunctionAlias().

1074 {
1075  char *result;
1076  HeapTuple procTuple;
1077  Datum proargmodes;
1078  Datum proargnames;
1079  bool isnull;
1080  ArrayType *arr;
1081  int numargs;
1082  char *argmodes;
1083  Datum *argnames;
1084  int numoutargs;
1085  int nargnames;
1086  int i;
1087 
1088  /* First fetch the function's pg_proc row */
1089  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1090  if (!HeapTupleIsValid(procTuple))
1091  elog(ERROR, "cache lookup failed for function %u", functionId);
1092 
1093  /* If there are no named OUT parameters, return NULL */
1094  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes) ||
1096  result = NULL;
1097  else
1098  {
1099  /* Get the data out of the tuple */
1100  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1102  &isnull);
1103  Assert(!isnull);
1104  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1106  &isnull);
1107  Assert(!isnull);
1108 
1109  /*
1110  * We expect the arrays to be 1-D arrays of the right types; verify
1111  * that. For the char array, we don't need to use deconstruct_array()
1112  * since the array data is just going to look like a C array of
1113  * values.
1114  */
1115  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1116  numargs = ARR_DIMS(arr)[0];
1117  if (ARR_NDIM(arr) != 1 ||
1118  numargs < 0 ||
1119  ARR_HASNULL(arr) ||
1120  ARR_ELEMTYPE(arr) != CHAROID)
1121  elog(ERROR, "proargmodes is not a 1-D char array");
1122  argmodes = (char *) ARR_DATA_PTR(arr);
1123  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1124  if (ARR_NDIM(arr) != 1 ||
1125  ARR_DIMS(arr)[0] != numargs ||
1126  ARR_HASNULL(arr) ||
1127  ARR_ELEMTYPE(arr) != TEXTOID)
1128  elog(ERROR, "proargnames is not a 1-D text array");
1129  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1130  &argnames, NULL, &nargnames);
1131  Assert(nargnames == numargs);
1132 
1133  /* scan for output argument(s) */
1134  result = NULL;
1135  numoutargs = 0;
1136  for (i = 0; i < numargs; i++)
1137  {
1138  if (argmodes[i] == PROARGMODE_IN ||
1139  argmodes[i] == PROARGMODE_VARIADIC)
1140  continue;
1141  Assert(argmodes[i] == PROARGMODE_OUT ||
1142  argmodes[i] == PROARGMODE_INOUT ||
1143  argmodes[i] == PROARGMODE_TABLE);
1144  if (++numoutargs > 1)
1145  {
1146  /* multiple out args, so forget it */
1147  result = NULL;
1148  break;
1149  }
1150  result = TextDatumGetCString(argnames[i]);
1151  if (result == NULL || result[0] == '\0')
1152  {
1153  /* Parameter is not named, so forget it */
1154  result = NULL;
1155  break;
1156  }
1157  }
1158  }
1159 
1160  ReleaseSysCache(procTuple);
1161 
1162  return result;
1163 }
#define TEXTOID
Definition: pg_type.h:324
#define PROARGMODE_VARIADIC
Definition: pg_proc.h:5558
#define PROARGMODE_INOUT
Definition: pg_proc.h:5557
#define PROARGMODE_OUT
Definition: pg_proc.h:5556
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define ARR_DIMS(a)
Definition: array.h:279
#define PROARGMODE_TABLE
Definition: pg_proc.h:5559
#define ARR_DATA_PTR(a)
Definition: array.h:307
bool heap_attisnull(HeapTuple tup, int attnum)
Definition: heaptuple.c:296
#define ARR_HASNULL(a)
Definition: array.h:276
#define Anum_pg_proc_proargmodes
Definition: pg_proc.h:111
#define PROARGMODE_IN
Definition: pg_proc.h:5555
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
#define TextDatumGetCString(d)
Definition: builtins.h:92
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
#define CHAROID
Definition: pg_type.h:296
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
#define ARR_NDIM(a)
Definition: array.h:275
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
int i
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ get_func_result_type()

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

Definition at line 272 of file funcapi.c.

References internal_get_result_type().

Referenced by check_sql_fn_retval(), and inline_set_returning_function().

275 {
276  return internal_get_result_type(functionId,
277  NULL,
278  NULL,
279  resultTypeId,
280  resultTupleDesc);
281 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

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

942 {
943  Datum protrftypes;
944  ArrayType *arr;
945  int nelems;
946  bool isNull;
947 
948  protrftypes = SysCacheGetAttr(PROCOID, procTup,
950  &isNull);
951  if (!isNull)
952  {
953  /*
954  * We expect the arrays to be 1-D arrays of the right types; verify
955  * that. For the OID and char arrays, we don't need to use
956  * deconstruct_array() since the array data is just going to look like
957  * a C array of values.
958  */
959  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
960  nelems = ARR_DIMS(arr)[0];
961  if (ARR_NDIM(arr) != 1 ||
962  nelems < 0 ||
963  ARR_HASNULL(arr) ||
964  ARR_ELEMTYPE(arr) != OIDOID)
965  elog(ERROR, "protrftypes is not a 1-D Oid array");
966  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
967  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
968  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
969  nelems * sizeof(Oid));
970 
971  return nelems;
972  }
973  else
974  return 0;
975 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#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:279
#define ARR_DATA_PTR(a)
Definition: array.h:307
#define ARR_HASNULL(a)
Definition: array.h:276
uintptr_t Datum
Definition: postgres.h:372
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1368
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define Assert(condition)
Definition: c.h:670
#define ARR_NDIM(a)
Definition: array.h:275
void * palloc(Size size)
Definition: mcxt.c:848
#define elog
Definition: elog.h:219
#define ARR_ELEMTYPE(a)
Definition: array.h:277
#define DatumGetArrayTypeP(X)
Definition: array.h:246

◆ HeapTupleHeaderGetDatum()

Datum HeapTupleHeaderGetDatum ( HeapTupleHeader  tuple)

Definition at line 1204 of file execTuples.c.

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

Referenced by populate_composite(), and populate_recordset_record().

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:1618
#define PointerGetDatum(X)
Definition: postgres.h:562
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:460
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:450
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:545
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:121
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:444

◆ init_MultiFuncCall()

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, 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:66
#define IsA(nodeptr, _type_)
Definition: nodes.h:563
TupleTableSlot * slot
Definition: funcapi.h:83
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:180
int errcode(int sqlerrcode)
Definition: elog.c:575
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:173
TupleDesc tuple_desc
Definition: funcapi.h:121
#define ERROR
Definition: elog.h:43
AttInMetadata * attinmeta
Definition: funcapi.h:100
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:342
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:819
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
ExprContext * econtext
Definition: execnodes.h:267
void * user_fctx
Definition: funcapi.h:91
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
uint64 max_calls
Definition: funcapi.h:75

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 128 of file funcapi.c.

References ExecClearTuple(), 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:83
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1341 of file funcapi.c.

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

1342 {
1343  RangeVar *relvar;
1344  Relation rel;
1345  TupleDesc tupdesc;
1346  List *relname_list;
1347 
1348  /* Open relation and copy the tuple description */
1349  relname_list = stringToQualifiedNameList(relname);
1350  relvar = makeRangeVarFromNameList(relname_list);
1351  rel = relation_openrv(relvar, AccessShareLock);
1352  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1354 
1355  return tupdesc;
1356 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:102
#define RelationGetDescr(relation)
Definition: rel.h:437
#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

◆ resolve_polymorphic_argtypes()

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

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

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

◆ TupleDescGetAttInMetadata()

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:90
int32 * atttypmods
Definition: funcapi.h:48
unsigned int Oid
Definition: postgres_ext.h:31
Oid * attioparams
Definition: funcapi.h:45
int natts
Definition: tupdesc.h:79
signed int int32
Definition: c.h:284
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:122
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1032
TupleDesc tupdesc
Definition: funcapi.h:39
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2632
void * palloc0(Size size)
Definition: mcxt.c:877
void * palloc(Size size)
Definition: mcxt.c:848
int i
FmgrInfo * attinfuncs
Definition: funcapi.h:42

◆ TupleDescGetSlot()

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

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1374 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, RECORDOID, strVal, tupleDesc::tdtypeid, tupleDesc::tdtypmod, TupleDescAttr, TupleDescInitEntry(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

1375 {
1376  Oid base_typeoid;
1377  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1378  TupleDesc tupdesc = NULL;
1379 
1380  /*
1381  * Build a suitable tupledesc representing the output rows. We
1382  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1383  * unlikely that legacy callers of this obsolete function would be
1384  * prepared to apply domain constraints.
1385  */
1386  if (functypclass == TYPEFUNC_COMPOSITE)
1387  {
1388  /* Composite data type, e.g. a table's row type */
1389  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1390 
1391  if (colaliases != NIL)
1392  {
1393  int natts = tupdesc->natts;
1394  int varattno;
1395 
1396  /* does the list length match the number of attributes? */
1397  if (list_length(colaliases) != natts)
1398  ereport(ERROR,
1399  (errcode(ERRCODE_DATATYPE_MISMATCH),
1400  errmsg("number of aliases does not match number of columns")));
1401 
1402  /* OK, use the aliases instead */
1403  for (varattno = 0; varattno < natts; varattno++)
1404  {
1405  char *label = strVal(list_nth(colaliases, varattno));
1406  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1407 
1408  if (label != NULL)
1409  namestrcpy(&(attr->attname), label);
1410  }
1411 
1412  /* The tuple type is now an anonymous record type */
1413  tupdesc->tdtypeid = RECORDOID;
1414  tupdesc->tdtypmod = -1;
1415  }
1416  }
1417  else if (functypclass == TYPEFUNC_SCALAR)
1418  {
1419  /* Base data type, i.e. scalar */
1420  char *attname;
1421 
1422  /* the alias list is required for base types */
1423  if (colaliases == NIL)
1424  ereport(ERROR,
1425  (errcode(ERRCODE_DATATYPE_MISMATCH),
1426  errmsg("no column alias was provided")));
1427 
1428  /* the alias list length must be 1 */
1429  if (list_length(colaliases) != 1)
1430  ereport(ERROR,
1431  (errcode(ERRCODE_DATATYPE_MISMATCH),
1432  errmsg("number of aliases does not match number of columns")));
1433 
1434  /* OK, get the column alias */
1435  attname = strVal(linitial(colaliases));
1436 
1437  tupdesc = CreateTemplateTupleDesc(1, false);
1438  TupleDescInitEntry(tupdesc,
1439  (AttrNumber) 1,
1440  attname,
1441  typeoid,
1442  -1,
1443  0);
1444  }
1445  else if (functypclass == TYPEFUNC_RECORD)
1446  {
1447  /* XXX can't support this because typmod wasn't passed in ... */
1448  ereport(ERROR,
1449  (errcode(ERRCODE_DATATYPE_MISMATCH),
1450  errmsg("could not determine row description for function returning record")));
1451  }
1452  else
1453  {
1454  /* crummy error message, but parser should have caught this */
1455  elog(ERROR, "function in FROM has unsupported return type");
1456  }
1457 
1458  return tupdesc;
1459 }
#define NIL
Definition: pg_list.h:69
Oid tdtypeid
Definition: tupdesc.h:80
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:216
int natts
Definition: tupdesc.h:79
int32 tdtypmod
Definition: tupdesc.h:81
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:796
#define RECORDOID
Definition: pg_type.h:680
TypeFuncClass
Definition: funcapi.h:155
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:505
#define ereport(elevel, rest)
Definition: elog.h:122
static char * label
Definition: pg_basebackup.c:82
static int list_length(const List *l)
Definition: pg_list.h:89
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
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:1652