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