gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

350 行
12KB

  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-2019, PostgreSQL Global Development Group
  12. *
  13. * src/include/funcapi.h
  14. *
  15. *-------------------------------------------------------------------------
  16. */
  17. #ifndef FUNCAPI_H
  18. #define FUNCAPI_H
  19. #include "fmgr.h"
  20. #include "access/tupdesc.h"
  21. #include "executor/executor.h"
  22. #include "executor/tuptable.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, and return that using SFRM_Materialize
  254. * mode (see fmgr/README). Alternatively, set up a callback to release
  255. * resources at query shutdown, using RegisterExprContextCallback().
  256. *
  257. *----------
  258. */
  259. /* from funcapi.c */
  260. extern FuncCallContext *init_MultiFuncCall(PG_FUNCTION_ARGS);
  261. extern FuncCallContext *per_MultiFuncCall(PG_FUNCTION_ARGS);
  262. extern void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx);
  263. #define SRF_IS_FIRSTCALL() (fcinfo->flinfo->fn_extra == NULL)
  264. #define SRF_FIRSTCALL_INIT() init_MultiFuncCall(fcinfo)
  265. #define SRF_PERCALL_SETUP() per_MultiFuncCall(fcinfo)
  266. #define SRF_RETURN_NEXT(_funcctx, _result) \
  267. do { \
  268. ReturnSetInfo *rsi; \
  269. (_funcctx)->call_cntr++; \
  270. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  271. rsi->isDone = ExprMultipleResult; \
  272. PG_RETURN_DATUM(_result); \
  273. } while (0)
  274. #define SRF_RETURN_NEXT_NULL(_funcctx) \
  275. do { \
  276. ReturnSetInfo *rsi; \
  277. (_funcctx)->call_cntr++; \
  278. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  279. rsi->isDone = ExprMultipleResult; \
  280. PG_RETURN_NULL(); \
  281. } while (0)
  282. #define SRF_RETURN_DONE(_funcctx) \
  283. do { \
  284. ReturnSetInfo *rsi; \
  285. end_MultiFuncCall(fcinfo, _funcctx); \
  286. rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
  287. rsi->isDone = ExprEndResult; \
  288. PG_RETURN_NULL(); \
  289. } while (0)
  290. /*----------
  291. * Support to ease writing of functions dealing with VARIADIC inputs
  292. *----------
  293. *
  294. * This function extracts a set of argument values, types and NULL markers
  295. * for a given input function. This returns a set of data:
  296. * - **values includes the set of Datum values extracted.
  297. * - **types the data type OID for each element.
  298. * - **nulls tracks if an element is NULL.
  299. *
  300. * variadic_start indicates the argument number where the VARIADIC argument
  301. * starts.
  302. * convert_unknown set to true will enforce the conversion of arguments
  303. * with unknown data type to text.
  304. *
  305. * The return result is the number of elements stored, or -1 in the case of
  306. * "VARIADIC NULL".
  307. */
  308. extern int extract_variadic_args(FunctionCallInfo fcinfo, int variadic_start,
  309. bool convert_unknown, Datum **values,
  310. Oid **types, bool **nulls);
  311. #endif /* FUNCAPI_H */
上海开阖软件有限公司 沪ICP备12045867号-1