]> granicus.if.org Git - postgresql/blob - src/include/fmgr.h
Tag appropriate files for rc3
[postgresql] / src / include / fmgr.h
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-2005, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * $PostgreSQL: pgsql/src/include/fmgr.h,v 1.36 2004/12/31 22:03:19 pgsql Exp $
15  *
16  *-------------------------------------------------------------------------
17  */
18 #ifndef FMGR_H
19 #define FMGR_H
20
21
22 /*
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
25  * signature.)
26  */
27
28 typedef struct FunctionCallInfoData *FunctionCallInfo;
29
30 typedef Datum (*PGFunction) (FunctionCallInfo fcinfo);
31
32 /*
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.
37  */
38 typedef struct FmgrInfo
39 {
40         PGFunction      fn_addr;                /* pointer to function or handler to be
41                                                                  * called */
42         Oid                     fn_oid;                 /* OID of function (NOT of handler, if
43                                                                  * any) */
44         short           fn_nargs;               /* 0..FUNC_MAX_ARGS, or -1 if variable arg
45                                                                  * count */
46         bool            fn_strict;              /* function is "strict" (NULL in => NULL
47                                                                  * out) */
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 */
52 } FmgrInfo;
53
54 /*
55  * This struct is the data actually passed to an fmgr-called function.
56  */
57 typedef struct FunctionCallInfoData
58 {
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
63                                                                  * NULL */
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;
68
69 /*
70  * This routine fills a FmgrInfo struct, given the OID
71  * of the function to be called.
72  */
73 extern void fmgr_info(Oid functionId, FmgrInfo *finfo);
74
75 /*
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.
79  */
80 extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo,
81                           MemoryContext mcxt);
82
83 /*
84  * Copy an FmgrInfo struct
85  */
86 extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
87                            MemoryContext destcxt);
88
89 /*
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
94  * before calling.
95  */
96 #define FunctionCallInvoke(fcinfo)      ((* (fcinfo)->flinfo->fn_addr) (fcinfo))
97
98
99 /*-------------------------------------------------------------------------
100  *              Support macros to ease writing fmgr-compatible functions
101  *
102  * A C-coded fmgr-compatible function should be declared as
103  *
104  *              Datum
105  *              function_name(PG_FUNCTION_ARGS)
106  *              {
107  *                      ...
108  *              }
109  *
110  * It should access its arguments using appropriate PG_GETARG_xxx macros
111  * and should return its result using PG_RETURN_xxx.
112  *
113  *-------------------------------------------------------------------------
114  */
115
116 /* Standard parameter list for fmgr-compatible functions */
117 #define PG_FUNCTION_ARGS        FunctionCallInfo fcinfo
118
119 /*
120  * Get number of arguments passed to function.
121  */
122 #define PG_NARGS() (fcinfo->nargs)
123
124 /*
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!
127  */
128 #define PG_ARGISNULL(n)  (fcinfo->argnull[n])
129
130 /*
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.
137  *
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.
140  */
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);
145
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)
153
154 /*
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
161  * memory.
162  */
163 #define PG_FREE_IF_COPY(ptr,n) \
164         do { \
165                 if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \
166                         pfree(ptr); \
167         } while (0)
168
169 /* Macros for fetching arguments of standard types */
170
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 #define DatumGetHeapTupleHeader(X)      ((HeapTupleHeader) PG_DETOAST_DATUM(X))
196 /* And we also offer variants that return an OK-to-write copy */
197 #define DatumGetByteaPCopy(X)           ((bytea *) PG_DETOAST_DATUM_COPY(X))
198 #define DatumGetTextPCopy(X)            ((text *) PG_DETOAST_DATUM_COPY(X))
199 #define DatumGetBpCharPCopy(X)          ((BpChar *) PG_DETOAST_DATUM_COPY(X))
200 #define DatumGetVarCharPCopy(X)         ((VarChar *) PG_DETOAST_DATUM_COPY(X))
201 #define DatumGetHeapTupleHeaderCopy(X)  ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X))
202 /* Variants which return n bytes starting at pos. m */
203 #define DatumGetByteaPSlice(X,m,n)      ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
204 #define DatumGetTextPSlice(X,m,n)       ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
205 #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
206 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
207 /* GETARG macros for varlena types will typically look like this: */
208 #define PG_GETARG_BYTEA_P(n)            DatumGetByteaP(PG_GETARG_DATUM(n))
209 #define PG_GETARG_TEXT_P(n)                     DatumGetTextP(PG_GETARG_DATUM(n))
210 #define PG_GETARG_BPCHAR_P(n)           DatumGetBpCharP(PG_GETARG_DATUM(n))
211 #define PG_GETARG_VARCHAR_P(n)          DatumGetVarCharP(PG_GETARG_DATUM(n))
212 #define PG_GETARG_HEAPTUPLEHEADER(n)    DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
213 /* And we also offer variants that return an OK-to-write copy */
214 #define PG_GETARG_BYTEA_P_COPY(n)       DatumGetByteaPCopy(PG_GETARG_DATUM(n))
215 #define PG_GETARG_TEXT_P_COPY(n)        DatumGetTextPCopy(PG_GETARG_DATUM(n))
216 #define PG_GETARG_BPCHAR_P_COPY(n)      DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
217 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
218 #define PG_GETARG_HEAPTUPLEHEADER_COPY(n)       DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n))
219 /* And a b-byte slice from position a -also OK to write */
220 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
221 #define PG_GETARG_TEXT_P_SLICE(n,a,b)  DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
222 #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
223 #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
224
225 /* To return a NULL do this: */
226 #define PG_RETURN_NULL()  \
227         do { fcinfo->isnull = true; return (Datum) 0; } while (0)
228
229 /* A few internal functions return void (which is not the same as NULL!) */
230 #define PG_RETURN_VOID()         return (Datum) 0
231
232 /* Macros for returning results of standard types */
233
234 #define PG_RETURN_DATUM(x)       return (x)
235 #define PG_RETURN_INT32(x)       return Int32GetDatum(x)
236 #define PG_RETURN_UINT32(x)  return UInt32GetDatum(x)
237 #define PG_RETURN_INT16(x)       return Int16GetDatum(x)
238 #define PG_RETURN_CHAR(x)        return CharGetDatum(x)
239 #define PG_RETURN_BOOL(x)        return BoolGetDatum(x)
240 #define PG_RETURN_OID(x)         return ObjectIdGetDatum(x)
241 #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
242 #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
243 #define PG_RETURN_NAME(x)        return NameGetDatum(x)
244 /* these macros hide the pass-by-reference-ness of the datatype: */
245 #define PG_RETURN_FLOAT4(x)  return Float4GetDatum(x)
246 #define PG_RETURN_FLOAT8(x)  return Float8GetDatum(x)
247 #define PG_RETURN_INT64(x)       return Int64GetDatum(x)
248 /* RETURN macros for other pass-by-ref types will typically look like this: */
249 #define PG_RETURN_BYTEA_P(x)   PG_RETURN_POINTER(x)
250 #define PG_RETURN_TEXT_P(x)    PG_RETURN_POINTER(x)
251 #define PG_RETURN_BPCHAR_P(x)  PG_RETURN_POINTER(x)
252 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
253 #define PG_RETURN_HEAPTUPLEHEADER(x)  PG_RETURN_POINTER(x)
254
255
256 /*-------------------------------------------------------------------------
257  *              Support for detecting call convention of dynamically-loaded functions
258  *
259  * Dynamically loaded functions may use either the version-1 ("new style")
260  * or version-0 ("old style") calling convention.  Version 1 is the call
261  * convention defined in this header file; version 0 is the old "plain C"
262  * convention.  A version-1 function must be accompanied by the macro call
263  *
264  *              PG_FUNCTION_INFO_V1(function_name);
265  *
266  * Note that internal functions do not need this decoration since they are
267  * assumed to be version-1.
268  *
269  *-------------------------------------------------------------------------
270  */
271
272 typedef struct
273 {
274         int                     api_version;    /* specifies call convention version
275                                                                  * number */
276         /* More fields may be added later, for version numbers > 1. */
277 } Pg_finfo_record;
278
279 /* Expected signature of an info function */
280 typedef Pg_finfo_record *(*PGFInfoFunction) (void);
281
282 /* Macro to build an info function associated with the given function name */
283
284 #define PG_FUNCTION_INFO_V1(funcname) \
285 extern Pg_finfo_record * CppConcat(pg_finfo_,funcname) (void); \
286 Pg_finfo_record * \
287 CppConcat(pg_finfo_,funcname) (void) \
288 { \
289         static Pg_finfo_record my_finfo = { 1 }; \
290         return &my_finfo; \
291 } \
292 extern int no_such_variable
293
294
295 /*-------------------------------------------------------------------------
296  *              Support routines and macros for callers of fmgr-compatible functions
297  *-------------------------------------------------------------------------
298  */
299
300 /* These are for invocation of a specifically named function with a
301  * directly-computed parameter list.  Note that neither arguments nor result
302  * are allowed to be NULL.
303  */
304 extern Datum DirectFunctionCall1(PGFunction func, Datum arg1);
305 extern Datum DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2);
306 extern Datum DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
307                                         Datum arg3);
308 extern Datum DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
309                                         Datum arg3, Datum arg4);
310 extern Datum DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
311                                         Datum arg3, Datum arg4, Datum arg5);
312 extern Datum DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
313                                         Datum arg3, Datum arg4, Datum arg5,
314                                         Datum arg6);
315 extern Datum DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
316                                         Datum arg3, Datum arg4, Datum arg5,
317                                         Datum arg6, Datum arg7);
318 extern Datum DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
319                                         Datum arg3, Datum arg4, Datum arg5,
320                                         Datum arg6, Datum arg7, Datum arg8);
321 extern Datum DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
322                                         Datum arg3, Datum arg4, Datum arg5,
323                                         Datum arg6, Datum arg7, Datum arg8,
324                                         Datum arg9);
325
326 /* These are for invocation of a previously-looked-up function with a
327  * directly-computed parameter list.  Note that neither arguments nor result
328  * are allowed to be NULL.
329  */
330 extern Datum FunctionCall1(FmgrInfo *flinfo, Datum arg1);
331 extern Datum FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2);
332 extern Datum FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
333                           Datum arg3);
334 extern Datum FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
335                           Datum arg3, Datum arg4);
336 extern Datum FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
337                           Datum arg3, Datum arg4, Datum arg5);
338 extern Datum FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
339                           Datum arg3, Datum arg4, Datum arg5,
340                           Datum arg6);
341 extern Datum FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
342                           Datum arg3, Datum arg4, Datum arg5,
343                           Datum arg6, Datum arg7);
344 extern Datum FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
345                           Datum arg3, Datum arg4, Datum arg5,
346                           Datum arg6, Datum arg7, Datum arg8);
347 extern Datum FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
348                           Datum arg3, Datum arg4, Datum arg5,
349                           Datum arg6, Datum arg7, Datum arg8,
350                           Datum arg9);
351
352 /* These are for invocation of a function identified by OID with a
353  * directly-computed parameter list.  Note that neither arguments nor result
354  * are allowed to be NULL.      These are essentially FunctionLookup() followed
355  * by FunctionCallN().  If the same function is to be invoked repeatedly,
356  * do the FunctionLookup() once and then use FunctionCallN().
357  */
358 extern Datum OidFunctionCall1(Oid functionId, Datum arg1);
359 extern Datum OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2);
360 extern Datum OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
361                                  Datum arg3);
362 extern Datum OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
363                                  Datum arg3, Datum arg4);
364 extern Datum OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
365                                  Datum arg3, Datum arg4, Datum arg5);
366 extern Datum OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
367                                  Datum arg3, Datum arg4, Datum arg5,
368                                  Datum arg6);
369 extern Datum OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
370                                  Datum arg3, Datum arg4, Datum arg5,
371                                  Datum arg6, Datum arg7);
372 extern Datum OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
373                                  Datum arg3, Datum arg4, Datum arg5,
374                                  Datum arg6, Datum arg7, Datum arg8);
375 extern Datum OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
376                                  Datum arg3, Datum arg4, Datum arg5,
377                                  Datum arg6, Datum arg7, Datum arg8,
378                                  Datum arg9);
379
380
381 /*
382  * Routines in fmgr.c
383  */
384 extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
385 extern void clear_external_function_hash(void *filehandle);
386 extern Oid      fmgr_internal_function(const char *proname);
387 extern Oid      get_fn_expr_rettype(FmgrInfo *flinfo);
388 extern Oid      get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
389
390 /*
391  * Routines in dfmgr.c
392  */
393 extern char *Dynamic_library_path;
394
395 extern PGFunction load_external_function(char *filename, char *funcname,
396                                            bool signalNotFound, void **filehandle);
397 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
398 extern void load_file(char *filename);
399
400
401 /*
402  * !!! OLD INTERFACE !!!
403  *
404  * fmgr() is the only remaining vestige of the old-style caller support
405  * functions.  It's no longer used anywhere in the Postgres distribution,
406  * but we should leave it around for a release or two to ease the transition
407  * for user-supplied C functions.  OidFunctionCallN() replaces it for new
408  * code.
409  */
410
411 /*
412  * DEPRECATED, DO NOT USE IN NEW CODE
413  */
414 extern char *fmgr(Oid procedureId,...);
415
416 #endif   /* FMGR_H */