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 1211 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().

1215 {
1216  TupleDesc desc;
1217  ArrayType *arr;
1218  int numargs;
1219  Oid *argtypes;
1220  char *argmodes;
1221  Datum *argnames = NULL;
1222  Oid *outargtypes;
1223  char **outargnames;
1224  int numoutargs;
1225  int nargnames;
1226  int i;
1227 
1228  /* Can't have output args if columns are null */
1229  if (proallargtypes == PointerGetDatum(NULL) ||
1230  proargmodes == PointerGetDatum(NULL))
1231  return NULL;
1232 
1233  /*
1234  * We expect the arrays to be 1-D arrays of the right types; verify that.
1235  * For the OID and char arrays, we don't need to use deconstruct_array()
1236  * since the array data is just going to look like a C array of values.
1237  */
1238  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1239  numargs = ARR_DIMS(arr)[0];
1240  if (ARR_NDIM(arr) != 1 ||
1241  numargs < 0 ||
1242  ARR_HASNULL(arr) ||
1243  ARR_ELEMTYPE(arr) != OIDOID)
1244  elog(ERROR, "proallargtypes is not a 1-D Oid array");
1245  argtypes = (Oid *) ARR_DATA_PTR(arr);
1246  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1247  if (ARR_NDIM(arr) != 1 ||
1248  ARR_DIMS(arr)[0] != numargs ||
1249  ARR_HASNULL(arr) ||
1250  ARR_ELEMTYPE(arr) != CHAROID)
1251  elog(ERROR, "proargmodes is not a 1-D char array");
1252  argmodes = (char *) ARR_DATA_PTR(arr);
1253  if (proargnames != PointerGetDatum(NULL))
1254  {
1255  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1256  if (ARR_NDIM(arr) != 1 ||
1257  ARR_DIMS(arr)[0] != numargs ||
1258  ARR_HASNULL(arr) ||
1259  ARR_ELEMTYPE(arr) != TEXTOID)
1260  elog(ERROR, "proargnames is not a 1-D text array");
1261  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1262  &argnames, NULL, &nargnames);
1263  Assert(nargnames == numargs);
1264  }
1265 
1266  /* zero elements probably shouldn't happen, but handle it gracefully */
1267  if (numargs <= 0)
1268  return NULL;
1269 
1270  /* extract output-argument types and names */
1271  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1272  outargnames = (char **) palloc(numargs * sizeof(char *));
1273  numoutargs = 0;
1274  for (i = 0; i < numargs; i++)
1275  {
1276  char *pname;
1277 
1278  if (argmodes[i] == PROARGMODE_IN ||
1279  argmodes[i] == PROARGMODE_VARIADIC)
1280  continue;
1281  Assert(argmodes[i] == PROARGMODE_OUT ||
1282  argmodes[i] == PROARGMODE_INOUT ||
1283  argmodes[i] == PROARGMODE_TABLE);
1284  outargtypes[numoutargs] = argtypes[i];
1285  if (argnames)
1286  pname = TextDatumGetCString(argnames[i]);
1287  else
1288  pname = NULL;
1289  if (pname == NULL || pname[0] == '\0')
1290  {
1291  /* Parameter is not named, so gin up a column name */
1292  pname = psprintf("column%d", numoutargs + 1);
1293  }
1294  outargnames[numoutargs] = pname;
1295  numoutargs++;
1296  }
1297 
1298  /*
1299  * If there is no output argument, or only one, the function does not
1300  * return tuples.
1301  */
1302  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1303  return NULL;
1304 
1305  desc = CreateTemplateTupleDesc(numoutargs);
1306  for (i = 0; i < numoutargs; i++)
1307  {
1308  TupleDescInitEntry(desc, i + 1,
1309  outargnames[i],
1310  outargtypes[i],
1311  -1,
1312  0);
1313  }
1314 
1315  return desc;
1316 }
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:732
#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:924
#define elog(elevel,...)
Definition: elog.h:226
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 1161 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().

