]> granicus.if.org Git - postgresql/blobdiff - src/include/fmgr.h
Revise collation derivation method and expression-tree representation.
[postgresql] / src / include / fmgr.h
index bbefebc222f75d781f3fac694706fa66be133976..9e5224d374da46f6535dd68282f91f6ec8deea7f 100644 (file)
@@ -8,16 +8,22 @@
  * or call fmgr-callable functions.
  *
  *
- * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: fmgr.h,v 1.26 2002/10/25 22:17:32 tgl Exp $
+ * src/include/fmgr.h
  *
  *-------------------------------------------------------------------------
  */
 #ifndef FMGR_H
 #define FMGR_H
 
+/* We don't want to include primnodes.h here, so make a stub reference */
+typedef struct Node *fmNodePtr;
+
+/* Likewise, avoid including stringinfo.h here */
+typedef struct StringInfoData *fmStringInfo;
+
 
 /*
  * All functions that can be called directly by fmgr must have this signature.
@@ -34,20 +40,25 @@ typedef Datum (*PGFunction) (FunctionCallInfo fcinfo);
  * before a function can be called through fmgr.  If the same function is
  * to be called multiple times, the lookup need be done only once and the
  * info struct saved for re-use.
+ *
+ * Note that fn_collation and fn_expr really are parse-time-determined
+ * information about the arguments, rather than about the function itself.
+ * But it's convenient to store them here rather than in FunctionCallInfoData,
+ * where they might more logically belong.
  */
 typedef struct FmgrInfo
 {
-       PGFunction      fn_addr;                /* pointer to function or handler to be
-                                                                * called */
-       Oid                     fn_oid;                 /* OID of function (NOT of handler, if
-                                                                * any) */
+       PGFunction      fn_addr;                /* pointer to function or handler to be called */
+       Oid                     fn_oid;                 /* OID of function (NOT of handler, if any) */
        short           fn_nargs;               /* 0..FUNC_MAX_ARGS, or -1 if variable arg
                                                                 * count */
-       bool            fn_strict;              /* function is "strict" (NULL in => NULL
-                                                                * out) */
+       bool            fn_strict;              /* function is "strict" (NULL in => NULL out) */
        bool            fn_retset;              /* function returns a set */
+       unsigned char fn_stats;         /* collect stats if track_functions > this */
+       Oid                     fn_collation;   /* collation that function should use */
        void       *fn_extra;           /* extra space for use by handler */
        MemoryContext fn_mcxt;          /* memory context to store fn_extra in */
+       fmNodePtr       fn_expr;                /* expression parse tree for call, or NULL */
 } FmgrInfo;
 
 /*
@@ -56,10 +67,9 @@ typedef struct FmgrInfo
 typedef struct FunctionCallInfoData
 {
        FmgrInfo   *flinfo;                     /* ptr to lookup info used for this call */
-       struct Node *context;           /* pass info about context of call */
-       struct Node *resultinfo;        /* pass or return extra info about result */
-       bool            isnull;                 /* function must set true if result is
-                                                                * NULL */
+       fmNodePtr       context;                /* pass info about context of call */
+       fmNodePtr       resultinfo;             /* pass or return extra info about result */
+       bool            isnull;                 /* function must set true if result is NULL */
        short           nargs;                  /* # arguments actually passed */
        Datum           arg[FUNC_MAX_ARGS];             /* Arguments passed to function */
        bool            argnull[FUNC_MAX_ARGS]; /* T if arg[i] is actually NULL */
@@ -79,12 +89,34 @@ extern void fmgr_info(Oid functionId, FmgrInfo *finfo);
 extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo,
                          MemoryContext mcxt);
 
+/* Macros for setting the fn_collation and fn_expr fields */
+#define fmgr_info_set_collation(collationId, finfo) \
+       ((finfo)->fn_collation = (collationId))
+#define fmgr_info_set_expr(expr, finfo) \
+       ((finfo)->fn_expr = (expr))
+
 /*
  * Copy an FmgrInfo struct
  */
 extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
                           MemoryContext destcxt);
 
