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