1162 {
1163  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1164  Datum proallargtypes;
1165  Datum proargmodes;
1166  Datum proargnames;
1167  bool isnull;
1168 
1169  /* Return NULL if the function isn't declared to return RECORD */
1170  if (procform->prorettype != RECORDOID)
1171  return NULL;
1172 
1173  /* If there are no OUT parameters, return NULL */
1174  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1175  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1176  return NULL;
1177 
1178  /* Get the data out of the tuple */
1179  proallargtypes = SysCacheGetAttr(PROCOID, procTuple,
1180  Anum_pg_proc_proallargtypes,
1181  &isnull);
1182  Assert(!isnull);
1183  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1184  Anum_pg_proc_proargmodes,
1185  &isnull);
1186  Assert(!isnull);
1187  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1188  Anum_pg_proc_proargnames,
1189  &isnull);
1190  if (isnull)
1191  proargnames = PointerGetDatum(NULL); /* just to be sure */
1192 
1193  return build_function_result_tupdesc_d(procform->prokind,
1194  proallargtypes,
1195  proargmodes,
1196  proargnames);
1197 }
#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:1211
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:1385
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define Assert(condition)
Definition: c.h:732

◆ 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 1464 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().

1467 {
1468  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
1469  Datum *args_res;
1470  bool *nulls_res;
1471  Oid *types_res;
1472  int nargs,
1473  i;
1474 
1475  *args = NULL;
1476  *types = NULL;
1477  *nulls = NULL;
1478 
1479  if (variadic)
1480  {
1482  Oid element_type;
1483  bool typbyval;
1484  char typalign;
1485  int16 typlen;
1486 
1487  Assert(PG_NARGS() == variadic_start + 1);
1488 
1489  if (PG_ARGISNULL(variadic_start))
1490  return -1;
1491 
1492  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
1493  element_type = ARR_ELEMTYPE(array_in);
1494 
1495  get_typlenbyvalalign(element_type,
1496  &typlen, &typbyval, &typalign);
1497  deconstruct_array(array_in, element_type, typlen, typbyval,
1498  typalign, &args_res, &nulls_res,
1499  &nargs);
1500 
1501  /* All the elements of the array have the same type */
1502  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1503  for (i = 0; i < nargs; i++)
1504  types_res[i] = element_type;
1505  }
1506  else
1507  {
1508  nargs = PG_NARGS() - variadic_start;
1509  Assert(nargs > 0);
1510  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
1511  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
1512  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
1513 
1514  for (i = 0; i < nargs; i++)
1515  {
1516  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
1517  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
1518  i + variadic_start);
1519 
1520  /*
1521  * Turn a constant (more or less literal) value that's of unknown
1522  * type into text if required. Unknowns come in as a cstring
1523  * pointer. Note: for functions declared as taking type "any", the
1524  * parser will not do any type conversion on unknown-type literals
1525  * (that is, undecorated strings or NULLs).
1526  */
1527  if (convert_unknown &&
1528  types_res[i] == UNKNOWNOID &&
1529  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
1530  {
1531  types_res[i] = TEXTOID;
1532 
1533  if (PG_ARGISNULL(i + variadic_start))
1534  args_res[i] = (Datum) 0;
1535  else
1536  args_res[i] =
1537  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
1538  }
1539  else
1540  {
1541  /* no conversion needed, just take the datum as given */
1542  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
1543  }
1544 
1545  if (!OidIsValid(types_res[i]) ||
1546  (convert_unknown && types_res[i] == UNKNOWNOID))
1547  ereport(ERROR,
1548  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1549  errmsg("could not determine data type for argument %d",
1550  i + 1)));
1551  }
1552  }
1553 
1554  /* Fill in results */
1555  *args = args_res;
1556  *nulls = nulls_res;
1557  *types = types_res;
1558 
1559  return nargs;
1560 }
signed short int16
Definition: c.h:345
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:570
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#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:955
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:732
#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:784
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(), 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:277

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

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

