]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/fmgr/fmgr.c
Fix initialization of fake LSN for unlogged relations
[postgresql] / src / backend / utils / fmgr / fmgr.c
index 42de04c60a8368038d2afdd3cde849251fdcb283..099ebd779ba26d2d2ee5f019d7d9f9e1505c0d82 100644 (file)
@@ -3,7 +3,7 @@
  * fmgr.c
  *       The Postgres function manager.
  *
- * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
 
 #include "postgres.h"
 
-#include "access/tuptoaster.h"
+#include "access/detoast.h"
 #include "catalog/pg_language.h"
 #include "catalog/pg_proc.h"
 #include "executor/functions.h"
-#include "executor/spi.h"
 #include "lib/stringinfo.h"
 #include "miscadmin.h"
 #include "nodes/nodeFuncs.h"
 #include "pgstat.h"
+#include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/fmgrtab.h"
 #include "utils/guc.h"
 PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook = NULL;
 PGDLLIMPORT fmgr_hook_type fmgr_hook = NULL;
 
-/*
- * Declaration for old-style function pointer type.  This is now used only
- * in fmgr_oldstyle() and is no longer exported.
- *
- * The m68k SVR4 ABI defines that pointers are returned in %a0 instead of
- * %d0. So if a function pointer is declared to return a pointer, the
- * compiler may look only into %a0, but if the called function was declared
- * to return an integer type, it puts its value only into %d0. So the
- * caller doesn't pick up the correct return value. The solution is to
- * declare the function pointer to return int, so the compiler picks up the
- * return value from %d0. (Functions returning pointers put their value
- * *additionally* into %d0 for compatibility.) The price is that there are
- * some warnings about int->pointer conversions ... which we can suppress
- * with suitably ugly casts in fmgr_oldstyle().
- */
-#if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
-typedef int32 (*func_ptr) ();
-#else
-typedef char *(*func_ptr) ();
-#endif
-
-/*
- * For an oldstyle function, fn_extra points to a record like this:
- */
-typedef struct
-{
-       func_ptr        func;                   /* Address of the oldstyle function */
-       bool            arg_toastable[FUNC_MAX_ARGS];   /* is n'th arg of a toastable
-                                                                                                * datatype? */
-} Oldstyle_fnextra;
-
 /*
  * Hashtable for fast lookup of external C functions
  */
@@ -77,51 +46,47 @@ typedef struct
        TransactionId fn_xmin;          /* for checking up-to-dateness */
        ItemPointerData fn_tid;
        PGFunction      user_fn;                /* the function's address */
-       const Pg_finfo_record *inforec;         /* address of its info record */
+       const Pg_finfo_record *inforec; /* address of its info record */
 } CFuncHashTabEntry;
 
 static HTAB *CFuncHash = NULL;
 
 
 static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
-                                          bool ignore_security);
+                                                                  bool ignore_security);
 static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
 static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
 static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
 static void record_C_func(HeapTuple procedureTuple,
-                         PGFunction user_fn, const Pg_finfo_record *inforec);
-static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
-static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
+                                                 PGFunction user_fn, const Pg_finfo_record *inforec);
+
+/* extern so it's callable via JIT */
+extern Datum fmgr_security_definer(PG_FUNCTION_ARGS);
 
 
 /*
- * Lookup routines for builtin-function table. We can search by either Oid
+ * Lookup routines for builtin-function table.  We can search by either Oid
  * or name, but search by Oid is much faster.
  */
 
 static const FmgrBuiltin *
 fmgr_isbuiltin(Oid id)
 {
-       int                     low = 0;
-       int                     high = fmgr_nbuiltins - 1;
+       uint16          index;
+
+       /* fast lookup only possible if original oid still assigned */
+       if (id > fmgr_last_builtin_oid)
+               return NULL;
 
        /*
-        * Loop invariant: low is the first index that could contain target entry,
-        * and high is the last index that could contain it.
+        * Lookup function data. If there's a miss in that range it's likely a
+        * nonexistent function, returning NULL here will trigger an ERROR later.
         */
-       while (low <= high)
-       {
-               int                     i = (high + low) / 2;
-               const FmgrBuiltin *ptr = &fmgr_builtins[i];
-
-               if (id == ptr->foid)
-                       return ptr;
-               else if (id > ptr->foid)
-                       low = i + 1;
-               else
-                       high = i - 1;
-       }
-       return NULL;
+       index = fmgr_builtin_oid_index[id];
+       if (index == InvalidOidBuiltinMapping)
+               return NULL;
+
+       return &fmgr_builtins[index];
 }
 
 /*
@@ -204,7 +169,7 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
                finfo->fn_nargs = fbp->nargs;
                finfo->fn_strict = fbp->strict;
                finfo->fn_retset = fbp->retset;
-               finfo->fn_stats = TRACK_FUNC_ALL;               /* ie, never track */
+               finfo->fn_stats = TRACK_FUNC_ALL;       /* ie, never track */
                finfo->fn_addr = fbp->func;
                finfo->fn_oid = functionId;
                return;
