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 296 of file funcapi.h.

◆ MAT_SRF_USE_EXPECTED_DESC

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

Definition at line 295 of file funcapi.h.

◆ SRF_FIRSTCALL_INIT

#define SRF_FIRSTCALL_INIT ( )    init_MultiFuncCall(fcinfo)

Definition at line 305 of file funcapi.h.

◆ SRF_IS_FIRSTCALL

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

Definition at line 303 of file funcapi.h.

◆ SRF_PERCALL_SETUP

#define SRF_PERCALL_SETUP ( )    per_MultiFuncCall(fcinfo)

Definition at line 307 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:297
#define PG_RETURN_NULL()
Definition: fmgr.h:345

Definition at line 327 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:296
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

Definition at line 309 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 318 of file funcapi.h.

◆ TupleGetDatum

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

Definition at line 235 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)

Definition at line 2071 of file execTuples.c.

2072 {
2073  if (tupdesc->tdtypeid == RECORDOID &&
2074  tupdesc->tdtypmod < 0)
2075  assign_record_type_typmod(tupdesc);
2076 
2077  return tupdesc; /* just for notational convenience */
2078 }
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1944

References assign_record_type_typmod(), TupleDescData::tdtypeid, and TupleDescData::tdtypmod.

Referenced by aclexplode(), brin_metapage_info(), bt_page_items_bytea(), bt_page_items_internal(), exec_eval_datum(), ExecEvalWholeRowVar(), ExecInitExprRec(), ExecInitFunctionScan(), get_expr_result_type(), hash_bitmap_info(), hash_metapage_info(), hash_page_items(), hash_page_stats(), init_sql_fcache(), InitMaterializedSRF(), MakeOldSnapshotTimeMappingTupleDesc(), pg_buffercache_pages(), pg_control_checkpoint(), pg_control_init(), pg_control_recovery(), pg_control_system(), pg_get_catalog_foreign_keys(), pg_get_object_address(), pg_get_publication_tables(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_partition_tree(), pg_prepared_xact(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), pg_stat_get_replication_slot(), pg_stat_get_subscription_stats(), pg_stat_get_wal(), pg_stats_ext_mcvlist_items(), pg_timezone_abbrevs(), pg_visibility_map_summary(), pg_visibility_tupdesc(), pg_walfile_name_offset(), pg_xact_commit_timestamp_origin(), pgstathashindex(), PLy_output_setup_record(), setup_firstcall(), ssl_extension_info(), test_enc_conversion(), test_predtest(), ts_setup_firstcall(), tsvector_unnest(), and TupleDescGetAttInMetadata().

◆ build_function_result_tupdesc_d()

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

Definition at line 1752 of file funcapi.c.

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

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 1702 of file funcapi.c.

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

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

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

◆ BuildTupleFromCStrings()

HeapTuple BuildTupleFromCStrings ( AttInMetadata attinmeta,
char **  values 
)

Definition at line 2135 of file execTuples.c.

2136 {
2137  TupleDesc tupdesc = attinmeta->tupdesc;
2138  int natts = tupdesc->natts;
2139  Datum *dvalues;
2140  bool *nulls;
2141  int i;
2142  HeapTuple tuple;
2143 
2144  dvalues = (Datum *) palloc(natts * sizeof(Datum));
2145  nulls = (bool *) palloc(natts * sizeof(bool));
2146 
2147  /*
2148  * Call the "in" function for each non-dropped attribute, even for nulls,
2149  * to support domains.
2150  */
2151  for (i = 0; i < natts; i++)
2152  {
2153  if (!TupleDescAttr(tupdesc, i)->attisdropped)
2154  {
2155  /* Non-dropped attributes */
2156  dvalues[i] = InputFunctionCall(&attinmeta->attinfuncs[i],
2157  values[i],
2158  attinmeta->attioparams[i],
2159  attinmeta->atttypmods[i]);
2160  if (values[i] != NULL)
2161  nulls[i] = false;
2162  else
2163  nulls[i] = true;
2164  }
2165  else
2166  {
2167  /* Handle dropped attributes by setting to NULL */
2168  dvalues[i] = (Datum) 0;
2169  nulls[i] = true;
2170  }
2171  }
2172 
2173  /*
2174  * Form a tuple
2175  */
2176  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
2177 
2178  /*
2179  * Release locally palloc'd space. XXX would probably be good to pfree
2180  * values of pass-by-reference datums, as well.
2181  */
2182  pfree(dvalues);
2183  pfree(nulls);
2184 
2185  return tuple;
2186 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1517
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void pfree(void *pointer)
Definition: mcxt.c:1306
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_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(), pg_logdir_ls_internal(), 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:957
static void shutdown_MultiFuncCall(Datum arg)
Definition: funcapi.c:238
ExprContext * econtext
Definition: execnodes.h:324

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 2006 of file funcapi.c.

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

◆ get_expr_result_tupdesc()

TupleDesc get_expr_result_tupdesc ( Node expr,
bool  noError 
)

Definition at line 543 of file funcapi.c.

544 {
545  TupleDesc tupleDesc;
546  TypeFuncClass functypclass;
547 
548  functypclass = get_expr_result_type(expr, NULL, &tupleDesc);
549 
550  if (functypclass == TYPEFUNC_COMPOSITE ||
551  functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
552  return tupleDesc;
553 
554  if (!noError)
555  {
556  Oid exprTypeId = exprType(expr);
557 
558  if (exprTypeId != RECORDOID)
559  ereport(ERROR,
560  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
561  errmsg("type %s is not composite",
562  format_type_be(exprTypeId))));
563  else
564  ereport(ERROR,
565  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
566  errmsg("record type has not been registered")));
567  }
568 
569  return NULL;
570 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43

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 292 of file funcapi.c.

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

References RowExpr::args, Assert(), BlessTupleDesc(), RowExpr::colnames, 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(), RowExpr::row_typeid, 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 1371 of file funcapi.c.

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

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert(), DatumGetArrayTypeP, deconstruct_array_builtin(), elog(), ERROR, GETSTRUCT, i, palloc(), PROCOID, 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 1514 of file funcapi.c.

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

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 1599 of file funcapi.c.

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

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

Referenced by chooseScalarFunctionAlias().

◆ get_func_result_type()

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

Definition at line 403 of file funcapi.c.

406 {
407  return internal_get_result_type(functionId,
408  NULL,
409  NULL,
410  resultTypeId,
411  resultTupleDesc);
412 }

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 1467 of file funcapi.c.

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

References ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, DatumGetArrayTypeP, elog(), ERROR, palloc(), PROCOID, 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:2224
HeapTupleHeader t_data
Definition: htup.h:68

References HeapTupleHeaderGetDatum(), and HeapTupleData::t_data.

Referenced by aclexplode(), brin_metapage_info(), bt_metap(), 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(), 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_wal_record_info(), pg_identify_object(), pg_identify_object_as_address(), pg_last_committed_xact(), pg_lock_status(), pg_old_snapshot_time_mapping(), pg_partition_tree(), pg_prepared_xact(), pg_replication_slot_advance(), pg_sequence_parameters(), pg_stat_file(), pg_stat_get_archiver(), 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 2224 of file execTuples.c.

2225 {
2226  Datum result;
2227  TupleDesc tupDesc;
2228 
2229  /* No work if there are no external TOAST pointers in the tuple */
2230  if (!HeapTupleHeaderHasExternal(tuple))
2231  return PointerGetDatum(tuple);
2232 
2233  /* Use the type data saved by heap_form_tuple to look up the rowtype */
2235  HeapTupleHeaderGetTypMod(tuple));
2236 
2237  /* And do the flattening */
2238  result = toast_flatten_tuple_to_datum(tuple,
2240  tupDesc);
2241 
2242  ReleaseTupleDesc(tupDesc);
2243 
2244  return result;
2245 }
Datum toast_flatten_tuple_to_datum(HeapTupleHeader tup, uint32 tup_len, TupleDesc tupleDesc)
Definition: heaptoast.c:449
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:446
#define HeapTupleHeaderHasExternal(tup)
Definition: htup_details.h:533
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1824

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:931
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
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:310
@ SFRM_Materialize
Definition: execnodes.h:309
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define MAT_SRF_BLESS
Definition: funcapi.h:296
#define MAT_SRF_USE_EXPECTED_DESC
Definition: funcapi.h:295
int work_mem
Definition: globals.c:125
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:254
SetFunctionReturnMode returnMode
Definition: execnodes.h:328
TupleDesc setDesc
Definition: execnodes.h:332
Tuplestorestate * setResult
Definition: execnodes.h:331
TupleDesc expectedDesc
Definition: execnodes.h:325
int allowedModes
Definition: execnodes.h:326
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318

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(), gist_page_items(), gist_page_items_bytea(), pg_available_extension_versions(), pg_available_extensions(), 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_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_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(), pgrowlocks(), postgres_fdw_get_connections(), 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 1871 of file funcapi.c.

1872 {
1873  RangeVar *relvar;
1874  Relation rel;
1875  TupleDesc tupdesc;
1876  List *relname_list;
1877 
1878  /* Open relation and copy the tuple description */
1879  relname_list = stringToQualifiedNameList(relname);
1880  relvar = makeRangeVarFromNameList(relname_list);
1881  rel = relation_openrv(relvar, AccessShareLock);
1882  tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
1884 
1885  return tupdesc;
1886 }
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3105
NameData relname
Definition: pg_class.h:38
List * stringToQualifiedNameList(const char *string)
Definition: regproc.c:1877
#define RelationGetDescr(relation)
Definition: rel.h:527
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
Definition: pg_list.h:52

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 1056 of file funcapi.c.

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

2087 {
2088  int natts = tupdesc->natts;
2089  int i;
2090  Oid atttypeid;
2091  Oid attinfuncid;
2092  FmgrInfo *attinfuncinfo;
2093  Oid *attioparams;
2094  int32 *atttypmods;
2095  AttInMetadata *attinmeta;
2096 
2097  attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
2098 
2099  /* "Bless" the tupledesc so that we can make rowtype datums with it */
2100  attinmeta->tupdesc = BlessTupleDesc(tupdesc);
2101 
2102  /*
2103  * Gather info needed later to call the "in" function for each attribute
2104  */
2105  attinfuncinfo = (FmgrInfo *) palloc0(natts * sizeof(FmgrInfo));
2106  attioparams = (Oid *) palloc0(natts * sizeof(Oid));
2107  atttypmods = (int32 *) palloc0(natts * sizeof(int32));
2108 
2109  for (i = 0; i < natts; i++)
2110  {
2111  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2112 
2113  /* Ignore dropped attributes */
2114  if (!att->attisdropped)
2115  {
2116  atttypeid = att->atttypid;
2117  getTypeInputInfo(atttypeid, &attinfuncid, &attioparams[i]);
2118  fmgr_info(attinfuncid, &attinfuncinfo[i]);
2119  atttypmods[i] = att->atttypmod;
2120  }
2121  }
2122  attinmeta->attinfuncs = attinfuncinfo;
2123  attinmeta->attioparams = attioparams;
2124  attinmeta->atttypmods = atttypmods;
2125 
2126  return attinmeta;
2127 }
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2832
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
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_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_logdir_ls_internal(), 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 1904 of file funcapi.c.

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

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.