398 {
399  TupleDesc tupleDesc;
400  TypeFuncClass functypclass;
401 
402  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
403 
404  if (functypclass == TYPEFUNC_COMPOSITE ||
405  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
406  return tupleDesc;
407 
408  if (!noError)
409  {
410  Oid exprTypeId = exprType(expr);
411 
412  if (exprTypeId != RECORDOID)
413  ereport(ERROR,
414  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
415  errmsg("type %s is not composite",
416  format_type_be(exprTypeId))));
417  else
418  ereport(ERROR,
419  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
420  errmsg("record type has not been registered")));
421  }
422 
423  return NULL;
424 }
int errcode(int sqlerrcode)
Definition: elog.c:570
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:42
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ get_expr_result_type()

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

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

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
231  {
232  /* handle as a generic expression; no chance to resolve RECORD */
233  Oid typid = exprType(expr);
234  Oid base_typid;
235 
236  if (resultTypeId)
237  *resultTypeId = typid;
238  if (resultTupleDesc)
239  *resultTupleDesc = NULL;
240  result = get_type_func_class(typid, &base_typid);
241  if ((result == TYPEFUNC_COMPOSITE ||
242  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
243  resultTupleDesc)
244  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
245  }
246 
247  return result;
248 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
unsigned int Oid
Definition: postgres_ext.h:31
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:781
TypeFuncClass
Definition: funcapi.h:147
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
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:277
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 831 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().

833 {
834  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
835  Datum proallargtypes;
836  Datum proargmodes;
837  Datum proargnames;
838  bool isNull;
839  ArrayType *arr;
840  int numargs;
841  Datum *elems;
842  int nelems;
843  int i;
844 
845  /* First discover the total number of parameters and get their types */
846  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
847  Anum_pg_proc_proallargtypes,
848  &isNull);
849  if (!isNull)
850  {
851  /*
852  * We expect the arrays to be 1-D arrays of the right types; verify
853  * that. For the OID and char arrays, we don't need to use
854  * deconstruct_array() since the array data is just going to look like
855  * a C array of values.
856  */
857  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
858  numargs = ARR_DIMS(arr)[0];
859  if (ARR_NDIM(arr) != 1 ||
860  numargs < 0 ||
861  ARR_HASNULL(arr) ||
862  ARR_ELEMTYPE(arr) != OIDOID)
863  elog(ERROR, "proallargtypes is not a 1-D Oid array");
864  Assert(numargs >= procStruct->pronargs);
865  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
866  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
867  numargs * sizeof(Oid));
868  }
869  else
870  {
871  /* If no proallargtypes, use proargtypes */
872  numargs = procStruct->proargtypes.dim1;
873  Assert(numargs == procStruct->pronargs);
874  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
875  memcpy(*p_argtypes, procStruct->proargtypes.values,
876  numargs * sizeof(Oid));
877  }
878 
879  /* Get argument names, if available */
880  proargnames = SysCacheGetAttr(PROCOID, procTup,
881  Anum_pg_proc_proargnames,
882  &isNull);
883  if (isNull)
884  *p_argnames = NULL;
885  else
886  {
888  TEXTOID, -1, false, 'i',
889  &elems, NULL, &nelems);
890  if (nelems != numargs) /* should not happen */
891  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
892  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
893  for (i = 0; i < numargs; i++)
894  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
895  }
896 
897  /* Get argument modes, if available */
898  proargmodes = SysCacheGetAttr(PROCOID, procTup,
899  Anum_pg_proc_proargmodes,
900  &isNull);
901  if (isNull)
902  *p_argmodes = NULL;
903  else
904  {
905  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
906  if (ARR_NDIM(arr) != 1 ||
907  ARR_DIMS(arr)[0] != numargs ||
908  ARR_HASNULL(arr) ||
909  ARR_ELEMTYPE(arr) != CHAROID)
910  elog(ERROR, "proargmodes is not a 1-D char array");
911  *p_argmodes = (char *) palloc(numargs * sizeof(char));
912  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
913  numargs * sizeof(char));
914  }
915 
916  return numargs;
917 }
#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:1385
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define Assert(condition)
Definition: c.h:732
#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:924
#define elog(elevel,...)
Definition: elog.h:226
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 973 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().