@@ -236,11 +201,11 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
         */
        if (!ignore_security &&
                (procedureStruct->prosecdef ||
-                !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig) ||
+                !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
                 FmgrHookIsNeeded(functionId)))
        {
                finfo->fn_addr = fmgr_security_definer;
-               finfo->fn_stats = TRACK_FUNC_ALL;               /* ie, never track */
+               finfo->fn_stats = TRACK_FUNC_ALL;       /* ie, never track */
                finfo->fn_oid = functionId;
                ReleaseSysCache(procedureTuple);
                return;
@@ -252,7 +217,7 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
 
                        /*
                         * For an ordinary builtin function, we should never get here
-                        * because the isbuiltin() search above will have succeeded.
+                        * because the fmgr_isbuiltin() search above will have succeeded.
                         * However, if the user has done a CREATE FUNCTION to create an
                         * alias for a builtin function, we can end up here.  In that case
                         * we have to look up the function by name.  The name of the
@@ -297,6 +262,95 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
        ReleaseSysCache(procedureTuple);
 }
 
+/*
+ * Return module and C function name providing implementation of functionId.
+ *
+ * If *mod == NULL and *fn == NULL, no C symbol is known to implement
+ * function.
+ *
+ * If *mod == NULL and *fn != NULL, the function is implemented by a symbol in
+ * the main binary.
+ *
+ * If *mod != NULL and *fn !=NULL the function is implemented in an extension
+ * shared object.
+ *
+ * The returned module and function names are pstrdup'ed into the current
+ * memory context.
+ */
+void
+fmgr_symbol(Oid functionId, char **mod, char **fn)
+{
+       HeapTuple       procedureTuple;
+       Form_pg_proc procedureStruct;
+       bool            isnull;
+       Datum           prosrcattr;
+       Datum           probinattr;
+
+       /* Otherwise we need the pg_proc entry */
+       procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionId));
+       if (!HeapTupleIsValid(procedureTuple))
+               elog(ERROR, "cache lookup failed for function %u", functionId);
+       procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
+
+       /*
+        */
+       if (procedureStruct->prosecdef ||
+               !heap_attisnull(procedureTuple, Anum_pg_proc_proconfig, NULL) ||
+               FmgrHookIsNeeded(functionId))
+       {
+               *mod = NULL;                    /* core binary */
+               *fn = pstrdup("fmgr_security_definer");
+               ReleaseSysCache(procedureTuple);
+               return;
+       }
+
+       /* see fmgr_info_cxt_security for the individual cases */
+       switch (procedureStruct->prolang)
+       {
+               case INTERNALlanguageId:
+                       prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
+                                                                                Anum_pg_proc_prosrc, &isnull);
+                       if (isnull)
+                               elog(ERROR, "null prosrc");
+
+                       *mod = NULL;            /* core binary */
+                       *fn = TextDatumGetCString(prosrcattr);
+                       break;
+
+               case ClanguageId:
+                       prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
+                                                                                Anum_pg_proc_prosrc, &isnull);
+                       if (isnull)
+                               elog(ERROR, "null prosrc for C function %u", functionId);
+
+                       probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
+                                                                                Anum_pg_proc_probin, &isnull);
+                       if (isnull)
+                               elog(ERROR, "null probin for C function %u", functionId);
+
+                       /*
+                        * No need to check symbol presence / API version here, already
+                        * checked in fmgr_info_cxt_security.
+                        */
+                       *mod = TextDatumGetCString(probinattr);
+                       *fn = TextDatumGetCString(prosrcattr);
+                       break;
+
+               case SQLlanguageId:
+                       *mod = NULL;            /* core binary */
+                       *fn = pstrdup("fmgr_sql");
+                       break;
+
+               default:
+                       *mod = NULL;
+                       *fn = NULL;                     /* unknown, pass pointer */
+                       break;
+       }
+
+       ReleaseSysCache(procedureTuple);
+}
+
+
 /*
  * Special fmgr_info processing for C-language functions.  Note that
  * finfo->fn_oid is not valid yet.
@@ -304,13 +358,10 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
 static void
 fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
 {
-       Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
        CFuncHashTabEntry *hashentry;
        PGFunction      user_fn;
        const Pg_finfo_record *inforec;
-       Oldstyle_fnextra *fnextra;
        bool            isnull;
-       int                     i;
 
        /*
         * See if we have the function address cached already
@@ -362,20 +413,6 @@ fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
 
        switch (inforec->api_version)
        {
-               case 0:
-                       /* Old style: need to use a handler */
-                       finfo->fn_addr = fmgr_oldstyle;
-                       fnextra = (Oldstyle_fnextra *)
-                               MemoryContextAllocZero(finfo->fn_mcxt,
-                                                                          sizeof(Oldstyle_fnextra));
-                       finfo->fn_extra = (void *) fnextra;
-                       fnextra->func = (func_ptr) user_fn;
-                       for (i = 0; i < procedureStruct->pronargs; i++)
-                       {
-                               fnextra->arg_toastable[i] =
-                                       TypeIsToastable(procedureStruct->proargtypes.values[i]);
-                       }
-                       break;
                case 1:
                        /* New style: call directly */
                        finfo->fn_addr = user_fn;
@@ -415,14 +452,6 @@ fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
                                                   CurrentMemoryContext, true);
        finfo->fn_addr = plfinfo.fn_addr;
 
-       /*
-        * If lookup of the PL handler function produced nonnull fn_extra,
-        * complain --- it must be an oldstyle function! We no longer support
-        * oldstyle PL handlers.
-        */
-       if (plfinfo.fn_extra != NULL)
-               elog(ERROR, "language %u has old-style handler", language);
-
        ReleaseSysCache(languageTuple);
 }
 
@@ -431,36 +460,32 @@ fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
  * The function is specified by a handle for the containing library
  * (obtained from load_external_function) as well as the function name.
  *
- * If no info function exists for the given name, it is not an error.
- * Instead we return a default info record for a version-0 function.
- * We want to raise an error here only if the info function returns
- * something bogus.
+ * If no info function exists for the given name an error is raised.
  *
  * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
  * can validate the information record for a function not yet entered into
  * pg_proc.
  */
 const Pg_finfo_record *
-fetch_finfo_record(void *filehandle, char *funcname)
+fetch_finfo_record(void *filehandle, const char *funcname)
 {
        char       *infofuncname;
        PGFInfoFunction infofunc;
        const Pg_finfo_record *inforec;
-       static Pg_finfo_record default_inforec = {0};
 
-       /* Compute name of info func */
-       infofuncname = (char *) palloc(strlen(funcname) + 10);
-       strcpy(infofuncname, "pg_finfo_");
-       strcat(infofuncname, funcname);
+       infofuncname = psprintf("pg_finfo_%s", funcname);
 
        /* Try to look up the info function */
        infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
                                                                                                                  infofuncname);
        if (infofunc == NULL)
        {
-               /* Not found --- assume version 0 */
-               pfree(infofuncname);
-               return &default_inforec;
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_FUNCTION),
+                                errmsg("could not find function information for function \"%s\"",
+                                               funcname),
+                                errhint("SQL-callable functions need an accompanying PG_FUNCTION_INFO_V1(funcname).")));
+               return NULL;                    /* silence compiler */
        }
 
        /* Found, so call it */
