PostgreSQL Source Code  git master
funcapi.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/regproc.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for funcapi.c:

Go to the source code of this file.

Functions

static void shutdown_MultiFuncCall (Datum arg)
 
static TypeFuncClass internal_get_result_type (Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
static bool resolve_polymorphic_tupdesc (TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
 
static TypeFuncClass get_type_func_class (Oid typid, Oid *base_typeid)
 
FuncCallContextinit_MultiFuncCall (PG_FUNCTION_ARGS)
 
FuncCallContextper_MultiFuncCall (PG_FUNCTION_ARGS)
 
void end_MultiFuncCall (PG_FUNCTION_ARGS, FuncCallContext *funcctx)
 
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_trftypes (HeapTuple procTup, Oid **p_trftypes)
 
int get_func_input_arg_names (Datum proargnames, Datum proargmodes, char ***arg_names)
 
char * get_func_result_name (Oid functionId)
 
TupleDesc build_function_result_tupdesc_t (HeapTuple procTuple)
 
TupleDesc build_function_result_tupdesc_d (char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
 
TupleDesc RelationNameGetTupleDesc (const char *relname)
 
TupleDesc TypeGetTupleDesc (Oid typeoid, List *colaliases)
 
int extract_variadic_args (FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
 

Function Documentation

◆ build_function_result_tupdesc_d()

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

Definition at line 1243 of file funcapi.c.

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

Referenced by build_function_result_tupdesc_t(), and ProcedureCreate().

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

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1193 of file funcapi.c.

References Assert, build_function_result_tupdesc_d(), GETSTRUCT, heap_attisnull(), PointerGetDatum, PROCOID, and SysCacheGetAttr().

Referenced by CallStmtResultDesc(), internal_get_result_type(), and ProcedureCreate().

1194 {
1195  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1196  Datum proallargtypes;
1197  Datum proargmodes;
1198  Datum proargnames;
1199  bool isnull;
1200 
1201  /* Return NULL if the function isn't declared to return RECORD */
1202  if (procform->prorettype != RECORDOID)
1203  return NULL;
1204 
1205  /* If there are no OUT parameters, return NULL */
1206  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1207  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1208  return NULL;
1209 
1210  /* Get the data out of the tuple */
1211  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1212  Anum_pg_proc_proallargtypes,
1213  &isnull);
1214  Assert(!isnull);
1215  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1216  Anum_pg_proc_proargmodes,
1217  &isnull);
1218  Assert(!isnull);
1219  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1220  Anum_pg_proc_proargnames,
1221  &isnull);
1222  if (isnull)
1223  proargnames = PointerGetDatum(NULL); /* just to be sure */
1224 
1225  return build_function_result_tupdesc_d(procform->prokind,
1226  proallargtypes,
1227  proargmodes,
1228  proargnames);
1229 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define PointerGetDatum(X)
Definition: postgres.h:556
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1243
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
uintptr_t Datum
Definition: postgres.h:367
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define Assert(condition)
Definition: c.h:733

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 140 of file funcapi.c.

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

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

◆ extract_variadic_args()

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

Definition at line 1496 of file funcapi.c.

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

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

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

◆ get_call_result_type()

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

Definition at line 196 of file funcapi.c.

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

Referenced by brin_metapage_info(), brin_page_items(), bt_metap(), bt_page_items(), bt_page_items_bytea(), bt_page_stats(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), each_worker(), each_worker_jsonb(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), materializeResult(), page_header(), pg_available_extension_versions(), pg_available_extensions(), pg_buffercache_pages(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_event_trigger_ddl_commands(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_get_replication_slots(), pg_hba_file_rules(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_progress_info(), pg_stat_get_subscription(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pg_stats_ext_mcvlist_items(), pg_stop_backup_v2(), pgp_armor_headers(), pgrowlocks(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_func_handler(), PLy_function_build_args(), setup_firstcall(), ssl_extension_info(), and storeRow().

199 {
200  return internal_get_result_type(fcinfo->flinfo->fn_oid,
201  fcinfo->flinfo->fn_expr,
202  (ReturnSetInfo *) fcinfo->resultinfo,
203  resultTypeId,
204  resultTupleDesc);
205 }
fmNodePtr resultinfo
Definition: fmgr.h:89
FmgrInfo * flinfo
Definition: fmgr.h:87
Oid fn_oid
Definition: fmgr.h:59
fmNodePtr fn_expr
Definition: fmgr.h:66
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:309

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 429 of file funcapi.c.

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

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

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

◆ get_expr_result_type()

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

Definition at line 212 of file funcapi.c.

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

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

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

◆ get_func_arg_info()

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

Definition at line 863 of file funcapi.c.

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

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

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

◆ get_func_input_arg_names()

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

Definition at line 1005 of file funcapi.c.

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

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

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

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1090 of file funcapi.c.

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert, DatumGetArrayTypeP, deconstruct_array(), elog, ERROR, heap_attisnull(), HeapTupleIsValid, i, ObjectIdGetDatum, PROCOID, ReleaseSysCache(), SearchSysCache1(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by chooseScalarFunctionAlias().

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

◆ get_func_result_type()

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

Definition at line 289 of file funcapi.c.

References internal_get_result_type().

Referenced by check_sql_fn_retval(), and inline_set_returning_function().

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

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 957 of file funcapi.c.

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

Referenced by print_function_trftypes().

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

◆ get_type_func_class()

static TypeFuncClass get_type_func_class ( Oid  typid,
Oid base_typeid 
)
static

Definition at line 813 of file funcapi.c.

References get_typtype(), getBaseType(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_OTHER, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by get_expr_result_type(), internal_get_result_type(), and TypeGetTupleDesc().

814 {
815  *base_typeid = typid;
816 
817  switch (get_typtype(typid))
818  {
819  case TYPTYPE_COMPOSITE:
820  return TYPEFUNC_COMPOSITE;
821  case TYPTYPE_BASE:
822  case TYPTYPE_ENUM:
823  case TYPTYPE_RANGE:
824  return TYPEFUNC_SCALAR;
825  case TYPTYPE_DOMAIN:
826  *base_typeid = typid = getBaseType(typid);
827  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
829  else /* domain base type can't be a pseudotype */
830  return TYPEFUNC_SCALAR;
831  case TYPTYPE_PSEUDO:
832  if (typid == RECORDOID)
833  return TYPEFUNC_RECORD;
834 
835  /*
836  * We treat VOID and CSTRING as legitimate scalar datatypes,
837  * mostly for the convenience of the JDBC driver (which wants to
838  * be able to do "SELECT * FROM foo()" for all legitimately
839  * user-callable functions).
840  */
841  if (typid == VOIDOID || typid == CSTRINGOID)
842  return TYPEFUNC_SCALAR;
843  return TYPEFUNC_OTHER;
844  }
845  /* shouldn't get here, probably */
846  return TYPEFUNC_OTHER;
847 }
char get_typtype(Oid typid)
Definition: lsyscache.c:2407
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2299

◆ init_MultiFuncCall()

FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 53 of file funcapi.c.

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

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

◆ internal_get_result_type()

static TypeFuncClass internal_get_result_type ( Oid  funcid,
Node call_expr,
ReturnSetInfo rsinfo,
Oid resultTypeId,
TupleDesc resultTupleDesc 
)
static

Definition at line 309 of file funcapi.c.

References assign_record_type_typmod(), build_function_result_tupdesc_t(), elog, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, exprType(), format_type_be(), get_type_func_class(), GETSTRUCT, HeapTupleIsValid, InvalidOid, IsA, lookup_rowtype_tupdesc_copy(), NameStr, ObjectIdGetDatum, PROCOID, ReleaseSysCache(), resolve_polymorphic_tupdesc(), SearchSysCache1(), TupleDescData::tdtypeid, TupleDescData::tdtypmod, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, and TYPEFUNC_SCALAR.

Referenced by get_call_result_type(), get_expr_result_type(), and get_func_result_type().

314 {
315  TypeFuncClass result;
316  HeapTuple tp;
317  Form_pg_proc procform;
318  Oid rettype;
319  Oid base_rettype;
320  TupleDesc tupdesc;
321 
322  /* First fetch the function's pg_proc row to inspect its rettype */
324  if (!HeapTupleIsValid(tp))
325  elog(ERROR, "cache lookup failed for function %u", funcid);
326  procform = (Form_pg_proc) GETSTRUCT(tp);
327 
328  rettype = procform->prorettype;
329 
330  /* Check for OUT parameters defining a RECORD result */
331  tupdesc = build_function_result_tupdesc_t(tp);
332  if (tupdesc)
333  {
334  /*
335  * It has OUT parameters, so it's basically like a regular composite
336  * type, except we have to be able to resolve any polymorphic OUT
337  * parameters.
338  */
339  if (resultTypeId)
340  *resultTypeId = rettype;
341 
342  if (resolve_polymorphic_tupdesc(tupdesc,
343  &procform->proargtypes,
344  call_expr))
345  {
346  if (tupdesc->tdtypeid == RECORDOID &&
347  tupdesc->tdtypmod < 0)
348  assign_record_type_typmod(tupdesc);
349  if (resultTupleDesc)
350  *resultTupleDesc = tupdesc;
351  result = TYPEFUNC_COMPOSITE;
352  }
353  else
354  {
355  if (resultTupleDesc)
356  *resultTupleDesc = NULL;
357  result = TYPEFUNC_RECORD;
358  }
359 
360  ReleaseSysCache(tp);
361 
362  return result;
363  }
364 
365  /*
366  * If scalar polymorphic result, try to resolve it.
367  */
368  if (IsPolymorphicType(rettype))
369  {
370  Oid newrettype = exprType(call_expr);
371 
372  if (newrettype == InvalidOid) /* this probably should not happen */
373  ereport(ERROR,
374  (errcode(ERRCODE_DATATYPE_MISMATCH),
375  errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
376  NameStr(procform->proname),
377  format_type_be(rettype))));
378  rettype = newrettype;
379  }
380 
381  if (resultTypeId)
382  *resultTypeId = rettype;
383  if (resultTupleDesc)
384  *resultTupleDesc = NULL; /* default result */
385 
386  /* Classify the result type */
387  result = get_type_func_class(rettype, &base_rettype);
388  switch (result)
389  {
390  case TYPEFUNC_COMPOSITE:
392  if (resultTupleDesc)
393  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
394  /* Named composite types can't have any polymorphic columns */
395  break;
396  case TYPEFUNC_SCALAR:
397  break;
398  case TYPEFUNC_RECORD:
399  /* We must get the tupledesc from call context */
400  if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
401  rsinfo->expectedDesc != NULL)
402  {
403  result = TYPEFUNC_COMPOSITE;
404  if (resultTupleDesc)
405  *resultTupleDesc = rsinfo->expectedDesc;
406  /* Assume no polymorphic columns here, either */
407  }
408  break;
409  default:
410  break;
411  }
412 
413  ReleaseSysCache(tp);
414 
415  return result;
416 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:608
char * format_type_be(Oid type_oid)
Definition: format_type.c:326
unsigned int Oid
Definition: postgres_ext.h:31
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1772
TupleDesc expectedDesc
Definition: execnodes.h:302
TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple)
Definition: funcapi.c:1193
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
int32 tdtypmod
Definition: tupdesc.h:83
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:813
TypeFuncClass
Definition: funcapi.h:147
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:610
static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
Definition: funcapi.c:465
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1686

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 128 of file funcapi.c.

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

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1361 of file funcapi.c.

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

1362 {
1363  RangeVar *relvar;
1364  Relation rel;
1365  TupleDesc tupdesc;
1366  List *relname_list;
1367 
1368  /* Open relation and copy the tuple description */
1369  relname_list = stringToQualifiedNameList(relname);
1370  relvar = makeRangeVarFromNameList(relname_list);
1371  rel = relation_openrv(relvar, AccessShareLock);
1372  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1374 
1375  return tupdesc;
1376 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:449
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
NameData relname
Definition: pg_class.h:35
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1687
Definition: pg_list.h:50

◆ resolve_polymorphic_argtypes()

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

Definition at line 661 of file funcapi.c.

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

Referenced by plpgsql_resolve_polymorphic_argtypes().

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

◆ resolve_polymorphic_tupdesc()

static bool resolve_polymorphic_tupdesc ( TupleDesc  tupdesc,
oidvector declared_args,
Node call_expr 
)
static

Definition at line 465 of file funcapi.c.

References atttypid, oidvector::dim1, exprInputCollation(), get_call_expr_argtype(), get_typcollation(), i, InvalidOid, NameStr, TupleDescData::natts, OidIsValid, resolve_generic_type(), TupleDescAttr, TupleDescInitEntry(), TupleDescInitEntryCollation(), type_is_array, type_is_enum(), and oidvector::values.

Referenced by internal_get_result_type().

467 {
468  int natts = tupdesc->natts;
469  int nargs = declared_args->dim1;
470  bool have_anyelement_result = false;
471  bool have_anyarray_result = false;
472  bool have_anyrange_result = false;
473  bool have_anynonarray = false;
474  bool have_anyenum = false;
475  Oid anyelement_type = InvalidOid;
476  Oid anyarray_type = InvalidOid;
477  Oid anyrange_type = InvalidOid;
478  Oid anycollation = InvalidOid;
479  int i;
480 
481  /* See if there are any polymorphic outputs; quick out if not */
482  for (i = 0; i < natts; i++)
483  {
484  switch (TupleDescAttr(tupdesc, i)->atttypid)
485  {
486  case ANYELEMENTOID:
487  have_anyelement_result = true;
488  break;
489  case ANYARRAYOID:
490  have_anyarray_result = true;
491  break;
492  case ANYNONARRAYOID:
493  have_anyelement_result = true;
494  have_anynonarray = true;
495  break;
496  case ANYENUMOID:
497  have_anyelement_result = true;
498  have_anyenum = true;
499  break;
500  case ANYRANGEOID:
501  have_anyrange_result = true;
502  break;
503  default:
504  break;
505  }
506  }
507  if (!have_anyelement_result && !have_anyarray_result &&
508  !have_anyrange_result)
509  return true;
510 
511  /*
512  * Otherwise, extract actual datatype(s) from input arguments. (We assume
513  * the parser already validated consistency of the arguments.)
514  */
515  if (!call_expr)
516  return false; /* no hope */
517 
518  for (i = 0; i < nargs; i++)
519  {
520  switch (declared_args->values[i])
521  {
522  case ANYELEMENTOID:
523  case ANYNONARRAYOID:
524  case ANYENUMOID:
525  if (!OidIsValid(anyelement_type))
526  anyelement_type = get_call_expr_argtype(call_expr, i);
527  break;
528  case ANYARRAYOID:
529  if (!OidIsValid(anyarray_type))
530  anyarray_type = get_call_expr_argtype(call_expr, i);
531  break;
532  case ANYRANGEOID:
533  if (!OidIsValid(anyrange_type))
534  anyrange_type = get_call_expr_argtype(call_expr, i);
535  break;
536  default:
537  break;
538  }
539  }
540 
541  /* If nothing found, parser messed up */
542  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
543  !OidIsValid(anyrange_type))
544  return false;
545 
546  /* If needed, deduce one polymorphic type from others */
547  if (have_anyelement_result && !OidIsValid(anyelement_type))
548  {
549  if (OidIsValid(anyarray_type))
550  anyelement_type = resolve_generic_type(ANYELEMENTOID,
551  anyarray_type,
552  ANYARRAYOID);
553  if (OidIsValid(anyrange_type))
554  {
555  Oid subtype = resolve_generic_type(ANYELEMENTOID,
556  anyrange_type,
557  ANYRANGEOID);
558 
559  /* check for inconsistent array and range results */
560  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
561  return false;
562  anyelement_type = subtype;
563  }
564  }
565 
566  if (have_anyarray_result && !OidIsValid(anyarray_type))
567  anyarray_type = resolve_generic_type(ANYARRAYOID,
568  anyelement_type,
569  ANYELEMENTOID);
570 
571  /*
572  * We can't deduce a range type from other polymorphic inputs, because
573  * there may be multiple range types for the same subtype.
574  */
575  if (have_anyrange_result && !OidIsValid(anyrange_type))
576  return false;
577 
578  /* Enforce ANYNONARRAY if needed */
579  if (have_anynonarray && type_is_array(anyelement_type))
580  return false;
581 
582  /* Enforce ANYENUM if needed */
583  if (have_anyenum && !type_is_enum(anyelement_type))
584  return false;
585 
586  /*
587  * Identify the collation to use for polymorphic OUT parameters. (It'll
588  * necessarily be the same for both anyelement and anyarray.) Note that
589  * range types are not collatable, so any possible internal collation of a
590  * range type is not considered here.
591  */
592  if (OidIsValid(anyelement_type))
593  anycollation = get_typcollation(anyelement_type);
594  else if (OidIsValid(anyarray_type))
595  anycollation = get_typcollation(anyarray_type);
596 
597  if (OidIsValid(anycollation))
598  {
599  /*
600  * The types are collatable, so consider whether to use a nondefault
601  * collation. We do so if we can identify the input collation used
602  * for the function.
603  */
604  Oid inputcollation = exprInputCollation(call_expr);
605 
606  if (OidIsValid(inputcollation))
607  anycollation = inputcollation;
608  }
609 
610  /* And finally replace the tuple column types as needed */
611  for (i = 0; i < natts; i++)
612  {
613  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
614 
615  switch (att->atttypid)
616  {
617  case ANYELEMENTOID:
618  case ANYNONARRAYOID:
619  case ANYENUMOID:
620  TupleDescInitEntry(tupdesc, i + 1,
621  NameStr(att->attname),
622  anyelement_type,
623  -1,
624  0);
625  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
626  break;
627  case ANYARRAYOID:
628  TupleDescInitEntry(tupdesc, i + 1,
629  NameStr(att->attname),
630  anyarray_type,
631  -1,
632  0);
633  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
634  break;
635  case ANYRANGEOID:
636  TupleDescInitEntry(tupdesc, i + 1,
637  NameStr(att->attname),
638  anyrange_type,
639  -1,
640  0);
641  /* no collation should be attached to a range type */
642  break;
643  default:
644  break;
645  }
646  }
647 
648  return true;
649 }
Oid resolve_generic_type(Oid declared_type, Oid context_actual_type, Oid context_declared_type)
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1836
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:639
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:597
int dim1
Definition: c.h:595
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:603
Oid atttypid
Definition: pg_attribute.h:49
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2823
Oid exprInputCollation(const Node *expr)
Definition: nodeFuncs.c:924
bool type_is_enum(Oid typid)
Definition: lsyscache.c:2456
#define type_is_array(typid)
Definition: lsyscache.h:184
int i
#define NameStr(name)
Definition: c.h:610

◆ shutdown_MultiFuncCall()

static void shutdown_MultiFuncCall ( Datum  arg)
static

Definition at line 158 of file funcapi.c.

References DatumGetPointer, FmgrInfo::fn_extra, MemoryContextDelete(), and FuncCallContext::multi_call_memory_ctx.

Referenced by end_MultiFuncCall(), and init_MultiFuncCall().

159 {
160  FmgrInfo *flinfo = (FmgrInfo *) DatumGetPointer(arg);
161  FuncCallContext *funcctx = (FuncCallContext *) flinfo->fn_extra;
162 
163  /* unbind from flinfo */
164  flinfo->fn_extra = NULL;
165 
166  /*
167  * Delete context that holds all multi-call data, including the
168  * FuncCallContext itself
169  */
171 }
Definition: fmgr.h:56
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
void * fn_extra
Definition: fmgr.h:64
#define DatumGetPointer(X)
Definition: postgres.h:549
void * arg

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1394 of file funcapi.c.

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

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