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-2002, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 * $Id: fmgr.h,v 1.27 2003/04/08 23:20:04 tgl Exp $
16 *-------------------------------------------------------------------------
23 * All functions that can be called directly by fmgr must have this signature.
24 * (Other functions can be called by using a handler that does have this
28 typedef struct FunctionCallInfoData *FunctionCallInfo;
30 typedef Datum (*PGFunction) (FunctionCallInfo fcinfo);
33 * This struct holds the system-catalog information that must be looked up
34 * before a function can be called through fmgr. If the same function is
35 * to be called multiple times, the lookup need be done only once and the
36 * info struct saved for re-use.
38 typedef struct FmgrInfo
40 PGFunction fn_addr; /* pointer to function or handler to be
42 Oid fn_oid; /* OID of function (NOT of handler, if
44 short fn_nargs; /* 0..FUNC_MAX_ARGS, or -1 if variable arg
46 bool fn_strict; /* function is "strict" (NULL in => NULL
48 bool fn_retset; /* function returns a set */
49 void *fn_extra; /* extra space for use by handler */
50 MemoryContext fn_mcxt; /* memory context to store fn_extra in */
51 struct Node *fn_expr; /* expression parse tree for call, or NULL */
55 * This struct is the data actually passed to an fmgr-called function.
57 typedef struct FunctionCallInfoData
59 FmgrInfo *flinfo; /* ptr to lookup info used for this call */
60 struct Node *context; /* pass info about context of call */
61 struct Node *resultinfo; /* pass or return extra info about result */
62 bool isnull; /* function must set true if result is
64 short nargs; /* # arguments actually passed */
65 Datum arg[FUNC_MAX_ARGS]; /* Arguments passed to function */
66 bool argnull[FUNC_MAX_ARGS]; /* T if arg[i] is actually NULL */
67 } FunctionCallInfoData;
70 * This routine fills a FmgrInfo struct, given the OID
71 * of the function to be called.
73 extern void fmgr_info(Oid functionId, FmgrInfo *finfo);
76 * Same, when the FmgrInfo struct is in a memory context longer-lived than
77 * CurrentMemoryContext. The specified context will be set as fn_mcxt
78 * and used to hold all subsidiary data of finfo.
80 extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo,
84 * Copy an FmgrInfo struct
86 extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
87 MemoryContext destcxt);
90 * This macro invokes a function given a filled-in FunctionCallInfoData
91 * struct. The macro result is the returned Datum --- but note that
92 * caller must still check fcinfo->isnull! Also, if function is strict,
93 * it is caller's responsibility to verify that no null arguments are present
96 #define FunctionCallInvoke(fcinfo) ((* (fcinfo)->flinfo->fn_addr) (fcinfo))
99 /*-------------------------------------------------------------------------
100 * Support macros to ease writing fmgr-compatible functions
102 * A C-coded fmgr-compatible function should be declared as
105 * function_name(PG_FUNCTION_ARGS)
110 * It should access its arguments using appropriate PG_GETARG_xxx macros
111 * and should return its result using PG_RETURN_xxx.
113 *-------------------------------------------------------------------------
116 /* Standard parameter list for fmgr-compatible functions */
117 #define PG_FUNCTION_ARGS FunctionCallInfo fcinfo
120 * Get number of arguments passed to function.
122 #define PG_NARGS() (fcinfo->nargs)
125 * If function is not marked "proisstrict" in pg_proc, it must check for
126 * null arguments using this macro. Do not try to GETARG a null argument!
128 #define PG_ARGISNULL(n) (fcinfo->argnull[n])
131 * Support for fetching detoasted copies of toastable datatypes (all of
132 * which are varlena types). pg_detoast_datum() gives you either the input
133 * datum (if not toasted) or a detoasted copy allocated with palloc().
134 * pg_detoast_datum_copy() always gives you a palloc'd copy --- use it
135 * if you need a modifiable copy of the input. Caller is expected to have
136 * checked for null inputs first, if necessary.
138 * Note: it'd be nice if these could be macros, but I see no way to do that
139 * without evaluating the arguments multiple times, which is NOT acceptable.
141 extern struct varlena *pg_detoast_datum(struct varlena * datum);
142 extern struct varlena *pg_detoast_datum_copy(struct varlena * datum);
143 extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
144 int32 first, int32 count);
146 #define PG_DETOAST_DATUM(datum) \
147 pg_detoast_datum((struct varlena *) DatumGetPointer(datum))
148 #define PG_DETOAST_DATUM_COPY(datum) \
149 pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum))
150 #define PG_DETOAST_DATUM_SLICE(datum,f,c) \
151 pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \
152 (int32) f, (int32) c)
155 * Support for cleaning up detoasted copies of inputs. This must only
156 * be used for pass-by-ref datatypes, and normally would only be used
157 * for toastable types. If the given pointer is different from the
158 * original argument, assume it's a palloc'd detoasted copy, and pfree it.
159 * NOTE: most functions on toastable types do not have to worry about this,
160 * but we currently require that support functions for indexes not leak
163 #define PG_FREE_IF_COPY(ptr,n) \
165 if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \
169 /* Macros for fetching arguments of standard types */
171 #define PG_GETARG_DATUM(n) (fcinfo->arg[n])
172 #define PG_GETARG_INT32(n) DatumGetInt32(PG_GETARG_DATUM(n))
173 #define PG_GETARG_UINT32(n) DatumGetUInt32(PG_GETARG_DATUM(n))
174 #define PG_GETARG_INT16(n) DatumGetInt16(PG_GETARG_DATUM(n))
175 #define PG_GETARG_UINT16(n) DatumGetUInt16(PG_GETARG_DATUM(n))
176 #define PG_GETARG_CHAR(n) DatumGetChar(PG_GETARG_DATUM(n))
177 #define PG_GETARG_BOOL(n) DatumGetBool(PG_GETARG_DATUM(n))
178 #define PG_GETARG_OID(n) DatumGetObjectId(PG_GETARG_DATUM(n))
179 #define PG_GETARG_POINTER(n) DatumGetPointer(PG_GETARG_DATUM(n))
180 #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n))
181 #define PG_GETARG_NAME(n) DatumGetName(PG_GETARG_DATUM(n))
182 /* these macros hide the pass-by-reference-ness of the datatype: */
183 #define PG_GETARG_FLOAT4(n) DatumGetFloat4(PG_GETARG_DATUM(n))
184 #define PG_GETARG_FLOAT8(n) DatumGetFloat8(PG_GETARG_DATUM(n))
185 #define PG_GETARG_INT64(n) DatumGetInt64(PG_GETARG_DATUM(n))
186 /* use this if you want the raw, possibly-toasted input datum: */
187 #define PG_GETARG_RAW_VARLENA_P(n) ((struct varlena *) PG_GETARG_POINTER(n))
188 /* use this if you want the input datum de-toasted: */
189 #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
190 /* DatumGetFoo macros for varlena types will typically look like this: */
191 #define DatumGetByteaP(X) ((bytea *) PG_DETOAST_DATUM(X))
192 #define DatumGetTextP(X) ((text *) PG_DETOAST_DATUM(X))
193 #define DatumGetBpCharP(X) ((BpChar *) PG_DETOAST_DATUM(X))
194 #define DatumGetVarCharP(X) ((VarChar *) PG_DETOAST_DATUM(X))
195 /* And we also offer variants that return an OK-to-write copy */
196 #define DatumGetByteaPCopy(X) ((bytea *) PG_DETOAST_DATUM_COPY(X))
197 #define DatumGetTextPCopy(X) ((text *) PG_DETOAST_DATUM_COPY(X))
198 #define DatumGetBpCharPCopy(X) ((BpChar *) PG_DETOAST_DATUM_COPY(X))
199 #define DatumGetVarCharPCopy(X) ((VarChar *) PG_DETOAST_DATUM_COPY(X))
200 /* Variants which return n bytes starting at pos. m */
201 #define DatumGetByteaPSlice(X,m,n) ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
202 #define DatumGetTextPSlice(X,m,n) ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
203 #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
204 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
205 /* GETARG macros for varlena types will typically look like this: */
206 #define PG_GETARG_BYTEA_P(n) DatumGetByteaP(PG_GETARG_DATUM(n))
207 #define PG_GETARG_TEXT_P(n) DatumGetTextP(PG_GETARG_DATUM(n))
208 #define PG_GETARG_BPCHAR_P(n) DatumGetBpCharP(PG_GETARG_DATUM(n))
209 #define PG_GETARG_VARCHAR_P(n) DatumGetVarCharP(PG_GETARG_DATUM(n))
210 /* And we also offer variants that return an OK-to-write copy */
211 #define PG_GETARG_BYTEA_P_COPY(n) DatumGetByteaPCopy(PG_GETARG_DATUM(n))
212 #define PG_GETARG_TEXT_P_COPY(n) DatumGetTextPCopy(PG_GETARG_DATUM(n))
213 #define PG_GETARG_BPCHAR_P_COPY(n) DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
214 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
215 /* And a b-byte slice from position a -also OK to write */
216 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
217 #define PG_GETARG_TEXT_P_SLICE(n,a,b) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
218 #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
219 #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
221 /* To return a NULL do this: */
222 #define PG_RETURN_NULL() \
223 do { fcinfo->isnull = true; return (Datum) 0; } while (0)
225 /* A few internal functions return void (which is not the same as NULL!) */
226 #define PG_RETURN_VOID() return (Datum) 0
228 /* Macros for returning results of standard types */
230 #define PG_RETURN_DATUM(x) return (x)
231 #define PG_RETURN_INT32(x) return Int32GetDatum(x)
232 #define PG_RETURN_UINT32(x) return UInt32GetDatum(x)
233 #define PG_RETURN_INT16(x) return Int16GetDatum(x)
234 #define PG_RETURN_CHAR(x) return CharGetDatum(x)
235 #define PG_RETURN_BOOL(x) return BoolGetDatum(x)
236 #define PG_RETURN_OID(x) return ObjectIdGetDatum(x)
237 #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
238 #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
239 #define PG_RETURN_NAME(x) return NameGetDatum(x)
240 /* these macros hide the pass-by-reference-ness of the datatype: */
241 #define PG_RETURN_FLOAT4(x) return Float4GetDatum(x)
242 #define PG_RETURN_FLOAT8(x) return Float8GetDatum(x)
243 #define PG_RETURN_INT64(x) return Int64GetDatum(x)
244 /* RETURN macros for other pass-by-ref types will typically look like this: */
245 #define PG_RETURN_BYTEA_P(x) PG_RETURN_POINTER(x)
246 #define PG_RETURN_TEXT_P(x) PG_RETURN_POINTER(x)
247 #define PG_RETURN_BPCHAR_P(x) PG_RETURN_POINTER(x)
248 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
251 /*-------------------------------------------------------------------------
252 * Support for detecting call convention of dynamically-loaded functions
254 * Dynamically loaded functions may use either the version-1 ("new style")
255 * or version-0 ("old style") calling convention. Version 1 is the call
256 * convention defined in this header file; version 0 is the old "plain C"
257 * convention. A version-1 function must be accompanied by the macro call
259 * PG_FUNCTION_INFO_V1(function_name);
261 * Note that internal functions do not need this decoration since they are
262 * assumed to be version-1.
264 *-------------------------------------------------------------------------
269 int api_version; /* specifies call convention version
271 /* More fields may be added later, for version numbers > 1. */
274 /* Expected signature of an info function */
275 typedef Pg_finfo_record *(*PGFInfoFunction) (void);
277 /* Macro to build an info function associated with the given function name */
279 #define PG_FUNCTION_INFO_V1(funcname) \
280 extern Pg_finfo_record * CppConcat(pg_finfo_,funcname) (void); \
282 CppConcat(pg_finfo_,funcname) (void) \
284 static Pg_finfo_record my_finfo = { 1 }; \
287 extern int no_such_variable
290 /*-------------------------------------------------------------------------
291 * Support routines and macros for callers of fmgr-compatible functions
292 *-------------------------------------------------------------------------
295 /* These are for invocation of a specifically named function with a
296 * directly-computed parameter list. Note that neither arguments nor result
297 * are allowed to be NULL.
299 extern Datum DirectFunctionCall1(PGFunction func, Datum arg1);
300 extern Datum DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2);
301 extern Datum DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
303 extern Datum DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
304 Datum arg3, Datum arg4);
305 extern Datum DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
306 Datum arg3, Datum arg4, Datum arg5);
307 extern Datum DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
308 Datum arg3, Datum arg4, Datum arg5,
310 extern Datum DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
311 Datum arg3, Datum arg4, Datum arg5,
312 Datum arg6, Datum arg7);
313 extern Datum DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
314 Datum arg3, Datum arg4, Datum arg5,
315 Datum arg6, Datum arg7, Datum arg8);
316 extern Datum DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
317 Datum arg3, Datum arg4, Datum arg5,
318 Datum arg6, Datum arg7, Datum arg8,
321 /* These are for invocation of a previously-looked-up function with a
322 * directly-computed parameter list. Note that neither arguments nor result
323 * are allowed to be NULL.
325 extern Datum FunctionCall1(FmgrInfo *flinfo, Datum arg1);
326 extern Datum FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2);
327 extern Datum FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
329 extern Datum FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
330 Datum arg3, Datum arg4);
331 extern Datum FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
332 Datum arg3, Datum arg4, Datum arg5);
333 extern Datum FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
334 Datum arg3, Datum arg4, Datum arg5,
336 extern Datum FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
337 Datum arg3, Datum arg4, Datum arg5,
338 Datum arg6, Datum arg7);
339 extern Datum FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
340 Datum arg3, Datum arg4, Datum arg5,
341 Datum arg6, Datum arg7, Datum arg8);
342 extern Datum FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
343 Datum arg3, Datum arg4, Datum arg5,
344 Datum arg6, Datum arg7, Datum arg8,
347 /* These are for invocation of a function identified by OID with a
348 * directly-computed parameter list. Note that neither arguments nor result
349 * are allowed to be NULL. These are essentially FunctionLookup() followed
350 * by FunctionCallN(). If the same function is to be invoked repeatedly,
351 * do the FunctionLookup() once and then use FunctionCallN().
353 extern Datum OidFunctionCall1(Oid functionId, Datum arg1);
354 extern Datum OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2);
355 extern Datum OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
357 extern Datum OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
358 Datum arg3, Datum arg4);
359 extern Datum OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
360 Datum arg3, Datum arg4, Datum arg5);
361 extern Datum OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
362 Datum arg3, Datum arg4, Datum arg5,
364 extern Datum OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
365 Datum arg3, Datum arg4, Datum arg5,
366 Datum arg6, Datum arg7);
367 extern Datum OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
368 Datum arg3, Datum arg4, Datum arg5,
369 Datum arg6, Datum arg7, Datum arg8);
370 extern Datum OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
371 Datum arg3, Datum arg4, Datum arg5,
372 Datum arg6, Datum arg7, Datum arg8,
379 extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
380 extern Oid fmgr_internal_function(const char *proname);
381 extern Oid get_fn_expr_rettype(FunctionCallInfo fcinfo);
382 extern Oid get_fn_expr_argtype(FunctionCallInfo fcinfo, int argnum);
385 * Routines in dfmgr.c
387 extern char *Dynamic_library_path;
389 extern PGFunction load_external_function(char *filename, char *funcname,
390 bool signalNotFound, void **filehandle);
391 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
392 extern void load_file(char *filename);
396 * !!! OLD INTERFACE !!!
398 * fmgr() is the only remaining vestige of the old-style caller support
399 * functions. It's no longer used anywhere in the Postgres distribution,
400 * but we should leave it around for a release or two to ease the transition
401 * for user-supplied C functions. OidFunctionCallN() replaces it for new
406 * DEPRECATED, DO NOT USE IN NEW CODE
408 extern char *fmgr(Oid procedureId,...);