@@ -471,7 +496,6 @@ fetch_finfo_record(void *filehandle, char *funcname)
                elog(ERROR, "null result from info function \"%s\"", infofuncname);
        switch (inforec->api_version)
        {
-               case 0:
                case 1:
                        /* OK, no additional fields to validate */
                        break;
@@ -505,7 +529,7 @@ fetch_finfo_record(void *filehandle, char *funcname)
 static CFuncHashTabEntry *
 lookup_C_func(HeapTuple procedureTuple)
 {
-       Oid                     fn_oid = HeapTupleGetOid(procedureTuple);
+       Oid                     fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
        CFuncHashTabEntry *entry;
 
        if (CFuncHash == NULL)
@@ -517,7 +541,7 @@ lookup_C_func(HeapTuple procedureTuple)
                                        NULL);
        if (entry == NULL)
                return NULL;                    /* no such entry */
-       if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
+       if (entry->fn_xmin == HeapTupleHeaderGetRawXmin(procedureTuple->t_data) &&
                ItemPointerEquals(&entry->fn_tid, &procedureTuple->t_self))
                return entry;                   /* OK */
        return NULL;                            /* entry is out of date */
@@ -530,7 +554,7 @@ static void
 record_C_func(HeapTuple procedureTuple,
                          PGFunction user_fn, const Pg_finfo_record *inforec)
 {
-       Oid                     fn_oid = HeapTupleGetOid(procedureTuple);
+       Oid                     fn_oid = ((Form_pg_proc) GETSTRUCT(procedureTuple))->oid;
        CFuncHashTabEntry *entry;
        bool            found;
 
@@ -542,11 +566,10 @@ record_C_func(HeapTuple procedureTuple,
                MemSet(&hash_ctl, 0, sizeof(hash_ctl));
                hash_ctl.keysize = sizeof(Oid);
                hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
-               hash_ctl.hash = oid_hash;
                CFuncHash = hash_create("CFuncHash",
                                                                100,
                                                                &hash_ctl,
-                                                               HASH_ELEM | HASH_FUNCTION);
+                                                               HASH_ELEM | HASH_BLOBS);
        }
 
        entry = (CFuncHashTabEntry *)
@@ -555,7 +578,7 @@ record_C_func(HeapTuple procedureTuple,
                                        HASH_ENTER,
                                        &found);
        /* OID is already filled in */
-       entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
+       entry->fn_xmin = HeapTupleHeaderGetRawXmin(procedureTuple->t_data);
        entry->fn_tid = procedureTuple->t_self;
        entry->user_fn = user_fn;
        entry->inforec = inforec;
@@ -580,7 +603,7 @@ clear_external_function_hash(void *filehandle)
  * Copy an FmgrInfo struct
  *
  * This is inherently somewhat bogus since we can't reliably duplicate
- * language-dependent subsidiary info. We cheat by zeroing fn_extra,
+ * language-dependent subsidiary info.  We cheat by zeroing fn_extra,
  * instead, meaning that subsidiary info will have to be recomputed.
  */
 void
@@ -589,18 +612,7 @@ fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
 {
        memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
        dstinfo->fn_mcxt = destcxt;
-       if (dstinfo->fn_addr == fmgr_oldstyle)
-       {
-               /* For oldstyle functions we must copy fn_extra */
-               Oldstyle_fnextra *fnextra;
-
-               fnextra = (Oldstyle_fnextra *)
-                       MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
-               memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
-               dstinfo->fn_extra = (void *) fnextra;
-       }
-       else
-               dstinfo->fn_extra = NULL;
+       dstinfo->fn_extra = NULL;
 }
 
 
@@ -621,246 +633,7 @@ fmgr_internal_function(const char *proname)
 
 
 /*
- * Handler for old-style "C" language functions
- */
-static Datum
-fmgr_oldstyle(PG_FUNCTION_ARGS)
-{
-       Oldstyle_fnextra *fnextra;
-       int                     n_arguments = fcinfo->nargs;
-       int                     i;
-       bool            isnull;
-       func_ptr        user_fn;
-       char       *returnValue;
-
-       if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
-               elog(ERROR, "fmgr_oldstyle received NULL pointer");
-       fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
-
-       /*
-        * Result is NULL if any argument is NULL, but we still call the function
-        * (peculiar, but that's the way it worked before, and after all this is a
-        * backwards-compatibility wrapper).  Note, however, that we'll never get
-        * here with NULL arguments if the function is marked strict.
-        *
-        * We also need to detoast any TOAST-ed inputs, since it's unlikely that
-        * an old-style function knows about TOASTing.
-        */
-       isnull = false;
-       for (i = 0; i < n_arguments; i++)
-       {
-               if (PG_ARGISNULL(i))
-                       isnull = true;
-               else if (fnextra->arg_toastable[i])
-                       fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
-       }
-       fcinfo->isnull = isnull;
-
-       user_fn = fnextra->func;
-
-       switch (n_arguments)
-       {
-               case 0:
-                       returnValue = (char *) (*user_fn) ();
-                       break;
-               case 1:
-
-                       /*
-                        * nullvalue() used to use isNull to check if arg is NULL; perhaps
-                        * there are other functions still out there that also rely on
-                        * this undocumented hack?
-                        */
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          &fcinfo->isnull);
-                       break;
-               case 2:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1]);
-                       break;
-               case 3:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2]);
-                       break;
-               case 4:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3]);
-                       break;
-               case 5:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4]);
-                       break;
-               case 6:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5]);
-                       break;
-               case 7:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6]);
-                       break;
-               case 8:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7]);
-                       break;
-               case 9:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8]);
-                       break;
-               case 10:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9]);
-                       break;
-               case 11:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10]);
-                       break;
-               case 12:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10],
-                                                                                          fcinfo->arg[11]);
-                       break;
-               case 13:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10],
-                                                                                          fcinfo->arg[11],
-                                                                                          fcinfo->arg[12]);
-                       break;
-               case 14:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10],
-                                                                                          fcinfo->arg[11],
-                                                                                          fcinfo->arg[12],
-                                                                                          fcinfo->arg[13]);
-                       break;
-               case 15:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10],
-                                                                                          fcinfo->arg[11],
-                                                                                          fcinfo->arg[12],
-                                                                                          fcinfo->arg[13],
-                                                                                          fcinfo->arg[14]);
-                       break;
-               case 16:
-                       returnValue = (char *) (*user_fn) (fcinfo->arg[0],
-                                                                                          fcinfo->arg[1],
-                                                                                          fcinfo->arg[2],
-                                                                                          fcinfo->arg[3],
-                                                                                          fcinfo->arg[4],
-                                                                                          fcinfo->arg[5],
-                                                                                          fcinfo->arg[6],
-                                                                                          fcinfo->arg[7],
-                                                                                          fcinfo->arg[8],
-                                                                                          fcinfo->arg[9],
-                                                                                          fcinfo->arg[10],
-                                                                                          fcinfo->arg[11],
-                                                                                          fcinfo->arg[12],
-                                                                                          fcinfo->arg[13],
-                                                                                          fcinfo->arg[14],
-                                                                                          fcinfo->arg[15]);
-                       break;
-               default:
-
-                       /*
-                        * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
-                        * above code, so mention the actual value in this error not
-                        * FUNC_MAX_ARGS.  You could add cases to the above if you needed
-                        * to support old-style functions with many arguments, but making
-                        * 'em be new-style is probably a better idea.
-                        */
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                        errmsg("function %u has too many arguments (%d, maximum is %d)",
-                                       fcinfo->flinfo->fn_oid, n_arguments, 16)));
-                       returnValue = NULL; /* keep compiler quiet */
-                       break;
-       }
-
-       return PointerGetDatum(returnValue);
-}
-
-
-/*
- * Support for security-definer and proconfig-using functions. We support
+ * Support for security-definer and proconfig-using functions.  We support
  * both of these features using the same call handler, because they are
  * often used together and it would be inefficient (as well as notationally
  * messy) to have two levels of call handler involved.
@@ -880,10 +653,10 @@ struct fmgr_security_definer_cache
  * (All this info is cached for the duration of the current query.)
  * To execute a call, we temporarily replace the flinfo with the cached
  * and looked-up one, while keeping the outer fcinfo (which contains all
- * the actual arguments, etc.) intact. This is not re-entrant, but then
- * the fcinfo itself can't be used re-entrantly anyway.
+ * the actual arguments, etc.) intact.  This is not re-entrant, but then
+ * the fcinfo itself can't be used reentrantly anyway.
  */
