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

Go to the source code of this file.

Data Structures

struct  AttInMetadata
 
struct  FuncCallContext
 

Macros

#define TupleGetDatum(_slot, _tuple)   HeapTupleGetDatum(_tuple)
 
#define MAT_SRF_USE_EXPECTED_DESC   0x01 /* use expectedDesc as tupdesc. */
 
#define MAT_SRF_BLESS
 
#define SRF_IS_FIRSTCALL()   (fcinfo->flinfo->fn_extra == NULL)
 
#define SRF_FIRSTCALL_INIT()   init_MultiFuncCall(fcinfo)
 
#define SRF_PERCALL_SETUP()   per_MultiFuncCall(fcinfo)
 
#define SRF_RETURN_NEXT(_funcctx, _result)
 
#define SRF_RETURN_NEXT_NULL(_funcctx)
 
#define SRF_RETURN_DONE(_funcctx)
 

Typedefs

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

Enumerations

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

Functions

TypeFuncClass get_call_result_type (FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_expr_result_type (Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TypeFuncClass get_func_result_type (Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
 
TupleDesc get_expr_result_tupdesc (Node *expr, bool noError)
 
bool resolve_polymorphic_argtypes (int numargs, Oid *argtypes, char *argmodes, Node *call_expr)
 
int get_func_arg_info (HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
 
int get_func_input_arg_names (Datum proargnames, Datum proargmodes, char ***arg_names)
 
int get_func_trftypes (HeapTuple procTup, Oid **p_trftypes)
 
char * get_func_result_name (Oid functionId)
 
TupleDesc build_function_result_tupdesc_d (char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
 
TupleDesc build_function_result_tupdesc_t (HeapTuple procTuple)
 
TupleDesc RelationNameGetTupleDesc (const char *relname)
 
TupleDesc TypeGetTupleDesc (Oid typeoid, List *colaliases)
 
TupleDesc BlessTupleDesc (TupleDesc tupdesc)
 
AttInMetadataTupleDescGetAttInMetadata (TupleDesc tupdesc)
 
HeapTuple BuildTupleFromCStrings (AttInMetadata *attinmeta, char **values)
 
Datum HeapTupleHeaderGetDatum (HeapTupleHeader tuple)
 
static Datum HeapTupleGetDatum (const HeapTupleData *tuple)
 
void InitMaterializedSRF (FunctionCallInfo fcinfo, bits32 flags)
 
FuncCallContextinit_MultiFuncCall (PG_FUNCTION_ARGS)
 
FuncCallContextper_MultiFuncCall (PG_FUNCTION_ARGS)
 
void end_MultiFuncCall (PG_FUNCTION_ARGS, FuncCallContext *funcctx)
 
int extract_variadic_args (FunctionCallInfo fcinfo, int variadic_start, bool convert_unknown, Datum **args, Oid **types, bool **nulls)
 

Macro Definition Documentation

◆ MAT_SRF_BLESS

#define MAT_SRF_BLESS
Value:
0x02 /* "Bless" a tuple descriptor with
* BlessTupleDesc(). */

Definition at line 297 of file funcapi.h.

◆ MAT_SRF_USE_EXPECTED_DESC

#define MAT_SRF_USE_EXPECTED_DESC   0x01 /* use expectedDesc as tupdesc. */

Definition at line 296 of file funcapi.h.

◆ SRF_FIRSTCALL_INIT

#define SRF_FIRSTCALL_INIT ( )    init_MultiFuncCall(fcinfo)

Definition at line 306 of file funcapi.h.

◆ SRF_IS_FIRSTCALL

#define SRF_IS_FIRSTCALL ( )    (fcinfo->flinfo->fn_extra == NULL)

Definition at line 304 of file funcapi.h.

◆ SRF_PERCALL_SETUP

#define SRF_PERCALL_SETUP ( )    per_MultiFuncCall(fcinfo)

Definition at line 308 of file funcapi.h.

◆ SRF_RETURN_DONE

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

Definition at line 328 of file funcapi.h.

◆ SRF_RETURN_NEXT

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

Definition at line 310 of file funcapi.h.

◆ SRF_RETURN_NEXT_NULL

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

Definition at line 319 of file funcapi.h.

◆ TupleGetDatum

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

Definition at line 236 of file funcapi.h.

Typedef Documentation

◆ AttInMetadata

typedef struct AttInMetadata AttInMetadata

◆ FuncCallContext

◆ TypeFuncClass

Enumeration Type Documentation

◆ TypeFuncClass

Enumerator
TYPEFUNC_SCALAR 
TYPEFUNC_COMPOSITE 
TYPEFUNC_COMPOSITE_DOMAIN 
TYPEFUNC_RECORD 
TYPEFUNC_OTHER 

Definition at line 146 of file funcapi.h.

147 {
148  TYPEFUNC_SCALAR, /* scalar result type */
149  TYPEFUNC_COMPOSITE, /* determinable rowtype result */
150  TYPEFUNC_COMPOSITE_DOMAIN, /* domain over determinable rowtype result */
151  TYPEFUNC_RECORD, /* indeterminate rowtype result */
152  TYPEFUNC_OTHER, /* bogus type, eg pseudotype */
153 } TypeFuncClass;
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
@ TYPEFUNC_OTHER
Definition: funcapi.h:152

Function Documentation

◆ BlessTupleDesc()

TupleDesc BlessTupleDesc ( TupleDesc  tupdesc)

◆ build_function_result_tupdesc_d()

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

Definition at line 1751 of file funcapi.c.

1755 {
1756  TupleDesc desc;
1757  ArrayType *arr;
1758  int numargs;
1759  Oid *argtypes;
1760  char *argmodes;
1761  Datum *argnames = NULL;
1762  Oid *outargtypes;
1763  char **outargnames;
1764  int numoutargs;
1765  int nargnames;
1766  int i;
1767 
1768  /* Can't have output args if columns are null */
1769  if (proallargtypes == PointerGetDatum(NULL) ||
1770  proargmodes == PointerGetDatum(NULL))
1771  return NULL;
1772 
1773  /*
1774  * We expect the arrays to be 1-D arrays of the right types; verify that.
1775  * For the OID and char arrays, we don't need to use deconstruct_array()
1776  * since the array data is just going to look like a C array of values.
1777  */
1778  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1779  numargs = ARR_DIMS(arr)[0];
1780  if (ARR_NDIM(arr) != 1 ||
1781  numargs < 0 ||
1782  ARR_HASNULL(arr) ||
1783  ARR_ELEMTYPE(arr) != OIDOID)
1784  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1785  argtypes = (Oid *) ARR_DATA_PTR(arr);
1786  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1787  if (ARR_NDIM(arr) != 1 ||
1788  ARR_DIMS(arr)[0] != numargs ||
1789  ARR_HASNULL(arr) ||
1790  ARR_ELEMTYPE(arr) != CHAROID)
1791  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1792  numargs);
1793  argmodes = (char *) ARR_DATA_PTR(arr);
1794  if (proargnames != PointerGetDatum(NULL))
1795  {
1796  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1797  if (ARR_NDIM(arr) != 1 ||
1798  ARR_DIMS(arr)[0] != numargs ||
1799  ARR_HASNULL(arr) ||
1800  ARR_ELEMTYPE(arr) != TEXTOID)
1801  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1802  numargs);
1803  deconstruct_array_builtin(arr, TEXTOID, &argnames, NULL, &nargnames);
1804  Assert(nargnames == numargs);
1805  }
1806 
1807  /* zero elements probably shouldn't happen, but handle it gracefully */
1808  if (numargs <= 0)
1809  return NULL;
1810 
1811  /* extract output-argument types and names */
1812  outargtypes = (Oid *) palloc(numargs * sizeof(Oid));
1813  outargnames = (char **) palloc(numargs * sizeof(char *));
1814  numoutargs = 0;
1815  for (i = 0; i < numargs; i++)
1816  {
1817  char *pname;
1818 
1819  if (argmodes[i] == PROARGMODE_IN ||
1820  argmodes[i] == PROARGMODE_VARIADIC)
1821  continue;
1822  Assert(argmodes[i] == PROARGMODE_OUT ||
1823  argmodes[i] == PROARGMODE_INOUT ||
1824  argmodes[i] == PROARGMODE_TABLE);
1825  outargtypes[numoutargs] = argtypes[i];
1826  if (argnames)
1827  pname = TextDatumGetCString(argnames[i]);
1828  else
1829  pname = NULL;
1830  if (pname == NULL || pname[0] == '\0')
1831  {
1832  /* Parameter is not named, so gin up a column name */
1833  pname = psprintf("column%d", numoutargs + 1);
1834  }
1835  outargnames[numoutargs] = pname;
1836  numoutargs++;
1837  }
1838 
1839  /*
1840  * If there is no output argument, or only one, the function does not
1841  * return tuples.
1842  */
1843  if (numoutargs < 2 && prokind != PROKIND_PROCEDURE)
1844  return NULL;
1845 
1846  desc = CreateTemplateTupleDesc(numoutargs);
1847  for (i = 0; i < numoutargs; i++)
1848  {
1849  TupleDescInitEntry(desc, i + 1,
1850  outargnames[i],
1851  outargtypes[i],
1852  -1,
1853  0);
1854  }
1855 
1856  return desc;
1857 }
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_DATA_PTR(a)
Definition: array.h:322
#define DatumGetArrayTypeP(X)
Definition: array.h:261
#define ARR_ELEMTYPE(a)
Definition: array.h:292
#define ARR_DIMS(a)
Definition: array.h:294
#define ARR_HASNULL(a)
Definition: array.h:291
void deconstruct_array_builtin(ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3685
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define Assert(condition)
Definition: c.h:861
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1317
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:67
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:651

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

Referenced by build_function_result_tupdesc_t(), and ProcedureCreate().

◆ build_function_result_tupdesc_t()

TupleDesc build_function_result_tupdesc_t ( HeapTuple  procTuple)

Definition at line 1705 of file funcapi.c.

1706 {
1707  Form_pg_proc procform = (Form_pg_proc) GETSTRUCT(procTuple);
1708  Datum proallargtypes;
1709  Datum proargmodes;
1710  Datum proargnames;
1711  bool isnull;
1712 
1713  /* Return NULL if the function isn't declared to return RECORD */
1714  if (procform->prorettype != RECORDOID)
1715  return NULL;
1716 
1717  /* If there are no OUT parameters, return NULL */
1718  if (heap_attisnull(procTuple, Anum_pg_proc_proallargtypes, NULL) ||
1719  heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL))
1720  return NULL;
1721 
1722  /* Get the data out of the tuple */
1723  proallargtypes = SysCacheGetAttrNotNull(PROCOID, procTuple,
1724  Anum_pg_proc_proallargtypes);
1725  proargmodes = SysCacheGetAttrNotNull(PROCOID, procTuple,
1726  Anum_pg_proc_proargmodes);
1727  proargnames = SysCacheGetAttr(PROCOID, procTuple,
1728  Anum_pg_proc_proargnames,
1729  &isnull);
1730  if (isnull)
1731  proargnames = PointerGetDatum(NULL); /* just to be sure */
1732 
1733  return build_function_result_tupdesc_d(procform->prokind,
1734  proallargtypes,
1735  proargmodes,
1736  proargnames);
1737 }
TupleDesc build_function_result_tupdesc_d(char prokind, Datum proallargtypes, Datum proargmodes, Datum proargnames)
Definition: funcapi.c:1751
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:455
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:596
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:627

References build_function_result_tupdesc_d(), GETSTRUCT, heap_attisnull(), PointerGetDatum(), SysCacheGetAttr(), and SysCacheGetAttrNotNull().

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

◆ BuildTupleFromCStrings()

HeapTuple BuildTupleFromCStrings ( AttInMetadata attinmeta,
char **  values 
)

Definition at line 2222 of file execTuples.c.

2223 {
2224  TupleDesc tupdesc = attinmeta->tupdesc;
2225  int natts = tupdesc->natts;
2226  Datum *dvalues;
2227  bool *nulls;
2228  int i;
2229  HeapTuple tuple;
2230 
2231  dvalues = (Datum *) palloc(natts * sizeof(Datum));
2232  nulls = (bool *) palloc(natts * sizeof(bool));
2233 
2234  /*
2235  * Call the "in" function for each non-dropped attribute, even for nulls,
2236  * to support domains.
2237  */
2238  for (i = 0; i < natts; i++)
2239  {
2240  if (!TupleDescAttr(tupdesc, i)->attisdropped)
2241  {
2242  /* Non-dropped attributes */
2243  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2244  values[i],
2245  attinmeta->attioparams[i],
2246  attinmeta->atttypmods[i]);
2247  if (values[i] != NULL)
2248  nulls[i] = false;
2249  else
2250  nulls[i] = true;
2251  }
2252  else
2253  {
2254  /* Handle dropped attributes by setting to NULL */
2255  dvalues[i] = (Datum) 0;
2256  nulls[i] = true;
2257  }
2258  }
2259 
2260  /*
2261  * Form a tuple
2262  */
2263  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2264 
2265  /*
2266  * Release locally palloc'd space. XXX would probably be good to pfree
2267  * values of pass-by-reference datums, as well.
2268  */
2269  pfree(dvalues);
2270  pfree(nulls);
2271 
2272  return tuple;
2273 }
static Datum values[MAXATTR]
Definition: bootstrap.c:150
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1530
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void pfree(void *pointer)
Definition: mcxt.c:1521
FmgrInfo * attinfuncs
Definition: funcapi.h:41
TupleDesc tupdesc
Definition: funcapi.h:38
Oid * attioparams
Definition: funcapi.h:44
int32 * atttypmods
Definition: funcapi.h:47
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References AttInMetadata::attinfuncs, AttInMetadata::attioparams, AttInMetadata::atttypmods, heap_form_tuple(), i, InputFunctionCall(), TupleDescData::natts, palloc(), pfree(), AttInMetadata::tupdesc, TupleDescAttr, and values.

Referenced by bt_metap(), bt_multi_page_stats(), bt_page_stats_internal(), build_pgstattuple_type(), build_tuplestore_recursively(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), libpqrcv_processTuples(), materializeResult(), pg_get_keywords(), pg_get_multixact_members(), pgp_armor_headers(), pgrowlocks(), pgstatindex_impl(), pltcl_build_tuple_result(), prs_process_call(), show_all_settings(), storeRow(), ts_process_call(), tt_process_call(), and xpath_table().

◆ end_MultiFuncCall()

void end_MultiFuncCall ( PG_FUNCTION_ARGS  ,
FuncCallContext funcctx 
)

Definition at line 220 of file funcapi.c.

221 {
222  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
223 
224  /* Deregister the shutdown callback */
227  PointerGetDatum(fcinfo->flinfo));
228 
229  /* But use it to do the real work */
230  shutdown_MultiFuncCall(PointerGetDatum(fcinfo->flinfo));
231 }
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:923
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:238
ExprContext * econtext
Definition: execnodes.h:335

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

◆ extract_variadic_args()

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

Definition at line 2005 of file funcapi.c.

2008 {
2009  bool variadic = get_fn_expr_variadic(fcinfo->flinfo);
2010  Datum *args_res;
2011  bool *nulls_res;
2012  Oid *types_res;
2013  int nargs,
2014  i;
2015 
2016  *args = NULL;
2017  *types = NULL;
2018  *nulls = NULL;
2019 
2020  if (variadic)
2021  {
2023  Oid element_type;
2024  bool typbyval;
2025  char typalign;
2026  int16 typlen;
2027 
2028  Assert(PG_NARGS() == variadic_start + 1);
2029 
2030  if (PG_ARGISNULL(variadic_start))
2031  return -1;
2032 
2033  array_in = PG_GETARG_ARRAYTYPE_P(variadic_start);
2034  element_type = ARR_ELEMTYPE(array_in);
2035 
2036  get_typlenbyvalalign(element_type,
2037  &typlen, &typbyval, &typalign);
2038  deconstruct_array(array_in, element_type, typlen, typbyval,
2039  typalign, &args_res, &nulls_res,
2040  &nargs);
2041 
2042  /* All the elements of the array have the same type */
2043  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
2044  for (i = 0; i < nargs; i++)
2045  types_res[i] = element_type;
2046  }
2047  else
2048  {
2049  nargs = PG_NARGS() - variadic_start;
2050  Assert(nargs > 0);
2051  nulls_res = (bool *) palloc0(nargs * sizeof(bool));
2052  args_res = (Datum *) palloc0(nargs * sizeof(Datum));
2053  types_res = (Oid *) palloc0(nargs * sizeof(Oid));
2054 
2055  for (i = 0; i < nargs; i++)
2056  {
2057  nulls_res[i] = PG_ARGISNULL(i + variadic_start);
2058  types_res[i] = get_fn_expr_argtype(fcinfo->flinfo,
2059  i + variadic_start);
2060 
2061  /*
2062  * Turn a constant (more or less literal) value that's of unknown
2063  * type into text if required. Unknowns come in as a cstring
2064  * pointer. Note: for functions declared as taking type "any", the
2065  * parser will not do any type conversion on unknown-type literals
2066  * (that is, undecorated strings or NULLs).
2067  */
2068  if (convert_unknown &&
2069  types_res[i] == UNKNOWNOID &&
2070  get_fn_expr_arg_stable(fcinfo->flinfo, i + variadic_start))
2071  {
2072  types_res[i] = TEXTOID;
2073 
2074  if (PG_ARGISNULL(i + variadic_start))
2075  args_res[i] = (Datum) 0;
2076  else
2077  args_res[i] =
2078  CStringGetTextDatum(PG_GETARG_POINTER(i + variadic_start));
2079  }
2080  else
2081  {
2082  /* no conversion needed, just take the datum as given */
2083  args_res[i] = PG_GETARG_DATUM(i + variadic_start);
2084  }
2085 
2086  if (!OidIsValid(types_res[i]) ||
2087  (convert_unknown && types_res[i] == UNKNOWNOID))
2088  ereport(ERROR,
2089  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2090  errmsg("could not determine data type for argument %d",
2091  i + 1)));
2092  }
2093  }
2094 
2095  /* Fill in results */
2096  *args = args_res;
2097  *nulls = nulls_res;
2098  *types = types_res;
2099 
2100  return nargs;
2101 }
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3619
Datum array_in(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:179
#define CStringGetTextDatum(s)
Definition: builtins.h:97
signed short int16
Definition: c.h:496
#define OidIsValid(objectId)
Definition: c.h:778
struct typedefs * types
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1975
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition: fmgr.c:2044
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1910
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_NARGS()
Definition: fmgr.h:203
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition: lsyscache.c:2271
void * palloc0(Size size)
Definition: mcxt.c:1347
char typalign
Definition: pg_type.h:176
FmgrInfo * flinfo
Definition: fmgr.h:87

References generate_unaccent_rules::args, 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, typalign, and types.

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

◆ get_call_result_type()

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

Definition at line 276 of file funcapi.c.

279 {
280  return internal_get_result_type(fcinfo->flinfo->fn_oid,
281  fcinfo->flinfo->fn_expr,
282  (ReturnSetInfo *) fcinfo->resultinfo,
283  resultTypeId,
284  resultTupleDesc);
285 }
static TypeFuncClass internal_get_result_type(Oid funcid, Node *call_expr, ReturnSetInfo *rsinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:430
fmNodePtr fn_expr
Definition: fmgr.h:66
Oid fn_oid
Definition: fmgr.h:59
fmNodePtr resultinfo
Definition: fmgr.h:89

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

Referenced by brin_metapage_info(), bt_metap(), bt_multi_page_stats(), bt_page_items_bytea(), bt_page_items_internal(), bt_page_stats_internal(), build_pgstattuple_type(), copy_replication_slot(), crosstab(), get_record_type_from_query(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), init_sql_fcache(), InitMaterializedSRF(), materializeResult(), page_header(), pg_backup_stop(), pg_buffercache_pages(), pg_buffercache_summary(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_catalog_foreign_keys(), pg_get_keywords(), pg_get_multixact_members(), pg_get_object_address(), pg_get_wal_record_info(), pg_get_wal_summarizer_state(), pg_identify_object(), pg_identify_object_as_address(), pg_input_error_info(), pg_last_committed_xact(), pg_partition_tree(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_split_walfile_name(), pg_stat_get_wal_receiver(), pg_stat_statements_info(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility_map_summary(), pg_xact_commit_timestamp_origin(), pgp_armor_headers(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_return_next_internal(), plperl_sv_to_datum(), plpgsql_exec_function(), pltcl_func_handler(), PLy_exec_function(), prs_setup_firstcall(), setup_firstcall(), ssl_extension_info(), storeRow(), test_enc_conversion(), ts_setup_firstcall(), tsvector_unnest(), and tt_setup_firstcall().

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 551 of file funcapi.c.

552 {
553  TupleDesc tupleDesc;
554  TypeFuncClass functypclass;
555 
556  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
557 
558  if (functypclass == TYPEFUNC_COMPOSITE ||
559  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
560  return tupleDesc;
561 
562  if (!noError)
563  {
564  Oid exprTypeId = exprType(expr);
565 
566  if (exprTypeId != RECORDOID)
567  ereport(ERROR,
568  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
569  errmsg("type %s is not composite",
570  format_type_be(exprTypeId))));
571  else
572  ereport(ERROR,
573  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
574  errmsg("record type has not been registered")));
575  }
576 
577  return NULL;
578 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:299
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42

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(), ParseComplexProjection(), and process_function_rte_ref().

◆ get_expr_result_type()

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

Definition at line 299 of file funcapi.c.

302 {
303  TypeFuncClass result;
304 
305  if (expr && IsA(expr, FuncExpr))
306  result = internal_get_result_type(((FuncExpr *) expr)->funcid,
307  expr,
308  NULL,
309  resultTypeId,
310  resultTupleDesc);
311  else if (expr && IsA(expr, OpExpr))
312  result = internal_get_result_type(get_opcode(((OpExpr *) expr)->opno),
313  expr,
314  NULL,
315  resultTypeId,
316  resultTupleDesc);
317  else if (expr && IsA(expr, RowExpr) &&
318  ((RowExpr *) expr)->row_typeid == RECORDOID)
319  {
320  /* We can resolve the record type by generating the tupdesc directly */
321  RowExpr *rexpr = (RowExpr *) expr;
322  TupleDesc tupdesc;
323  AttrNumber i = 1;
324  ListCell *lcc,
325  *lcn;
326 
327  tupdesc = CreateTemplateTupleDesc(list_length(rexpr->args));
328  Assert(list_length(rexpr->args) == list_length(rexpr->colnames));
329  forboth(lcc, rexpr->args, lcn, rexpr->colnames)
330  {
331  Node *col = (Node *) lfirst(lcc);
332  char *colname = strVal(lfirst(lcn));
333 
334  TupleDescInitEntry(tupdesc, i,
335  colname,
336  exprType(col),
337  exprTypmod(col),
338  0);
340  exprCollation(col));
341  i++;
342  }
343  if (resultTypeId)
344  *resultTypeId = rexpr->row_typeid;
345  if (resultTupleDesc)
346  *resultTupleDesc = BlessTupleDesc(tupdesc);
347  return TYPEFUNC_COMPOSITE;
348  }
349  else if (expr && IsA(expr, Const) &&
350  ((Const *) expr)->consttype == RECORDOID &&
351  !((Const *) expr)->constisnull)
352  {
353  /*
354  * When EXPLAIN'ing some queries with SEARCH/CYCLE clauses, we may
355  * need to resolve field names of a RECORD-type Const. The datum
356  * should contain a typmod that will tell us that.
357  */
358  HeapTupleHeader rec;
359  Oid tupType;
360  int32 tupTypmod;
361 
362  rec = DatumGetHeapTupleHeader(((Const *) expr)->constvalue);
363  tupType = HeapTupleHeaderGetTypeId(rec);
364  tupTypmod = HeapTupleHeaderGetTypMod(rec);
365  if (resultTypeId)
366  *resultTypeId = tupType;
367  if (tupType != RECORDOID || tupTypmod >= 0)
368  {
369  /* Should be able to look it up */
370  if (resultTupleDesc)
371  *resultTupleDesc = lookup_rowtype_tupdesc_copy(tupType,
372  tupTypmod);
373  return TYPEFUNC_COMPOSITE;
374  }
375  else
376  {
377  /* This shouldn't really happen ... */
378  if (resultTupleDesc)
379  *resultTupleDesc = NULL;
380  return TYPEFUNC_RECORD;
381  }
382  }
383  else
384  {
385  /* handle as a generic expression; no chance to resolve RECORD */
386  Oid typid = exprType(expr);
387  Oid base_typid;
388 
389  if (resultTypeId)
390  *resultTypeId = typid;
391  if (resultTupleDesc)
392  *resultTupleDesc = NULL;
393  result = get_type_func_class(typid, &base_typid);
394  if ((result == TYPEFUNC_COMPOSITE ||
395  result == TYPEFUNC_COMPOSITE_DOMAIN) &&
396  resultTupleDesc)
397  *resultTupleDesc = lookup_rowtype_tupdesc_copy(base_typid, -1);
398  }
399 
400  return result;
401 }
int16 AttrNumber
Definition: attnum.h:21
signed int int32
Definition: c.h:497
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2158
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
static TypeFuncClass get_type_func_class(Oid typid, Oid *base_typeid)
Definition: funcapi.c:1328
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:466
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:456
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1285
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
Definition: nodes.h:129
List * args
Definition: primnodes.h:1411
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:833
TupleDesc lookup_rowtype_tupdesc_copy(Oid type_id, int32 typmod)
Definition: typcache.c:1884
#define strVal(v)
Definition: value.h:82

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

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

◆ get_func_arg_info()

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

Definition at line 1379 of file funcapi.c.

1381 {
1382  Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1383  Datum proallargtypes;
1384  Datum proargmodes;
1385  Datum proargnames;
1386  bool isNull;
1387  ArrayType *arr;
1388  int numargs;
1389  Datum *elems;
1390  int nelems;
1391  int i;
1392 
1393  /* First discover the total number of parameters and get their types */
1394  proallargtypes = SysCacheGetAttr(PROCOID, procTup,
1395  Anum_pg_proc_proallargtypes,
1396  &isNull);
1397  if (!isNull)
1398  {
1399  /*
1400  * We expect the arrays to be 1-D arrays of the right types; verify
1401  * that. For the OID and char arrays, we don't need to use
1402  * deconstruct_array() since the array data is just going to look like
1403  * a C array of values.
1404  */
1405  arr = DatumGetArrayTypeP(proallargtypes); /* ensure not toasted */
1406  numargs = ARR_DIMS(arr)[0];
1407  if (ARR_NDIM(arr) != 1 ||
1408  numargs < 0 ||
1409  ARR_HASNULL(arr) ||
1410  ARR_ELEMTYPE(arr) != OIDOID)
1411  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
1412  Assert(numargs >= procStruct->pronargs);
1413  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1414  memcpy(*p_argtypes, ARR_DATA_PTR(arr),
1415  numargs * sizeof(Oid));
1416  }
1417  else
1418  {
1419  /* If no proallargtypes, use proargtypes */
1420  numargs = procStruct->proargtypes.dim1;
1421  Assert(numargs == procStruct->pronargs);
1422  *p_argtypes = (Oid *) palloc(numargs * sizeof(Oid));
1423  memcpy(*p_argtypes, procStruct->proargtypes.values,
1424  numargs * sizeof(Oid));
1425  }
1426 
1427  /* Get argument names, if available */
1428  proargnames = SysCacheGetAttr(PROCOID, procTup,
1429  Anum_pg_proc_proargnames,
1430  &isNull);
1431  if (isNull)
1432  *p_argnames = NULL;
1433  else
1434  {
1435  deconstruct_array_builtin(DatumGetArrayTypeP(proargnames), TEXTOID,
1436  &elems, NULL, &nelems);
1437  if (nelems != numargs) /* should not happen */
1438  elog(ERROR, "proargnames must have the same number of elements as the function has arguments");
1439  *p_argnames = (char **) palloc(sizeof(char *) * numargs);
1440  for (i = 0; i < numargs; i++)
1441  (*p_argnames)[i] = TextDatumGetCString(elems[i]);
1442  }
1443 
1444  /* Get argument modes, if available */
1445  proargmodes = SysCacheGetAttr(PROCOID, procTup,
1446  Anum_pg_proc_proargmodes,
1447  &isNull);
1448  if (isNull)
1449  *p_argmodes = NULL;
1450  else
1451  {
1452  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1453  if (ARR_NDIM(arr) != 1 ||
1454  ARR_DIMS(arr)[0] != numargs ||
1455  ARR_HASNULL(arr) ||
1456  ARR_ELEMTYPE(arr) != CHAROID)
1457  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1458  numargs);
1459  *p_argmodes = (char *) palloc(numargs * sizeof(char));
1460  memcpy(*p_argmodes, ARR_DATA_PTR(arr),
1461  numargs * sizeof(char));
1462  }
1463 
1464  return numargs;
1465 }

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

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

◆ get_func_input_arg_names()

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

Definition at line 1522 of file funcapi.c.

1524 {
1525  ArrayType *arr;
1526  int numargs;
1527  Datum *argnames;
1528  char *argmodes;
1529  char **inargnames;
1530  int numinargs;
1531  int i;
1532 
1533  /* Do nothing if null proargnames */
1534  if (proargnames == PointerGetDatum(NULL))
1535  {
1536  *arg_names = NULL;
1537  return 0;
1538  }
1539 
1540  /*
1541  * We expect the arrays to be 1-D arrays of the right types; verify that.
1542  * For proargmodes, we don't need to use deconstruct_array() since the
1543  * array data is just going to look like a C array of values.
1544  */
1545  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1546  if (ARR_NDIM(arr) != 1 ||
1547  ARR_HASNULL(arr) ||
1548  ARR_ELEMTYPE(arr) != TEXTOID)
1549  elog(ERROR, "proargnames is not a 1-D text array or it contains nulls");
1550  deconstruct_array_builtin(arr, TEXTOID, &argnames, NULL, &numargs);
1551  if (proargmodes != PointerGetDatum(NULL))
1552  {
1553  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1554  if (ARR_NDIM(arr) != 1 ||
1555  ARR_DIMS(arr)[0] != numargs ||
1556  ARR_HASNULL(arr) ||
1557  ARR_ELEMTYPE(arr) != CHAROID)
1558  elog(ERROR, "proargmodes is not a 1-D char array of length %d or it contains nulls",
1559  numargs);
1560  argmodes = (char *) ARR_DATA_PTR(arr);
1561  }
1562  else
1563  argmodes = NULL;
1564 
1565  /* zero elements probably shouldn't happen, but handle it gracefully */
1566  if (numargs <= 0)
1567  {
1568  *arg_names = NULL;
1569  return 0;
1570  }
1571 
1572  /* extract input-argument names */
1573  inargnames = (char **) palloc(numargs * sizeof(char *));
1574  numinargs = 0;
1575  for (i = 0; i < numargs; i++)
1576  {
1577  if (argmodes == NULL ||
1578  argmodes[i] == PROARGMODE_IN ||
1579  argmodes[i] == PROARGMODE_INOUT ||
1580  argmodes[i] == PROARGMODE_VARIADIC)
1581  {
1582  char *pname = TextDatumGetCString(argnames[i]);
1583 
1584  if (pname[0] != '\0')
1585  inargnames[numinargs] = pname;
1586  else
1587  inargnames[numinargs] = NULL;
1588  numinargs++;
1589  }
1590  }
1591 
1592  *arg_names = inargnames;
1593  return numinargs;
1594 }

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

Referenced by prepare_sql_fn_parse_info(), and ProcedureCreate().

◆ get_func_result_name()

char* get_func_result_name ( Oid  functionId)

Definition at line 1607 of file funcapi.c.

1608 {
1609  char *result;
1610  HeapTuple procTuple;
1611  Datum proargmodes;
1612  Datum proargnames;
1613  ArrayType *arr;
1614  int numargs;
1615  char *argmodes;
1616  Datum *argnames;
1617  int numoutargs;
1618  int nargnames;
1619  int i;
1620 
1621  /* First fetch the function's pg_proc row */
1622  procTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
1623  if (!HeapTupleIsValid(procTuple))
1624  elog(ERROR, "cache lookup failed for function %u", functionId);
1625 
1626  /* If there are no named OUT parameters, return NULL */
1627  if (heap_attisnull(procTuple, Anum_pg_proc_proargmodes, NULL) ||
1628  heap_attisnull(procTuple, Anum_pg_proc_proargnames, NULL))
1629  result = NULL;
1630  else
1631  {
1632  /* Get the data out of the tuple */
1633  proargmodes = SysCacheGetAttrNotNull(PROCOID, procTuple,
1634  Anum_pg_proc_proargmodes);
1635  proargnames = SysCacheGetAttrNotNull(PROCOID, procTuple,
1636  Anum_pg_proc_proargnames);
1637 
1638  /*
1639  * We expect the arrays to be 1-D arrays of the right types; verify
1640  * that. For the char array, we don't need to use deconstruct_array()
1641  * since the array data is just going to look like a C array of
1642  * values.
1643  */
1644  arr = DatumGetArrayTypeP(proargmodes); /* ensure not toasted */
1645  numargs = ARR_DIMS(arr)[0];
1646  if (ARR_NDIM(arr) != 1 ||
1647  numargs < 0 ||
1648  ARR_HASNULL(arr) ||
1649  ARR_ELEMTYPE(arr) != CHAROID)
1650  elog(ERROR, "proargmodes is not a 1-D char array or it contains nulls");
1651  argmodes = (char *) ARR_DATA_PTR(arr);
1652  arr = DatumGetArrayTypeP(proargnames); /* ensure not toasted */
1653  if (ARR_NDIM(arr) != 1 ||
1654  ARR_DIMS(arr)[0] != numargs ||
1655  ARR_HASNULL(arr) ||
1656  ARR_ELEMTYPE(arr) != TEXTOID)
1657  elog(ERROR, "proargnames is not a 1-D text array of length %d or it contains nulls",
1658  numargs);
1659  deconstruct_array_builtin(arr, TEXTOID, &argnames, NULL, &nargnames);
1660  Assert(nargnames == numargs);
1661 
1662  /* scan for output argument(s) */
1663  result = NULL;
1664  numoutargs = 0;
1665  for (i = 0; i < numargs; i++)
1666  {
1667  if (argmodes[i] == PROARGMODE_IN ||
1668  argmodes[i] == PROARGMODE_VARIADIC)
1669  continue;
1670  Assert(argmodes[i] == PROARGMODE_OUT ||
1671  argmodes[i] == PROARGMODE_INOUT ||
1672  argmodes[i] == PROARGMODE_TABLE);
1673  if (++numoutargs > 1)
1674  {
1675  /* multiple out args, so forget it */
1676  result = NULL;
1677  break;
1678  }
1679  result = TextDatumGetCString(argnames[i]);
1680  if (result == NULL || result[0] == '\0')
1681  {
1682  /* Parameter is not named, so forget it */
1683  result = NULL;
1684  break;
1685  }
1686  }
1687  }
1688 
1689  ReleaseSysCache(procTuple);
1690 
1691  return result;
1692 }
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

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

Referenced by chooseScalarFunctionAlias().

◆ get_func_result_type()

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

Definition at line 410 of file funcapi.c.

413 {
414  return internal_get_result_type(functionId,
415  NULL,
416  NULL,
417  resultTypeId,
418  resultTupleDesc);
419 }

References internal_get_result_type().

Referenced by fmgr_sql_validator().

◆ get_func_trftypes()

int get_func_trftypes ( HeapTuple  procTup,
Oid **  p_trftypes 
)

Definition at line 1475 of file funcapi.c.

1477 {
1478  Datum protrftypes;
1479  ArrayType *arr;
1480  int nelems;
1481  bool isNull;
1482 
1483  protrftypes = SysCacheGetAttr(PROCOID, procTup,
1484  Anum_pg_proc_protrftypes,
1485  &isNull);
1486  if (!isNull)
1487  {
1488  /*
1489  * We expect the arrays to be 1-D arrays of the right types; verify
1490  * that. For the OID and char arrays, we don't need to use
1491  * deconstruct_array() since the array data is just going to look like
1492  * a C array of values.
1493  */
1494  arr = DatumGetArrayTypeP(protrftypes); /* ensure not toasted */
1495  nelems = ARR_DIMS(arr)[0];
1496  if (ARR_NDIM(arr) != 1 ||
1497  nelems < 0 ||
1498  ARR_HASNULL(arr) ||
1499  ARR_ELEMTYPE(arr) != OIDOID)
1500  elog(ERROR, "protrftypes is not a 1-D Oid array or it contains nulls");
1501  *p_trftypes = (Oid *) palloc(nelems * sizeof(Oid));
1502  memcpy(*p_trftypes, ARR_DATA_PTR(arr),
1503  nelems * sizeof(Oid));
1504 
1505  return nelems;
1506  }
1507  else
1508  return 0;
1509 }

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

Referenced by print_function_trftypes().

◆ HeapTupleGetDatum()

static Datum HeapTupleGetDatum ( const HeapTupleData tuple)
inlinestatic

Definition at line 230 of file funcapi.h.

231 {
232  return HeapTupleHeaderGetDatum(tuple->t_data);
233 }
Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple)
Definition: execTuples.c:2311
HeapTupleHeader t_data
Definition: htup.h:68

References HeapTupleHeaderGetDatum(), and HeapTupleData::t_data.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), bt_multi_page_stats(), bt_page_print_tuples(), bt_page_stats_internal(), build_pgstattuple_type(), copy_replication_slot(), dblink_get_pkey(), exec_eval_datum(), ExecEvalConvertRowtype(), ExecEvalFieldStoreForm(), ExecEvalRow(), gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), gist_page_opaque_info(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), heap_page_items(), heap_tuple_infomask_flags(), hstore_each(), hstore_populate_record(), injection_points_stats_fixed(), page_header(), pg_backup_stop(), pg_buffercache_pages(), pg_buffercache_summary(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_get_catalog_foreign_keys(), pg_get_keywords(), pg_get_multixact_members(), pg_get_object_address(), pg_get_publication_tables(), pg_get_sequence_data(), pg_get_wal_record_info(), pg_get_wal_summarizer_state(), pg_identify_object(), pg_identify_object_as_address(), pg_input_error_info(), pg_last_committed_xact(), pg_lock_status(), pg_partition_tree(), pg_prepared_xact(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_split_walfile_name(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_backend_subxact(), pg_stat_get_replication_slot(), pg_stat_get_subscription_stats(), pg_stat_get_wal(), pg_stat_get_wal_receiver(), pg_stat_statements_info(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility(), pg_visibility_map(), pg_visibility_map_rel(), pg_visibility_map_summary(), pg_visibility_rel(), pg_walfile_name_offset(), pg_xact_commit_timestamp_origin(), pgp_armor_headers(), pgstatginindex_internal(), pgstathashindex(), pgstatindex_impl(), pgstattuple_approx_internal(), plperl_hash_to_datum(), plperl_return_next_internal(), pltcl_build_tuple_result(), pltcl_func_handler(), prs_process_call(), show_all_settings(), ssl_extension_info(), test_enc_conversion(), test_predtest(), ts_process_call(), tsvector_unnest(), and tt_process_call().

◆ HeapTupleHeaderGetDatum()

Datum HeapTupleHeaderGetDatum ( HeapTupleHeader  tuple)

Definition at line 2311 of file execTuples.c.

2312 {
2313  Datum result;
2314  TupleDesc tupDesc;
2315 
2316  /* No work if there are no external TOAST pointers in the tuple */
2317  if (!HeapTupleHeaderHasExternal(tuple))
2318  return PointerGetDatum(tuple);
2319 
2320  /* Use the type data saved by heap_form_tuple to look up the rowtype */
2322  HeapTupleHeaderGetTypMod(tuple));
2323 
2324  /* And do the flattening */
2325  result = toast_flatten_tuple_to_datum(tuple,
2327  tupDesc);
2328 
2329  ReleaseTupleDesc(tupDesc);
2330 
2331  return result;
2332 }
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: heaptoast.c:449
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:537
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1850

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

Referenced by HeapTupleGetDatum(), populate_composite(), and populate_recordset_record().

◆ init_MultiFuncCall()

FuncCallContext* init_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 133 of file funcapi.c.

134 {
135  FuncCallContext *retval;
136 
137  /*
138  * Bail if we're called in the wrong context
139  */
140  if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
141  ereport(ERROR,
142  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
143  errmsg("set-valued function called in context that cannot accept a set")));
144 
145  if (fcinfo->flinfo->fn_extra == NULL)
146  {
147  /*
148  * First call
149  */
150  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
151  MemoryContext multi_call_ctx;
152 
153  /*
154  * Create a suitably long-lived context to hold cross-call data
155  */
156  multi_call_ctx = AllocSetContextCreate(fcinfo->flinfo->fn_mcxt,
157  "SRF multi-call context",
159 
160  /*
161  * Allocate suitably long-lived space and zero it
162  */
163  retval = (FuncCallContext *)
164  MemoryContextAllocZero(multi_call_ctx,
165  sizeof(FuncCallContext));
166 
167  /*
168  * initialize the elements
169  */
170  retval->call_cntr = 0;
171  retval->max_calls = 0;
172  retval->user_fctx = NULL;
173  retval->attinmeta = NULL;
174  retval->tuple_desc = NULL;
175  retval->multi_call_memory_ctx = multi_call_ctx;
176 
177  /*
178  * save the pointer for cross-call use
179  */
180  fcinfo->flinfo->fn_extra = retval;
181 
182  /*
183  * Ensure we will get shut down cleanly if the exprcontext is not run
184  * to completion.
185  */
188  PointerGetDatum(fcinfo->flinfo));
189  }
190  else
191  {
192  /* second and subsequent calls */
193  elog(ERROR, "init_MultiFuncCall cannot be called more than once");
194 
195  /* never reached, but keep compiler happy */
196  retval = NULL;
197  }
198 
199  return retval;
200 }
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:897
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112

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.

◆ InitMaterializedSRF()

void InitMaterializedSRF ( FunctionCallInfo  fcinfo,
bits32  flags 
)

Definition at line 76 of file funcapi.c.

77 {
78  bool random_access;
79  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
80  Tuplestorestate *tupstore;
81  MemoryContext old_context,
82  per_query_ctx;
83  TupleDesc stored_tupdesc;
84 
85  /* check to see if caller supports returning a tuplestore */
86  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
87  ereport(ERROR,
88  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
89  errmsg("set-valued function called in context that cannot accept a set")));
90  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
91  ((flags & MAT_SRF_USE_EXPECTED_DESC) != 0 && rsinfo->expectedDesc == NULL))
92  ereport(ERROR,
93  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94  errmsg("materialize mode required, but it is not allowed in this context")));
95 
96  /*
97  * Store the tuplestore and the tuple descriptor in ReturnSetInfo. This
98  * must be done in the per-query memory context.
99  */
100  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
101  old_context = MemoryContextSwitchTo(per_query_ctx);
102 
103  /* build a tuple descriptor for our result type */
104  if ((flags & MAT_SRF_USE_EXPECTED_DESC) != 0)
105  stored_tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
106  else
107  {
108  if (get_call_result_type(fcinfo, NULL, &stored_tupdesc) != TYPEFUNC_COMPOSITE)
109  elog(ERROR, "return type must be a row type");
110  }
111 
112  /* If requested, bless the tuple descriptor */
113  if ((flags & MAT_SRF_BLESS) != 0)
114  BlessTupleDesc(stored_tupdesc);
115 
116  random_access = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
117 
118  tupstore = tuplestore_begin_heap(random_access, false, work_mem);
119  rsinfo->returnMode = SFRM_Materialize;
120  rsinfo->setResult = tupstore;
121  rsinfo->setDesc = stored_tupdesc;
122  MemoryContextSwitchTo(old_context);
123 }
@ SFRM_Materialize_Random
Definition: execnodes.h:321
@ SFRM_Materialize
Definition: execnodes.h:320
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define MAT_SRF_BLESS
Definition: funcapi.h:297
#define MAT_SRF_USE_EXPECTED_DESC
Definition: funcapi.h:296
int work_mem
Definition: globals.c:130
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
MemoryContextSwitchTo(old_ctx)
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:265
SetFunctionReturnMode returnMode
Definition: execnodes.h:339
TupleDesc setDesc
Definition: execnodes.h:343
Tuplestorestate * setResult
Definition: execnodes.h:342
TupleDesc expectedDesc
Definition: execnodes.h:336
int allowedModes
Definition: execnodes.h:337
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:330