+/*
+ * This macro initializes all the fields of a FunctionCallInfoData except
+ * for the arg[] and argnull[] arrays. Performance testing has shown that
+ * the fastest way to set up argnull[] for small numbers of arguments is to
+ * explicitly set each required element to false, so we don't try to zero
+ * out the argnull[] array in the macro.
+ */
+#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Context, Resultinfo) \
+       do { \
+               (Fcinfo).flinfo = (Flinfo); \
+               (Fcinfo).context = (Context); \
+               (Fcinfo).resultinfo = (Resultinfo); \
+               (Fcinfo).isnull = false; \
+               (Fcinfo).nargs = (Nargs); \
+       } while (0)
+
 /*
  * This macro invokes a function given a filled-in FunctionCallInfoData
  * struct.     The macro result is the returned Datum --- but note that
@@ -115,6 +147,12 @@ extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
 /* Standard parameter list for fmgr-compatible functions */
 #define PG_FUNCTION_ARGS       FunctionCallInfo fcinfo
 
+/*
+ * Get collation function should use.
+ */
+#define PG_GET_COLLATION() \
+       (fcinfo->flinfo ? fcinfo->flinfo->fn_collation : InvalidOid)
+
 /*
  * Get number of arguments passed to function.
  */
@@ -134,6 +172,17 @@ extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
  * if you need a modifiable copy of the input. Caller is expected to have
  * checked for null inputs first, if necessary.
  *
+ * pg_detoast_datum_packed() will return packed (1-byte header) datums
+ * unmodified. It will still expand an externally toasted or compressed datum.
+ * The resulting datum can be accessed using VARSIZE_ANY() and VARDATA_ANY()
+ * (beware of multiple evaluations in those macros!)
+ *
+ * WARNING: It is only safe to use pg_detoast_datum_packed() and
+ * VARDATA_ANY() if you really don't care about the alignment. Either because
+ * you're working with something like text where the alignment doesn't matter
+ * or because you're not going to access its constituent parts and just use
+ * things like memcpy on it anyways.
+ *
  * Note: it'd be nice if these could be macros, but I see no way to do that
  * without evaluating the arguments multiple times, which is NOT acceptable.
  */
@@ -141,6 +190,7 @@ extern struct varlena *pg_detoast_datum(struct varlena * datum);
 extern struct varlena *pg_detoast_datum_copy(struct varlena * datum);
 extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
                                           int32 first, int32 count);
+extern struct varlena *pg_detoast_datum_packed(struct varlena * datum);
 
 #define PG_DETOAST_DATUM(datum) \
        pg_detoast_datum((struct varlena *) DatumGetPointer(datum))
@@ -148,7 +198,10 @@ extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
        pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum))
 #define PG_DETOAST_DATUM_SLICE(datum,f,c) \
                pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \
-               (int32) f, (int32) c)
+               (int32) (f), (int32) (c))
+/* WARNING -- unaligned pointer */
+#define PG_DETOAST_DATUM_PACKED(datum) \
+       pg_detoast_datum_packed((struct varlena *) DatumGetPointer(datum))
 
 /*
  * Support for cleaning up detoasted copies of inputs. This must only
@@ -186,16 +239,24 @@ extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
 #define PG_GETARG_RAW_VARLENA_P(n)     ((struct varlena *) PG_GETARG_POINTER(n))
 /* use this if you want the input datum de-toasted: */
 #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n))
+/* and this if you can handle 1-byte-header datums: */
+#define PG_GETARG_VARLENA_PP(n) PG_DETOAST_DATUM_PACKED(PG_GETARG_DATUM(n))
 /* DatumGetFoo macros for varlena types will typically look like this: */
 #define DatumGetByteaP(X)                      ((bytea *) PG_DETOAST_DATUM(X))
+#define DatumGetByteaPP(X)                     ((bytea *) PG_DETOAST_DATUM_PACKED(X))
 #define DatumGetTextP(X)                       ((text *) PG_DETOAST_DATUM(X))
+#define DatumGetTextPP(X)                      ((text *) PG_DETOAST_DATUM_PACKED(X))
 #define DatumGetBpCharP(X)                     ((BpChar *) PG_DETOAST_DATUM(X))