975 {
976  ArrayType *arr;
977  int numargs;
978  Datum *argnames;
979  char *argmodes;
980  char **inargnames;
981  int numinargs;
982  int i;
983 
984  /* Do nothing if null proargnames */
985  if (proargnames == PointerGetDatum(NULL))
986  {
987  *arg_names = NULL;
988  return 0;
989  }
990 
991  /*
992  * We expect the arrays to be 1-D arrays of the right types; verify that.
993  * For proargmodes, we don't need to use deconstruct_array() since the
994  * array data is just going to look like a C array of values.
995  */
996  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
997  if (ARR_NDIM(arr) != 1 ||
998  ARR_HASNULL(arr) ||
999  ARR_ELEMTYPE(arr) != TEXTOID)
1000  elog(ERROR, "proargnames is not a 1-D text array");
1001  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1002  &argnames, NULL, &numargs);
1003  if (proargmodes != PointerGetDatum(NULL))
1004  {
1005  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1006  if (ARR_NDIM(arr) != 1 ||
1007  ARR_DIMS(arr)[0] != numargs ||
1008  ARR_HASNULL(arr) ||
1009  ARR_ELEMTYPE(arr) != CHAROID)
1010  elog(ERROR, "proargmodes is not a 1-D char array");
1011  argmodes = (char *) ARR_DATA_PTR(arr);
1012  }
1013  else
1014  argmodes = NULL;
1015 
1016  /* zero elements probably shouldn't happen, but handle it gracefully */
1017  if (numargs <= 0)
1018  {
1019  *arg_names = NULL;
1020  return 0;
1021  }
1022 
1023  /* extract input-argument names */
1024  inargnames = (char **) palloc(numargs * sizeof(char *));
1025  numinargs = 0;
1026  for (i = 0; i < numargs; i++)
1027  {
1028  if (argmodes == NULL ||
1029  argmodes[i] == PROARGMODE_IN ||
1030  argmodes[i] == PROARGMODE_INOUT ||
1031  argmodes[i] == PROARGMODE_VARIADIC)
1032  {
1033  char *pname = TextDatumGetCString(argnames[i]);
1034 
1035  if (pname[0] != '\0')
1036  inargnames[numinargs] = pname;
1037  else
1038  inargnames[numinargs] = NULL;
1039  numinargs++;
1040  }
1041  }
1042 
1043  *arg_names = inargnames;
1044  return numinargs;
1045 }
#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:924
#define elog(elevel,...)
Definition: elog.h:226
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 1058 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().

1059 {
1060  char *result;
1061  HeapTuple procTuple;
1062  Datum proargmodes;
1063  Datum proargnames;
1064  bool isnull;
1065  ArrayType *arr;
1066  int numargs;
1067  char *argmodes;
1068  Datum *argnames;
1069  int numoutargs;
1070  int nargnames;
1071  int i;
1072 
1073  /* First fetch the function's pg_proc row */
1074  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1075  if (!HeapTupleIsValid(procTuple))
1076  elog(ERROR, "cache lookup failed for function %u", functionId);
1077 
1078  /* If there are no named OUT parameters, return NULL */
1079  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1080  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1081  result = NULL;
1082  else
1083  {
1084  /* Get the data out of the tuple */
1085  proargmodes = SysCacheGetAttr(PROCOID, procTuple,
1086  Anum_pg_proc_proargmodes,
1087  &isnull);
1088  Assert(!isnull);
1089  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1090  Anum_pg_proc_proargnames,
1091  &isnull);
1092  Assert(!isnull);
1093 
1094  /*
1095  * We expect the arrays to be 1-D arrays of the right types; verify
1096  * that. For the char array, we don't need to use deconstruct_array()
1097  * since the array data is just going to look like a C array of
1098  * values.
1099  */
1100  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1101  numargs = ARR_DIMS(arr)[0];
1102  if (ARR_NDIM(arr) != 1 ||
1103  numargs < 0 ||
1104  ARR_HASNULL(arr) ||
1105  ARR_ELEMTYPE(arr) != CHAROID)
1106  elog(ERROR, "proargmodes is not a 1-D char array");
1107  argmodes = (char *) ARR_DATA_PTR(arr);
1108  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1109  if (ARR_NDIM(arr) != 1 ||
1110  ARR_DIMS(arr)[0] != numargs ||
1111  ARR_HASNULL(arr) ||
1112  ARR_ELEMTYPE(arr) != TEXTOID)
1113  elog(ERROR, "proargnames is not a 1-D text array");
1114  deconstruct_array(arr, TEXTOID, -1, false, 'i',
1115  &argnames, NULL, &nargnames);
1116  Assert(nargnames == numargs);
1117 
1118  /* scan for output argument(s) */
1119  result = NULL;
1120  numoutargs = 0;
1121  for (i = 0; i < numargs; i++)
1122  {
1123  if (argmodes[i] == PROARGMODE_IN ||
1124  argmodes[i] == PROARGMODE_VARIADIC)
1125  continue;
1126  Assert(argmodes[i] == PROARGMODE_OUT ||
1127  argmodes[i] == PROARGMODE_INOUT ||
1128  argmodes[i] == PROARGMODE_TABLE);
1129  if (++numoutargs > 1)
1130  {
1131  /* multiple out args, so forget it */
1132  result = NULL;
1133  break;
1134  }
1135  result = TextDatumGetCString(argnames[i]);
1136  if (result == NULL || result[0] == '\0')
1137  {
1138  /* Parameter is not named, so forget it */
1139  result = NULL;
1140  break;
1141  }
1142  }
1143  }
1144 
1145  ReleaseSysCache(procTuple);
1146 
1147  return result;
1148 }
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:1124
#define TextDatumGetCString(d)
Definition: builtins.h:84
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1385
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
#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:226
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 257 of file funcapi.c.

