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