+#define DatumGetBpCharPP(X)                    ((BpChar *) PG_DETOAST_DATUM_PACKED(X))
 #define DatumGetVarCharP(X)                    ((VarChar *) PG_DETOAST_DATUM(X))
+#define DatumGetVarCharPP(X)           ((VarChar *) PG_DETOAST_DATUM_PACKED(X))
+#define DatumGetHeapTupleHeader(X)     ((HeapTupleHeader) PG_DETOAST_DATUM(X))
 /* And we also offer variants that return an OK-to-write copy */
 #define DatumGetByteaPCopy(X)          ((bytea *) PG_DETOAST_DATUM_COPY(X))
 #define DatumGetTextPCopy(X)           ((text *) PG_DETOAST_DATUM_COPY(X))
 #define DatumGetBpCharPCopy(X)         ((BpChar *) PG_DETOAST_DATUM_COPY(X))
 #define DatumGetVarCharPCopy(X)                ((VarChar *) PG_DETOAST_DATUM_COPY(X))
+#define DatumGetHeapTupleHeaderCopy(X) ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X))
 /* Variants which return n bytes starting at pos. m */
 #define DatumGetByteaPSlice(X,m,n)     ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n))
 #define DatumGetTextPSlice(X,m,n)      ((text *) PG_DETOAST_DATUM_SLICE(X,m,n))
@@ -203,14 +264,20 @@ extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
 /* GETARG macros for varlena types will typically look like this: */
 #define PG_GETARG_BYTEA_P(n)           DatumGetByteaP(PG_GETARG_DATUM(n))
+#define PG_GETARG_BYTEA_PP(n)          DatumGetByteaPP(PG_GETARG_DATUM(n))
 #define PG_GETARG_TEXT_P(n)                    DatumGetTextP(PG_GETARG_DATUM(n))
+#define PG_GETARG_TEXT_PP(n)           DatumGetTextPP(PG_GETARG_DATUM(n))
 #define PG_GETARG_BPCHAR_P(n)          DatumGetBpCharP(PG_GETARG_DATUM(n))
+#define PG_GETARG_BPCHAR_PP(n)         DatumGetBpCharPP(PG_GETARG_DATUM(n))
 #define PG_GETARG_VARCHAR_P(n)         DatumGetVarCharP(PG_GETARG_DATUM(n))
+#define PG_GETARG_VARCHAR_PP(n)                DatumGetVarCharPP(PG_GETARG_DATUM(n))
+#define PG_GETARG_HEAPTUPLEHEADER(n)   DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
 /* And we also offer variants that return an OK-to-write copy */
 #define PG_GETARG_BYTEA_P_COPY(n)      DatumGetByteaPCopy(PG_GETARG_DATUM(n))
 #define PG_GETARG_TEXT_P_COPY(n)       DatumGetTextPCopy(PG_GETARG_DATUM(n))
 #define PG_GETARG_BPCHAR_P_COPY(n)     DatumGetBpCharPCopy(PG_GETARG_DATUM(n))
 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n))
+#define PG_GETARG_HEAPTUPLEHEADER_COPY(n)      DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n))
 /* And a b-byte slice from position a -also OK to write */
 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b)
 #define PG_GETARG_TEXT_P_SLICE(n,a,b)  DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
@@ -245,6 +312,7 @@ extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
 #define PG_RETURN_TEXT_P(x)    PG_RETURN_POINTER(x)
 #define PG_RETURN_BPCHAR_P(x)  PG_RETURN_POINTER(x)
 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
+#define PG_RETURN_HEAPTUPLEHEADER(x)  PG_RETURN_POINTER(x)
 
 
 /*-------------------------------------------------------------------------
@@ -265,27 +333,98 @@ extern struct varlena *pg_detoast_datum_slice(struct varlena * datum,
 
 typedef struct
 {
-       int                     api_version;    /* specifies call convention version
-                                                                * number */
+       int                     api_version;    /* specifies call convention version number */
        /* More fields may be added later, for version numbers > 1. */
 } Pg_finfo_record;
 
 /* Expected signature of an info function */