References internal_get_result_type().

Referenced by check_sql_fn_retval(), and inline_set_returning_function().

260 {
261  return internal_get_result_type(functionId,
262  NULL,
263  NULL,
264  resultTypeId,
265  resultTupleDesc);
266 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:277

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

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

927 {
928  Datum protrftypes;
929  ArrayType *arr;
930  int nelems;
931  bool isNull;
932 
933  protrftypes = SysCacheGetAttr(PROCOID, procTup,
934  Anum_pg_proc_protrftypes,
935  &isNull);
936  if (!isNull)
937  {
938  /*
939  * We expect the arrays to be 1-D arrays of the right types; verify
940  * that. For the OID and char arrays, we don't need to use
941  * deconstruct_array() since the array data is just going to look like
942  * a C array of values.
943  */
944  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
945  nelems = ARR_DIMS(arr)[0];
946  if (ARR_NDIM(arr) != 1 ||
947  nelems < 0 ||
948  ARR_HASNULL(arr) ||
949  ARR_ELEMTYPE(arr) != OIDOID)
950  elog(ERROR, "protrftypes is not a 1-D Oid array");
951  Assert(nelems >= ((Form_pg_proc) GETSTRUCT(procTup))->pronargs);
952  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
953  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
954  nelems * sizeof(Oid));
955 
956  return nelems;
957  }
958  else
959  return 0;
960 }
#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:1385
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:134
#define Assert(condition)
Definition: c.h:732
#define ARR_NDIM(a)
Definition: array.h:278
void * palloc(Size size)
Definition: mcxt.c:924
#define elog(elevel,...)
Definition: elog.h:226
#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 781 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().

782 {
783  *base_typeid = typid;
784 
785  switch (get_typtype(typid))
786  {
787  case TYPTYPE_COMPOSITE:
788  return TYPEFUNC_COMPOSITE;
789  case TYPTYPE_BASE:
790  case TYPTYPE_ENUM:
791  case TYPTYPE_RANGE:
792  return TYPEFUNC_SCALAR;
793  case TYPTYPE_DOMAIN:
794  *base_typeid = typid = getBaseType(typid);
795  if (get_typtype(typid) == TYPTYPE_COMPOSITE)
797  else /* domain base type can't be a pseudotype */
798  return TYPEFUNC_SCALAR;
799  case TYPTYPE_PSEUDO:
800  if (typid == RECORDOID)
801  return TYPEFUNC_RECORD;
802 
803  /*
804  * We treat VOID and CSTRING as legitimate scalar datatypes,
805  * mostly for the convenience of the JDBC driver (which wants to
806  * be able to do "SELECT * FROM foo()" for all legitimately
807  * user-callable functions).
808  */
809  if (typid == VOIDOID || typid == CSTRINGOID)
810  return TYPEFUNC_SCALAR;
811  return TYPEFUNC_OTHER;
812  }
813  /* shouldn't get here, probably */
814  return TYPEFUNC_OTHER;
815 }
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:169
#define PointerGetDatum(X)
Definition: postgres.h:556
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:201
int errcode(int sqlerrcode)
Definition: elog.c:570
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:814
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:784
#define elog(elevel,...)
Definition: elog.h:226
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 277 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().