-static Datum
+extern Datum
 fmgr_security_definer(PG_FUNCTION_ARGS)
 {
        Datum           result;
@@ -944,7 +717,7 @@ fmgr_security_definer(PG_FUNCTION_ARGS)
 
        if (OidIsValid(fcache->userid))
                SetUserIdAndSecContext(fcache->userid,
-                                                       save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
+                                                          save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
 
        if (fcache->proconfig)
        {
@@ -960,7 +733,7 @@ fmgr_security_definer(PG_FUNCTION_ARGS)
 
        /*
         * We don't need to restore GUC or userid settings on error, because the
-        * ensuing xact or subxact abort will do that.  The PG_TRY block is only
+        * ensuing xact or subxact abort will do that.  The PG_TRY block is only
         * needed to clean up the flinfo link.
         */
        save_flinfo = fcinfo->flinfo;
@@ -1013,24 +786,24 @@ fmgr_security_definer(PG_FUNCTION_ARGS)
 /*
  * These are for invocation of a specifically named function with a
  * directly-computed parameter list.  Note that neither arguments nor result
- * are allowed to be NULL.     Also, the function cannot be one that needs to
+ * are allowed to be NULL.  Also, the function cannot be one that needs to
  * look at FmgrInfo, since there won't be any.
  */
 Datum
 DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 1);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 1, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 1, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.argnull[0] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1039,20 +812,20 @@ DirectFunctionCall1Coll(PGFunction func, Oid collation, Datum arg1)
 Datum
 DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 2);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 2, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 2, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1062,22 +835,22 @@ Datum
 DirectFunctionCall3Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 3);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 3, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 3, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1087,24 +860,24 @@ Datum
 DirectFunctionCall4Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3, Datum arg4)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 4);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 4, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 4, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1114,26 +887,26 @@ Datum
 DirectFunctionCall5Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3, Datum arg4, Datum arg5)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 5);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 5, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 5, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1144,28 +917,28 @@ DirectFunctionCall6Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3, Datum arg4, Datum arg5,
                                                Datum arg6)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 6);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 6, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, NULL, 6, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
 
-       result = (*func) (&fcinfo);
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1176,30 +949,30 @@ DirectFunctionCall7Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3, Datum arg4, Datum arg5,
                                                Datum arg6, Datum arg7)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 7);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 7, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-
-       result = (*func) (&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, NULL, 7, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1210,32 +983,32 @@ DirectFunctionCall8Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg3, Datum arg4, Datum arg5,
                                                Datum arg6, Datum arg7, Datum arg8)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 8);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 8, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-
-       result = (*func) (&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, NULL, 8, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+       fcinfo->args[7].value = arg8;
+       fcinfo->args[7].isnull = false;
+
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
@@ -1247,61 +1020,128 @@ DirectFunctionCall9Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2,
                                                Datum arg6, Datum arg7, Datum arg8,
                                                Datum arg9)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 9);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, NULL, 9, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.arg[8] = arg9;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-       fcinfo.argnull[8] = false;