-typedef Pg_finfo_record *(*PGFInfoFunction) (void);
-
-/* Macro to build an info function associated with the given function name */
+typedef const Pg_finfo_record *(*PGFInfoFunction) (void);
 
+/*
+ *     Macro to build an info function associated with the given function name.
+ *     Win32 loadable functions usually link with 'dlltool --export-all', but it
+ *     doesn't hurt to add PGDLLIMPORT in case they don't.
+ */
 #define PG_FUNCTION_INFO_V1(funcname) \
-extern Pg_finfo_record * CppConcat(pg_finfo_,funcname) (void); \
-Pg_finfo_record * \
+extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
+const Pg_finfo_record * \
 CppConcat(pg_finfo_,funcname) (void) \
 { \
-       static Pg_finfo_record my_finfo = { 1 }; \
+       static const Pg_finfo_record my_finfo = { 1 }; \
        return &my_finfo; \
 } \
 extern int no_such_variable
 
 
+/*-------------------------------------------------------------------------
+ *             Support for verifying backend compatibility of loaded modules
+ *
+ * We require dynamically-loaded modules to include the macro call
+ *             PG_MODULE_MAGIC;
+ * so that we can check for obvious incompatibility, such as being compiled
+ * for a different major PostgreSQL version.
+ *
+ * To compile with versions of PostgreSQL that do not support this,
+ * you may put an #ifdef/#endif test around it.  Note that in a multiple-
+ * source-file module, the macro call should only appear once.
+ *
+ * The specific items included in the magic block are intended to be ones that
+ * are custom-configurable and especially likely to break dynamically loaded
+ * modules if they were compiled with other values.  Also, the length field
+ * can be used to detect definition changes.
+ *
+ * Note: we compare magic blocks with memcmp(), so there had better not be
+ * any alignment pad bytes in them.
+ *
+ * Note: when changing the contents of magic blocks, be sure to adjust the
+ * incompatible_module_error() function in dfmgr.c.
+ *-------------------------------------------------------------------------
+ */
+
+/* Definition of the magic block structure */
+typedef struct
+{
+       int                     len;                    /* sizeof(this struct) */
+       int                     version;                /* PostgreSQL major version */
+       int                     funcmaxargs;    /* FUNC_MAX_ARGS */
+       int                     indexmaxkeys;   /* INDEX_MAX_KEYS */
+       int                     namedatalen;    /* NAMEDATALEN */
+       int                     float4byval;    /* FLOAT4PASSBYVAL */
+       int                     float8byval;    /* FLOAT8PASSBYVAL */
+} Pg_magic_struct;
+
+/* The actual data block contents */
+#define PG_MODULE_MAGIC_DATA \
+{ \
+       sizeof(Pg_magic_struct), \
+       PG_VERSION_NUM / 100, \
+       FUNC_MAX_ARGS, \
+       INDEX_MAX_KEYS, \
+       NAMEDATALEN, \
+       FLOAT4PASSBYVAL, \
+       FLOAT8PASSBYVAL \
+}
+
+/*
+ * Declare the module magic function.  It needs to be a function as the dlsym
+ * in the backend is only guaranteed to work on functions, not data
+ */
+typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void);
+
+#define PG_MAGIC_FUNCTION_NAME Pg_magic_func
+#define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func"
+
+#define PG_MODULE_MAGIC \
+extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
+const Pg_magic_struct * \
+PG_MAGIC_FUNCTION_NAME(void) \
+{ \
+       static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
+       return &Pg_magic_data; \
+} \
+extern int no_such_variable
+
+
 /*-------------------------------------------------------------------------
  *             Support routines and macros for callers of fmgr-compatible functions
  *-------------------------------------------------------------------------
@@ -317,6 +456,12 @@ extern Datum DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
                                        Datum arg6, Datum arg7, Datum arg8,
                                        Datum arg9);
 
+/* The same, but passing a collation to use */
+extern Datum DirectFunctionCall1WithCollation(PGFunction func, Oid collation,
+                                                                                         Datum arg1);
+extern Datum DirectFunctionCall2WithCollation(PGFunction func, Oid collation,
+                                                                                         Datum arg1, Datum arg2);
+
 /* These are for invocation of a previously-looked-up function with a
  * directly-computed parameter list.  Note that neither arguments nor result
  * are allowed to be NULL.
@@ -349,6 +494,7 @@ extern Datum FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
  * by FunctionCallN(). If the same function is to be invoked repeatedly,
  * do the FunctionLookup() once and then use FunctionCallN().
  */
