funcapi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*-------------------------------------------------------------------------
  2. *
  3. * funcapi.h
  4. * Definitions for functions which return composite type and/or sets
  5. *
  6. * This file must be included by all Postgres modules that either define
  7. * or call FUNCAPI-callable functions or macros.
  8. *
  9. *
  10. * Copyright (c) 2002-2016, PostgreSQL Global Development Group
  11. *
  12. * src/include/funcapi.h
  13. *
  14. *-------------------------------------------------------------------------
  15. */
  16. #ifndef FUNCAPI_H
  17. #define FUNCAPI_H
  18. #include "fmgr.h"
  19. #include "access/tupdesc.h"
  20. #include "executor/executor.h"
  21. #include "executor/tuptable.h"
  22. /*-------------------------------------------------------------------------
  23. * Support to ease writing Functions returning composite types
  24. *-------------------------------------------------------------------------
  25. *
  26. * This struct holds arrays of individual attribute information
  27. * needed to create a tuple from raw C strings. It also requires
  28. * a copy of the TupleDesc. The information carried here
  29. * is derived from the TupleDesc, but it is stored here to
  30. * avoid redundant cpu cycles on each call to an SRF.
  31. */
  32. typedef struct AttInMetadata
  33. {
  34. /* full TupleDesc */
  35. TupleDesc tupdesc;
  36. /* array of attribute type input function finfo */
  37. FmgrInfo *attinfuncs;
  38. /* array of attribute type i/o parameter OIDs */
  39. Oid *attioparams;
  40. /* array of attribute typmod */
  41. int32 *atttypmods;
  42. } AttInMetadata;
  43. /*-------------------------------------------------------------------------
  44. * Support struct to ease writing Set Returning Functions (SRFs)
  45. *-------------------------------------------------------------------------
  46. *
  47. * This struct holds function context for Set Returning Functions.
  48. * Use fn_extra to hold a pointer to it across calls
  49. */
  50. typedef struct FuncCallContext
  51. {
  52. /*
  53. * Number of times we've been called before
  54. *
  55. * call_cntr is initialized to 0 for you by SRF_FIRSTCALL_INIT(), and
  56. * incremented for you every time SRF_RETURN_NEXT() is called.
  57. */
  58. uint64 call_cntr;
  59. /*
  60. * OPTIONAL maximum number of calls
  61. *
  62. * max_calls is here for convenience only and setting it is optional. If
  63. * not set, you must provide alternative means to know when the function
  64. * is done.
  65. */
  66. uint64 max_calls;
  67. /*
  68. * OPTIONAL pointer to result slot
  69. *
  70. * This is obsolete and only present for backwards compatibility, viz,
  71. * user-defined SRFs that use the deprecated TupleDescGetSlot().
  72. */
  73. TupleTableSlot *slot;
  74. /*
  75. * OPTIONAL pointer to miscellaneous user-provided context information
  76. *
  77. * user_fctx is for use as a pointer to your own struct to retain
  78. * arbitrary context information between calls of your function.
  79. */
  80. void *user_fctx;
  81. /*
  82. * OPTIONAL pointer to struct containing attribute type input metadata
  83. *
  84. * attinmeta is for use when returning tuples (i.e. composite data types)
  85. * and is not used when returning base data types. It is only needed if
  86. * you intend to use BuildTupleFromCStrings() to create the return tuple.
  87. */
  88. AttInMetadata *attinmeta;
  89. /*
  90. * memory context used for structures that must live for multiple calls
  91. *
  92. * multi_call_memory_ctx is set by SRF_FIRSTCALL_INIT() for you, and used
  93. * by SRF_RETURN_DONE() for cleanup. It is the most appropriate memory
  94. * context for any memory that is to be reused across multiple calls of
  95. * the SRF.
  96. */
  97. MemoryContext multi_call_memory_ctx;
  98. /*
  99. * OPTIONAL pointer to struct containing tuple description
  100. *
  101. * tuple_desc is for use when returning tuples (i.e. composite data types)
  102. * and is only needed if you are going to build the tuples with
  103. * heap_form_tuple() rather than with BuildTupleFromCStrings(). Note that
  104. * the TupleDesc pointer stored here should usually have been run through
  105. * BlessTupleDesc() first.
  106. */
  107. TupleDesc tuple_desc;
  108. } FuncCallContext;
  109. /*----------
  110. * Support to ease writing functions returning composite types
  111. *
  112. * External declarations:
  113. * get_call_result_type:
  114. * Given a function's call info record, determine the kind of datatype
  115. * it is supposed to return. If resultTypeId isn't NULL, *resultTypeId
  116. * receives the actual datatype OID (this is mainly useful for scalar
  117. * result types). If resultTupleDesc isn't NULL, *resultTupleDesc
  118. * receives a pointer to a TupleDesc when the result is of a composite
  119. * type, or NULL when it's a scalar result or the rowtype could not be
  120. * determined. NB: the tupledesc should be copied if it is to be
  121. * accessed over a long period.
  122. * get_expr_result_type:
  123. * Given an expression node, return the same info as for
  124. * get_call_result_type. Note: the cases in which rowtypes cannot be
  125. * determined are different from the cases for get_call_result_type.
  126. * get_func_result_type:
  127. * Given only a function's OID, return the same info as for
  128. * get_call_result_type. Note: the cases in which rowtypes cannot be
  129. * determined are different from the cases for get_call_result_type.
  130. * Do *not* use this if you can use one of the others.
  131. *----------
  132. */
  133. /* Type categories for get_call_result_type and siblings */
  134. typedef enum TypeFuncClass
  135. {
  136. TYPEFUNC_SCALAR, /* scalar result type */
  137. TYPEFUNC_COMPOSITE, /* determinable rowtype result */
  138. TYPEFUNC_RECORD, /* indeterminate rowtype result */
  139. TYPEFUNC_OTHER /* bogus type, eg pseudotype */
  140. } TypeFuncClass;
  141. extern TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo,
  142. Oid *resultTypeId,
  143. TupleDesc *resultTupleDesc);
  144. extern TypeFuncClass get_expr_result_type(Node *expr,
  145. Oid *resultTypeId,
  146. TupleDesc *resultTupleDesc);
  147. extern TypeFuncClass get_func_result_type(Oid functionId,
  148. Oid *resultTypeId,
  149. TupleDesc *resultTupleDesc);
  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(Datum proallargtypes,
  161. Datum proargmodes,
  162. Datum proargnames);
  163. extern TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple);
  164. /*----------
  165. * Support to ease writing functions returning composite types
  166. *
  167. * External declarations:
  168. * TupleDesc BlessTupleDesc(TupleDesc tupdesc) - "Bless" a completed tuple
  169. * descriptor so that it can be used to return properly labeled tuples.
  170. * You need to call this if you are going to use heap_form_tuple directly.
  171. * TupleDescGetAttInMetadata does it for you, however, so no need to call
  172. * it if you call TupleDescGetAttInMetadata.
  173. * AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc) - Build an
  174. * AttInMetadata struct based on the given TupleDesc. AttInMetadata can
  175. * be used in conjunction with C strings to produce a properly formed
  176. * tuple.
  177. * HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values) -
  178. * build a HeapTuple given user data in C string form. values is an array
  179. * of C strings, one for each attribute of the return tuple.
  180. * Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple) - convert a
  181. * HeapTupleHeader to a Datum.
  182. *
  183. * Macro declarations:
  184. * HeapTupleGetDatum(HeapTuple tuple) - convert a HeapTuple to a Datum.
  185. *
  186. * Obsolete routines and macros:
  187. * TupleDesc RelationNameGetTupleDesc(const char *relname) - Use to get a
  188. * TupleDesc based on a named relation.
  189. * TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases) - Use to get a
  190. * TupleDesc based on a type OID.
  191. * TupleTableSlot *TupleDescGetSlot(TupleDesc tupdesc) - Builds a
  192. * TupleTableSlot, which is not needed anymore.
  193. * TupleGetDatum(TupleTableSlot *slot, HeapTuple tuple) - get a Datum
  194. * given a tuple and a slot.
  195. *----------
  196. */
  197. #define HeapTupleGetDatum(tuple) HeapTupleHeaderGetDatum((tuple)->t_data)
  198. /* obsolete version of above */
  199. #define TupleGetDatum(_slot, _tuple) HeapTupleGetDatum(_tuple)
  200. extern TupleDesc RelationNameGetTupleDesc(const char *relname);
  201. extern TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases);
  202. /* from execTuples.c */
  203. extern TupleDesc BlessTupleDesc(TupleDesc tupdesc);
  204. extern AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc);
  205. extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values);
  206. extern Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple);
  207. extern TupleTableSlot *TupleDescGetSlot(TupleDesc tupdesc);
  208. /*----------
  209. * Support for Set Returning Functions (SRFs)
  210. *
  211. * The basic API for SRFs looks something like:
  212. *
  213. * Datum
  214. * my_Set_Returning_Function(PG_FUNCTION_ARGS)
  215. * {
  216. * FuncCallContext *funcctx;
  217. * Datum result;
  218. * MemoryContext oldcontext;
  219. * <user defined declarations>
  220. *
  221. * if (SRF_IS_FIRSTCALL())
  222. * {
  223. * funcctx = SRF_FIRSTCALL_INIT();
  224. * // switch context when allocating stuff to be used in later calls
  225. * oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
  226. * <user defined code>
  227. * <if returning composite>
  228. * <build TupleDesc, and perhaps AttInMetaData>
  229. * <endif returning composite>
  230. * <user defined code>
  231. * // return to original context when allocating transient memory
  232. * MemoryContextSwitchTo(oldcontext);
  233. * }
  234. * <user defined code>
  235. * funcctx = SRF_PERCALL_SETUP();
  236. * <user defined code>
  237. *
  238. * if (funcctx->call_cntr < funcctx->max_calls)
  239. * {
  240. * <user defined code>
  241. * <obtain result Datum>
  242. * SRF_RETURN_NEXT(funcctx, result);
  243. * }
  244. * else
  245. * SRF_RETURN_DONE(funcctx);
  246. * }
  247. *
  248. *----------
  249. */
  250. /* from funcapi.c */
  251. extern FuncCallContext *init_MultiFuncCall(PG_FUNCTION_ARGS);
  252. extern FuncCallContext *per_MultiFuncCall(PG_FUNCTION_ARGS);
  253. extern void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx);
  254. #define SRF_IS_FIRSTCALL() (fcinfo->flinfo->fn_extra == NULL)
  255. #define SRF_FIRSTCALL_INIT() init_MultiFuncCall(fcinfo)
  256. #define SRF_PERCALL_SETUP() per_MultiFuncCall(fcinfo)
  257. #define SRF_RETURN_NEXT(_funcctx, _result) \
  258. do { \
  259. ReturnSetInfo *rsi; \
  260. (_funcctx)->call_cntr++; \
  261. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  262. rsi->isDone = ExprMultipleResult; \
  263. PG_RETURN_DATUM(_result); \
  264. } while (0)
  265. #define SRF_RETURN_NEXT_NULL(_funcctx) \
  266. do { \
  267. ReturnSetInfo *rsi; \
  268. (_funcctx)->call_cntr++; \
  269. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  270. rsi->isDone = ExprMultipleResult; \
  271. PG_RETURN_NULL(); \
  272. } while (0)
  273. #define SRF_RETURN_DONE(_funcctx) \
  274. do { \
  275. ReturnSetInfo *rsi; \
  276. end_MultiFuncCall(fcinfo, _funcctx); \
  277. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  278. rsi->isDone = ExprEndResult; \
  279. PG_RETURN_NULL(); \
  280. } while (0)
  281. #endif /* FUNCAPI_H */