gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
33KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * fmgr.h
  4. * Definitions for the Postgres function manager and function-call
  5. * interface.
  6. *
  7. * This file must be included by all Postgres modules that either define
  8. * or call fmgr-callable functions.
  9. *
  10. *
  11. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/fmgr.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef FMGR_H
  19. #define FMGR_H
  20. /* We don't want to include primnodes.h here, so make some stub references */
  21. typedef struct Node *fmNodePtr;
  22. typedef struct Aggref *fmAggrefPtr;
  23. /* Likewise, avoid including execnodes.h here */
  24. typedef void (*fmExprContextCallbackFunction) (Datum arg);
  25. /* Likewise, avoid including stringinfo.h here */
  26. typedef struct StringInfoData *fmStringInfo;
  27. /*
  28. * All functions that can be called directly by fmgr must have this signature.
  29. * (Other functions can be called by using a handler that does have this
  30. * signature.)
  31. */
  32. typedef struct FunctionCallInfoBaseData *FunctionCallInfo;
  33. typedef Datum (*PGFunction) (FunctionCallInfo fcinfo);
  34. /*
  35. * This struct holds the system-catalog information that must be looked up
  36. * before a function can be called through fmgr. If the same function is
  37. * to be called multiple times, the lookup need be done only once and the
  38. * info struct saved for re-use.
  39. *
  40. * Note that fn_expr really is parse-time-determined information about the
  41. * arguments, rather than about the function itself. But it's convenient to
  42. * store it here rather than in FunctionCallInfoBaseData, where it might more
  43. * logically belong.
  44. *
  45. * fn_extra is available for use by the called function; all other fields
  46. * should be treated as read-only after the struct is created.
  47. */
  48. typedef struct FmgrInfo
  49. {
  50. PGFunction fn_addr; /* pointer to function or handler to be called */
  51. Oid fn_oid; /* OID of function (NOT of handler, if any) */
  52. short fn_nargs; /* number of input args (0..FUNC_MAX_ARGS) */
  53. bool fn_strict; /* function is "strict" (NULL in => NULL out) */
  54. bool fn_retset; /* function returns a set */
  55. unsigned char fn_stats; /* collect stats if track_functions > this */
  56. void *fn_extra; /* extra space for use by handler */
  57. MemoryContext fn_mcxt; /* memory context to store fn_extra in */
  58. fmNodePtr fn_expr; /* expression parse tree for call, or NULL */
  59. } FmgrInfo;
  60. /*
  61. * This struct is the data actually passed to an fmgr-called function.
  62. *
  63. * The called function is expected to set isnull, and possibly resultinfo or
  64. * fields in whatever resultinfo points to. It should not change any other
  65. * fields. (In particular, scribbling on the argument arrays is a bad idea,
  66. * since some callers assume they can re-call with the same arguments.)
  67. *
  68. * Note that enough space for arguments needs to be provided, either by using
  69. * SizeForFunctionCallInfo() in dynamic allocations, or by using
  70. * LOCAL_FCINFO() for on-stack allocations.
  71. *
  72. * This struct is named *BaseData, rather than *Data, to break pre v12 code
  73. * that allocated FunctionCallInfoData itself, as it'd often silently break
  74. * old code due to no space for arguments being provided.
  75. */
  76. typedef struct FunctionCallInfoBaseData
  77. {
  78. FmgrInfo *flinfo; /* ptr to lookup info used for this call */
  79. fmNodePtr context; /* pass info about context of call */
  80. fmNodePtr resultinfo; /* pass or return extra info about result */
  81. Oid fncollation; /* collation for function to use */
  82. #define FIELDNO_FUNCTIONCALLINFODATA_ISNULL 4
  83. bool isnull; /* function must set true if result is NULL */
  84. short nargs; /* # arguments actually passed */
  85. #define FIELDNO_FUNCTIONCALLINFODATA_ARGS 6
  86. NullableDatum args[FLEXIBLE_ARRAY_MEMBER];
  87. } FunctionCallInfoBaseData;
  88. /*
  89. * Space needed for a FunctionCallInfoBaseData struct with sufficient space
  90. * for `nargs` arguments.
  91. */
  92. #define SizeForFunctionCallInfo(nargs) \
  93. (offsetof(FunctionCallInfoBaseData, args) + \
  94. sizeof(NullableDatum) * (nargs))
  95. /*
  96. * This macro ensures that `name` points to a stack-allocated
  97. * FunctionCallInfoBaseData struct with sufficient space for `nargs` arguments.
  98. */
  99. #define LOCAL_FCINFO(name, nargs) \
  100. /* use union with FunctionCallInfoBaseData to guarantee alignment */ \
  101. union \
  102. { \
  103. FunctionCallInfoBaseData fcinfo; \
  104. /* ensure enough space for nargs args is available */ \
  105. char fcinfo_data[SizeForFunctionCallInfo(nargs)]; \
  106. } name##data; \
  107. FunctionCallInfo name = &name##data.fcinfo
  108. /*
  109. * This routine fills a FmgrInfo struct, given the OID
  110. * of the function to be called.
  111. */
  112. extern void fmgr_info(Oid functionId, FmgrInfo *finfo);
  113. /*
  114. * Same, when the FmgrInfo struct is in a memory context longer-lived than
  115. * CurrentMemoryContext. The specified context will be set as fn_mcxt
  116. * and used to hold all subsidiary data of finfo.
  117. */
  118. extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo,
  119. MemoryContext mcxt);
  120. /* Convenience macro for setting the fn_expr field */
  121. #define fmgr_info_set_expr(expr, finfo) \
  122. ((finfo)->fn_expr = (expr))
  123. /*
  124. * Copy an FmgrInfo struct
  125. */
  126. extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
  127. MemoryContext destcxt);
  128. extern void fmgr_symbol(Oid functionId, char **mod, char **fn);
  129. /*
  130. * This macro initializes all the fields of a FunctionCallInfoBaseData except
  131. * for the args[] array.
  132. */
  133. #define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo) \
  134. do { \
  135. (Fcinfo).flinfo = (Flinfo); \
  136. (Fcinfo).context = (Context); \
  137. (Fcinfo).resultinfo = (Resultinfo); \
  138. (Fcinfo).fncollation = (Collation); \
  139. (Fcinfo).isnull = false; \
  140. (Fcinfo).nargs = (Nargs); \
  141. } while (0)
  142. /*
  143. * This macro invokes a function given a filled-in FunctionCallInfoBaseData
  144. * struct. The macro result is the returned Datum --- but note that
  145. * caller must still check fcinfo->isnull! Also, if function is strict,
  146. * it is caller's responsibility to verify that no null arguments are present
  147. * before calling.
  148. *
  149. * Some code performs multiple calls without redoing InitFunctionCallInfoData,
  150. * possibly altering the argument values. This is okay, but be sure to reset
  151. * the fcinfo->isnull flag before each call, since callees are permitted to
  152. * assume that starts out false.
  153. */
  154. #define FunctionCallInvoke(fcinfo) ((* (fcinfo)->flinfo->fn_addr) (fcinfo))
  155. /*-------------------------------------------------------------------------
  156. * Support macros to ease writing fmgr-compatible functions
  157. *
  158. * A C-coded fmgr-compatible function should be declared as
  159. *
  160. * Datum
  161. * function_name(PG_FUNCTION_ARGS)
  162. * {
  163. * ...
  164. * }
  165. *
  166. * It should access its arguments using appropriate PG_GETARG_xxx macros
  167. * and should return its result using PG_RETURN_xxx.
  168. *
  169. *-------------------------------------------------------------------------
  170. */
  171. /* Standard parameter list for fmgr-compatible functions */
  172. #define PG_FUNCTION_ARGS FunctionCallInfo fcinfo
  173. /*
  174. * Get collation function should use.
  175. */
  176. #define PG_GET_COLLATION() (fcinfo->fncollation)
  177. /*
  178. * Get number of arguments passed to function.
  179. */
  180. #define PG_NARGS() (fcinfo->nargs)
  181. /*
  182. * If function is not marked "proisstrict" in pg_proc, it must check for
  183. * null arguments using this macro. Do not try to GETARG a null argument!
  184. */
  185. #define PG_ARGISNULL(n) (fcinfo->args[n].isnull)
  186. /*
  187. * Support for fetching detoasted copies of toastable datatypes (all of
  188. * which are varlena types). pg_detoast_datum() gives you either the input
  189. * datum (if not toasted) or a detoasted copy allocated with palloc().
  190. * pg_detoast_datum_copy() always gives you a palloc'd copy --- use it
  191. * if you need a modifiable copy of the input. Caller is expected to have
  192. * checked for null inputs first, if necessary.
  193. *
  194. * pg_detoast_datum_packed() will return packed (1-byte header) datums
  195. * unmodified. It will still expand an externally toasted or compressed datum.
  196. * The resulting datum can be accessed using VARSIZE_ANY() and VARDATA_ANY()
  197. * (beware of multiple evaluations in those macros!)
  198. *
  199. * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
  200. * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
  201. * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
  202. * int32 or wider field in the struct representing the datum layout requires
  203. * aligned data. memcpy() is alignment-oblivious, as are most operations on
  204. * datatypes, such as text, whose layout struct contains only char fields.
  205. *
  206. * Note: it'd be nice if these could be macros, but I see no way to do that
  207. * without evaluating the arguments multiple times, which is NOT acceptable.
  208. */
  209. extern struct varlena *pg_detoast_datum(struct varlena *datum);
  210. extern struct varlena *pg_detoast_datum_copy(struct varlena *datum);
  211. extern struct varlena *pg_detoast_datum_slice(struct varlena *datum,
  212. int32 first, int32 count);
  213. extern struct varlena *pg_detoast_datum_packed(struct varlena *datum);
  214. #define PG_DETOAST_DATUM(datum) \
  215. pg_detoast_datum((struct varlena *) DatumGetPointer(datum))
  216. #define PG_DETOAST_DATUM_COPY(datum) \
  217. pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum))
  218. #define PG_DETOAST_DATUM_SLICE(datum,f,c) \
  219. pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \
  220. (int32) (f), (int32) (c))
  221. /* WARNING -- unaligned pointer */
  222. #define PG_DETOAST_DATUM_PACKED(datum) \
  223. pg_detoast_datum_packed((struct varlena *) DatumGetPointer(datum))
  224. /*
  225. * Support for cleaning up detoasted copies of inputs. This must only
  226. * be used for pass-by-ref datatypes, and normally would only be used
  227. * for toastable types. If the given pointer is different from the
  228. * original argument, assume it's a palloc'd detoasted copy, and pfree it.
  229. * NOTE: most functions on toastable types do not have to worry about this,
  230. * but we currently require that support functions for indexes not leak
  231. * memory.
  232. */
  233. #define PG_FREE_IF_COPY(ptr,n) \
  234. do { \
  235. if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \
  236. pfree(ptr); \
  237. } while (0)
  238. /* Macros for fetching arguments of standard types */
  239. #define PG_GETARG_DATUM(n) (fcinfo->args[n].value)
  240. #define PG_GETARG_INT32(n) DatumGetInt32(PG_GETARG_DATUM(n))
  241. #define PG_GETARG_UINT32(n) DatumGetUInt32(PG_GETARG_DATUM(n))
  242. #define PG_GETARG_INT16(n) DatumGetInt16(PG_GETARG_DATUM(n))
  243. #define PG_GETARG_UINT16(n) DatumGetUInt16(PG_GETARG_DATUM(n))
  244. #define PG_GETARG_CHAR(n) DatumGetChar(PG_GETARG_DATUM(n))
  245. #define PG_GETARG_BOOL(n) DatumGetBool(PG_GETARG_DATUM(n))
  246. #define PG_GETARG_OID(n) DatumGetObjectId(PG_GETARG_DATUM(n))
  247. #define PG_GETARG_POINTER(n) DatumGetPointer(PG_GETARG_DATUM(n))
  248. #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n))
  249. #define PG_GETARG_NAME(n) DatumGetName(PG_GETARG_DATUM(n))
  250. /* these macros hide the pass-by-reference-ness of the datatype: */
  251. #define PG_GETARG_FLOAT4(n) DatumGetFloat4(PG_GETARG_DATUM(n))
  252. #define PG_GETARG_FLOAT8(n) DatumGetFloat8(PG_GETARG_DATUM(n))
  253. #define PG_GETARG_INT64(n) DatumGetInt64(PG_GETARG_DATUM(n))
  254. /* use this if you want the raw, possibly-toasted input datum: */
  255. #define PG_GETARG_RAW_VARLENA_P(n) ((struct varlena *) PG_GETARG_POINTER(n))
  256. /* use this if you want the input datum de-toasted: */
  257. #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
  258. /* and this if you can handle 1-byte-header datums: */
  259. #define PG_GETARG_VARLENA_PP(n) PG_DETOAST_DATUM_PACKED(PG_GETARG_DATUM(n))
  260. /* DatumGetFoo macros for varlena types will typically look like this: */
  261. #define DatumGetByteaPP(X) ((bytea *) PG_DETOAST_DATUM_PACKED(X))
  262. #define DatumGetTextPP(X) ((text *) PG_DETOAST_DATUM_PACKED(X))
  263. #define DatumGetBpCharPP(X) ((BpChar *) PG_DETOAST_DATUM_PACKED(X))
  264. #define DatumGetVarCharPP(X) ((VarChar *) PG_DETOAST_DATUM_PACKED(X))
  265. #define DatumGetHeapTupleHeader(X) ((HeapTupleHeader) PG_DETOAST_DATUM(X))
  266. /* And we also offer variants that return an OK-to-write copy */
  267. #define DatumGetByteaPCopy(X) ((bytea *) PG_DETOAST_DATUM_COPY(X))
  268. #define DatumGetTextPCopy(X) ((text *) PG_DETOAST_DATUM_COPY(X))
  269. #define DatumGetBpCharPCopy(X) ((BpChar *) PG_DETOAST_DATUM_COPY(X))
  270. #define DatumGetVarCharPCopy(X) ((VarChar *) PG_DETOAST_DATUM_COPY(X))
  271. #define DatumGetHeapTupleHeaderCopy(X) ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X))
  272. /* Variants which return n bytes starting at pos. m */
  273. #define DatumGetByteaPSlice(X,m,n) ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
  274. #define DatumGetTextPSlice(X,m,n) ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
  275. #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
  276. #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
  277. /* GETARG macros for varlena types will typically look like this: */
  278. #define PG_GETARG_BYTEA_PP(n) DatumGetByteaPP(PG_GETARG_DATUM(n))
  279. #define PG_GETARG_TEXT_PP(n) DatumGetTextPP(PG_GETARG_DATUM(n))
  280. #define PG_GETARG_BPCHAR_PP(n) DatumGetBpCharPP(PG_GETARG_DATUM(n))
  281. #define PG_GETARG_VARCHAR_PP(n) DatumGetVarCharPP(PG_GETARG_DATUM(n))
  282. #define PG_GETARG_HEAPTUPLEHEADER(n) DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
  283. /* And we also offer variants that return an OK-to-write copy */
  284. #define PG_GETARG_BYTEA_P_COPY(n) DatumGetByteaPCopy(PG_GETARG_DATUM(n))
  285. #define PG_GETARG_TEXT_P_COPY(n) DatumGetTextPCopy(PG_GETARG_DATUM(n))
  286. #define PG_GETARG_BPCHAR_P_COPY(n) DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
  287. #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
  288. #define PG_GETARG_HEAPTUPLEHEADER_COPY(n) DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n))
  289. /* And a b-byte slice from position a -also OK to write */
  290. #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
  291. #define PG_GETARG_TEXT_P_SLICE(n,a,b) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
  292. #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
  293. #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
  294. /*
  295. * Obsolescent variants that guarantee INT alignment for the return value.
  296. * Few operations on these particular types need alignment, mainly operations
  297. * that cast the VARDATA pointer to a type like int16[]. Most code should use
  298. * the ...PP(X) counterpart. Nonetheless, these appear frequently in code
  299. * predating the PostgreSQL 8.3 introduction of the ...PP(X) variants.
  300. */
  301. #define DatumGetByteaP(X) ((bytea *) PG_DETOAST_DATUM(X))
  302. #define DatumGetTextP(X) ((text *) PG_DETOAST_DATUM(X))
  303. #define DatumGetBpCharP(X) ((BpChar *) PG_DETOAST_DATUM(X))
  304. #define DatumGetVarCharP(X) ((VarChar *) PG_DETOAST_DATUM(X))
  305. #define PG_GETARG_BYTEA_P(n) DatumGetByteaP(PG_GETARG_DATUM(n))
  306. #define PG_GETARG_TEXT_P(n) DatumGetTextP(PG_GETARG_DATUM(n))
  307. #define PG_GETARG_BPCHAR_P(n) DatumGetBpCharP(PG_GETARG_DATUM(n))
  308. #define PG_GETARG_VARCHAR_P(n) DatumGetVarCharP(PG_GETARG_DATUM(n))
  309. /* To return a NULL do this: */
  310. #define PG_RETURN_NULL() \
  311. do { fcinfo->isnull = true; return (Datum) 0; } while (0)
  312. /* A few internal functions return void (which is not the same as NULL!) */
  313. #define PG_RETURN_VOID() return (Datum) 0
  314. /* Macros for returning results of standard types */
  315. #define PG_RETURN_DATUM(x) return (x)
  316. #define PG_RETURN_INT32(x) return Int32GetDatum(x)
  317. #define PG_RETURN_UINT32(x) return UInt32GetDatum(x)
  318. #define PG_RETURN_INT16(x) return Int16GetDatum(x)
  319. #define PG_RETURN_UINT16(x) return UInt16GetDatum(x)
  320. #define PG_RETURN_CHAR(x) return CharGetDatum(x)
  321. #define PG_RETURN_BOOL(x) return BoolGetDatum(x)
  322. #define PG_RETURN_OID(x) return ObjectIdGetDatum(x)
  323. #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
  324. #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
  325. #define PG_RETURN_NAME(x) return NameGetDatum(x)
  326. /* these macros hide the pass-by-reference-ness of the datatype: */
  327. #define PG_RETURN_FLOAT4(x) return Float4GetDatum(x)
  328. #define PG_RETURN_FLOAT8(x) return Float8GetDatum(x)
  329. #define PG_RETURN_INT64(x) return Int64GetDatum(x)
  330. #define PG_RETURN_UINT64(x) return UInt64GetDatum(x)
  331. /* RETURN macros for other pass-by-ref types will typically look like this: */
  332. #define PG_RETURN_BYTEA_P(x) PG_RETURN_POINTER(x)
  333. #define PG_RETURN_TEXT_P(x) PG_RETURN_POINTER(x)
  334. #define PG_RETURN_BPCHAR_P(x) PG_RETURN_POINTER(x)
  335. #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
  336. #define PG_RETURN_HEAPTUPLEHEADER(x) return HeapTupleHeaderGetDatum(x)
  337. /*-------------------------------------------------------------------------
  338. * Support for detecting call convention of dynamically-loaded functions
  339. *
  340. * Dynamically loaded functions currently can only use the version-1 ("new
  341. * style") calling convention. Version-0 ("old style") is not supported
  342. * anymore. Version 1 is the call convention defined in this header file, and
  343. * must be accompanied by the macro call
  344. *
  345. * PG_FUNCTION_INFO_V1(function_name);
  346. *
  347. * Note that internal functions do not need this decoration since they are
  348. * assumed to be version-1.
  349. *
  350. *-------------------------------------------------------------------------
  351. */
  352. typedef struct
  353. {
  354. int api_version; /* specifies call convention version number */
  355. /* More fields may be added later, for version numbers > 1. */
  356. } Pg_finfo_record;
  357. /* Expected signature of an info function */
  358. typedef const Pg_finfo_record *(*PGFInfoFunction) (void);
  359. /*
  360. * Macro to build an info function associated with the given function name.
  361. *
  362. * As a convenience, also provide an "extern" declaration for the given
  363. * function name, so that writers of C functions need not write that too.
  364. *
  365. * On Windows, the function and info function must be exported. Our normal
  366. * build processes take care of that via .DEF files or --export-all-symbols.
  367. * Module authors using a different build process might need to manually
  368. * declare the function PGDLLEXPORT. We do that automatically here for the
  369. * info function, since authors shouldn't need to be explicitly aware of it.
  370. */
  371. #define PG_FUNCTION_INFO_V1(funcname) \
  372. extern Datum funcname(PG_FUNCTION_ARGS); \
  373. extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
  374. const Pg_finfo_record * \
  375. CppConcat(pg_finfo_,funcname) (void) \
  376. { \
  377. static const Pg_finfo_record my_finfo = { 1 }; \
  378. return &my_finfo; \
  379. } \
  380. extern int no_such_variable
  381. /*-------------------------------------------------------------------------
  382. * Support for verifying backend compatibility of loaded modules
  383. *
  384. * We require dynamically-loaded modules to include the macro call
  385. * PG_MODULE_MAGIC;
  386. * so that we can check for obvious incompatibility, such as being compiled
  387. * for a different major PostgreSQL version.
  388. *
  389. * To compile with versions of PostgreSQL that do not support this,
  390. * you may put an #ifdef/#endif test around it. Note that in a multiple-
  391. * source-file module, the macro call should only appear once.
  392. *
  393. * The specific items included in the magic block are intended to be ones that
  394. * are custom-configurable and especially likely to break dynamically loaded
  395. * modules if they were compiled with other values. Also, the length field
  396. * can be used to detect definition changes.
  397. *
  398. * Note: we compare magic blocks with memcmp(), so there had better not be
  399. * any alignment pad bytes in them.
  400. *
  401. * Note: when changing the contents of magic blocks, be sure to adjust the
  402. * incompatible_module_error() function in dfmgr.c.
  403. *-------------------------------------------------------------------------
  404. */
  405. /* Definition of the magic block structure */
  406. typedef struct
  407. {
  408. int len; /* sizeof(this struct) */
  409. int version; /* PostgreSQL major version */
  410. int funcmaxargs; /* FUNC_MAX_ARGS */
  411. int indexmaxkeys; /* INDEX_MAX_KEYS */
  412. int namedatalen; /* NAMEDATALEN */
  413. int float4byval; /* FLOAT4PASSBYVAL */
  414. int float8byval; /* FLOAT8PASSBYVAL */
  415. } Pg_magic_struct;
  416. /* The actual data block contents */
  417. #define PG_MODULE_MAGIC_DATA \
  418. { \
  419. sizeof(Pg_magic_struct), \
  420. PG_VERSION_NUM / 100, \
  421. FUNC_MAX_ARGS, \
  422. INDEX_MAX_KEYS, \
  423. NAMEDATALEN, \
  424. FLOAT4PASSBYVAL, \
  425. FLOAT8PASSBYVAL \
  426. }
  427. /*
  428. * Declare the module magic function. It needs to be a function as the dlsym
  429. * in the backend is only guaranteed to work on functions, not data
  430. */
  431. typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
  432. #define PG_MAGIC_FUNCTION_NAME Pg_magic_func
  433. #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
  434. #define PG_MODULE_MAGIC \
  435. extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
  436. const Pg_magic_struct * \
  437. PG_MAGIC_FUNCTION_NAME(void) \
  438. { \
  439. static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
  440. return &Pg_magic_data; \
  441. } \
  442. extern int no_such_variable
  443. /*-------------------------------------------------------------------------
  444. * Support routines and macros for callers of fmgr-compatible functions
  445. *-------------------------------------------------------------------------
  446. */
  447. /* These are for invocation of a specifically named function with a
  448. * directly-computed parameter list. Note that neither arguments nor result
  449. * are allowed to be NULL.
  450. */
  451. extern Datum DirectFunctionCall1Coll(PGFunction func, Oid collation,
  452. Datum arg1);
  453. extern Datum DirectFunctionCall2Coll(PGFunction func, Oid collation,
  454. Datum arg1, Datum arg2);
  455. extern Datum DirectFunctionCall3Coll(PGFunction func, Oid collation,
  456. Datum arg1, Datum arg2,
  457. Datum arg3);
  458. extern Datum DirectFunctionCall4Coll(PGFunction func, Oid collation,
  459. Datum arg1, Datum arg2,
  460. Datum arg3, Datum arg4);
  461. extern Datum DirectFunctionCall5Coll(PGFunction func, Oid collation,
  462. Datum arg1, Datum arg2,
  463. Datum arg3, Datum arg4, Datum arg5);
  464. extern Datum DirectFunctionCall6Coll(PGFunction func, Oid collation,
  465. Datum arg1, Datum arg2,
  466. Datum arg3, Datum arg4, Datum arg5,
  467. Datum arg6);
  468. extern Datum DirectFunctionCall7Coll(PGFunction func, Oid collation,
  469. Datum arg1, Datum arg2,
  470. Datum arg3, Datum arg4, Datum arg5,
  471. Datum arg6, Datum arg7);
  472. extern Datum DirectFunctionCall8Coll(PGFunction func, Oid collation,
  473. Datum arg1, Datum arg2,
  474. Datum arg3, Datum arg4, Datum arg5,
  475. Datum arg6, Datum arg7, Datum arg8);
  476. extern Datum DirectFunctionCall9Coll(PGFunction func, Oid collation,
  477. Datum arg1, Datum arg2,
  478. Datum arg3, Datum arg4, Datum arg5,
  479. Datum arg6, Datum arg7, Datum arg8,
  480. Datum arg9);
  481. /*
  482. * These functions work like the DirectFunctionCall functions except that
  483. * they use the flinfo parameter to initialise the fcinfo for the call.
  484. * It's recommended that the callee only use the fn_extra and fn_mcxt
  485. * fields, as other fields will typically describe the calling function
  486. * not the callee. Conversely, the calling function should not have
  487. * used fn_extra, unless its use is known to be compatible with the callee's.
  488. */
  489. extern Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo,
  490. Oid collation, Datum arg1);
  491. extern Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo,
  492. Oid collation, Datum arg1, Datum arg2);
  493. /* These are for invocation of a previously-looked-up function with a
  494. * directly-computed parameter list. Note that neither arguments nor result
  495. * are allowed to be NULL.
  496. */
  497. extern Datum FunctionCall0Coll(FmgrInfo *flinfo, Oid collation);
  498. extern Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation,
  499. Datum arg1);
  500. extern Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation,
  501. Datum arg1, Datum arg2);
  502. extern Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation,
  503. Datum arg1, Datum arg2,
  504. Datum arg3);
  505. extern Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation,
  506. Datum arg1, Datum arg2,
  507. Datum arg3, Datum arg4);
  508. extern Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation,
  509. Datum arg1, Datum arg2,
  510. Datum arg3, Datum arg4, Datum arg5);
  511. extern Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation,
  512. Datum arg1, Datum arg2,
  513. Datum arg3, Datum arg4, Datum arg5,
  514. Datum arg6);
  515. extern Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation,
  516. Datum arg1, Datum arg2,
  517. Datum arg3, Datum arg4, Datum arg5,
  518. Datum arg6, Datum arg7);
  519. extern Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation,
  520. Datum arg1, Datum arg2,
  521. Datum arg3, Datum arg4, Datum arg5,
  522. Datum arg6, Datum arg7, Datum arg8);
  523. extern Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation,
  524. Datum arg1, Datum arg2,
  525. Datum arg3, Datum arg4, Datum arg5,
  526. Datum arg6, Datum arg7, Datum arg8,
  527. Datum arg9);
  528. /* These are for invocation of a function identified by OID with a
  529. * directly-computed parameter list. Note that neither arguments nor result
  530. * are allowed to be NULL. These are essentially fmgr_info() followed by
  531. * FunctionCallN(). If the same function is to be invoked repeatedly, do the
  532. * fmgr_info() once and then use FunctionCallN().
  533. */
  534. extern Datum OidFunctionCall0Coll(Oid functionId, Oid collation);
  535. extern Datum OidFunctionCall1Coll(Oid functionId, Oid collation,
  536. Datum arg1);
  537. extern Datum OidFunctionCall2Coll(Oid functionId, Oid collation,
  538. Datum arg1, Datum arg2);
  539. extern Datum OidFunctionCall3Coll(Oid functionId, Oid collation,
  540. Datum arg1, Datum arg2,
  541. Datum arg3);
  542. extern Datum OidFunctionCall4Coll(Oid functionId, Oid collation,
  543. Datum arg1, Datum arg2,
  544. Datum arg3, Datum arg4);
  545. extern Datum OidFunctionCall5Coll(Oid functionId, Oid collation,
  546. Datum arg1, Datum arg2,
  547. Datum arg3, Datum arg4, Datum arg5);
  548. extern Datum OidFunctionCall6Coll(Oid functionId, Oid collation,
  549. Datum arg1, Datum arg2,
  550. Datum arg3, Datum arg4, Datum arg5,
  551. Datum arg6);
  552. extern Datum OidFunctionCall7Coll(Oid functionId, Oid collation,
  553. Datum arg1, Datum arg2,
  554. Datum arg3, Datum arg4, Datum arg5,
  555. Datum arg6, Datum arg7);
  556. extern Datum OidFunctionCall8Coll(Oid functionId, Oid collation,
  557. Datum arg1, Datum arg2,
  558. Datum arg3, Datum arg4, Datum arg5,
  559. Datum arg6, Datum arg7, Datum arg8);
  560. extern Datum OidFunctionCall9Coll(Oid functionId, Oid collation,
  561. Datum arg1, Datum arg2,
  562. Datum arg3, Datum arg4, Datum arg5,
  563. Datum arg6, Datum arg7, Datum arg8,
  564. Datum arg9);
  565. /* These macros allow the collation argument to be omitted (with a default of
  566. * InvalidOid, ie, no collation). They exist mostly for backwards
  567. * compatibility of source code.
  568. */
  569. #define DirectFunctionCall1(func, arg1) \
  570. DirectFunctionCall1Coll(func, InvalidOid, arg1)
  571. #define DirectFunctionCall2(func, arg1, arg2) \
  572. DirectFunctionCall2Coll(func, InvalidOid, arg1, arg2)
  573. #define DirectFunctionCall3(func, arg1, arg2, arg3) \
  574. DirectFunctionCall3Coll(func, InvalidOid, arg1, arg2, arg3)
  575. #define DirectFunctionCall4(func, arg1, arg2, arg3, arg4) \
  576. DirectFunctionCall4Coll(func, InvalidOid, arg1, arg2, arg3, arg4)
  577. #define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5) \
  578. DirectFunctionCall5Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  579. #define DirectFunctionCall6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
  580. DirectFunctionCall6Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  581. #define DirectFunctionCall7(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  582. DirectFunctionCall7Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  583. #define DirectFunctionCall8(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  584. DirectFunctionCall8Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  585. #define DirectFunctionCall9(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  586. DirectFunctionCall9Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  587. #define FunctionCall1(flinfo, arg1) \
  588. FunctionCall1Coll(flinfo, InvalidOid, arg1)
  589. #define FunctionCall2(flinfo, arg1, arg2) \
  590. FunctionCall2Coll(flinfo, InvalidOid, arg1, arg2)
  591. #define FunctionCall3(flinfo, arg1, arg2, arg3) \
  592. FunctionCall3Coll(flinfo, InvalidOid, arg1, arg2, arg3)
  593. #define FunctionCall4(flinfo, arg1, arg2, arg3, arg4) \
  594. FunctionCall4Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4)
  595. #define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5) \
  596. FunctionCall5Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  597. #define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6) \
  598. FunctionCall6Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  599. #define FunctionCall7(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  600. FunctionCall7Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  601. #define FunctionCall8(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  602. FunctionCall8Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  603. #define FunctionCall9(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  604. FunctionCall9Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  605. #define OidFunctionCall0(functionId) \
  606. OidFunctionCall0Coll(functionId, InvalidOid)
  607. #define OidFunctionCall1(functionId, arg1) \
  608. OidFunctionCall1Coll(functionId, InvalidOid, arg1)
  609. #define OidFunctionCall2(functionId, arg1, arg2) \
  610. OidFunctionCall2Coll(functionId, InvalidOid, arg1, arg2)
  611. #define OidFunctionCall3(functionId, arg1, arg2, arg3) \
  612. OidFunctionCall3Coll(functionId, InvalidOid, arg1, arg2, arg3)
  613. #define OidFunctionCall4(functionId, arg1, arg2, arg3, arg4) \
  614. OidFunctionCall4Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4)
  615. #define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5) \
  616. OidFunctionCall5Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  617. #define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6) \
  618. OidFunctionCall6Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  619. #define OidFunctionCall7(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  620. OidFunctionCall7Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  621. #define OidFunctionCall8(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  622. OidFunctionCall8Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  623. #define OidFunctionCall9(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  624. OidFunctionCall9Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  625. /* Special cases for convenient invocation of datatype I/O functions. */
  626. extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str,
  627. Oid typioparam, int32 typmod);
  628. extern Datum OidInputFunctionCall(Oid functionId, char *str,
  629. Oid typioparam, int32 typmod);
  630. extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
  631. extern char *OidOutputFunctionCall(Oid functionId, Datum val);
  632. extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf,
  633. Oid typioparam, int32 typmod);
  634. extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf,
  635. Oid typioparam, int32 typmod);
  636. extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val);
  637. extern bytea *OidSendFunctionCall(Oid functionId, Datum val);
  638. /*
  639. * Routines in fmgr.c
  640. */
  641. extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, const char *funcname);
  642. extern void clear_external_function_hash(void *filehandle);
  643. extern Oid fmgr_internal_function(const char *proname);
  644. extern Oid get_fn_expr_rettype(FmgrInfo *flinfo);
  645. extern Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
  646. extern Oid get_call_expr_argtype(fmNodePtr expr, int argnum);
  647. extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum);
  648. extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum);
  649. extern bool get_fn_expr_variadic(FmgrInfo *flinfo);
  650. extern bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid);
  651. /*
  652. * Routines in dfmgr.c
  653. */
  654. extern char *Dynamic_library_path;
  655. extern PGFunction load_external_function(const char *filename, const char *funcname,
  656. bool signalNotFound, void **filehandle);
  657. extern PGFunction lookup_external_function(void *filehandle, const char *funcname);
  658. extern void load_file(const char *filename, bool restricted);
  659. extern void **find_rendezvous_variable(const char *varName);
  660. extern Size EstimateLibraryStateSpace(void);
  661. extern void SerializeLibraryState(Size maxsize, char *start_address);
  662. extern void RestoreLibraryState(char *start_address);
  663. /*
  664. * Support for aggregate functions
  665. *
  666. * These are actually in executor/nodeAgg.c, but we declare them here since
  667. * the whole point is for callers to not be overly friendly with nodeAgg.
  668. */
  669. /* AggCheckCallContext can return one of the following codes, or 0: */
  670. #define AGG_CONTEXT_AGGREGATE 1 /* regular aggregate */
  671. #define AGG_CONTEXT_WINDOW 2 /* window function */
  672. extern int AggCheckCallContext(FunctionCallInfo fcinfo,
  673. MemoryContext *aggcontext);
  674. extern fmAggrefPtr AggGetAggref(FunctionCallInfo fcinfo);
  675. extern MemoryContext AggGetTempMemoryContext(FunctionCallInfo fcinfo);
  676. extern bool AggStateIsShared(FunctionCallInfo fcinfo);
  677. extern void AggRegisterCallback(FunctionCallInfo fcinfo,
  678. fmExprContextCallbackFunction func,
  679. Datum arg);
  680. /*
  681. * We allow plugin modules to hook function entry/exit. This is intended
  682. * as support for loadable security policy modules, which may want to
  683. * perform additional privilege checks on function entry or exit, or to do
  684. * other internal bookkeeping. To make this possible, such modules must be
  685. * able not only to support normal function entry and exit, but also to trap
  686. * the case where we bail out due to an error; and they must also be able to
  687. * prevent inlining.
  688. */
  689. typedef enum FmgrHookEventType
  690. {
  691. FHET_START,
  692. FHET_END,
  693. FHET_ABORT
  694. } FmgrHookEventType;
  695. typedef bool (*needs_fmgr_hook_type) (Oid fn_oid);
  696. typedef void (*fmgr_hook_type) (FmgrHookEventType event,
  697. FmgrInfo *flinfo, Datum *arg);
  698. extern PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook;
  699. extern PGDLLIMPORT fmgr_hook_type fmgr_hook;
  700. #define FmgrHookIsNeeded(fn_oid) \
  701. (!needs_fmgr_hook ? false : (*needs_fmgr_hook)(fn_oid))
  702. #endif /* FMGR_H */
上海开阖软件有限公司 沪ICP备12045867号-1