1 /*-------------------------------------------------------------------------
4 * Definitions for the Postgres function manager and function-call
7 * This file must be included by all Postgres modules that either define
8 * or call fmgr-callable functions.
11 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/include/fmgr.h,v 1.49 2007/01/05 22:19:50 momjian Exp $
16 *-------------------------------------------------------------------------
21 /* We don't want to include primnodes.h here, so make a stub reference */
22 typedef struct Node *fmNodePtr;
24 /* Likewise, avoid including stringinfo.h here */
25 typedef struct StringInfoData *fmStringInfo;
29 * All functions that can be called directly by fmgr must have this signature.
30 * (Other functions can be called by using a handler that does have this
34 typedef struct FunctionCallInfoData *FunctionCallInfo;
36 typedef Datum (*PGFunction) (FunctionCallInfo fcinfo);
39 * This struct holds the system-catalog information that must be looked up
40 * before a function can be called through fmgr. If the same function is
41 * to be called multiple times, the lookup need be done only once and the
42 * info struct saved for re-use.
44 typedef struct FmgrInfo
46 PGFunction fn_addr; /* pointer to function or handler to be called */
47 Oid fn_oid; /* OID of function (NOT of handler, if any) */
48 short fn_nargs; /* 0..FUNC_MAX_ARGS, or -1 if variable arg
50 bool fn_strict; /* function is "strict" (NULL in => NULL out) */
51 bool fn_retset; /* function returns a set */
52 void *fn_extra; /* extra space for use by handler */
53 MemoryContext fn_mcxt; /* memory context to store fn_extra in */
54 fmNodePtr fn_expr; /* expression parse tree for call, or NULL */
58 * This struct is the data actually passed to an fmgr-called function.
60 typedef struct FunctionCallInfoData
62 FmgrInfo *flinfo; /* ptr to lookup info used for this call */
63 fmNodePtr context; /* pass info about context of call */
64 fmNodePtr resultinfo; /* pass or return extra info about result */
65 bool isnull; /* function must set true if result is NULL */
66 short nargs; /* # arguments actually passed */
67 Datum arg[FUNC_MAX_ARGS]; /* Arguments passed to function */
68 bool argnull[FUNC_MAX_ARGS]; /* T if arg[i] is actually NULL */
69 } FunctionCallInfoData;
72 * This routine fills a FmgrInfo struct, given the OID
73 * of the function to be called.
75 extern void fmgr_info(Oid functionId, FmgrInfo *finfo);
78 * Same, when the FmgrInfo struct is in a memory context longer-lived than
79 * CurrentMemoryContext. The specified context will be set as fn_mcxt
80 * and used to hold all subsidiary data of finfo.
82 extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo,
86 * Copy an FmgrInfo struct
88 extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
89 MemoryContext destcxt);
92 * This macro initializes all the fields of a FunctionCallInfoData except
93 * for the arg[] and argnull[] arrays. Performance testing has shown that
94 * the fastest way to set up argnull[] for small numbers of arguments is to
95 * explicitly set each required element to false, so we don't try to zero
96 * out the argnull[] array in the macro.
98 #define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Context, Resultinfo) \
100 (Fcinfo).flinfo = (Flinfo); \
101 (Fcinfo).context = (Context); \
102 (Fcinfo).resultinfo = (Resultinfo); \
103 (Fcinfo).isnull = false; \
104 (Fcinfo).nargs = (Nargs); \
108 * This macro invokes a function given a filled-in FunctionCallInfoData
109 * struct. The macro result is the returned Datum --- but note that
110 * caller must still check fcinfo->isnull! Also, if function is strict,
111 * it is caller's responsibility to verify that no null arguments are present
114 #define FunctionCallInvoke(fcinfo) ((* (fcinfo)->flinfo->fn_addr) (fcinfo))
117 /*-------------------------------------------------------------------------
118 * Support macros to ease writing fmgr-compatible functions
120 * A C-coded fmgr-compatible function should be declared as
123 * function_name(PG_FUNCTION_ARGS)
128 * It should access its arguments using appropriate PG_GETARG_xxx macros
129 * and should return its result using PG_RETURN_xxx.
131 *-------------------------------------------------------------------------
134 /* Standard parameter list for fmgr-compatible functions */
135 #define PG_FUNCTION_ARGS FunctionCallInfo fcinfo
138 * Get number of arguments passed to function.
140 #define PG_NARGS() (fcinfo->nargs)
143 * If function is not marked "proisstrict" in pg_proc, it must check for
144 * null arguments using this macro. Do not try to GETARG a null argument!
146 #define PG_ARGISNULL(n) (fcinfo->argnull[n])
149 * Support for fetching detoasted copies of toastable datatypes (all of
150 * which are varlena types). pg_detoast_datum() gives you either the input
151 * datum (if not toasted) or a detoasted copy allocated with palloc().
152 * pg_detoast_datum_copy() always gives you a palloc'd copy --- use it
153 * if you need a modifiable copy of the input. Caller is expected to have
154 * checked for null inputs first, if necessary.
156 * Note: it'd be nice if these could be macros, but I see no way to do that
157 * without evaluating the arguments multiple times, which is NOT acceptable.
159 extern struct varlena *pg_detoast_datum(struct varlena * datum);
160 extern struct varlena *pg_detoast_datum_copy(struct varlena * datum);
161 extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
162 int32 first, int32 count);
164 #define PG_DETOAST_DATUM(datum) \
165 pg_detoast_datum((struct varlena *) DatumGetPointer(datum))
166 #define PG_DETOAST_DATUM_COPY(datum) \
167 pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum))
168 #define PG_DETOAST_DATUM_SLICE(datum,f,c) \
169 pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \
170 (int32) f, (int32) c)
173 * Support for cleaning up detoasted copies of inputs. This must only
174 * be used for pass-by-ref datatypes, and normally would only be used
175 * for toastable types. If the given pointer is different from the
176 * original argument, assume it's a palloc'd detoasted copy, and pfree it.
177 * NOTE: most functions on toastable types do not have to worry about this,
178 * but we currently require that support functions for indexes not leak
181 #define PG_FREE_IF_COPY(ptr,n) \
183 if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \
187 /* Macros for fetching arguments of standard types */
189 #define PG_GETARG_DATUM(n) (fcinfo->arg[n])
190 #define PG_GETARG_INT32(n) DatumGetInt32(PG_GETARG_DATUM(n))
191 #define PG_GETARG_UINT32(n) DatumGetUInt32(PG_GETARG_DATUM(n))
192 #define PG_GETARG_INT16(n) DatumGetInt16(PG_GETARG_DATUM(n))
193 #define PG_GETARG_UINT16(n) DatumGetUInt16(PG_GETARG_DATUM(n))
194 #define PG_GETARG_CHAR(n) DatumGetChar(PG_GETARG_DATUM(n))
195 #define PG_GETARG_BOOL(n) DatumGetBool(PG_GETARG_DATUM(n))
196 #define PG_GETARG_OID(n) DatumGetObjectId(PG_GETARG_DATUM(n))
197 #define PG_GETARG_POINTER(n) DatumGetPointer(PG_GETARG_DATUM(n))
198 #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n))
199 #define PG_GETARG_NAME(n) DatumGetName(PG_GETARG_DATUM(n))
200 /* these macros hide the pass-by-reference-ness of the datatype: */
201 #define PG_GETARG_FLOAT4(n) DatumGetFloat4(PG_GETARG_DATUM(n))
202 #define PG_GETARG_FLOAT8(n) DatumGetFloat8(PG_GETARG_DATUM(n))
203 #define PG_GETARG_INT64(n) DatumGetInt64(PG_GETARG_DATUM(n))
204 /* use this if you want the raw, possibly-toasted input datum: */
205 #define PG_GETARG_RAW_VARLENA_P(n) ((struct varlena *) PG_GETARG_POINTER(n))
206 /* use this if you want the input datum de-toasted: */
207 #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
208 /* DatumGetFoo macros for varlena types will typically look like this: */
209 #define DatumGetByteaP(X) ((bytea *) PG_DETOAST_DATUM(X))
210 #define DatumGetTextP(X) ((text *) PG_DETOAST_DATUM(X))
211 #define DatumGetBpCharP(X) ((BpChar *) PG_DETOAST_DATUM(X))
212 #define DatumGetVarCharP(X) ((VarChar *) PG_DETOAST_DATUM(X))
213 #define DatumGetHeapTupleHeader(X) ((HeapTupleHeader) PG_DETOAST_DATUM(X))
214 /* And we also offer variants that return an OK-to-write copy */
215 #define DatumGetByteaPCopy(X) ((bytea *) PG_DETOAST_DATUM_COPY(X))
216 #define DatumGetTextPCopy(X) ((text *) PG_DETOAST_DATUM_COPY(X))
217 #define DatumGetBpCharPCopy(X) ((BpChar *) PG_DETOAST_DATUM_COPY(X))
218 #define DatumGetVarCharPCopy(X) ((VarChar *) PG_DETOAST_DATUM_COPY(X))
219 #define DatumGetHeapTupleHeaderCopy(X) ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X))
220 /* Variants which return n bytes starting at pos. m */
221 #define DatumGetByteaPSlice(X,m,n) ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
222 #define DatumGetTextPSlice(X,m,n) ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
223 #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
224 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
225 /* GETARG macros for varlena types will typically look like this: */
226 #define PG_GETARG_BYTEA_P(n) DatumGetByteaP(PG_GETARG_DATUM(n))
227 #define PG_GETARG_TEXT_P(n) DatumGetTextP(PG_GETARG_DATUM(n))
228 #define PG_GETARG_BPCHAR_P(n) DatumGetBpCharP(PG_GETARG_DATUM(n))
229 #define PG_GETARG_VARCHAR_P(n) DatumGetVarCharP(PG_GETARG_DATUM(n))
230 #define PG_GETARG_HEAPTUPLEHEADER(n) DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
231 /* And we also offer variants that return an OK-to-write copy */
232 #define PG_GETARG_BYTEA_P_COPY(n) DatumGetByteaPCopy(PG_GETARG_DATUM(n))
233 #define PG_GETARG_TEXT_P_COPY(n) DatumGetTextPCopy(PG_GETARG_DATUM(n))
234 #define PG_GETARG_BPCHAR_P_COPY(n) DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
235 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
236 #define PG_GETARG_HEAPTUPLEHEADER_COPY(n) DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n))
237 /* And a b-byte slice from position a -also OK to write */
238 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
239 #define PG_GETARG_TEXT_P_SLICE(n,a,b) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
240 #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
241 #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
243 /* To return a NULL do this: */
244 #define PG_RETURN_NULL() \
245 do { fcinfo->isnull = true; return (Datum) 0; } while (0)
247 /* A few internal functions return void (which is not the same as NULL!) */
248 #define PG_RETURN_VOID() return (Datum) 0
250 /* Macros for returning results of standard types */
252 #define PG_RETURN_DATUM(x) return (x)
253 #define PG_RETURN_INT32(x) return Int32GetDatum(x)
254 #define PG_RETURN_UINT32(x) return UInt32GetDatum(x)
255 #define PG_RETURN_INT16(x) return Int16GetDatum(x)
256 #define PG_RETURN_CHAR(x) return CharGetDatum(x)
257 #define PG_RETURN_BOOL(x) return BoolGetDatum(x)
258 #define PG_RETURN_OID(x) return ObjectIdGetDatum(x)
259 #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
260 #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
261 #define PG_RETURN_NAME(x) return NameGetDatum(x)
262 /* these macros hide the pass-by-reference-ness of the datatype: */
263 #define PG_RETURN_FLOAT4(x) return Float4GetDatum(x)
264 #define PG_RETURN_FLOAT8(x) return Float8GetDatum(x)
265 #define PG_RETURN_INT64(x) return Int64GetDatum(x)
266 /* RETURN macros for other pass-by-ref types will typically look like this: */
267 #define PG_RETURN_BYTEA_P(x) PG_RETURN_POINTER(x)
268 #define PG_RETURN_TEXT_P(x) PG_RETURN_POINTER(x)
269 #define PG_RETURN_BPCHAR_P(x) PG_RETURN_POINTER(x)
270 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
271 #define PG_RETURN_HEAPTUPLEHEADER(x) PG_RETURN_POINTER(x)
274 /*-------------------------------------------------------------------------
275 * Support for detecting call convention of dynamically-loaded functions
277 * Dynamically loaded functions may use either the version-1 ("new style")
278 * or version-0 ("old style") calling convention. Version 1 is the call
279 * convention defined in this header file; version 0 is the old "plain C"
280 * convention. A version-1 function must be accompanied by the macro call
282 * PG_FUNCTION_INFO_V1(function_name);
284 * Note that internal functions do not need this decoration since they are
285 * assumed to be version-1.
287 *-------------------------------------------------------------------------
292 int api_version; /* specifies call convention version number */
293 /* More fields may be added later, for version numbers > 1. */
296 /* Expected signature of an info function */
297 typedef const Pg_finfo_record *(*PGFInfoFunction) (void);
300 * Macro to build an info function associated with the given function name.
301 * Win32 loadable functions usually link with 'dlltool --export-all', but it
302 * doesn't hurt to add DLLIMPORT in case they don't.
304 #define PG_FUNCTION_INFO_V1(funcname) \
305 extern DLLIMPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
306 const Pg_finfo_record * \
307 CppConcat(pg_finfo_,funcname) (void) \
309 static const Pg_finfo_record my_finfo = { 1 }; \
312 extern int no_such_variable
315 /*-------------------------------------------------------------------------
316 * Support for verifying backend compatibility of loaded modules
318 * We require dynamically-loaded modules to include the macro call
320 * so that we can check for obvious incompatibility, such as being compiled
321 * for a different major PostgreSQL version.
323 * To compile with versions of PostgreSQL that do not support this,
324 * you may put an #ifdef/#endif test around it. Note that in a multiple-
325 * source-file module, the macro call should only appear once.
327 * The specific items included in the magic block are intended to be ones that
328 * are custom-configurable and especially likely to break dynamically loaded
329 * modules if they were compiled with other values. Also, the length field
330 * can be used to detect definition changes.
331 *-------------------------------------------------------------------------
334 /* Definition of the magic block structure */
337 int len; /* sizeof(this struct) */
338 int version; /* PostgreSQL major version */
339 int funcmaxargs; /* FUNC_MAX_ARGS */
340 int indexmaxkeys; /* INDEX_MAX_KEYS */
341 int namedatalen; /* NAMEDATALEN */
344 /* The actual data block contents */
345 #define PG_MODULE_MAGIC_DATA \
347 sizeof(Pg_magic_struct), \
348 PG_VERSION_NUM / 100, \
355 * Declare the module magic function. It needs to be a function as the dlsym
356 * in the backend is only guaranteed to work on functions, not data
358 typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
360 #define PG_MAGIC_FUNCTION_NAME Pg_magic_func
361 #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
363 #define PG_MODULE_MAGIC \
364 extern DLLIMPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
365 const Pg_magic_struct * \
366 PG_MAGIC_FUNCTION_NAME(void) \
368 static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
369 return &Pg_magic_data; \
371 extern int no_such_variable
374 /*-------------------------------------------------------------------------
375 * Support routines and macros for callers of fmgr-compatible functions
376 *-------------------------------------------------------------------------
379 /* These are for invocation of a specifically named function with a
380 * directly-computed parameter list. Note that neither arguments nor result
381 * are allowed to be NULL.
383 extern Datum DirectFunctionCall1(PGFunction func, Datum arg1);
384 extern Datum DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2);
385 extern Datum DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
387 extern Datum DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
388 Datum arg3, Datum arg4);
389 extern Datum DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
390 Datum arg3, Datum arg4, Datum arg5);
391 extern Datum DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
392 Datum arg3, Datum arg4, Datum arg5,
394 extern Datum DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
395 Datum arg3, Datum arg4, Datum arg5,
396 Datum arg6, Datum arg7);
397 extern Datum DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
398 Datum arg3, Datum arg4, Datum arg5,
399 Datum arg6, Datum arg7, Datum arg8);
400 extern Datum DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
401 Datum arg3, Datum arg4, Datum arg5,
402 Datum arg6, Datum arg7, Datum arg8,
405 /* These are for invocation of a previously-looked-up function with a
406 * directly-computed parameter list. Note that neither arguments nor result
407 * are allowed to be NULL.
409 extern Datum FunctionCall1(FmgrInfo *flinfo, Datum arg1);
410 extern Datum FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2);
411 extern Datum FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
413 extern Datum FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
414 Datum arg3, Datum arg4);
415 extern Datum FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
416 Datum arg3, Datum arg4, Datum arg5);
417 extern Datum FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
418 Datum arg3, Datum arg4, Datum arg5,
420 extern Datum FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
421 Datum arg3, Datum arg4, Datum arg5,
422 Datum arg6, Datum arg7);
423 extern Datum FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
424 Datum arg3, Datum arg4, Datum arg5,
425 Datum arg6, Datum arg7, Datum arg8);
426 extern Datum FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
427 Datum arg3, Datum arg4, Datum arg5,
428 Datum arg6, Datum arg7, Datum arg8,
431 /* These are for invocation of a function identified by OID with a
432 * directly-computed parameter list. Note that neither arguments nor result
433 * are allowed to be NULL. These are essentially FunctionLookup() followed
434 * by FunctionCallN(). If the same function is to be invoked repeatedly,
435 * do the FunctionLookup() once and then use FunctionCallN().
437 extern Datum OidFunctionCall1(Oid functionId, Datum arg1);
438 extern Datum OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2);
439 extern Datum OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
441 extern Datum OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
442 Datum arg3, Datum arg4);
443 extern Datum OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
444 Datum arg3, Datum arg4, Datum arg5);
445 extern Datum OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
446 Datum arg3, Datum arg4, Datum arg5,
448 extern Datum OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
449 Datum arg3, Datum arg4, Datum arg5,
450 Datum arg6, Datum arg7);
451 extern Datum OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
452 Datum arg3, Datum arg4, Datum arg5,
453 Datum arg6, Datum arg7, Datum arg8);
454 extern Datum OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
455 Datum arg3, Datum arg4, Datum arg5,
456 Datum arg6, Datum arg7, Datum arg8,
459 /* Special cases for convenient invocation of datatype I/O functions. */
460 extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str,
461 Oid typioparam, int32 typmod);
462 extern Datum OidInputFunctionCall(Oid functionId, char *str,
463 Oid typioparam, int32 typmod);
464 extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
465 extern char *OidOutputFunctionCall(Oid functionId, Datum val);
466 extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf,
467 Oid typioparam, int32 typmod);
468 extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf,
469 Oid typioparam, int32 typmod);
470 extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val);
471 extern bytea *OidSendFunctionCall(Oid functionId, Datum val);
477 extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
478 extern void clear_external_function_hash(void *filehandle);
479 extern Oid fmgr_internal_function(const char *proname);
480 extern Oid get_fn_expr_rettype(FmgrInfo *flinfo);
481 extern Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
482 extern Oid get_call_expr_argtype(fmNodePtr expr, int argnum);
485 * Routines in dfmgr.c
487 extern char *Dynamic_library_path;
489 extern PGFunction load_external_function(char *filename, char *funcname,
490 bool signalNotFound, void **filehandle);
491 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
492 extern void load_file(const char *filename, bool restricted);
493 extern void **find_rendezvous_variable(const char *varName);
497 * !!! OLD INTERFACE !!!
499 * fmgr() is the only remaining vestige of the old-style caller support
500 * functions. It's no longer used anywhere in the Postgres distribution,
501 * but we should leave it around for a release or two to ease the transition
502 * for user-supplied C functions. OidFunctionCallN() replaces it for new
507 * DEPRECATED, DO NOT USE IN NEW CODE
509 extern char *fmgr(Oid procedureId,...);