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