-
-       result = (*func) (&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, NULL, 9, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+       fcinfo->args[7].value = arg8;
+       fcinfo->args[7].isnull = false;
+       fcinfo->args[8].value = arg9;
+       fcinfo->args[8].isnull = false;
+
+       result = (*func) (fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
+       if (fcinfo->isnull)
                elog(ERROR, "function %p returned NULL", (void *) func);
 
        return result;
 }
 
+/*
+ * These functions work like the DirectFunctionCall functions except that
+ * they use the flinfo parameter to initialise the fcinfo for the call.
+ * It's recommended that the callee only use the fn_extra and fn_mcxt
+ * fields, as other fields will typically describe the calling function
+ * not the callee.  Conversely, the calling function should not have
+ * used fn_extra, unless its use is known to be compatible with the callee's.
+ */
+
+Datum
+CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1)
+{
+       LOCAL_FCINFO(fcinfo, 1);
+       Datum           result;
+
+       InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+
+       result = (*func) (fcinfo);
+
+       /* Check for null result, since caller is clearly not expecting one */
+       if (fcinfo->isnull)
+               elog(ERROR, "function %p returned NULL", (void *) func);
+
+       return result;
+}
+
+Datum
+CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
+{
+       LOCAL_FCINFO(fcinfo, 2);
+       Datum           result;
+
+       InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+
+       result = (*func) (fcinfo);
+
+       /* Check for null result, since caller is clearly not expecting one */
+       if (fcinfo->isnull)
+               elog(ERROR, "function %p returned NULL", (void *) func);
+
+       return result;
+}
 
 /*
  * 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.
  */
+Datum
+FunctionCall0Coll(FmgrInfo *flinfo, Oid collation)
+{
+       LOCAL_FCINFO(fcinfo, 0);
+       Datum           result;
+
+       InitFunctionCallInfoData(*fcinfo, flinfo, 0, collation, NULL, NULL);
+
+       result = FunctionCallInvoke(fcinfo);
+
+       /* Check for null result, since caller is clearly not expecting one */
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
+
+       return result;
+}
+
 Datum
 FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 1);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 1, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 1, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.argnull[0] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1309,25 +1149,21 @@ FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
 Datum
 FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
 {
-       /*
-        * XXX if you change this routine, see also the inlined version in
-        * utils/sort/tuplesort.c!
-        */
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 2);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 2, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 2, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1336,23 +1172,23 @@ Datum
 FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 3);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 3, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 3, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1361,25 +1197,25 @@ Datum
 FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3, Datum arg4)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 4);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 4, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 4, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1388,27 +1224,27 @@ Datum
 FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3, Datum arg4, Datum arg5)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 5);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 5, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 5, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1418,29 +1254,29 @@ FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3, Datum arg4, Datum arg5,
                                  Datum arg6)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 6);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 6, collation, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 6, collation, NULL, NULL);
 
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1450,31 +1286,31 @@ FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3, Datum arg4, Datum arg5,
                                  Datum arg6, Datum arg7)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 7);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 7, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 7, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1484,33 +1320,33 @@ FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg3, Datum arg4, Datum arg5,
                                  Datum arg6, Datum arg7, Datum arg8)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 8);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 8, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 8, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+       fcinfo->args[7].value = arg8;
+       fcinfo->args[7].isnull = false;
+
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1521,35 +1357,35 @@ FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
                                  Datum arg6, Datum arg7, Datum arg8,
                                  Datum arg9)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 9);
        Datum           result;
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 9, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.arg[8] = arg9;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-       fcinfo.argnull[8] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 9, collation, NULL, NULL);
+
+       fcinfo->args[0].value = arg1;
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = arg2;
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = arg3;
+       fcinfo->args[2].isnull = false;
+       fcinfo->args[3].value = arg4;
+       fcinfo->args[3].isnull = false;
+       fcinfo->args[4].value = arg5;
+       fcinfo->args[4].isnull = false;
+       fcinfo->args[5].value = arg6;
+       fcinfo->args[5].isnull = false;
+       fcinfo->args[6].value = arg7;
+       fcinfo->args[6].isnull = false;
+       fcinfo->args[7].value = arg8;
+       fcinfo->args[7].isnull = false;
+       fcinfo->args[8].value = arg9;
+       fcinfo->args[8].isnull = false;
+
+       result = FunctionCallInvoke(fcinfo);
 
        /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
+       if (fcinfo->isnull)
+               elog(ERROR, "function %u returned NULL", flinfo->fn_oid);
 
        return result;
 }
@@ -1558,76 +1394,38 @@ FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2,
 /*
  * These are for invocation of a function identified by OID with a
  * directly-computed parameter list.  Note that neither arguments nor result
- * are allowed to be NULL.     These are essentially fmgr_info() followed
- * by FunctionCallN(). If the same function is to be invoked repeatedly,
+ * are allowed to be NULL.  These are essentially fmgr_info() followed
+ * by FunctionCallN().  If the same function is to be invoked repeatedly,
  * do the fmgr_info() once and then use FunctionCallN().
  */
 Datum
 OidFunctionCall0Coll(Oid functionId, Oid collation)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 0, collation, NULL, NULL);
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall0Coll(&flinfo, collation);
 }
 
 Datum
 OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 1, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.argnull[0] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall1Coll(&flinfo, collation, arg1);
 }
 
 Datum
 OidFunctionCall2Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 2, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall2Coll(&flinfo, collation, arg1, arg2);
 }
 
 Datum
@@ -1635,27 +1433,10 @@ OidFunctionCall3Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg3)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 3, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall3Coll(&flinfo, collation, arg1, arg2, arg3);
 }
 
 Datum
@@ -1663,29 +1444,10 @@ OidFunctionCall4Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg3, Datum arg4)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 4, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall4Coll(&flinfo, collation, arg1, arg2, arg3, arg4);
 }
 
 Datum
@@ -1693,31 +1455,10 @@ OidFunctionCall5Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg3, Datum arg4, Datum arg5)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 5, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall5Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5);
 }
 
 Datum
@@ -1726,33 +1467,11 @@ OidFunctionCall6Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg6)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 6, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall6Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
+                                                        arg6);
 }
 
 Datum
@@ -1761,35 +1480,11 @@ OidFunctionCall7Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg6, Datum arg7)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 7, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall7Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
+                                                        arg6, arg7);
 }
 
 Datum
@@ -1798,37 +1493,11 @@ OidFunctionCall8Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg6, Datum arg7, Datum arg8)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 8, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall8Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
+                                                        arg6, arg7, arg8);
 }
 
 Datum
