]> granicus.if.org Git - postgresql/blob - src/include/fmgr.h
Implement types regprocedure, regoper, regoperator, regclass, regtype
[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-2001, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * $Id: fmgr.h,v 1.21 2002/04/25 02:56:56 tgl 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 (over multiple
49                                                                  * calls) */
50         void       *fn_extra;           /* extra space for use by handler */
51         MemoryContext fn_mcxt;          /* memory context to store fn_extra in */
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  * If function is not marked "proisstrict" in pg_proc, it must check for
121  * null arguments using this macro.  Do not try to GETARG a null argument!
122  */
123 #define PG_ARGISNULL(n)  (fcinfo->argnull[n])
124
125 /*
126  * Support for fetching detoasted copies of toastable datatypes (all of
127  * which are varlena types).  pg_detoast_datum() gives you either the input
128  * datum (if not toasted) or a detoasted copy allocated with palloc().
129  * pg_detoast_datum_copy() always gives you a palloc'd copy --- use it
130  * if you need a modifiable copy of the input.  Caller is expected to have
131  * checked for null inputs first, if necessary.
132  *
133  * Note: it'd be nice if these could be macros, but I see no way to do that
134  * without evaluating the arguments multiple times, which is NOT acceptable.
135  */
136 extern struct varlena *pg_detoast_datum(struct varlena * datum);
137 extern struct varlena *pg_detoast_datum_copy(struct varlena * datum);
138 extern struct varlena *pg_detoast_datum_slice(struct varlena * datum, 
139                                                                                           int32 first, int32 count); 
140
141 #define PG_DETOAST_DATUM(datum) \
142         pg_detoast_datum((struct varlena *) DatumGetPointer(datum))
143 #define PG_DETOAST_DATUM_COPY(datum) \
144         pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum))
145 #define PG_DETOAST_DATUM_SLICE(datum,f,c) \
146         pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \
147         (int32) f, (int32) c)
148
149 /*
150  * Support for cleaning up detoasted copies of inputs.  This must only
151  * be used for pass-by-ref datatypes, and normally would only be used
152  * for toastable types.  If the given pointer is different from the
153  * original argument, assume it's a palloc'd detoasted copy, and pfree it.
154  * NOTE: most functions on toastable types do not have to worry about this,
155  * but we currently require that support functions for indexes not leak
156  * memory.
157  */
158 #define PG_FREE_IF_COPY(ptr,n) \
159         do { \
160                 if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \
161                         pfree(ptr); \
162         } while (0)
163
164 /* Macros for fetching arguments of standard types */
165
166 #define PG_GETARG_DATUM(n)       (fcinfo->arg[n])
167 #define PG_GETARG_INT32(n)       DatumGetInt32(PG_GETARG_DATUM(n))
168 #define PG_GETARG_UINT32(n)  DatumGetUInt32(PG_GETARG_DATUM(n))
169 #define PG_GETARG_INT16(n)       DatumGetInt16(PG_GETARG_DATUM(n))
170 #define PG_GETARG_UINT16(n)  DatumGetUInt16(PG_GETARG_DATUM(n))
171 #define PG_GETARG_CHAR(n)        DatumGetChar(PG_GETARG_DATUM(n))
172 #define PG_GETARG_BOOL(n)        DatumGetBool(PG_GETARG_DATUM(n))
173 #define PG_GETARG_OID(n)         DatumGetObjectId(PG_GETARG_DATUM(n))
174 #define PG_GETARG_POINTER(n) DatumGetPointer(PG_GETARG_DATUM(n))
175 #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n))
176 #define PG_GETARG_NAME(n)        DatumGetName(PG_GETARG_DATUM(n))
177 /* these macros hide the pass-by-reference-ness of the datatype: */
178 #define PG_GETARG_FLOAT4(n)  DatumGetFloat4(PG_GETARG_DATUM(n))
179 #define PG_GETARG_FLOAT8(n)  DatumGetFloat8(PG_GETARG_DATUM(n))
180 #define PG_GETARG_INT64(n)       DatumGetInt64(PG_GETARG_DATUM(n))
181 /* use this if you want the raw, possibly-toasted input datum: */
182 #define PG_GETARG_RAW_VARLENA_P(n)      ((struct varlena *) PG_GETARG_POINTER(n))
183 /* use this if you want the input datum de-toasted: */
184 #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
185 /* DatumGetFoo macros for varlena types will typically look like this: */
186 #define DatumGetByteaP(X)                       ((bytea *) PG_DETOAST_DATUM(X))
187 #define DatumGetTextP(X)                        ((text *) PG_DETOAST_DATUM(X))
188 #define DatumGetBpCharP(X)                      ((BpChar *) PG_DETOAST_DATUM(X))
189 #define DatumGetVarCharP(X)                     ((VarChar *) PG_DETOAST_DATUM(X))
190 /* And we also offer variants that return an OK-to-write copy */
191 #define DatumGetByteaPCopy(X)           ((bytea *) PG_DETOAST_DATUM_COPY(X))
192 #define DatumGetTextPCopy(X)            ((text *) PG_DETOAST_DATUM_COPY(X))
193 #define DatumGetBpCharPCopy(X)          ((BpChar *) PG_DETOAST_DATUM_COPY(X))
194 #define DatumGetVarCharPCopy(X)         ((VarChar *) PG_DETOAST_DATUM_COPY(X))
195 /* Variants which return n bytes starting at pos. m */
196 #define DatumGetByteaPSlice(X,m,n)  ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
197 #define DatumGetTextPSlice(X,m,n)   ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
198 #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
199 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
200 /* GETARG macros for varlena types will typically look like this: */
201 #define PG_GETARG_BYTEA_P(n)            DatumGetByteaP(PG_GETARG_DATUM(n))
202 #define PG_GETARG_TEXT_P(n)                     DatumGetTextP(PG_GETARG_DATUM(n))
203 #define PG_GETARG_BPCHAR_P(n)           DatumGetBpCharP(PG_GETARG_DATUM(n))
204 #define PG_GETARG_VARCHAR_P(n)          DatumGetVarCharP(PG_GETARG_DATUM(n))
205 /* And we also offer variants that return an OK-to-write copy */
206 #define PG_GETARG_BYTEA_P_COPY(n)       DatumGetByteaPCopy(PG_GETARG_DATUM(n))
207 #define PG_GETARG_TEXT_P_COPY(n)        DatumGetTextPCopy(PG_GETARG_DATUM(n))
208 #define PG_GETARG_BPCHAR_P_COPY(n)      DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
209 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
210 /* And a b-byte slice from position a -also OK to write */
211 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
212 #define PG_GETARG_TEXT_P_SLICE(n,a,b)  DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
213 #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
214 #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
215
216 /* To return a NULL do this: */
217 #define PG_RETURN_NULL()  \
218         do { fcinfo->isnull = true; return (Datum) 0; } while (0)
219
220 /* A few internal functions return void (which is not the same as NULL!) */
221 #define PG_RETURN_VOID()         return (Datum) 0
222
223 /* Macros for returning results of standard types */
224
225 #define PG_RETURN_DATUM(x)       return (x)
226 #define PG_RETURN_INT32(x)       return Int32GetDatum(x)
227 #define PG_RETURN_UINT32(x)  return UInt32GetDatum(x)
228 #define PG_RETURN_INT16(x)       return Int16GetDatum(x)
229 #define PG_RETURN_CHAR(x)        return CharGetDatum(x)
230 #define PG_RETURN_BOOL(x)        return BoolGetDatum(x)
231 #define PG_RETURN_OID(x)         return ObjectIdGetDatum(x)
232 #define PG_RETURN_POINTER(x) return PointerGetDatum(x)
233 #define PG_RETURN_CSTRING(x) return CStringGetDatum(x)
234 #define PG_RETURN_NAME(x)        return NameGetDatum(x)
235 /* these macros hide the pass-by-reference-ness of the datatype: */
236 #define PG_RETURN_FLOAT4(x)  return Float4GetDatum(x)
237 #define PG_RETURN_FLOAT8(x)  return Float8GetDatum(x)
238 #define PG_RETURN_INT64(x)       return Int64GetDatum(x)
239 /* RETURN macros for other pass-by-ref types will typically look like this: */
240 #define PG_RETURN_BYTEA_P(x)   PG_RETURN_POINTER(x)
241 #define PG_RETURN_TEXT_P(x)    PG_RETURN_POINTER(x)
242 #define PG_RETURN_BPCHAR_P(x)  PG_RETURN_POINTER(x)
243 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
244
245
246 /*-------------------------------------------------------------------------
247  *              Support for detecting call convention of dynamically-loaded functions
248  *
249  * Dynamically loaded functions may use either the version-1 ("new style")
250  * or version-0 ("old style") calling convention.  Version 1 is the call
251  * convention defined in this header file; version 0 is the old "plain C"
252  * convention.  A version-1 function must be accompanied by the macro call
253  *
254  *              PG_FUNCTION_INFO_V1(function_name);
255  *
256  * Note that internal functions do not need this decoration since they are
257  * assumed to be version-1.
258  *
259  *-------------------------------------------------------------------------
260  */
261
262 typedef struct
263 {
264         int                     api_version;    /* specifies call convention version
265                                                                  * number */
266         /* More fields may be added later, for version numbers > 1. */
267 } Pg_finfo_record;
268
269 /* Expected signature of an info function */
270 typedef Pg_finfo_record *(*PGFInfoFunction) (void);
271
272 /* Macro to build an info function associated with the given function name */
273
274 #define PG_FUNCTION_INFO_V1(funcname) \
275 extern Pg_finfo_record * CppConcat(pg_finfo_,funcname) (void); \
276 Pg_finfo_record * \
277 CppConcat(pg_finfo_,funcname) (void) \
278 { \
279         static Pg_finfo_record my_finfo = { 1 }; \
280         return &my_finfo; \
281 }
282
283
284 /*-------------------------------------------------------------------------
285  *              Support routines and macros for callers of fmgr-compatible functions
286  *-------------------------------------------------------------------------
287  */
288
289 /* These are for invocation of a specifically named function with a
290  * directly-computed parameter list.  Note that neither arguments nor result
291  * are allowed to be NULL.
292  */
293 extern Datum DirectFunctionCall1(PGFunction func, Datum arg1);
294 extern Datum DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2);
295 extern Datum DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
296                                         Datum arg3);
297 extern Datum DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
298                                         Datum arg3, Datum arg4);
299 extern Datum DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
300                                         Datum arg3, Datum arg4, Datum arg5);
301 extern Datum DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
302                                         Datum arg3, Datum arg4, Datum arg5,
303                                         Datum arg6);
304 extern Datum DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
305                                         Datum arg3, Datum arg4, Datum arg5,
306                                         Datum arg6, Datum arg7);
307 extern Datum DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
308                                         Datum arg3, Datum arg4, Datum arg5,
309                                         Datum arg6, Datum arg7, Datum arg8);
310 extern Datum DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
311                                         Datum arg3, Datum arg4, Datum arg5,
312                                         Datum arg6, Datum arg7, Datum arg8,
313                                         Datum arg9);
314
315 /* These are for invocation of a previously-looked-up function with a
316  * directly-computed parameter list.  Note that neither arguments nor result
317  * are allowed to be NULL.
318  */
319 extern Datum FunctionCall1(FmgrInfo *flinfo, Datum arg1);
320 extern Datum FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2);
321 extern Datum FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
322                           Datum arg3);
323 extern Datum FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
324                           Datum arg3, Datum arg4);
325 extern Datum FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
326                           Datum arg3, Datum arg4, Datum arg5);
327 extern Datum FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
328                           Datum arg3, Datum arg4, Datum arg5,
329                           Datum arg6);
330 extern Datum FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
331                           Datum arg3, Datum arg4, Datum arg5,
332                           Datum arg6, Datum arg7);
333 extern Datum FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
334                           Datum arg3, Datum arg4, Datum arg5,
335                           Datum arg6, Datum arg7, Datum arg8);
336 extern Datum FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
337                           Datum arg3, Datum arg4, Datum arg5,
338                           Datum arg6, Datum arg7, Datum arg8,
339                           Datum arg9);
340
341 /* These are for invocation of a function identified by OID with a
342  * directly-computed parameter list.  Note that neither arguments nor result
343  * are allowed to be NULL.      These are essentially FunctionLookup() followed
344  * by FunctionCallN().  If the same function is to be invoked repeatedly,
345  * do the FunctionLookup() once and then use FunctionCallN().
346  */
347 extern Datum OidFunctionCall1(Oid functionId, Datum arg1);
348 extern Datum OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2);
349 extern Datum OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
350                                  Datum arg3);
351 extern Datum OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
352                                  Datum arg3, Datum arg4);
353 extern Datum OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
354                                  Datum arg3, Datum arg4, Datum arg5);
355 extern Datum OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
356                                  Datum arg3, Datum arg4, Datum arg5,
357                                  Datum arg6);
358 extern Datum OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
359                                  Datum arg3, Datum arg4, Datum arg5,
360                                  Datum arg6, Datum arg7);
361 extern Datum OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
362                                  Datum arg3, Datum arg4, Datum arg5,
363                                  Datum arg6, Datum arg7, Datum arg8);
364 extern Datum OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
365                                  Datum arg3, Datum arg4, Datum arg5,
366                                  Datum arg6, Datum arg7, Datum arg8,
367                                  Datum arg9);
368
369
370 /*
371  * Routines in fmgr.c
372  */
373 extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
374 extern Oid      fmgr_internal_function(const char *proname);
375
376 /*
377  * Routines in dfmgr.c
378  */
379 extern char *Dynamic_library_path;
380
381 extern PGFunction load_external_function(char *filename, char *funcname,
382                                            bool signalNotFound, void **filehandle);
383 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
384 extern void load_file(char *filename);
385
386
387 /*
388  * !!! OLD INTERFACE !!!
389  *
390  * fmgr() is the only remaining vestige of the old-style caller support
391  * functions.  It's no longer used anywhere in the Postgres distribution,
392  * but we should leave it around for a release or two to ease the transition
393  * for user-supplied C functions.  OidFunctionCallN() replaces it for new
394  * code.
395  */
396
397 /*
398  * DEPRECATED, DO NOT USE IN NEW CODE
399  */
400 extern char *fmgr(Oid procedureId,...);
401
402 #endif   /* FMGR_H */