282 {
283  TypeFuncClass result;
284  HeapTuple tp;
285  Form_pg_proc procform;
286  Oid rettype;
287  Oid base_rettype;
288  TupleDesc tupdesc;
289 
290  /* First fetch the function's pg_proc row to inspect its rettype */
292  if (!HeapTupleIsValid(tp))
293  elog(ERROR, "cache lookup failed for function %u", funcid);
294  procform = (Form_pg_proc) GETSTRUCT(tp);
295 
296  rettype = procform->prorettype;
297 
298  /* Check for OUT parameters defining a RECORD result */
299  tupdesc = build_function_result_tupdesc_t(tp);
300  if (tupdesc)
301  {
302  /*
303  * It has OUT parameters, so it's basically like a regular composite
304  * type, except we have to be able to resolve any polymorphic OUT
305  * parameters.
306  */
307  if (resultTypeId)
308  *resultTypeId = rettype;
309 
310  if (resolve_polymorphic_tupdesc(tupdesc,
311  &procform->proargtypes,
312  call_expr))
313  {
314  if (tupdesc->tdtypeid == RECORDOID &&
315  tupdesc->tdtypmod < 0)
316  assign_record_type_typmod(tupdesc);
317  if (resultTupleDesc)
318  *resultTupleDesc = tupdesc;
319  result = TYPEFUNC_COMPOSITE;
320  }
321  else
322  {
323  if (resultTupleDesc)
324  *resultTupleDesc = NULL;
325  result = TYPEFUNC_RECORD;
326  }
327 
328  ReleaseSysCache(tp);
329 
330  return result;
331  }
332 
333  /*
334  * If scalar polymorphic result, try to resolve it.
335  */
336  if (IsPolymorphicType(rettype))
337  {
338  Oid newrettype = exprType(call_expr);
339 
340  if (newrettype == InvalidOid) /* this probably should not happen */
341  ereport(ERROR,
342  (errcode(ERRCODE_DATATYPE_MISMATCH),
343  errmsg("could not determine actual result type for function \"%s\" declared to return type %s",
344  NameStr(procform->proname),
345  format_type_be(rettype))));
346  rettype = newrettype;
347  }
348 
349  if (resultTypeId)
350  *resultTypeId = rettype;
351  if (resultTupleDesc)
352  *resultTupleDesc = NULL; /* default result */
353 
354  /* Classify the result type */
355  result = get_type_func_class(rettype, &base_rettype);
356  switch (result)
357  {
358  case TYPEFUNC_COMPOSITE:
360  if (resultTupleDesc)
361  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_rettype, -1);
362  /* Named composite types can't have any polymorphic columns */
363  break;
364  case TYPEFUNC_SCALAR:
365  break;
366  case TYPEFUNC_RECORD:
367  /* We must get the tupledesc from call context */
368  if (rsinfo && IsA(rsinfo, ReturnSetInfo) &&
369  rsinfo->expectedDesc != NULL)
370  {
371  result = TYPEFUNC_COMPOSITE;
372  if (resultTupleDesc)
373  *resultTupleDesc = rsinfo->expectedDesc;
374  /* Assume no polymorphic columns here, either */
375  }
376  break;
377  default:
378  break;
379  }
380 
381  ReleaseSysCache(tp);
382 
383  return result;
384 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:570
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:1161
#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:781
TypeFuncClass
Definition: funcapi.h:147
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
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:42
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define NameStr(name)
Definition: c.h:609
static bool resolve_polymorphic_tupdesc(TupleDesc tupdesc, oidvector *declared_args, Node *call_expr)
Definition: funcapi.c:433
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 1329 of file funcapi.c.

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