@@ -1838,39 +1507,11 @@ OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
                                         Datum arg9)
 {
        FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       Datum           result;
 
        fmgr_info(functionId, &flinfo);
 
-       InitFunctionCallInfoData(fcinfo, &flinfo, 9, collation, NULL, NULL);
-
-       fcinfo.arg[0] = arg1;
-       fcinfo.arg[1] = arg2;
-       fcinfo.arg[2] = arg3;
-       fcinfo.arg[3] = arg4;
-       fcinfo.arg[4] = arg5;
-       fcinfo.arg[5] = arg6;
-       fcinfo.arg[6] = arg7;
-       fcinfo.arg[7] = arg8;
-       fcinfo.arg[8] = arg9;
-       fcinfo.argnull[0] = false;
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-       fcinfo.argnull[3] = false;
-       fcinfo.argnull[4] = false;
-       fcinfo.argnull[5] = false;
-       fcinfo.argnull[6] = false;
-       fcinfo.argnull[7] = false;
-       fcinfo.argnull[8] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return result;
+       return FunctionCall9Coll(&flinfo, collation, arg1, arg2, arg3, arg4, arg5,
+                                                        arg6, arg7, arg8, arg9);
 }
 
 
@@ -1885,52 +1526,41 @@ OidFunctionCall9Coll(Oid functionId, Oid collation, Datum arg1, Datum arg2,
  * the caller should assume the result is NULL, but we'll call the input
  * function anyway if it's not strict.  So this is almost but not quite
  * the same as FunctionCall3.
- *
- * One important difference from the bare function call is that we will
- * push any active SPI context, allowing SPI-using I/O functions to be
- * called from other SPI functions without extra notation.     This is a hack,
- * but the alternative of expecting all SPI functions to do SPI_push/SPI_pop
- * around I/O calls seems worse.
  */
 Datum
 InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 3);
        Datum           result;
-       bool            pushed;
 
        if (str == NULL && flinfo->fn_strict)
                return (Datum) 0;               /* just return null result */
 
-       pushed = SPI_push_conditional();
+       InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
 
-       InitFunctionCallInfoData(fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
+       fcinfo->args[0].value = CStringGetDatum(str);
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = Int32GetDatum(typmod);
+       fcinfo->args[2].isnull = false;
 
-       fcinfo.arg[0] = CStringGetDatum(str);
-       fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
-       fcinfo.arg[2] = Int32GetDatum(typmod);
-       fcinfo.argnull[0] = (str == NULL);
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
-
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Should get null result if and only if str is NULL */
        if (str == NULL)
        {
-               if (!fcinfo.isnull)
+               if (!fcinfo->isnull)
                        elog(ERROR, "input function %u returned non-NULL",
-                                fcinfo.flinfo->fn_oid);
+                                flinfo->fn_oid);
        }
        else
        {
-               if (fcinfo.isnull)
+               if (fcinfo->isnull)
                        elog(ERROR, "input function %u returned NULL",
-                                fcinfo.flinfo->fn_oid);
+                                flinfo->fn_oid);
        }
 
-       SPI_pop_conditional(pushed);
-
        return result;
 }
 
@@ -1939,22 +1569,12 @@ InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
  *
  * Do not call this on NULL datums.
  *
- * This is almost just window dressing for FunctionCall1, but it includes
- * SPI context pushing for the same reasons as InputFunctionCall.
+ * This is currently little more than window dressing for FunctionCall1.
  */
 char *
 OutputFunctionCall(FmgrInfo *flinfo, Datum val)
 {
-       char       *result;
-       bool            pushed;
-
-       pushed = SPI_push_conditional();
-
-       result = DatumGetCString(FunctionCall1(flinfo, val));
-
-       SPI_pop_conditional(pushed);
-
-       return result;
+       return DatumGetCString(FunctionCall1(flinfo, val));
 }
 
 /*
@@ -1963,49 +1583,43 @@ OutputFunctionCall(FmgrInfo *flinfo, Datum val)
  * "buf" may be NULL to indicate we are reading a NULL.  In this case
  * the caller should assume the result is NULL, but we'll call the receive
  * function anyway if it's not strict.  So this is almost but not quite
- * the same as FunctionCall3.  Also, this includes SPI context pushing for
- * the same reasons as InputFunctionCall.
+ * the same as FunctionCall3.
  */
 Datum
 ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
                                        Oid typioparam, int32 typmod)
 {
-       FunctionCallInfoData fcinfo;
+       LOCAL_FCINFO(fcinfo, 3);
        Datum           result;
-       bool            pushed;
 
        if (buf == NULL && flinfo->fn_strict)
                return (Datum) 0;               /* just return null result */
 
-       pushed = SPI_push_conditional();
-
-       InitFunctionCallInfoData(fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
+       InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
 
-       fcinfo.arg[0] = PointerGetDatum(buf);
-       fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
-       fcinfo.arg[2] = Int32GetDatum(typmod);
-       fcinfo.argnull[0] = (buf == NULL);
-       fcinfo.argnull[1] = false;
-       fcinfo.argnull[2] = false;
+       fcinfo->args[0].value = PointerGetDatum(buf);
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = Int32GetDatum(typmod);
+       fcinfo->args[2].isnull = false;
 
-       result = FunctionCallInvoke(&fcinfo);
+       result = FunctionCallInvoke(fcinfo);
 
        /* Should get null result if and only if buf is NULL */
        if (buf == NULL)
        {
-               if (!fcinfo.isnull)
+               if (!fcinfo->isnull)
                        elog(ERROR, "receive function %u returned non-NULL",
-                                fcinfo.flinfo->fn_oid);
+                                flinfo->fn_oid);
        }
        else
        {
-               if (fcinfo.isnull)
+               if (fcinfo->isnull)
                        elog(ERROR, "receive function %u returned NULL",
-                                fcinfo.flinfo->fn_oid);
+                                flinfo->fn_oid);
        }
 
-       SPI_pop_conditional(pushed);
-
        return result;
 }
 
@@ -2016,22 +1630,12 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
  *
  * This is little more than window dressing for FunctionCall1, but it does
  * guarantee a non-toasted result, which strictly speaking the underlying
