fmgr.h 31 KB

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