+extern Datum OidFunctionCall0(Oid functionId);
 extern Datum OidFunctionCall1(Oid functionId, Datum arg1);
 extern Datum OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2);
 extern Datum OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
@@ -371,12 +517,32 @@ extern Datum OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
                                 Datum arg6, Datum arg7, Datum arg8,
                                 Datum arg9);
 
+/* Special cases for convenient invocation of datatype I/O functions. */
+extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str,
+                                 Oid typioparam, int32 typmod);
+extern Datum OidInputFunctionCall(Oid functionId, char *str,
+                                        Oid typioparam, int32 typmod);
+extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
+extern char *OidOutputFunctionCall(Oid functionId, Datum val);
+extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf,
+                                       Oid typioparam, int32 typmod);
+extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf,
+                                          Oid typioparam, int32 typmod);
+extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val);
+extern bytea *OidSendFunctionCall(Oid functionId, Datum val);
+
 
 /*
  * Routines in fmgr.c
  */
-extern Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
+extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, char *funcname);
+extern void clear_external_function_hash(void *filehandle);
 extern Oid     fmgr_internal_function(const char *proname);
+extern Oid     get_fn_expr_rettype(FmgrInfo *flinfo);
+extern Oid     get_fn_expr_argtype(FmgrInfo *flinfo, int argnum);
+extern Oid     get_call_expr_argtype(fmNodePtr expr, int argnum);
+extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum);
+extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum);
 
 /*
  * Routines in dfmgr.c
@@ -386,8 +552,49 @@ extern char *Dynamic_library_path;
 extern PGFunction load_external_function(char *filename, char *funcname,
                                           bool signalNotFound, void **filehandle);
 extern PGFunction lookup_external_function(void *filehandle, char *funcname);
-extern void load_file(char *filename);
+extern void load_file(const char *filename, bool restricted);
+extern void **find_rendezvous_variable(const char *varName);
+
+/*
+ * Support for aggregate functions
+ *
+ * This is actually in executor/nodeAgg.c, but we declare it here since the
+ * whole point is for callers of it to not be overly friendly with nodeAgg.
+ */
+
+/* AggCheckCallContext can return one of the following codes, or 0: */
+#define AGG_CONTEXT_AGGREGATE  1               /* regular aggregate */
+#define AGG_CONTEXT_WINDOW             2               /* window function */
+
+extern int AggCheckCallContext(FunctionCallInfo fcinfo,
+                                       MemoryContext *aggcontext);
+
+/*
+ * We allow plugin modules to hook function entry/exit.  This is intended
+ * as support for loadable security policy modules, which may want to
+ * perform additional privilege checks on function entry or exit, or to do
+ * other internal bookkeeping.  To make this possible, such modules must be
+ * able not only to support normal function entry and exit, but also to trap
+ * the case where we bail out due to an error; and they must also be able to
+ * prevent inlining.
+ */
+typedef enum FmgrHookEventType
+{
+       FHET_START,
+       FHET_END,
+       FHET_ABORT
+} FmgrHookEventType;
+
+typedef bool (*needs_fmgr_hook_type)(Oid fn_oid);
+
+typedef void (*fmgr_hook_type)(FmgrHookEventType event,
+                                                          FmgrInfo *flinfo, Datum *arg);
+
+extern PGDLLIMPORT needs_fmgr_hook_type        needs_fmgr_hook;
+extern PGDLLIMPORT fmgr_hook_type              fmgr_hook;
 
+#define FmgrHookIsNeeded(fn_oid)                                                       \
+       (!needs_fmgr_hook ? false : (*needs_fmgr_hook)(fn_oid))
 
 /*
  * !!! OLD INTERFACE !!!