- * function doesn't.  Also, this includes SPI context pushing for the same
- * reasons as InputFunctionCall.
+ * function doesn't.
  */
 bytea *
 SendFunctionCall(FmgrInfo *flinfo, Datum val)
 {
-       bytea      *result;
-       bool            pushed;
-
-       pushed = SPI_push_conditional();
-
-       result = DatumGetByteaP(FunctionCall1(flinfo, val));
-
-       SPI_pop_conditional(pushed);
-
-       return result;
+       return DatumGetByteaP(FunctionCall1(flinfo, val));
 }
 
 /*
@@ -2076,67 +1680,12 @@ OidSendFunctionCall(Oid functionId, Datum val)
 }
 
 
-/*
- * !!! OLD INTERFACE !!!
- *
- * fmgr() is the only remaining vestige of the old-style caller support
- * functions.  It's no longer used anywhere in the Postgres distribution,
- * but we should leave it around for a release or two to ease the transition
- * for user-supplied C functions.  OidFunctionCallN() replaces it for new
- * code.
- *
- * DEPRECATED, DO NOT USE IN NEW CODE
- */
-char *
-fmgr(Oid procedureId,...)
-{
-       FmgrInfo        flinfo;
-       FunctionCallInfoData fcinfo;
-       int                     n_arguments;
-       Datum           result;
-
-       fmgr_info(procedureId, &flinfo);
-
-       MemSet(&fcinfo, 0, sizeof(fcinfo));
-       fcinfo.flinfo = &flinfo;
-       fcinfo.nargs = flinfo.fn_nargs;
-       n_arguments = fcinfo.nargs;
-
-       if (n_arguments > 0)
-       {
-               va_list         pvar;
-               int                     i;
-
-               if (n_arguments > FUNC_MAX_ARGS)
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
-                        errmsg("function %u has too many arguments (%d, maximum is %d)",
-                                       flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
-               va_start(pvar, procedureId);
-               for (i = 0; i < n_arguments; i++)
-                       fcinfo.arg[i] = PointerGetDatum(va_arg(pvar, char *));
-               va_end(pvar);
-       }
-
-       result = FunctionCallInvoke(&fcinfo);
-
-       /* Check for null result, since caller is clearly not expecting one */
-       if (fcinfo.isnull)
-               elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
-
-       return DatumGetPointer(result);
-}
-
-
 /*-------------------------------------------------------------------------
  *             Support routines for standard maybe-pass-by-reference datatypes
  *
  * int8, float4, and float8 can be passed by value if Datum is wide enough.
  * (For backwards-compatibility reasons, we allow pass-by-ref to be chosen
- * at compile time even if pass-by-val is possible.)  For the float types,
- * we need a support routine even if we are passing by value, because many
- * machines pass int and float function parameters/results differently;
- * so we need to play weird games with unions.
+ * at compile time even if pass-by-val is possible.)
  *
  * Note: there is only one switch controlling the pass-by-value option for
  * both int8 and float8; this is to avoid making things unduly complicated
@@ -2154,79 +1703,31 @@ Int64GetDatum(int64 X)
        *retval = X;
        return PointerGetDatum(retval);
 }
-#endif   /* USE_FLOAT8_BYVAL */
+#endif                                                 /* USE_FLOAT8_BYVAL */
+
+#ifndef USE_FLOAT4_BYVAL
 
 Datum
 Float4GetDatum(float4 X)
 {
-#ifdef USE_FLOAT4_BYVAL
-       union
-       {
-               float4          value;
-               int32           retval;
-       }                       myunion;
-
-       myunion.value = X;
-       return SET_4_BYTES(myunion.retval);
-#else
        float4     *retval = (float4 *) palloc(sizeof(float4));
 
        *retval = X;
        return PointerGetDatum(retval);
-#endif
 }
+#endif
 
-#ifdef USE_FLOAT4_BYVAL
-
-float4
-DatumGetFloat4(Datum X)
-{
-       union
-       {
-               int32           value;
-               float4          retval;
-       }                       myunion;
-
-       myunion.value = GET_4_BYTES(X);
-       return myunion.retval;
-}
-#endif   /* USE_FLOAT4_BYVAL */
+#ifndef USE_FLOAT8_BYVAL
 
 Datum
 Float8GetDatum(float8 X)
 {
-#ifdef USE_FLOAT8_BYVAL
-       union
-       {
-               float8          value;
-               int64           retval;
-       }                       myunion;
-
-       myunion.value = X;
-       return SET_8_BYTES(myunion.retval);
-#else
        float8     *retval = (float8 *) palloc(sizeof(float8));
 
        *retval = X;
        return PointerGetDatum(retval);
-#endif
-}
-
-#ifdef USE_FLOAT8_BYVAL
-
-float8
-DatumGetFloat8(Datum X)
-{
-       union
-       {
-               int64           value;
-               float8          retval;
-       }                       myunion;
-
-       myunion.value = GET_8_BYTES(X);
-       return myunion.retval;
 }
-#endif   /* USE_FLOAT8_BYVAL */
+#endif
 
 
 /*-------------------------------------------------------------------------
@@ -2235,19 +1736,19 @@ DatumGetFloat8(Datum X)
  */
 
 struct varlena *
-pg_detoast_datum(struct varlena * datum)
+pg_detoast_datum(struct varlena *datum)
 {
        if (VARATT_IS_EXTENDED(datum))
-               return heap_tuple_untoast_attr(datum);
+               return detoast_attr(datum);
        else
                return datum;
 }
 
 struct varlena *
-pg_detoast_datum_copy(struct varlena * datum)
+pg_detoast_datum_copy(struct varlena *datum)
 {
        if (VARATT_IS_EXTENDED(datum))
-               return heap_tuple_untoast_attr(datum);
+               return detoast_attr(datum);
        else
        {
                /* Make a modifiable copy of the varlena object */
@@ -2260,17 +1761,17 @@ pg_detoast_datum_copy(struct varlena * datum)
 }
 
 struct varlena *
-pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
+pg_detoast_datum_slice(struct varlena *datum, int32 first, int32 count)
 {
        /* Only get the specified portion from the toast rel */
-       return heap_tuple_untoast_attr_slice(datum, first, count);
+       return detoast_attr_slice(datum, first, count);
 }
 
 struct varlena *
-pg_detoast_datum_packed(struct varlena * datum)
+pg_detoast_datum_packed(struct varlena *datum)
 {
        if (VARATT_IS_COMPRESSED(datum) || VARATT_IS_EXTERNAL(datum))
-               return heap_tuple_untoast_attr(datum);
+               return detoast_attr(datum);
        else
                return datum;
 }
@@ -2348,8 +1849,6 @@ get_call_expr_argtype(Node *expr, int argnum)
                args = ((DistinctExpr *) expr)->args;
        else if (IsA(expr, ScalarArrayOpExpr))
                args = ((ScalarArrayOpExpr *) expr)->args;
-       else if (IsA(expr, ArrayCoerceExpr))
-               args = list_make1(((ArrayCoerceExpr *) expr)->arg);
        else if (IsA(expr, NullIfExpr))
                args = ((NullIfExpr *) expr)->args;
        else if (IsA(expr, WindowFunc))
@@ -2363,16 +1862,12 @@ get_call_expr_argtype(Node *expr, int argnum)
        argtype = exprType((Node *) list_nth(args, argnum));
 
        /*
-        * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the
-        * underlying function will actually get passed is the element type of the
-        * array.
+        * special hack for ScalarArrayOpExpr: what the underlying function will
+        * actually get passed is the element type of the array.
         */
        if (IsA(expr, ScalarArrayOpExpr) &&
                argnum == 1)
                argtype = get_base_element_type(argtype);
-       else if (IsA(expr, ArrayCoerceExpr) &&
-                        argnum == 0)
-               argtype = get_base_element_type(argtype);
 
        return argtype;
 }
@@ -2419,8 +1914,6 @@ get_call_expr_arg_stable(Node *expr, int argnum)
                args = ((DistinctExpr *) expr)->args;
        else if (IsA(expr, ScalarArrayOpExpr))
                args = ((ScalarArrayOpExpr *) expr)->args;
-       else if (IsA(expr, ArrayCoerceExpr))
-               args = list_make1(((ArrayCoerceExpr *) expr)->arg);
        else if (IsA(expr, NullIfExpr))
                args = ((NullIfExpr *) expr)->args;
        else if (IsA(expr, WindowFunc))
@@ -2451,6 +1944,8 @@ get_call_expr_arg_stable(Node *expr, int argnum)
  * Get the VARIADIC flag from the function invocation
  *
  * Returns false (the default assumption) if information is not available
+ *
+ * Note this is generally only of interest to VARIADIC ANY functions
  */
 bool
 get_fn_expr_variadic(FmgrInfo *flinfo)
@@ -2471,3 +1966,91 @@ get_fn_expr_variadic(FmgrInfo *flinfo)
        else
                return false;
 }
