]> granicus.if.org Git - postgresql/blob - src/include/fmgr.h
Reindent table partitioning code.
[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-2017, 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  *
348  *      As a convenience, also provide an "extern" declaration for the given
349  *      function name, so that writers of C functions need not write that too.
350  *
351  *      On Windows, the function and info function must be exported.  Our normal
352  *      build processes take care of that via .DEF files or --export-all-symbols.
353  *      Module authors using a different build process might need to manually
354  *      declare the function PGDLLEXPORT.  We do that automatically here for the
355  *      info function, since authors shouldn't need to be explicitly aware of it.
356  */
357 #define PG_FUNCTION_INFO_V1(funcname) \
358 extern Datum funcname(PG_FUNCTION_ARGS); \
359 extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
360 const Pg_finfo_record * \
361 CppConcat(pg_finfo_,funcname) (void) \
362 { \
363         static const Pg_finfo_record my_finfo = { 1 }; \
364         return &my_finfo; \
365 } \
366 extern int no_such_variable
367
368
369 /*-------------------------------------------------------------------------
370  *              Support for verifying backend compatibility of loaded modules
371  *
372  * We require dynamically-loaded modules to include the macro call
373  *              PG_MODULE_MAGIC;
374  * so that we can check for obvious incompatibility, such as being compiled
375  * for a different major PostgreSQL version.
376  *
377  * To compile with versions of PostgreSQL that do not support this,
378  * you may put an #ifdef/#endif test around it.  Note that in a multiple-
379  * source-file module, the macro call should only appear once.
380  *
381  * The specific items included in the magic block are intended to be ones that
382  * are custom-configurable and especially likely to break dynamically loaded
383  * modules if they were compiled with other values.  Also, the length field
384  * can be used to detect definition changes.
385  *
386  * Note: we compare magic blocks with memcmp(), so there had better not be
387  * any alignment pad bytes in them.
388  *
389  * Note: when changing the contents of magic blocks, be sure to adjust the
390  * incompatible_module_error() function in dfmgr.c.
391  *-------------------------------------------------------------------------
392  */
393
394 /* Definition of the magic block structure */
395 typedef struct
396 {
397         int                     len;                    /* sizeof(this struct) */
398         int                     version;                /* PostgreSQL major version */
399         int                     funcmaxargs;    /* FUNC_MAX_ARGS */
400         int                     indexmaxkeys;   /* INDEX_MAX_KEYS */
401         int                     namedatalen;    /* NAMEDATALEN */
402         int                     float4byval;    /* FLOAT4PASSBYVAL */
403         int                     float8byval;    /* FLOAT8PASSBYVAL */
404 } Pg_magic_struct;
405
406 /* The actual data block contents */
407 #define PG_MODULE_MAGIC_DATA \
408 { \
409         sizeof(Pg_magic_struct), \
410         PG_VERSION_NUM / 100, \
411         FUNC_MAX_ARGS, \
412         INDEX_MAX_KEYS, \
413         NAMEDATALEN, \
414         FLOAT4PASSBYVAL, \
415         FLOAT8PASSBYVAL \
416 }
417
418 /*
419  * Declare the module magic function.  It needs to be a function as the dlsym
420  * in the backend is only guaranteed to work on functions, not data
421  */
422 typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
423
424 #define PG_MAGIC_FUNCTION_NAME Pg_magic_func
425 #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
426
427 #define PG_MODULE_MAGIC \
428 extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
429 const Pg_magic_struct * \
430 PG_MAGIC_FUNCTION_NAME(void) \
431 { \
432         static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
433         return &Pg_magic_data; \
434 } \
435 extern int no_such_variable
436
437
438 /*-------------------------------------------------------------------------
439  *              Support routines and macros for callers of fmgr-compatible functions
440  *-------------------------------------------------------------------------
441  */
442
443 /* These are for invocation of a specifically named function with a
444  * directly-computed parameter list.  Note that neither arguments nor result
445  * are allowed to be NULL.
446  */
447 extern Datum DirectFunctionCall1Coll(PGFunction func, Oid collation,
448                                                 Datum arg1);
449 extern Datum DirectFunctionCall2Coll(PGFunction func, Oid collation,
450                                                 Datum arg1, Datum arg2);
451 extern Datum DirectFunctionCall3Coll(PGFunction func, Oid collation,
452                                                 Datum arg1, Datum arg2,
453                                                 Datum arg3);
454 extern Datum DirectFunctionCall4Coll(PGFunction func, Oid collation,
455                                                 Datum arg1, Datum arg2,
456                                                 Datum arg3, Datum arg4);
457 extern Datum DirectFunctionCall5Coll(PGFunction func, Oid collation,
458                                                 Datum arg1, Datum arg2,
459                                                 Datum arg3, Datum arg4, Datum arg5);
460 extern Datum DirectFunctionCall6Coll(PGFunction func, Oid collation,
461                                                 Datum arg1, Datum arg2,
462                                                 Datum arg3, Datum arg4, Datum arg5,
463                                                 Datum arg6);
464 extern Datum DirectFunctionCall7Coll(PGFunction func, Oid collation,
465                                                 Datum arg1, Datum arg2,
466                                                 Datum arg3, Datum arg4, Datum arg5,
467                                                 Datum arg6, Datum arg7);
468 extern Datum DirectFunctionCall8Coll(PGFunction func, Oid collation,
469                                                 Datum arg1, Datum arg2,
470                                                 Datum arg3, Datum arg4, Datum arg5,
471                                                 Datum arg6, Datum arg7, Datum arg8);
472 extern Datum DirectFunctionCall9Coll(PGFunction func, Oid collation,
473                                                 Datum arg1, Datum arg2,
474                                                 Datum arg3, Datum arg4, Datum arg5,
475                                                 Datum arg6, Datum arg7, Datum arg8,
476                                                 Datum arg9);
477
478 /* These are for invocation of a previously-looked-up function with a
479  * directly-computed parameter list.  Note that neither arguments nor result
480  * are allowed to be NULL.
481  */
482 extern Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation,
483                                   Datum arg1);
484 extern Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation,
485                                   Datum arg1, Datum arg2);
486 extern Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation,
487                                   Datum arg1, Datum arg2,
488                                   Datum arg3);
489 extern Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation,
490                                   Datum arg1, Datum arg2,
491                                   Datum arg3, Datum arg4);
492 extern Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation,
493                                   Datum arg1, Datum arg2,
494                                   Datum arg3, Datum arg4, Datum arg5);
495 extern Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation,
496                                   Datum arg1, Datum arg2,
497                                   Datum arg3, Datum arg4, Datum arg5,
498                                   Datum arg6);
499 extern Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation,
500                                   Datum arg1, Datum arg2,
501                                   Datum arg3, Datum arg4, Datum arg5,
502                                   Datum arg6, Datum arg7);
503 extern Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation,
504                                   Datum arg1, Datum arg2,
505                                   Datum arg3, Datum arg4, Datum arg5,
506                                   Datum arg6, Datum arg7, Datum arg8);
507 extern Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation,
508                                   Datum arg1, Datum arg2,
509                                   Datum arg3, Datum arg4, Datum arg5,
510                                   Datum arg6, Datum arg7, Datum arg8,
511                                   Datum arg9);
512
513 /* These are for invocation of a function identified by OID with a
514  * directly-computed parameter list.  Note that neither arguments nor result
515  * are allowed to be NULL.  These are essentially fmgr_info() followed by
516  * FunctionCallN().  If the same function is to be invoked repeatedly, do the
517  * fmgr_info() once and then use FunctionCallN().
518  */
519 extern Datum OidFunctionCall0Coll(Oid functionId, Oid collation);
520 extern Datum OidFunctionCall1Coll(Oid functionId, Oid collation,
521                                          Datum arg1);
522 extern Datum OidFunctionCall2Coll(Oid functionId, Oid collation,
523                                          Datum arg1, Datum arg2);
524 extern Datum OidFunctionCall3Coll(Oid functionId, Oid collation,
525                                          Datum arg1, Datum arg2,
526                                          Datum arg3);
527 extern Datum OidFunctionCall4Coll(Oid functionId, Oid collation,
528                                          Datum arg1, Datum arg2,
529                                          Datum arg3, Datum arg4);
530 extern Datum OidFunctionCall5Coll(Oid functionId, Oid collation,
531                                          Datum arg1, Datum arg2,
532                                          Datum arg3, Datum arg4, Datum arg5);
533 extern Datum OidFunctionCall6Coll(Oid functionId, Oid collation,
534                                          Datum arg1, Datum arg2,
535                                          Datum arg3, Datum arg4, Datum arg5,
536                                          Datum arg6);
537 extern Datum OidFunctionCall7Coll(Oid functionId, Oid collation,
538                                          Datum arg1, Datum arg2,
539                                          Datum arg3, Datum arg4, Datum arg5,
540                                          Datum arg6, Datum arg7);
541 extern Datum OidFunctionCall8Coll(Oid functionId, Oid collation,
542                                          Datum arg1, Datum arg2,
543                                          Datum arg3, Datum arg4, Datum arg5,
544                                          Datum arg6, Datum arg7, Datum arg8);
545 extern Datum OidFunctionCall9Coll(Oid functionId, Oid collation,
546                                          Datum arg1, Datum arg2,
547                                          Datum arg3, Datum arg4, Datum arg5,
548                                          Datum arg6, Datum arg7, Datum arg8,
549                                          Datum arg9);
550
551 /* These macros allow the collation argument to be omitted (with a default of
552  * InvalidOid, ie, no collation).  They exist mostly for backwards
553  * compatibility of source code.
554  */
555 #define DirectFunctionCall1(func, arg1) \
556         DirectFunctionCall1Coll(func, InvalidOid, arg1)
557 #define DirectFunctionCall2(func, arg1, arg2) \
558         DirectFunctionCall2Coll(func, InvalidOid, arg1, arg2)
559 #define DirectFunctionCall3(func, arg1, arg2, arg3) \
560         DirectFunctionCall3Coll(func, InvalidOid, arg1, arg2, arg3)
561 #define DirectFunctionCall4(func, arg1, arg2, arg3, arg4) \
562         DirectFunctionCall4Coll(func, InvalidOid, arg1, arg2, arg3, arg4)
563 #define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5) \
564         DirectFunctionCall5Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5)
565 #define DirectFunctionCall6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
566         DirectFunctionCall6Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
567 #define DirectFunctionCall7(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
568         DirectFunctionCall7Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
569 #define DirectFunctionCall8(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
570         DirectFunctionCall8Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
571 #define DirectFunctionCall9(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
572         DirectFunctionCall9Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
573 #define FunctionCall1(flinfo, arg1) \
574         FunctionCall1Coll(flinfo, InvalidOid, arg1)
575 #define FunctionCall2(flinfo, arg1, arg2) \
576         FunctionCall2Coll(flinfo, InvalidOid, arg1, arg2)
577 #define FunctionCall3(flinfo, arg1, arg2, arg3) \
578         FunctionCall3Coll(flinfo, InvalidOid, arg1, arg2, arg3)
579 #define FunctionCall4(flinfo, arg1, arg2, arg3, arg4) \
580         FunctionCall4Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4)
581 #define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5) \
582         FunctionCall5Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5)
583 #define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6) \
584         FunctionCall6Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
585 #define FunctionCall7(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
586         FunctionCall7Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
587 #define FunctionCall8(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
588         FunctionCall8Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
589 #define FunctionCall9(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
590         FunctionCall9Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
591 #define OidFunctionCall0(functionId) \
592         OidFunctionCall0Coll(functionId, InvalidOid)
593 #define OidFunctionCall1(functionId, arg1) \
594         OidFunctionCall1Coll(functionId, InvalidOid, arg1)
595 #define OidFunctionCall2(functionId, arg1, arg2) \
596         OidFunctionCall2Coll(functionId, InvalidOid, arg1, arg2)
597 #define OidFunctionCall3(functionId, arg1, arg2, arg3) \
598         OidFunctionCall3Coll(functionId, InvalidOid, arg1, arg2, arg3)
599 #define OidFunctionCall4(functionId, arg1, arg2, arg3, arg4) \
600         OidFunctionCall4Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4)
601 #define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5) \
602         OidFunctionCall5Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5)
603 #define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6) \
604         OidFunctionCall6Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6)
605 #define OidFunctionCall7(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
606         OidFunctionCall7Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
607 #define OidFunctionCall8(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
608         OidFunctionCall8Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
609 #define OidFunctionCall9(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \
610         OidFunctionCall9Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
611
612
613 /* Special cases for convenient invocation of datatype I/O functions. */
614 extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str,
615                                   Oid typioparam, int32 typmod);
616 extern Datum OidInputFunctionCall(Oid functionId, char *str,
617                                          Oid typioparam, int32 typmod);
618 extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
619 extern char *OidOutputFunctionCall(Oid functionId, Datum val);
620 extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf,
621                                         Oid typioparam, int32 typmod);
622 extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf,
623                                            Oid typioparam, int32 typmod);
624 extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val);
625 extern bytea *OidSendFunctionCall(Oid functionId, Datum val);
626
627
628 /*
629  * Routines in fmgr.c
630  */
631 extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
632 extern void clear_external_function_hash(void *filehandle);
633 extern Oid      fmgr_internal_function(const char *proname);
634 extern Oid      get_fn_expr_rettype(FmgrInfo *flinfo);
635 extern Oid      get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
636 extern Oid      get_call_expr_argtype(fmNodePtr expr, int argnum);
637 extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum);
638 extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum);
639 extern bool get_fn_expr_variadic(FmgrInfo *flinfo);
640 extern bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid);
641
642 /*
643  * Routines in dfmgr.c
644  */
645 extern char *Dynamic_library_path;
646
647 extern PGFunction load_external_function(char *filename, char *funcname,
648                                            bool signalNotFound, void **filehandle);
649 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
650 extern void load_file(const char *filename, bool restricted);
651 extern void **find_rendezvous_variable(const char *varName);
652 extern Size EstimateLibraryStateSpace(void);
653 extern void SerializeLibraryState(Size maxsize, char *start_address);
654 extern void RestoreLibraryState(char *start_address);
655
656 /*
657  * Support for aggregate functions
658  *
659  * These are actually in executor/nodeAgg.c, but we declare them here since
660  * the whole point is for callers to not be overly friendly with nodeAgg.
661  */
662
663 /* AggCheckCallContext can return one of the following codes, or 0: */
664 #define AGG_CONTEXT_AGGREGATE   1               /* regular aggregate */
665 #define AGG_CONTEXT_WINDOW              2               /* window function */
666
667 extern int AggCheckCallContext(FunctionCallInfo fcinfo,
668                                         MemoryContext *aggcontext);
669 extern fmAggrefPtr AggGetAggref(FunctionCallInfo fcinfo);
670 extern MemoryContext AggGetTempMemoryContext(FunctionCallInfo fcinfo);
671 extern void AggRegisterCallback(FunctionCallInfo fcinfo,
672                                         fmExprContextCallbackFunction func,
673                                         Datum arg);
674
675 /*
676  * We allow plugin modules to hook function entry/exit.  This is intended
677  * as support for loadable security policy modules, which may want to
678  * perform additional privilege checks on function entry or exit, or to do
679  * other internal bookkeeping.  To make this possible, such modules must be
680  * able not only to support normal function entry and exit, but also to trap
681  * the case where we bail out due to an error; and they must also be able to
682  * prevent inlining.
683  */
684 typedef enum FmgrHookEventType
685 {
686         FHET_START,
687         FHET_END,
688         FHET_ABORT
689 } FmgrHookEventType;
690
691 typedef bool (*needs_fmgr_hook_type) (Oid fn_oid);
692
693 typedef void (*fmgr_hook_type) (FmgrHookEventType event,
694                                                                                         FmgrInfo *flinfo, Datum *arg);
695
696 extern PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook;
697 extern PGDLLIMPORT fmgr_hook_type fmgr_hook;
698
699 #define FmgrHookIsNeeded(fn_oid)                                                        \
700         (!needs_fmgr_hook ? false : (*needs_fmgr_hook)(fn_oid))
701
702 /*
703  * !!! OLD INTERFACE !!!
704  *
705  * fmgr() is the only remaining vestige of the old-style caller support
706  * functions.  It's no longer used anywhere in the Postgres distribution,
707  * but we should leave it around for a release or two to ease the transition
708  * for user-supplied C functions.  OidFunctionCallN() replaces it for new
709  * code.
710  */
711
712 /*
713  * DEPRECATED, DO NOT USE IN NEW CODE
714  */
715 extern char *fmgr(Oid procedureId,...);
716
717 #endif   /* FMGR_H */