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