+
+/*-------------------------------------------------------------------------
+ *             Support routines for procedural language implementations
+ *-------------------------------------------------------------------------
+ */
+
+/*
+ * Verify that a validator is actually associated with the language of a
+ * particular function and that the user has access to both the language and
+ * the function.  All validators should call this before doing anything
+ * substantial.  Doing so ensures a user cannot achieve anything with explicit
+ * calls to validators that he could not achieve with CREATE FUNCTION or by
+ * simply calling an existing function.
+ *
+ * When this function returns false, callers should skip all validation work
+ * and call PG_RETURN_VOID().  This never happens at present; it is reserved
+ * for future expansion.
+ *
+ * In particular, checking that the validator corresponds to the function's
+ * language allows untrusted language validators to assume they process only
+ * superuser-chosen source code.  (Untrusted language call handlers, by
+ * definition, do assume that.)  A user lacking the USAGE language privilege
+ * would be unable to reach the validator through CREATE FUNCTION, so we check
+ * that to block explicit calls as well.  Checking the EXECUTE privilege on
+ * the function is often superfluous, because most users can clone the
+ * function to get an executable copy.  It is meaningful against users with no
+ * database TEMP right and no permanent schema CREATE right, thereby unable to
+ * create any function.  Also, if the function tracks persistent state by
+ * function OID or name, validating the original function might permit more
+ * mischief than creating and validating a clone thereof.
+ */
+bool
+CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
+{
+       HeapTuple       procTup;
+       HeapTuple       langTup;
+       Form_pg_proc procStruct;
+       Form_pg_language langStruct;
+       AclResult       aclresult;
+
+       /*
+        * Get the function's pg_proc entry.  Throw a user-facing error for bad
+        * OID, because validators can be called with user-specified OIDs.
+        */
+       procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(functionOid));
+       if (!HeapTupleIsValid(procTup))
+               ereport(ERROR,
+                               (errcode(ERRCODE_UNDEFINED_FUNCTION),
+                                errmsg("function with OID %u does not exist", functionOid)));
+       procStruct = (Form_pg_proc) GETSTRUCT(procTup);
+
+       /*
+        * Fetch pg_language entry to know if this is the correct validation
+        * function for that pg_proc entry.
+        */
+       langTup = SearchSysCache1(LANGOID, ObjectIdGetDatum(procStruct->prolang));
+       if (!HeapTupleIsValid(langTup))
+               elog(ERROR, "cache lookup failed for language %u", procStruct->prolang);
+       langStruct = (Form_pg_language) GETSTRUCT(langTup);
+
+       if (langStruct->lanvalidator != validatorOid)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                errmsg("language validation function %u called for language %u instead of %u",
+                                               validatorOid, procStruct->prolang,
+                                               langStruct->lanvalidator)));
+
+       /* first validate that we have permissions to use the language */
+       aclresult = pg_language_aclcheck(procStruct->prolang, GetUserId(),
+                                                                        ACL_USAGE);
+       if (aclresult != ACLCHECK_OK)
+               aclcheck_error(aclresult, OBJECT_LANGUAGE,
+                                          NameStr(langStruct->lanname));
+
+       /*
+        * Check whether we are allowed to execute the function itself. If we can
+        * execute it, there should be no possible side-effect of
+        * compiling/validation that execution can't have.
+        */
+       aclresult = pg_proc_aclcheck(functionOid, GetUserId(), ACL_EXECUTE);
+       if (aclresult != ACLCHECK_OK)
+               aclcheck_error(aclresult, OBJECT_FUNCTION, NameStr(procStruct->proname));
+
+       ReleaseSysCache(procTup);
+       ReleaseSysCache(langTup);
+
+       return true;
+}