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