funcapi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*-------------------------------------------------------------------------
  2. *
  3. * funcapi.h
  4. * Definitions for functions which return composite type and/or sets
  5. * or work on VARIADIC inputs.
  6. *
  7. * This file must be included by all Postgres modules that either define
  8. * or call FUNCAPI-callable functions or macros.
  9. *
  10. *
  11. * Copyright (c) 2002-2022, PostgreSQL Global Development Group
  12. *
  13. * src/include/funcapi.h
  14. *
  15. *-------------------------------------------------------------------------
  16. */
  17. #ifndef FUNCAPI_H
  18. #define FUNCAPI_H
  19. #include "access/tupdesc.h"
  20. #include "executor/executor.h"
  21. #include "executor/tuptable.h"
  22. #include "fmgr.h"
  23. /*-------------------------------------------------------------------------
  24. * Support to ease writing Functions returning composite types
  25. *-------------------------------------------------------------------------
  26. *
  27. * This struct holds arrays of individual attribute information
  28. * needed to create a tuple from raw C strings. It also requires
  29. * a copy of the TupleDesc. The information carried here
  30. * is derived from the TupleDesc, but it is stored here to
  31. * avoid redundant cpu cycles on each call to an SRF.
  32. */
  33. typedef struct AttInMetadata
  34. {
  35. /* full TupleDesc */
  36. TupleDesc tupdesc;
  37. /* array of attribute type input function finfo */
  38. FmgrInfo *attinfuncs;
  39. /* array of attribute type i/o parameter OIDs */
  40. Oid *attioparams;
  41. /* array of attribute typmod */
  42. int32 *atttypmods;
  43. } AttInMetadata;
  44. /*-------------------------------------------------------------------------
  45. * Support struct to ease writing Set Returning Functions (SRFs)
  46. *-------------------------------------------------------------------------
  47. *
  48. * This struct holds function context for Set Returning Functions.
  49. * Use fn_extra to hold a pointer to it across calls
  50. */
  51. typedef struct FuncCallContext
  52. {
  53. /*
  54. * Number of times we've been called before
  55. *
  56. * call_cntr is initialized to 0 for you by SRF_FIRSTCALL_INIT(), and
  57. * incremented for you every time SRF_RETURN_NEXT() is called.
  58. */
  59. uint64 call_cntr;
  60. /*
  61. * OPTIONAL maximum number of calls
  62. *
  63. * max_calls is here for convenience only and setting it is optional. If
  64. * not set, you must provide alternative means to know when the function
  65. * is done.
  66. */
  67. uint64 max_calls;
  68. /*
  69. * OPTIONAL pointer to miscellaneous user-provided context information
  70. *
  71. * user_fctx is for use as a pointer to your own struct to retain
  72. * arbitrary context information between calls of your function.
  73. */
  74. void *user_fctx;
  75. /*
  76. * OPTIONAL pointer to struct containing attribute type input metadata
  77. *
  78. * attinmeta is for use when returning tuples (i.e. composite data types)
  79. * and is not used when returning base data types. It is only needed if
  80. * you intend to use BuildTupleFromCStrings() to create the return tuple.
  81. */
  82. AttInMetadata *attinmeta;
  83. /*
  84. * memory context used for structures that must live for multiple calls
  85. *
  86. * multi_call_memory_ctx is set by SRF_FIRSTCALL_INIT() for you, and used
  87. * by SRF_RETURN_DONE() for cleanup. It is the most appropriate memory
  88. * context for any memory that is to be reused across multiple calls of
  89. * the SRF.
  90. */
  91. MemoryContext multi_call_memory_ctx;
  92. /*
  93. * OPTIONAL pointer to struct containing tuple description
  94. *
  95. * tuple_desc is for use when returning tuples (i.e. composite data types)
  96. * and is only needed if you are going to build the tuples with
  97. * heap_form_tuple() rather than with BuildTupleFromCStrings(). Note that
  98. * the TupleDesc pointer stored here should usually have been run through
  99. * BlessTupleDesc() first.
  100. */
  101. TupleDesc tuple_desc;
  102. } FuncCallContext;
  103. /*----------
  104. * Support to ease writing functions returning composite types
  105. *
  106. * External declarations:
  107. * get_call_result_type:
  108. * Given a function's call info record, determine the kind of datatype
  109. * it is supposed to return. If resultTypeId isn't NULL, *resultTypeId
  110. * receives the actual datatype OID (this is mainly useful for scalar
  111. * result types). If resultTupleDesc isn't NULL, *resultTupleDesc
  112. * receives a pointer to a TupleDesc when the result is of a composite
  113. * type, or NULL when it's a scalar result or the rowtype could not be
  114. * determined. NB: the tupledesc should be copied if it is to be
  115. * accessed over a long period.
  116. * get_expr_result_type:
  117. * Given an expression node, return the same info as for
  118. * get_call_result_type. Note: the cases in which rowtypes cannot be
  119. * determined are different from the cases for get_call_result_type.
  120. * get_func_result_type:
  121. * Given only a function's OID, return the same info as for
  122. * get_call_result_type. Note: the cases in which rowtypes cannot be
  123. * determined are different from the cases for get_call_result_type.
  124. * Do *not* use this if you can use one of the others.
  125. *
  126. * See also get_expr_result_tupdesc(), which is a convenient wrapper around
  127. * get_expr_result_type() for use when the caller only cares about
  128. * determinable-rowtype cases.
  129. *----------
  130. */
  131. /* Type categories for get_call_result_type and siblings */
  132. typedef enum TypeFuncClass
  133. {
  134. TYPEFUNC_SCALAR, /* scalar result type */
  135. TYPEFUNC_COMPOSITE, /* determinable rowtype result */
  136. TYPEFUNC_COMPOSITE_DOMAIN, /* domain over determinable rowtype result */
  137. TYPEFUNC_RECORD, /* indeterminate rowtype result */
  138. TYPEFUNC_OTHER /* bogus type, eg pseudotype */
  139. } TypeFuncClass;
  140. extern TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo,
  141. Oid *resultTypeId,
  142. TupleDesc *resultTupleDesc);
  143. extern TypeFuncClass get_expr_result_type(Node *expr,
  144. Oid *resultTypeId,
  145. TupleDesc *resultTupleDesc);
  146. extern TypeFuncClass get_func_result_type(Oid functionId,
  147. Oid *resultTypeId,
  148. TupleDesc *resultTupleDesc);
  149. extern TupleDesc get_expr_result_tupdesc(Node *expr, bool noError);
  150. extern bool resolve_polymorphic_argtypes(int numargs, Oid *argtypes,
  151. char *argmodes,
  152. Node *call_expr);
  153. extern int get_func_arg_info(HeapTuple procTup,
  154. Oid **p_argtypes, char ***p_argnames,
  155. char **p_argmodes);
  156. extern int get_func_input_arg_names(Datum proargnames, Datum proargmodes,
  157. char ***arg_names);
  158. extern int get_func_trftypes(HeapTuple procTup, Oid **p_trftypes);
  159. extern char *get_func_result_name(Oid functionId);
  160. extern TupleDesc build_function_result_tupdesc_d(char prokind,
  161. Datum proallargtypes,
  162. Datum proargmodes,
  163. Datum proargnames);
  164. extern TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple);
  165. /*----------
  166. * Support to ease writing functions returning composite types
  167. *
  168. * External declarations:
  169. * TupleDesc BlessTupleDesc(TupleDesc tupdesc) - "Bless" a completed tuple
  170. * descriptor so that it can be used to return properly labeled tuples.
  171. * You need to call this if you are going to use heap_form_tuple directly.
  172. * TupleDescGetAttInMetadata does it for you, however, so no need to call
  173. * it if you call TupleDescGetAttInMetadata.
  174. * AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc) - Build an
  175. * AttInMetadata struct based on the given TupleDesc. AttInMetadata can
  176. * be used in conjunction with C strings to produce a properly formed
  177. * tuple.
  178. * HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values) -
  179. * build a HeapTuple given user data in C string form. values is an array
  180. * of C strings, one for each attribute of the return tuple.
  181. * Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple) - convert a
  182. * HeapTupleHeader to a Datum.
  183. *
  184. * Macro declarations:
  185. * HeapTupleGetDatum(HeapTuple tuple) - convert a HeapTuple to a Datum.
  186. *
  187. * Obsolete routines and macros:
  188. * TupleDesc RelationNameGetTupleDesc(const char *relname) - Use to get a
  189. * TupleDesc based on a named relation.
  190. * TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases) - Use to get a
  191. * TupleDesc based on a type OID.
  192. * TupleGetDatum(TupleTableSlot *slot, HeapTuple tuple) - get a Datum
  193. * given a tuple and a slot.
  194. *----------
  195. */
  196. #define HeapTupleGetDatum(tuple) HeapTupleHeaderGetDatum((tuple)->t_data)
  197. /* obsolete version of above */
  198. #define TupleGetDatum(_slot, _tuple) HeapTupleGetDatum(_tuple)
  199. extern TupleDesc RelationNameGetTupleDesc(const char *relname);
  200. extern TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases);
  201. /* from execTuples.c */
  202. extern TupleDesc BlessTupleDesc(TupleDesc tupdesc);
  203. extern AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc);
  204. extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values);
  205. extern Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple);
  206. /*----------
  207. * Support for Set Returning Functions (SRFs)
  208. *
  209. * The basic API for SRFs using ValuePerCall mode looks something like this:
  210. *
  211. * Datum
  212. * my_Set_Returning_Function(PG_FUNCTION_ARGS)
  213. * {
  214. * FuncCallContext *funcctx;
  215. * Datum result;
  216. * MemoryContext oldcontext;
  217. * <user defined declarations>
  218. *
  219. * if (SRF_IS_FIRSTCALL())
  220. * {
  221. * funcctx = SRF_FIRSTCALL_INIT();
  222. * // switch context when allocating stuff to be used in later calls
  223. * oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
  224. * <user defined code>
  225. * <if returning composite>
  226. * <build TupleDesc, and perhaps AttInMetadata>
  227. * <endif returning composite>
  228. * <user defined code>
  229. * // return to original context when allocating transient memory
  230. * MemoryContextSwitchTo(oldcontext);
  231. * }
  232. * <user defined code>
  233. * funcctx = SRF_PERCALL_SETUP();
  234. * <user defined code>
  235. *
  236. * if (funcctx->call_cntr < funcctx->max_calls)
  237. * {
  238. * <user defined code>
  239. * <obtain result Datum>
  240. * SRF_RETURN_NEXT(funcctx, result);
  241. * }
  242. * else
  243. * SRF_RETURN_DONE(funcctx);
  244. * }
  245. *
  246. * NOTE: there is no guarantee that a SRF using ValuePerCall mode will be
  247. * run to completion; for example, a query with LIMIT might stop short of
  248. * fetching all the rows. Therefore, do not expect that you can do resource
  249. * cleanup just before SRF_RETURN_DONE(). You need not worry about releasing
  250. * memory allocated in multi_call_memory_ctx, but holding file descriptors or
  251. * other non-memory resources open across calls is a bug. SRFs that need
  252. * such resources should not use these macros, but instead populate a
  253. * tuplestore during a single call, as set up by InitMaterializedSRF() (see
  254. * fmgr/README). Alternatively, set up a callback to release resources
  255. * at query shutdown, using RegisterExprContextCallback().
  256. *
  257. *----------
  258. */
  259. /* from funcapi.c */
  260. /* flag bits for InitMaterializedSRF() */
  261. #define MAT_SRF_USE_EXPECTED_DESC 0x01 /* use expectedDesc as tupdesc. */
  262. #define MAT_SRF_BLESS 0x02 /* "Bless" a tuple descriptor with
  263. * BlessTupleDesc(). */
  264. extern void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags);
  265. /* Compatibility declarations, for v15 */
  266. #define SRF_SINGLE_USE_EXPECTED MAT_SRF_USE_EXPECTED_DESC
  267. #define SRF_SINGLE_BLESS MAT_SRF_BLESS
  268. extern void SetSingleFuncCall(FunctionCallInfo fcinfo, bits32 flags);
  269. extern FuncCallContext *init_MultiFuncCall(PG_FUNCTION_ARGS);
  270. extern FuncCallContext *per_MultiFuncCall(PG_FUNCTION_ARGS);
  271. extern void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx);
  272. #define SRF_IS_FIRSTCALL() (fcinfo->flinfo->fn_extra == NULL)
  273. #define SRF_FIRSTCALL_INIT() init_MultiFuncCall(fcinfo)
  274. #define SRF_PERCALL_SETUP() per_MultiFuncCall(fcinfo)
  275. #define SRF_RETURN_NEXT(_funcctx, _result) \
  276. do { \
  277. ReturnSetInfo *rsi; \
  278. (_funcctx)->call_cntr++; \
  279. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  280. rsi->isDone = ExprMultipleResult; \
  281. PG_RETURN_DATUM(_result); \
  282. } while (0)
  283. #define SRF_RETURN_NEXT_NULL(_funcctx) \
  284. do { \
  285. ReturnSetInfo *rsi; \
  286. (_funcctx)->call_cntr++; \
  287. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  288. rsi->isDone = ExprMultipleResult; \
  289. PG_RETURN_NULL(); \
  290. } while (0)
  291. #define SRF_RETURN_DONE(_funcctx) \
  292. do { \
  293. ReturnSetInfo *rsi; \
  294. end_MultiFuncCall(fcinfo, _funcctx); \
  295. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  296. rsi->isDone = ExprEndResult; \
  297. PG_RETURN_NULL(); \
  298. } while (0)
  299. /*----------
  300. * Support to ease writing of functions dealing with VARIADIC inputs
  301. *----------
  302. *
  303. * This function extracts a set of argument values, types and NULL markers
  304. * for a given input function. This returns a set of data:
  305. * - **values includes the set of Datum values extracted.
  306. * - **types the data type OID for each element.
  307. * - **nulls tracks if an element is NULL.
  308. *
  309. * variadic_start indicates the argument number where the VARIADIC argument
  310. * starts.
  311. * convert_unknown set to true will enforce the conversion of arguments
  312. * with unknown data type to text.
  313. *
  314. * The return result is the number of elements stored, or -1 in the case of
  315. * "VARIADIC NULL".
  316. */
  317. extern int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start,
  318. bool convert_unknown, Datum **values,
  319. Oid **types, bool **nulls);
  320. #endif /* FUNCAPI_H */