References ReturnSetInfo::allowedModes, BlessTupleDesc(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, get_call_result_type(), if(), IsA, MAT_SRF_BLESS, MAT_SRF_USE_EXPECTED_DESC, MemoryContextSwitchTo(), FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, tuplestore_begin_heap(), TYPEFUNC_COMPOSITE, and work_mem.

Referenced by brin_page_items(), dblink_get_notify(), each_worker(), each_worker_jsonb(), elements_worker(), elements_worker_jsonb(), get_altertable_subcmdinfo(), GetWALRecordsInfo(), gist_page_items(), gist_page_items_bytea(), pg_available_extension_versions(), pg_available_extensions(), pg_available_wal_summaries(), pg_config(), pg_cursor(), pg_event_trigger_ddl_commands(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_get_backend_memory_contexts(), pg_get_replication_slots(), pg_get_shmem_allocations(), pg_get_wait_events(), pg_get_wal_block_info(), pg_hba_file_rules(), pg_ident_file_mappings(), pg_logical_slot_get_changes_guts(), pg_ls_dir(), pg_ls_dir_files(), pg_prepared_statement(), pg_show_replication_origin_status(), pg_stat_get_activity(), pg_stat_get_io(), pg_stat_get_progress_info(), pg_stat_get_recovery_prefetch(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pg_tablespace_databases(), pg_wal_summary_contents(), pgrowlocks(), postgres_fdw_get_connections_internal(), show_all_file_settings(), text_to_table(), verify_heapam(), and xpath_table().

◆ per_MultiFuncCall()

FuncCallContext* per_MultiFuncCall ( PG_FUNCTION_ARGS  )

Definition at line 208 of file funcapi.c.

209 {
210  FuncCallContext *retval = (FuncCallContext *) fcinfo->flinfo->fn_extra;
211 
212  return retval;
213 }

◆ RelationNameGetTupleDesc()

TupleDesc RelationNameGetTupleDesc ( const char *  relname)

Definition at line 1870 of file funcapi.c.

1871 {
1872  RangeVar *relvar;
1873  Relation rel;
1874  TupleDesc tupdesc;
1875  List *relname_list;
1876 
1877  /* Open relation and copy the tuple description */
1878  relname_list = stringToQualifiedNameList(relname, NULL);
1879  relvar = makeRangeVarFromNameList(relname_list);
1880  rel = relation_openrv(relvar, AccessShareLock);
1881  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1883 
1884  return tupdesc;
1885 }
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(const List *names)
Definition: namespace.c:3539
NameData relname
Definition: pg_class.h:38
List * stringToQualifiedNameList(const char *string, Node *escontext)
Definition: regproc.c:1797
#define RelationGetDescr(relation)
Definition: rel.h:531
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:137
Definition: pg_list.h:54

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

◆ resolve_polymorphic_argtypes()

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

Definition at line 1064 of file funcapi.c.

1066 {
1067  bool have_polymorphic_result = false;
1068  bool have_anyelement_result = false;
1069  bool have_anyarray_result = false;
1070  bool have_anyrange_result = false;
1071  bool have_anymultirange_result = false;
1072  bool have_anycompatible_result = false;
1073  bool have_anycompatible_array_result = false;
1074  bool have_anycompatible_range_result = false;
1075  bool have_anycompatible_multirange_result = false;
1076  polymorphic_actuals poly_actuals;
1077  polymorphic_actuals anyc_actuals;
1078  int inargno;
1079  int i;
1080 
1081  /*
1082  * First pass: resolve polymorphic inputs, check for outputs. As in
1083  * resolve_polymorphic_tupdesc, we rely on the parser to have enforced
1084  * type consistency and coerced ANYCOMPATIBLE args to a common supertype.
1085  */
1086  memset(&poly_actuals, 0, sizeof(poly_actuals));
1087  memset(&anyc_actuals, 0, sizeof(anyc_actuals));
1088  inargno = 0;
1089  for (i = 0; i < numargs; i++)
1090  {
1091  char argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
1092 
1093  switch (argtypes[i])
1094  {
1095  case ANYELEMENTOID:
1096  case ANYNONARRAYOID:
1097  case ANYENUMOID:
1098  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1099  {
1100  have_polymorphic_result = true;
1101  have_anyelement_result = true;
1102  }
1103  else
1104  {
1105  if (!OidIsValid(poly_actuals.anyelement_type))
1106  {
1107  poly_actuals.anyelement_type =
1108  get_call_expr_argtype(call_expr, inargno);
1109  if (!OidIsValid(poly_actuals.anyelement_type))
1110  return false;
1111  }
1112  argtypes[i] = poly_actuals.anyelement_type;
1113  }
1114  break;
1115  case ANYARRAYOID:
1116  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1117  {
1118  have_polymorphic_result = true;
1119  have_anyarray_result = true;
1120  }
1121  else
1122  {
1123  if (!OidIsValid(poly_actuals.anyarray_type))
1124  {
1125  poly_actuals.anyarray_type =
1126  get_call_expr_argtype(call_expr, inargno);
1127  if (!OidIsValid(poly_actuals.anyarray_type))
1128  return false;
1129  }
1130  argtypes[i] = poly_actuals.anyarray_type;
1131  }
1132  break;
1133  case ANYRANGEOID:
1134  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1135  {
1136  have_polymorphic_result = true;
1137  have_anyrange_result = true;
1138  }
1139  else
1140  {
1141  if (!OidIsValid(poly_actuals.anyrange_type))
1142  {
1143  poly_actuals.anyrange_type =
1144  get_call_expr_argtype(call_expr, inargno);
1145  if (!OidIsValid(poly_actuals.anyrange_type))
1146  return false;
1147  }
1148  argtypes[i] = poly_actuals.anyrange_type;
1149  }
1150  break;
1151  case ANYMULTIRANGEOID:
1152  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1153  {
1154  have_polymorphic_result = true;
1155  have_anymultirange_result = true;
1156  }
1157  else
1158  {
1159  if (!OidIsValid(poly_actuals.anymultirange_type))
1160  {
1161  poly_actuals.anymultirange_type =
1162  get_call_expr_argtype(call_expr, inargno);
1163  if (!OidIsValid(poly_actuals.anymultirange_type))
1164  return false;
1165  }
1166  argtypes[i] = poly_actuals.anymultirange_type;
1167  }
1168  break;
1169  case ANYCOMPATIBLEOID:
1170  case ANYCOMPATIBLENONARRAYOID:
1171  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1172  {
1173  have_polymorphic_result = true;
1174  have_anycompatible_result = true;
1175  }
1176  else
1177  {
1178  if (!OidIsValid(anyc_actuals.anyelement_type))
1179  {
1180  anyc_actuals.anyelement_type =
1181  get_call_expr_argtype(call_expr, inargno);
1182  if (!OidIsValid(anyc_actuals.anyelement_type))
1183  return false;
1184  }
1185  argtypes[i] = anyc_actuals.anyelement_type;
1186  }
1187  break;
1188  case ANYCOMPATIBLEARRAYOID:
1189  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1190  {
1191  have_polymorphic_result = true;
1192  have_anycompatible_array_result = true;
1193  }
1194  else
1195  {
1196  if (!OidIsValid(anyc_actuals.anyarray_type))
1197  {
1198  anyc_actuals.anyarray_type =
1199  get_call_expr_argtype(call_expr, inargno);
1200  if (!OidIsValid(anyc_actuals.anyarray_type))
1201  return false;
1202  }
1203  argtypes[i] = anyc_actuals.anyarray_type;
1204  }
1205  break;
1206  case ANYCOMPATIBLERANGEOID:
1207  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1208  {
1209  have_polymorphic_result = true;
1210  have_anycompatible_range_result = true;
1211  }
1212  else
1213  {
1214  if (!OidIsValid(anyc_actuals.anyrange_type))
1215  {
1216  anyc_actuals.anyrange_type =
1217  get_call_expr_argtype(call_expr, inargno);
1218  if (!OidIsValid(anyc_actuals.anyrange_type))
1219  return false;
1220  }
1221  argtypes[i] = anyc_actuals.anyrange_type;
1222  }
1223  break;
1224  case ANYCOMPATIBLEMULTIRANGEOID:
1225  if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
1226  {
1227  have_polymorphic_result = true;
1228  have_anycompatible_multirange_result = true;
1229  }
1230  else
1231  {
1232  if (!OidIsValid(anyc_actuals.anymultirange_type))
1233  {
1234  anyc_actuals.anymultirange_type =
1235  get_call_expr_argtype(call_expr, inargno);
1236  if (!OidIsValid(anyc_actuals.anymultirange_type))
1237  return false;
1238  }
1239  argtypes[i] = anyc_actuals.anymultirange_type;
1240  }
1241  break;
1242  default:
1243  break;
1244  }
1245  if (argmode != PROARGMODE_OUT && argmode != PROARGMODE_TABLE)
1246  inargno++;
1247  }
1248 
1249  /* Done? */
1250  if (!have_polymorphic_result)
1251  return true;
1252 
1253  /* If needed, deduce one polymorphic type from others */
1254  if (have_anyelement_result && !OidIsValid(poly_actuals.anyelement_type))
1255  resolve_anyelement_from_others(&poly_actuals);
1256 
1257  if (have_anyarray_result && !OidIsValid(poly_actuals.anyarray_type))
1258  resolve_anyarray_from_others(&poly_actuals);
1259 
1260  if (have_anyrange_result && !OidIsValid(poly_actuals.anyrange_type))
1261  resolve_anyrange_from_others(&poly_actuals);
1262 
1263  if (have_anymultirange_result && !OidIsValid(poly_actuals.anymultirange_type))
1264  resolve_anymultirange_from_others(&poly_actuals);
1265 
1266  if (have_anycompatible_result && !OidIsValid(anyc_actuals.anyelement_type))
1267  resolve_anyelement_from_others(&anyc_actuals);
1268 
1269  if (have_anycompatible_array_result && !OidIsValid(anyc_actuals.anyarray_type))
1270  resolve_anyarray_from_others(&anyc_actuals);
1271 
1272  if (have_anycompatible_range_result && !OidIsValid(anyc_actuals.anyrange_type))
1273  resolve_anyrange_from_others(&anyc_actuals);
1274 
1275  if (have_anycompatible_multirange_result && !OidIsValid(anyc_actuals.anymultirange_type))
1276  resolve_anymultirange_from_others(&anyc_actuals);
1277 
1278  /* And finally replace the output column types as needed */
1279  for (i = 0; i < numargs; i++)
1280  {
1281  switch (argtypes[i])
1282  {
1283  case ANYELEMENTOID:
1284  case ANYNONARRAYOID:
1285  case ANYENUMOID:
1286  argtypes[i] = poly_actuals.anyelement_type;
1287  break;
1288  case ANYARRAYOID:
1289  argtypes[i] = poly_actuals.anyarray_type;
1290  break;
1291  case ANYRANGEOID:
1292  argtypes[i] = poly_actuals.anyrange_type;
1293  break;
1294  case ANYMULTIRANGEOID:
1295  argtypes[i] = poly_actuals.anymultirange_type;
1296  break;
1297  case ANYCOMPATIBLEOID:
1298  case ANYCOMPATIBLENONARRAYOID:
1299  argtypes[i] = anyc_actuals.anyelement_type;
1300  break;
1301  case ANYCOMPATIBLEARRAYOID:
1302  argtypes[i] = anyc_actuals.anyarray_type;
1303  break;
1304  case ANYCOMPATIBLERANGEOID:
1305  argtypes[i] = anyc_actuals.anyrange_type;
1306  break;
1307  case ANYCOMPATIBLEMULTIRANGEOID:
1308  argtypes[i] = anyc_actuals.anymultirange_type;
1309  break;
1310  default:
1311  break;
1312  }
1313  }
1314 
1315  return true;
1316 }
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:1929
static void resolve_anyelement_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:589
static void resolve_anyrange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:681
static void resolve_anymultirange_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:710
static void resolve_anyarray_from_others(polymorphic_actuals *actuals)
Definition: funcapi.c:655
Oid anymultirange_type
Definition: funcapi.c:40

References polymorphic_actuals::anyarray_type, polymorphic_actuals::anyelement_type, polymorphic_actuals::anymultirange_type, polymorphic_actuals::anyrange_type, get_call_expr_argtype(), i, OidIsValid, resolve_anyarray_from_others(), resolve_anyelement_from_others(), resolve_anymultirange_from_others(), and resolve_anyrange_from_others().

Referenced by plpgsql_resolve_polymorphic_argtypes().

◆ TupleDescGetAttInMetadata()

AttInMetadata* TupleDescGetAttInMetadata ( TupleDesc  tupdesc)

Definition at line 2173 of file execTuples.c.

2174 {
2175  int natts = tupdesc->natts;
2176  int i;
2177  Oid atttypeid;
2178  Oid attinfuncid;
2179  FmgrInfo *attinfuncinfo;
2180  Oid *attioparams;
2181  int32 *atttypmods;
2182  AttInMetadata *attinmeta;
2183 
2184  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
2185 
2186  /* "Bless" the tupledesc so that we can make rowtype datums with it */
2187  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2188 
2189  /*
2190  * Gather info needed later to call the "in" function for each attribute
2191  */
2192  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2193  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2194  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2195 
2196  for (i = 0; i < natts; i++)
2197  {
2198  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2199 
2200  /* Ignore dropped attributes */
2201  if (!att->attisdropped)
2202  {
2203  atttypeid = att->atttypid;
2204  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2205  fmgr_info(attinfuncid, &attinfuncinfo[i]);
2206  atttypmods[i] = att->atttypmod;
2207  }
2208  }
2209  attinmeta->attinfuncs = attinfuncinfo;
2210  attinmeta->attioparams = attioparams;
2211  attinmeta->atttypmods = atttypmods;
2212 
2213  return attinmeta;
2214 }
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2874
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
Definition: fmgr.h:57

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

Referenced by bt_metap(), bt_multi_page_stats(), bt_page_stats_internal(), build_pgstattuple_type(), connectby_text(), connectby_text_serial(), create_foreign_modify(), crosstab(), dblink_get_pkey(), get_crosstab_tuplestore(), hash_page_items(), libpqrcv_processTuples(), materializeResult(), pg_get_keywords(), pg_get_multixact_members(), pg_stats_ext_mcvlist_items(), pgp_armor_headers(), pgrowlocks(), pgstatindex_impl(), pltcl_build_tuple_result(), pltcl_func_handler(), pltcl_init_tuple_store(), postgresAcquireSampleRowsFunc(), postgresBeginDirectModify(), postgresBeginForeignScan(), prs_setup_firstcall(), show_all_settings(), storeRow(), ts_setup_firstcall(), tt_setup_firstcall(), and xpath_table().

◆ TypeGetTupleDesc()

TupleDesc TypeGetTupleDesc ( Oid  typeoid,
List colaliases 
)

Definition at line 1903 of file funcapi.c.

1904 {
1905  Oid base_typeoid;
1906  TypeFuncClass functypclass = get_type_func_class(typeoid, &base_typeoid);
1907  TupleDesc tupdesc = NULL;
1908 
1909  /*
1910  * Build a suitable tupledesc representing the output rows. We
1911  * intentionally do not support TYPEFUNC_COMPOSITE_DOMAIN here, as it's
1912  * unlikely that legacy callers of this obsolete function would be
1913  * prepared to apply domain constraints.
1914  */
1915  if (functypclass == TYPEFUNC_COMPOSITE)
1916  {
1917  /* Composite data type, e.g. a table's row type */
1918  tupdesc = lookup_rowtype_tupdesc_copy(base_typeoid, -1);
1919 
1920  if (colaliases != NIL)
1921  {
1922  int natts = tupdesc->natts;
1923  int varattno;
1924 
1925  /* does the list length match the number of attributes? */
1926  if (list_length(colaliases) != natts)
1927  ereport(ERROR,
1928  (errcode(ERRCODE_DATATYPE_MISMATCH),
1929  errmsg("number of aliases does not match number of columns")));
1930 
1931  /* OK, use the aliases instead */
1932  for (varattno = 0; varattno < natts; varattno++)
1933  {
1934  char *label = strVal(list_nth(colaliases, varattno));
1935  Form_pg_attribute attr = TupleDescAttr(tupdesc, varattno);
1936 
1937  if (label != NULL)
1938  namestrcpy(&(attr->attname), label);
1939  }
1940 
1941  /* The tuple type is now an anonymous record type */
1942  tupdesc->tdtypeid = RECORDOID;
1943  tupdesc->tdtypmod = -1;
1944  }
1945  }
1946  else if (functypclass == TYPEFUNC_SCALAR)
1947  {
1948  /* Base data type, i.e. scalar */
1949  char *attname;
1950 
1951  /* the alias list is required for base types */
1952  if (colaliases == NIL)
1953  ereport(ERROR,
1954  (errcode(ERRCODE_DATATYPE_MISMATCH),
1955  errmsg("no column alias was provided")));
1956 
1957  /* the alias list length must be 1 */
1958  if (list_length(colaliases) != 1)
1959  ereport(ERROR,
1960  (errcode(ERRCODE_DATATYPE_MISMATCH),
1961  errmsg("number of aliases does not match number of columns")));
1962 
1963  /* OK, get the column alias */
1964  attname = strVal(linitial(colaliases));
1965 
1966  tupdesc = CreateTemplateTupleDesc(1);
1967  TupleDescInitEntry(tupdesc,
1968  (AttrNumber) 1,
1969  attname,
1970  typeoid,
1971  -1,
1972  0);
1973  }
1974  else if (functypclass == TYPEFUNC_RECORD)
1975  {
1976  /* XXX can't support this because typmod wasn't passed in ... */
1977  ereport(ERROR,
1978  (errcode(ERRCODE_DATATYPE_MISMATCH),
1979  errmsg("could not determine row description for function returning record")));
1980  }
1981  else
1982  {
1983  /* crummy error message, but parser should have caught this */
1984  elog(ERROR, "function in FROM has unsupported return type");
1985  }
1986 
1987  return tupdesc;
1988 }
void namestrcpy(Name name, const char *str)
Definition: name.c:233
NameData attname
Definition: pg_attribute.h:41
static char * label
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299

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.