1330 {
1331  RangeVar *relvar;
1332  Relation rel;
1333  TupleDesc tupdesc;
1334  List *relname_list;
1335 
1336  /* Open relation and copy the tuple description */
1337  relname_list = stringToQualifiedNameList(relname);
1338  relvar = makeRangeVarFromNameList(relname_list);
1339  rel = relation_openrv(relvar, AccessShareLock);
1340  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1342 
1343  return tupdesc;
1344 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define RelationGetDescr(relation)
Definition: rel.h:442
#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 629 of file funcapi.c.

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

Referenced by plpgsql_resolve_polymorphic_argtypes().

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

435 {
436  int natts = tupdesc->natts;
437  int nargs = declared_args->dim1;
438  bool have_anyelement_result = false;
439  bool have_anyarray_result = false;
440  bool have_anyrange_result = false;
441  bool have_anynonarray = false;
442  bool have_anyenum = false;
443  Oid anyelement_type = InvalidOid;
444  Oid anyarray_type = InvalidOid;
445  Oid anyrange_type = InvalidOid;
446  Oid anycollation = InvalidOid;
447  int i;
448 
449  /* See if there are any polymorphic outputs; quick out if not */
450  for (i = 0; i < natts; i++)
451  {
452  switch (TupleDescAttr(tupdesc, i)->atttypid)
453  {
454  case ANYELEMENTOID:
455  have_anyelement_result = true;
456  break;
457  case ANYARRAYOID:
458  have_anyarray_result = true;
459  break;
460  case ANYNONARRAYOID:
461  have_anyelement_result = true;
462  have_anynonarray = true;
463  break;
464  case ANYENUMOID:
465  have_anyelement_result = true;
466  have_anyenum = true;
467  break;
468  case ANYRANGEOID:
469  have_anyrange_result = true;
470  break;
471  default:
472  break;
473  }
474  }
475  if (!have_anyelement_result && !have_anyarray_result &&
476  !have_anyrange_result)
477  return true;
478 
479  /*
480  * Otherwise, extract actual datatype(s) from input arguments. (We assume
481  * the parser already validated consistency of the arguments.)
482  */
483  if (!call_expr)
484  return false; /* no hope */
485 
486  for (i = 0; i < nargs; i++)
487  {
488  switch (declared_args->values[i])
489  {
490  case ANYELEMENTOID:
491  case ANYNONARRAYOID:
492  case ANYENUMOID:
493  if (!OidIsValid(anyelement_type))
494  anyelement_type = get_call_expr_argtype(call_expr, i);
495  break;
496  case ANYARRAYOID:
497  if (!OidIsValid(anyarray_type))
498  anyarray_type = get_call_expr_argtype(call_expr, i);
499  break;
500  case ANYRANGEOID:
501  if (!OidIsValid(anyrange_type))
502  anyrange_type = get_call_expr_argtype(call_expr, i);
503  break;
504  default:
505  break;
506  }
507  }
508 
509  /* If nothing found, parser messed up */
510  if (!OidIsValid(anyelement_type) && !OidIsValid(anyarray_type) &&
511  !OidIsValid(anyrange_type))
512  return false;
513 
514  /* If needed, deduce one polymorphic type from others */
515  if (have_anyelement_result && !OidIsValid(anyelement_type))
516  {
517  if (OidIsValid(anyarray_type))
518  anyelement_type = resolve_generic_type(ANYELEMENTOID,
519  anyarray_type,
520  ANYARRAYOID);
521  if (OidIsValid(anyrange_type))
522  {
523  Oid subtype = resolve_generic_type(ANYELEMENTOID,
524  anyrange_type,
525  ANYRANGEOID);
526 
527  /* check for inconsistent array and range results */
528  if (OidIsValid(anyelement_type) && anyelement_type != subtype)
529  return false;
530  anyelement_type = subtype;
531  }
532  }
533 
534  if (have_anyarray_result && !OidIsValid(anyarray_type))
535  anyarray_type = resolve_generic_type(ANYARRAYOID,
536  anyelement_type,
537  ANYELEMENTOID);
538 
539  /*
540  * We can't deduce a range type from other polymorphic inputs, because
541  * there may be multiple range types for the same subtype.
542  */
543  if (have_anyrange_result && !OidIsValid(anyrange_type))
544  return false;
545 
546  /* Enforce ANYNONARRAY if needed */
547  if (have_anynonarray && type_is_array(anyelement_type))
548  return false;
549 
550  /* Enforce ANYENUM if needed */
551  if (have_anyenum && !type_is_enum(anyelement_type))
552  return false;
553 
554  /*
555  * Identify the collation to use for polymorphic OUT parameters. (It'll
556  * necessarily be the same for both anyelement and anyarray.) Note that
557  * range types are not collatable, so any possible internal collation of a
558  * range type is not considered here.
559  */
560  if (OidIsValid(anyelement_type))
561  anycollation = get_typcollation(anyelement_type);
562  else if (OidIsValid(anyarray_type))
563  anycollation = get_typcollation(anyarray_type);
564 
565  if (OidIsValid(anycollation))
566  {
567  /*
568  * The types are collatable, so consider whether to use a nondefault
569  * collation. We do so if we can identify the input collation used
570  * for the function.
571  */
572  Oid inputcollation = exprInputCollation(call_expr);
573 
574  if (OidIsValid(inputcollation))
575  anycollation = inputcollation;
576  }
577 
578  /* And finally replace the tuple column types as needed */
579  for (i = 0; i < natts; i++)
580  {
581  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
582 
583  switch (att->atttypid)
584  {
585  case ANYELEMENTOID:
586  case ANYNONARRAYOID:
587  case ANYENUMOID:
588  TupleDescInitEntry(tupdesc, i + 1,
589  NameStr(att->attname),
590  anyelement_type,
591  -1,
592  0);
593  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
594  break;
595  case ANYARRAYOID:
596  TupleDescInitEntry(tupdesc, i + 1,
597  NameStr(att->attname),
598  anyarray_type,
599  -1,
600  0);
601  TupleDescInitEntryCollation(tupdesc, i + 1, anycollation);
602  break;
603  case ANYRANGEOID:
604  TupleDescInitEntry(tupdesc, i + 1,
605  NameStr(att->attname),
606  anyrange_type,
607  -1,
608  0);
609  /* no collation should be attached to a range type */
610  break;
611  default:
612  break;
613  }
614  }
615 
616  return true;
617 }
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:638
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:769
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:596
int dim1
Definition: c.h:594
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:925
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:609

◆ 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 1362 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.

1363 {
1364  Oid base_typeoid;
1365  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1366  TupleDesc tupdesc = NULL;
1367 
1368  /*
1369  * Build a suitable tupledesc representing the output rows. We
1370  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1371  * unlikely that legacy callers of this obsolete function would be
1372  * prepared to apply domain constraints.
1373  */
1374  if (functypclass == TYPEFUNC_COMPOSITE)
1375  {
1376  /* Composite data type, e.g. a table's row type */
1377  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1378 
1379  if (colaliases != NIL)
1380  {
1381  int natts = tupdesc->natts;
1382  int varattno;
1383 
1384  /* does the list length match the number of attributes? */
1385  if (list_length(colaliases) != natts)
1386  ereport(ERROR,
1387  (errcode(ERRCODE_DATATYPE_MISMATCH),
1388  errmsg("number of aliases does not match number of columns")));
1389 
1390  /* OK, use the aliases instead */
1391  for (varattno = 0; varattno < natts; varattno++)
1392  {
1393  char *label = strVal(list_nth(colaliases, varattno));
1394  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1395 
1396  if (label != NULL)
1397  namestrcpy(&(attr->attname), label);
1398  }
1399 
1400  /* The tuple type is now an anonymous record type */
1401  tupdesc->tdtypeid = RECORDOID;
1402  tupdesc->tdtypmod = -1;
1403  }
1404  }
1405  else if (functypclass == TYPEFUNC_SCALAR)
1406  {
1407  /* Base data type, i.e. scalar */
1408  char *attname;
1409 
1410  /* the alias list is required for base types */
1411  if (colaliases == NIL)
1412  ereport(ERROR,
1413  (errcode(ERRCODE_DATATYPE_MISMATCH),
1414  errmsg("no column alias was provided")));
1415 
1416  /* the alias list length must be 1 */
1417  if (list_length(colaliases) != 1)
1418  ereport(ERROR,
1419  (errcode(ERRCODE_DATATYPE_MISMATCH),
1420  errmsg("number of aliases does not match number of columns")));
1421 
1422  /* OK, get the column alias */
1423  attname = strVal(linitial(colaliases));
1424 
1425  tupdesc = CreateTemplateTupleDesc(1);
1426  TupleDescInitEntry(tupdesc,
1427  (AttrNumber) 1,
1428  attname,
1429  typeoid,
1430  -1,
1431  0);
1432  }
1433  else if (functypclass == TYPEFUNC_RECORD)
1434  {
1435  /* XXX can't support this because typmod wasn't passed in ... */
1436  ereport(ERROR,
1437  (errcode(ERRCODE_DATATYPE_MISMATCH),
1438  errmsg("could not determine row description for function returning record")));
1439  }
1440  else
1441  {
1442  /* crummy error message, but parser should have caught this */
1443  elog(ERROR, "function in FROM has unsupported return type");
1444  }
1445 
1446  return tupdesc;
1447 }
#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:570
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:781
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:90
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:784
#define elog(elevel,...)
Definition: elog.h:226
int16 AttrNumber
Definition: attnum.h:21
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1686