fmgr.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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-2022, 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. #define PG_GETARG_TRANSACTIONID(n) DatumGetTransactionId(PG_GETARG_DATUM(n))
  251. /* these macros hide the pass-by-reference-ness of the datatype: */
  252. #define PG_GETARG_FLOAT4(n) DatumGetFloat4(PG_GETARG_DATUM(n))
  253. #define PG_GETARG_FLOAT8(n) DatumGetFloat8(PG_GETARG_DATUM(n))
  254. #define PG_GETARG_INT64(n) DatumGetInt64(PG_GETARG_DATUM(n))
  255. /* use this if you want the raw, possibly-toasted input datum: */
  256. #define PG_GETARG_RAW_VARLENA_P(n) ((struct varlena *) PG_GETARG_POINTER(n))
  257. /* use this if you want the input datum de-toasted: */
  258. #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
  259. /* and this if you can handle 1-byte-header datums: */
  260. #define PG_GETARG_VARLENA_PP(n) PG_DETOAST_DATUM_PACKED(PG_GETARG_DATUM(n))
  261. /* DatumGetFoo macros for varlena types will typically look like this: */
  262. #define DatumGetByteaPP(X) ((bytea *) PG_DETOAST_DATUM_PACKED(X))
  263. #define DatumGetTextPP(X) ((text *) PG_DETOAST_DATUM_PACKED(X))
  264. #define DatumGetBpCharPP(X) ((BpChar *) PG_DETOAST_DATUM_PACKED(X))
  265. #define DatumGetVarCharPP(X) ((VarChar *) PG_DETOAST_DATUM_PACKED(X))
  266. #define DatumGetHeapTupleHeader(X) ((HeapTupleHeader) PG_DETOAST_DATUM(X))
  267. /* And we also offer variants that return an OK-to-write copy */
  268. #define DatumGetByteaPCopy(X) ((bytea *) PG_DETOAST_DATUM_COPY(X))
  269. #define DatumGetTextPCopy(X) ((text *) PG_DETOAST_DATUM_COPY(X))
  270. #define DatumGetBpCharPCopy(X) ((BpChar *) PG_DETOAST_DATUM_COPY(X))
  271. #define DatumGetVarCharPCopy(X) ((VarChar *) PG_DETOAST_DATUM_COPY(X))
  272. #define DatumGetHeapTupleHeaderCopy(X) ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X))
  273. /* Variants which return n bytes starting at pos. m */
  274. #define DatumGetByteaPSlice(X,m,n) ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
  275. #define DatumGetTextPSlice(X,m,n) ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
  276. #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
  277. #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
  278. /* GETARG macros for varlena types will typically look like this: */
  279. #define PG_GETARG_BYTEA_PP(n) DatumGetByteaPP(PG_GETARG_DATUM(n))
  280. #define PG_GETARG_TEXT_PP(n) DatumGetTextPP(PG_GETARG_DATUM(n))
  281. #define PG_GETARG_BPCHAR_PP(n) DatumGetBpCharPP(PG_GETARG_DATUM(n))
  282. #define PG_GETARG_VARCHAR_PP(n) DatumGetVarCharPP(PG_GETARG_DATUM(n))
  283. #define PG_GETARG_HEAPTUPLEHEADER(n) DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
  284. /* And we also offer variants that return an OK-to-write copy */
  285. #define PG_GETARG_BYTEA_P_COPY(n) DatumGetByteaPCopy(PG_GETARG_DATUM(n))
  286. #define PG_GETARG_TEXT_P_COPY(n) DatumGetTextPCopy(PG_GETARG_DATUM(n))
  287. #define PG_GETARG_BPCHAR_P_COPY(n) DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
  288. #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
  289. #define PG_GETARG_HEAPTUPLEHEADER_COPY(n) DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n))
  290. /* And a b-byte slice from position a -also OK to write */
  291. #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
  292. #define PG_GETARG_TEXT_P_SLICE(n,a,b) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
  293. #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
  294. #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
  295. /*
  296. * Obsolescent variants that guarantee INT alignment for the return value.
  297. * Few operations on these particular types need alignment, mainly operations
  298. * that cast the VARDATA pointer to a type like int16[]. Most code should use
  299. * the ...PP(X) counterpart. Nonetheless, these appear frequently in code
  300. * predating the PostgreSQL 8.3 introduction of the ...PP(X) variants.
  301. */
  302. #define DatumGetByteaP(X) ((bytea *) PG_DETOAST_DATUM(X))
  303. #define DatumGetTextP(X) ((text *) PG_DETOAST_DATUM(X))
  304. #define DatumGetBpCharP(X) ((BpChar *) PG_DETOAST_DATUM(X))
  305. #define DatumGetVarCharP(X) ((VarChar *) PG_DETOAST_DATUM(X))
  306. #define PG_GETARG_BYTEA_P(n) DatumGetByteaP(PG_GETARG_DATUM(n))
  307. #define PG_GETARG_TEXT_P(n) DatumGetTextP(PG_GETARG_DATUM(n))
  308. #define PG_GETARG_BPCHAR_P(n) DatumGetBpCharP(PG_GETARG_DATUM(n))
  309. #define PG_GETARG_VARCHAR_P(n) DatumGetVarCharP(PG_GETARG_DATUM(n))
  310. /* To access options from opclass support functions use this: */
  311. #define PG_HAS_OPCLASS_OPTIONS() has_fn_opclass_options(fcinfo->flinfo)
  312. #define PG_GET_OPCLASS_OPTIONS() get_fn_opclass_options(fcinfo->flinfo)
  313. /* To return a NULL do this: */
  314. #define PG_RETURN_NULL() \
  315. do { fcinfo->isnull = true; return (Datum) 0; } while (0)
  316. /* A few internal functions return void (which is not the same as NULL!) */
  317. #define PG_RETURN_VOID() return (Datum) 0
  318. /* Macros for returning results of standard types */
  319. #define PG_RETURN_DATUM(x) return (x)
  320. #define PG_RETURN_INT32(x) return Int32GetDatum(x)
  321. #define PG_RETURN_UINT32(x) return UInt32GetDatum(x)
  322. #define PG_RETURN_INT16(x) return Int16GetDatum(x)
  323. #define PG_RETURN_UINT16(x) return UInt16GetDatum(x)
  324. #define PG_RETURN_CHAR(x) return CharGetDatum(x)
  325. #define PG_RETURN_BOOL(x) return BoolGetDatum(x)
  326. #define PG_RETURN_OID(x) return ObjectIdGetDatum(x)
  327. #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
  328. #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
  329. #define PG_RETURN_NAME(x) return NameGetDatum(x)
  330. #define PG_RETURN_TRANSACTIONID(x) return TransactionIdGetDatum(x)
  331. /* these macros hide the pass-by-reference-ness of the datatype: */
  332. #define PG_RETURN_FLOAT4(x) return Float4GetDatum(x)
  333. #define PG_RETURN_FLOAT8(x) return Float8GetDatum(x)
  334. #define PG_RETURN_INT64(x) return Int64GetDatum(x)
  335. #define PG_RETURN_UINT64(x) return UInt64GetDatum(x)
  336. /* RETURN macros for other pass-by-ref types will typically look like this: */
  337. #define PG_RETURN_BYTEA_P(x) PG_RETURN_POINTER(x)
  338. #define PG_RETURN_TEXT_P(x) PG_RETURN_POINTER(x)
  339. #define PG_RETURN_BPCHAR_P(x) PG_RETURN_POINTER(x)
  340. #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
  341. #define PG_RETURN_HEAPTUPLEHEADER(x) return HeapTupleHeaderGetDatum(x)
  342. /*-------------------------------------------------------------------------
  343. * Support for detecting call convention of dynamically-loaded functions
  344. *
  345. * Dynamically loaded functions currently can only use the version-1 ("new
  346. * style") calling convention. Version-0 ("old style") is not supported
  347. * anymore. Version 1 is the call convention defined in this header file, and
  348. * must be accompanied by the macro call
  349. *
  350. * PG_FUNCTION_INFO_V1(function_name);
  351. *
  352. * Note that internal functions do not need this decoration since they are
  353. * assumed to be version-1.
  354. *
  355. *-------------------------------------------------------------------------
  356. */
  357. typedef struct
  358. {
  359. int api_version; /* specifies call convention version number */
  360. /* More fields may be added later, for version numbers > 1. */
  361. } Pg_finfo_record;
  362. /* Expected signature of an info function */
  363. typedef const Pg_finfo_record *(*PGFInfoFunction) (void);
  364. /*
  365. * Macro to build an info function associated with the given function name.
  366. *
  367. * As a convenience, also provide an "extern" declaration for the given
  368. * function name, so that writers of C functions need not write that too.
  369. *
  370. * On Windows, the function and info function must be exported. Our normal
  371. * build processes take care of that via .DEF files or --export-all-symbols.
  372. * Module authors using a different build process might need to manually
  373. * declare the function PGDLLEXPORT. We do that automatically here for the
  374. * info function, since authors shouldn't need to be explicitly aware of it.
  375. */
  376. #define PG_FUNCTION_INFO_V1(funcname) \
  377. extern Datum funcname(PG_FUNCTION_ARGS); \
  378. extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
  379. const Pg_finfo_record * \
  380. CppConcat(pg_finfo_,funcname) (void) \
  381. { \
  382. static const Pg_finfo_record my_finfo = { 1 }; \
  383. return &my_finfo; \
  384. } \
  385. extern int no_such_variable
  386. /*-------------------------------------------------------------------------
  387. * Support for verifying backend compatibility of loaded modules
  388. *
  389. * We require dynamically-loaded modules to include the macro call
  390. * PG_MODULE_MAGIC;
  391. * so that we can check for obvious incompatibility, such as being compiled
  392. * for a different major PostgreSQL version.
  393. *
  394. * To compile with versions of PostgreSQL that do not support this,
  395. * you may put an #ifdef/#endif test around it. Note that in a multiple-
  396. * source-file module, the macro call should only appear once.
  397. *
  398. * The specific items included in the magic block are intended to be ones that
  399. * are custom-configurable and especially likely to break dynamically loaded
  400. * modules if they were compiled with other values. Also, the length field
  401. * can be used to detect definition changes.
  402. *
  403. * Note: we compare magic blocks with memcmp(), so there had better not be
  404. * any alignment pad bytes in them.
  405. *
  406. * Note: when changing the contents of magic blocks, be sure to adjust the
  407. * incompatible_module_error() function in dfmgr.c.
  408. *-------------------------------------------------------------------------
  409. */
  410. /* Definition of the magic block structure */
  411. typedef struct
  412. {
  413. int len; /* sizeof(this struct) */
  414. int version; /* PostgreSQL major version */
  415. int funcmaxargs; /* FUNC_MAX_ARGS */
  416. int indexmaxkeys; /* INDEX_MAX_KEYS */
  417. int namedatalen; /* NAMEDATALEN */
  418. int float8byval; /* FLOAT8PASSBYVAL */
  419. char abi_extra[32]; /* see pg_config_manual.h */
  420. } Pg_magic_struct;
  421. /* The actual data block contents */
  422. #define PG_MODULE_MAGIC_DATA \
  423. { \
  424. sizeof(Pg_magic_struct), \
  425. PG_VERSION_NUM / 100, \
  426. FUNC_MAX_ARGS, \
  427. INDEX_MAX_KEYS, \
  428. NAMEDATALEN, \
  429. FLOAT8PASSBYVAL, \
  430. FMGR_ABI_EXTRA, \
  431. }
  432. StaticAssertDecl(sizeof(FMGR_ABI_EXTRA) <= sizeof(((Pg_magic_struct *) 0)->abi_extra),
  433. "FMGR_ABI_EXTRA too long");
  434. /*
  435. * Declare the module magic function. It needs to be a function as the dlsym
  436. * in the backend is only guaranteed to work on functions, not data
  437. */
  438. typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
  439. #define PG_MAGIC_FUNCTION_NAME Pg_magic_func
  440. #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
  441. #define PG_MODULE_MAGIC \
  442. extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
  443. const Pg_magic_struct * \
  444. PG_MAGIC_FUNCTION_NAME(void) \
  445. { \
  446. static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
  447. return &Pg_magic_data; \
  448. } \
  449. extern int no_such_variable
  450. /*-------------------------------------------------------------------------
  451. * Support routines and macros for callers of fmgr-compatible functions
  452. *-------------------------------------------------------------------------
  453. */
  454. /* These are for invocation of a specifically named function with a
  455. * directly-computed parameter list. Note that neither arguments nor result
  456. * are allowed to be NULL. Also, the function cannot be one that needs to
  457. * look at FmgrInfo, since there won't be any.
  458. */
  459. extern Datum DirectFunctionCall1Coll(PGFunction func, Oid collation,
  460. Datum arg1);
  461. extern Datum DirectFunctionCall2Coll(PGFunction func, Oid collation,
  462. Datum arg1, Datum arg2);
  463. extern Datum DirectFunctionCall3Coll(PGFunction func, Oid collation,
  464. Datum arg1, Datum arg2,
  465. Datum arg3);
  466. extern Datum DirectFunctionCall4Coll(PGFunction func, Oid collation,
  467. Datum arg1, Datum arg2,
  468. Datum arg3, Datum arg4);
  469. extern Datum DirectFunctionCall5Coll(PGFunction func, Oid collation,
  470. Datum arg1, Datum arg2,
  471. Datum arg3, Datum arg4, Datum arg5);
  472. extern Datum DirectFunctionCall6Coll(PGFunction func, Oid collation,
  473. Datum arg1, Datum arg2,
  474. Datum arg3, Datum arg4, Datum arg5,
  475. Datum arg6);
  476. extern Datum DirectFunctionCall7Coll(PGFunction func, Oid collation,
  477. Datum arg1, Datum arg2,
  478. Datum arg3, Datum arg4, Datum arg5,
  479. Datum arg6, Datum arg7);
  480. extern Datum DirectFunctionCall8Coll(PGFunction func, Oid collation,
  481. Datum arg1, Datum arg2,
  482. Datum arg3, Datum arg4, Datum arg5,
  483. Datum arg6, Datum arg7, Datum arg8);
  484. extern Datum DirectFunctionCall9Coll(PGFunction func, Oid collation,
  485. Datum arg1, Datum arg2,
  486. Datum arg3, Datum arg4, Datum arg5,
  487. Datum arg6, Datum arg7, Datum arg8,
  488. Datum arg9);
  489. /*
  490. * These functions work like the DirectFunctionCall functions except that
  491. * they use the flinfo parameter to initialise the fcinfo for the call.
  492. * It's recommended that the callee only use the fn_extra and fn_mcxt
  493. * fields, as other fields will typically describe the calling function
  494. * not the callee. Conversely, the calling function should not have
  495. * used fn_extra, unless its use is known to be compatible with the callee's.
  496. */
  497. extern Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo,
  498. Oid collation, Datum arg1);
  499. extern Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo,
  500. Oid collation, Datum arg1, Datum arg2);
  501. /* These are for invocation of a previously-looked-up function with a
  502. * directly-computed parameter list. Note that neither arguments nor result
  503. * are allowed to be NULL.
  504. */
  505. extern Datum FunctionCall0Coll(FmgrInfo *flinfo, Oid collation);
  506. extern Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation,
  507. Datum arg1);
  508. extern Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation,
  509. Datum arg1, Datum arg2);
  510. extern Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation,
  511. Datum arg1, Datum arg2,
  512. Datum arg3);
  513. extern Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation,
  514. Datum arg1, Datum arg2,
  515. Datum arg3, Datum arg4);
  516. extern Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation,
  517. Datum arg1, Datum arg2,
  518. Datum arg3, Datum arg4, Datum arg5);
  519. extern Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation,
  520. Datum arg1, Datum arg2,
  521. Datum arg3, Datum arg4, Datum arg5,
  522. Datum arg6);
  523. extern Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation,
  524. Datum arg1, Datum arg2,
  525. Datum arg3, Datum arg4, Datum arg5,
  526. Datum arg6, Datum arg7);
  527. extern Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation,
  528. Datum arg1, Datum arg2,
  529. Datum arg3, Datum arg4, Datum arg5,
  530. Datum arg6, Datum arg7, Datum arg8);
  531. extern Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation,
  532. Datum arg1, Datum arg2,
  533. Datum arg3, Datum arg4, Datum arg5,
  534. Datum arg6, Datum arg7, Datum arg8,
  535. Datum arg9);
  536. /* These are for invocation of a function identified by OID with a
  537. * directly-computed parameter list. Note that neither arguments nor result
  538. * are allowed to be NULL. These are essentially fmgr_info() followed by
  539. * FunctionCallN(). If the same function is to be invoked repeatedly, do the
  540. * fmgr_info() once and then use FunctionCallN().
  541. */
  542. extern Datum OidFunctionCall0Coll(Oid functionId, Oid collation);
  543. extern Datum OidFunctionCall1Coll(Oid functionId, Oid collation,
  544. Datum arg1);
  545. extern Datum OidFunctionCall2Coll(Oid functionId, Oid collation,
  546. Datum arg1, Datum arg2);
  547. extern Datum OidFunctionCall3Coll(Oid functionId, Oid collation,
  548. Datum arg1, Datum arg2,
  549. Datum arg3);
  550. extern Datum OidFunctionCall4Coll(Oid functionId, Oid collation,
  551. Datum arg1, Datum arg2,
  552. Datum arg3, Datum arg4);
  553. extern Datum OidFunctionCall5Coll(Oid functionId, Oid collation,
  554. Datum arg1, Datum arg2,
  555. Datum arg3, Datum arg4, Datum arg5);
  556. extern Datum OidFunctionCall6Coll(Oid functionId, Oid collation,
  557. Datum arg1, Datum arg2,
  558. Datum arg3, Datum arg4, Datum arg5,
  559. Datum arg6);
  560. extern Datum OidFunctionCall7Coll(Oid functionId, Oid collation,
  561. Datum arg1, Datum arg2,
  562. Datum arg3, Datum arg4, Datum arg5,
  563. Datum arg6, Datum arg7);
  564. extern Datum OidFunctionCall8Coll(Oid functionId, Oid collation,
  565. Datum arg1, Datum arg2,
  566. Datum arg3, Datum arg4, Datum arg5,
  567. Datum arg6, Datum arg7, Datum arg8);
  568. extern Datum OidFunctionCall9Coll(Oid functionId, Oid collation,
  569. Datum arg1, Datum arg2,
  570. Datum arg3, Datum arg4, Datum arg5,
  571. Datum arg6, Datum arg7, Datum arg8,
  572. Datum arg9);
  573. /* These macros allow the collation argument to be omitted (with a default of
  574. * InvalidOid, ie, no collation). They exist mostly for backwards
  575. * compatibility of source code.
  576. */
  577. #define DirectFunctionCall1(func, arg1) \
  578. DirectFunctionCall1Coll(func, InvalidOid, arg1)
  579. #define DirectFunctionCall2(func, arg1, arg2) \
  580. DirectFunctionCall2Coll(func, InvalidOid, arg1, arg2)
  581. #define DirectFunctionCall3(func, arg1, arg2, arg3) \
  582. DirectFunctionCall3Coll(func, InvalidOid, arg1, arg2, arg3)
  583. #define DirectFunctionCall4(func, arg1, arg2, arg3, arg4) \
  584. DirectFunctionCall4Coll(func, InvalidOid, arg1, arg2, arg3, arg4)
  585. #define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5) \
  586. DirectFunctionCall5Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  587. #define DirectFunctionCall6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
  588. DirectFunctionCall6Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  589. #define DirectFunctionCall7(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  590. DirectFunctionCall7Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  591. #define DirectFunctionCall8(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  592. DirectFunctionCall8Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  593. #define DirectFunctionCall9(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  594. DirectFunctionCall9Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  595. #define FunctionCall1(flinfo, arg1) \
  596. FunctionCall1Coll(flinfo, InvalidOid, arg1)
  597. #define FunctionCall2(flinfo, arg1, arg2) \
  598. FunctionCall2Coll(flinfo, InvalidOid, arg1, arg2)
  599. #define FunctionCall3(flinfo, arg1, arg2, arg3) \
  600. FunctionCall3Coll(flinfo, InvalidOid, arg1, arg2, arg3)
  601. #define FunctionCall4(flinfo, arg1, arg2, arg3, arg4) \
  602. FunctionCall4Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4)
  603. #define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5) \
  604. FunctionCall5Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  605. #define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6) \
  606. FunctionCall6Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  607. #define FunctionCall7(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  608. FunctionCall7Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  609. #define FunctionCall8(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  610. FunctionCall8Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  611. #define FunctionCall9(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  612. FunctionCall9Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  613. #define OidFunctionCall0(functionId) \
  614. OidFunctionCall0Coll(functionId, InvalidOid)
  615. #define OidFunctionCall1(functionId, arg1) \
  616. OidFunctionCall1Coll(functionId, InvalidOid, arg1)
  617. #define OidFunctionCall2(functionId, arg1, arg2) \
  618. OidFunctionCall2Coll(functionId, InvalidOid, arg1, arg2)
  619. #define OidFunctionCall3(functionId, arg1, arg2, arg3) \
  620. OidFunctionCall3Coll(functionId, InvalidOid, arg1, arg2, arg3)
  621. #define OidFunctionCall4(functionId, arg1, arg2, arg3, arg4) \
  622. OidFunctionCall4Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4)
  623. #define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5) \
  624. OidFunctionCall5Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5)
  625. #define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6) \
  626. OidFunctionCall6Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
  627. #define OidFunctionCall7(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  628. OidFunctionCall7Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  629. #define OidFunctionCall8(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  630. OidFunctionCall8Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  631. #define OidFunctionCall9(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
  632. OidFunctionCall9Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  633. /* Special cases for convenient invocation of datatype I/O functions. */
  634. extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str,
  635. Oid typioparam, int32 typmod);
  636. extern Datum OidInputFunctionCall(Oid functionId, char *str,
  637. Oid typioparam, int32 typmod);
  638. extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
  639. extern char *OidOutputFunctionCall(Oid functionId, Datum val);
  640. extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf,
  641. Oid typioparam, int32 typmod);
  642. extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf,
  643. Oid typioparam, int32 typmod);
  644. extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val);
  645. extern bytea *OidSendFunctionCall(Oid functionId, Datum val);
  646. /*
  647. * Routines in fmgr.c
  648. */
  649. extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, const char *funcname);
  650. extern Oid fmgr_internal_function(const char *proname);
  651. extern Oid get_fn_expr_rettype(FmgrInfo *flinfo);
  652. extern Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
  653. extern Oid get_call_expr_argtype(fmNodePtr expr, int argnum);
  654. extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum);
  655. extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum);
  656. extern bool get_fn_expr_variadic(FmgrInfo *flinfo);
  657. extern bytea *get_fn_opclass_options(FmgrInfo *flinfo);
  658. extern bool has_fn_opclass_options(FmgrInfo *flinfo);
  659. extern void set_fn_opclass_options(FmgrInfo *flinfo, bytea *options);
  660. extern bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid);
  661. /*
  662. * Routines in dfmgr.c
  663. */
  664. extern PGDLLIMPORT char *Dynamic_library_path;
  665. extern void *load_external_function(const char *filename, const char *funcname,
  666. bool signalNotFound, void **filehandle);
  667. extern void *lookup_external_function(void *filehandle, const char *funcname);
  668. extern void load_file(const char *filename, bool restricted);
  669. extern void **find_rendezvous_variable(const char *varName);
  670. extern Size EstimateLibraryStateSpace(void);
  671. extern void SerializeLibraryState(Size maxsize, char *start_address);
  672. extern void RestoreLibraryState(char *start_address);
  673. /*
  674. * Support for aggregate functions
  675. *
  676. * These are actually in executor/nodeAgg.c, but we declare them here since
  677. * the whole point is for callers to not be overly friendly with nodeAgg.
  678. */
  679. /* AggCheckCallContext can return one of the following codes, or 0: */
  680. #define AGG_CONTEXT_AGGREGATE 1 /* regular aggregate */
  681. #define AGG_CONTEXT_WINDOW 2 /* window function */
  682. extern int AggCheckCallContext(FunctionCallInfo fcinfo,
  683. MemoryContext *aggcontext);
  684. extern fmAggrefPtr AggGetAggref(FunctionCallInfo fcinfo);
  685. extern MemoryContext AggGetTempMemoryContext(FunctionCallInfo fcinfo);
  686. extern bool AggStateIsShared(FunctionCallInfo fcinfo);
  687. extern void AggRegisterCallback(FunctionCallInfo fcinfo,
  688. fmExprContextCallbackFunction func,
  689. Datum arg);
  690. /*
  691. * We allow plugin modules to hook function entry/exit. This is intended
  692. * as support for loadable security policy modules, which may want to
  693. * perform additional privilege checks on function entry or exit, or to do
  694. * other internal bookkeeping. To make this possible, such modules must be
  695. * able not only to support normal function entry and exit, but also to trap
  696. * the case where we bail out due to an error; and they must also be able to
  697. * prevent inlining.
  698. */
  699. typedef enum FmgrHookEventType
  700. {
  701. FHET_START,
  702. FHET_END,
  703. FHET_ABORT
  704. } FmgrHookEventType;
  705. typedef bool (*needs_fmgr_hook_type) (Oid fn_oid);
  706. typedef void (*fmgr_hook_type) (FmgrHookEventType event,
  707. FmgrInfo *flinfo, Datum *arg);
  708. extern PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook;
  709. extern PGDLLIMPORT fmgr_hook_type fmgr_hook;
  710. #define FmgrHookIsNeeded(fn_oid) \
  711. (!needs_fmgr_hook ? false : (*needs_fmgr_hook)(fn_oid))
  712. #endif /* FMGR_H */