- There are two procedural languages available with the standard
-
Postgres distribution (PL
TCL and PLSQL), and other
- languages can be defined.
+ There are currently three procedural languages available in the standard
+
Postgres distribution (PL
SQL, PLTCL and
+ PLPERL), and other languages can be defined.
Refer to for
more information.
Internal functions are declared in CREATE FUNCTION
- with language name internal or
- newinternal, depending on whether they follow the
- old (pre-7.1) or new (7.1 and later) function call conventions.
- The details of the call conventions are the same as for
- C and newC functions respectively;
- see the next section for details.
+ with language name internal.
The string which specifies the object file (the first string in the AS
clause) should be the full path of the object
- code file for the function, bracketed by quotation marks. If a
+ code file for the function, bracketed by single quote marks. If a
link symbol is given in the AS clause, the link symbol should also be
- bracketed by single quotation marks, and should be exactly the
+ bracketed by single quote marks, and should be exactly the
same as the name of the function in the C source code. On Unix systems
the command nm will print all of the link
symbols in a dynamically loadable object.
Two different calling conventions are currently used for C functions.
- The
"old style" (pre-Postgres-7.1) method
- is selected by writing language name 'C' in the
- CREATE FUNCTION command, while the "new style"
- (7.1 and later) method is selecting by writing language name
- 'newC'. Old-style functions are now deprecated
+ The newer "version 1" calling convention is indicated by writing
+ a PG_FUNCTION_INFO_V1() macro call for the function,
+ as illustrated below. Lack of such a macro indicates an old-style
+ ("version 0") function. The language name specified in CREATE FUNCTION
+ is 'C' in either case. Old-style functions are now deprecated
because of portability problems and lack of functionality, but they
are still supported for compatibility reasons.
include/postgres.h
|
- char
+ "char"
char
N/A
TimeInterval
utils/nabstime.h
- |
- uint2
- uint16
- include/c.h
-
- |
- uint4
- uint32
- include/c.h
-
|
xid
(XID *)
- Obviously, the data field is not long enough to hold
+ Obviously, the data field shown here is not long enough to hold
all possible strings; it's impossible to declare such
a structure in
C. When manipulating
variable-length types, we must be careful to allocate
-
Old-style Calling Conventions for C-Language Functions
+
Version-0 Calling Conventions for C-Language Functions
We present the "old style" calling convention first --- although
this approach is now deprecated, it's easier to get a handle on
- initially. In the "old style" method, the arguments and result
+ initially. In the version-0 method, the arguments and result
of the C function are just declared in normal C style, but being
careful to use the C representation of each SQL data type as shown
above.
- Although this old-style calling convention is simple to use,
+ Although this calling convention is simple to use,
it is not very portable; on some architectures there are problems
with passing smaller-than-int data types this way. Also, there is
no simple way to return a NULL result, nor to cope with NULL arguments
- in any way other than making the function strict. The new-style
+ in any way other than making the function strict. The version-1
convention, presented next, overcomes these objections.
-
New-style Calling Conventions for C-Language Functions
+
Version-1 Calling Conventions for C-Language Functions
- The new-style calling convention relies on macros to suppress most
+ The version-1 calling convention relies on macros to suppress most
of the complexity of passing arguments and results. The C declaration
- of a new-style function is always
+ of a version-1 function is always
Datum funcname(PG_FUNCTION_ARGS)
- Each actual argument is fetched using a PG_GETARG_xxx() macro that
+ In addition, the macro call
+ PG_FUNCTION_INFO_V1(funcname);
+
+ must appear in the same source file (conventionally it's written
+ just before the function itself). This macro call is not needed
+ for "internal"-language functions, since Postgres currently assumes
+ all internal functions are version-1. However, it is
+ required for dynamically-loaded functions.
+
+
+ In a version-1 function,
+ each actual argument is fetched using a PG_GETARG_xxx() macro that
corresponds to the argument's datatype, and the result is returned
using a PG_RETURN_xxx() macro for the return type.
#include "fmgr.h"
/* By Value */
+
+PG_FUNCTION_INFO_V1(add_one);
Datum
add_one(PG_FUNCTION_ARGS)
/* By Reference, Fixed Length */
+PG_FUNCTION_INFO_V1(add_one_float8);
+
Datum
add_one_float8(PG_FUNCTION_ARGS)
{
PG_RETURN_FLOAT8(arg + 1.0);
}
+PG_FUNCTION_INFO_V1(makepoint);
+
Datum
makepoint(PG_FUNCTION_ARGS)
{
/* By Reference, Variable Length */
+PG_FUNCTION_INFO_V1(copytext);
+
Datum
copytext(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(new_t);
}
+PG_FUNCTION_INFO_V1(concat_text);
+
Datum
concat_text(PG_FUNCTION_ARGS)
{
The CREATE FUNCTION commands are the same as
- for the old-style equivalents, except that the language is specified
- as 'newC' not 'C'.
+ for the old-style equivalents.
- At first glance, the new-style coding conventions may appear to be
+ At first glance, the version-1 coding conventions may appear to be
just pointless obscurantism. However, they do offer a number of
improvements, because the macros can hide unnecessary detail.
An example is that in coding add_one_float8, we no longer need to
to deal with fetching "toasted" (compressed or out-of-line) values.
The old-style copytext and concat_text functions shown above are
actually wrong in the presence of toasted values, because they don't
- call pg_detoast_datum() on their inputs.
+ call pg_detoast_datum() on their inputs. (The handler for old-style
+ dynamically-loaded functions currently takes care of this detail,
+ but it does so less efficiently than is possible for a version-1
+ function.)
- The new-style function call conventions also make it possible to
+ The version-1 function call conventions also make it possible to
test for NULL inputs to a non-strict function, return a NULL result
(from either strict or non-strict functions), return "set" results,
and implement trigger functions and procedural-language call handlers.
return salary > limit;
}
-/* In new-style coding, the above would look like this: */
+/* In version-1 coding, the above would look like this: */
+
+PG_FUNCTION_INFO_V1(c_overpaid);
Datum
c_overpaid(PG_FUNCTION_ARGS)
#
#
# IDENTIFICATION
-# $Header: /cvsroot/pgsql/src/backend/utils/Attic/Gen_fmgrtab.sh,v 1.17 2000/07/13 16:07:06 petere Exp $
+# $Header: /cvsroot/pgsql/src/backend/utils/Attic/Gen_fmgrtab.sh,v 1.18 2000/11/20 20:36:48 tgl Exp $
#
#-------------------------------------------------------------------------
#
# Generate the file containing raw pg_proc tuple data
-# (but only for "internal" and "newinternal" language procedures...).
+# (but only for "internal" language procedures...).
#
# Unlike genbki.sh, which can run through cpp last, we have to
# deal with preprocessor statements first (before we sort the
-e 's/[ ]*).*$//' | \
$AWK '
/^#/ { print; next; }
-$4 == "11" { print; next; }
$4 == "12" { print; next; }' > $CPPTMPFILE
if [ $? -ne 0 ]; then
# Generate fmgr's built-in-function table.
#
# Print out the function declarations, then the table that refers to them.
-# NB: the function declarations are bogus in the case of old-style functions,
-# although they should be correct for new-style. Therefore we need to compile
-# this table definition as a separate C file that won't need to include any
-# "real" declarations for those functions!
#
cat > "$$-$TABLEFILE" <
/*-------------------------------------------------------------------------
* It has been GENERATED by $CMDNAME
* from $INFILE
*
- * We lie here to cc about the return type and arguments of old-style
- * builtin functions; all ld cares about is the fact that it
- * will need to resolve an external function reference.
- *
*-------------------------------------------------------------------------
*/
# conditional expression instead. Not all awks have conditional expressions.
$AWK 'BEGIN {
- Strict["t"] = "true"
- Strict["f"] = "false"
- OldStyle["11"] = "true"
- OldStyle["12"] = "false"
+ Bool["t"] = "true"
+ Bool["f"] = "false"
}
{ printf (" { %d, \"%s\", %d, %s, %s, %s },\n"), \
- $1, $(NF-1), $9, Strict[$8], OldStyle[$4], $(NF-1)
+ $1, $(NF-1), $9, Bool[$8], Bool[$10], $(NF-1)
}' $RAWFILE >> "$$-$TABLEFILE"
if [ $? -ne 0 ]; then
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.47 2000/11/16 22:30:33 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.48 2000/11/20 20:36:49 tgl Exp $
*
* NOTES
* Eventually, the index information should go through here, too.
#endif
+char
+get_typstorage(Oid typid)
+{
+ HeapTuple tp;
+
+ tp = SearchSysCache(TYPEOID,
+ ObjectIdGetDatum(typid),
+ 0, 0, 0);
+ if (HeapTupleIsValid(tp))
+ {
+ Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
+ char result;
+
+ result = typtup->typstorage;
+ ReleaseSysCache(tp);
+ return result;
+ }
+ else
+ return 'p';
+}
+
/*
* get_typdefault
*
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.45 2000/11/16 22:30:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.46 2000/11/20 20:36:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
+#include "postgres.h"
+
#include
#include
-#include "postgres.h"
-
-#include "catalog/pg_proc.h"
#include "dynloader.h"
#include "utils/dynamic_loader.h"
-#include "utils/builtins.h"
-#include "utils/syscache.h"
/*
#define SAME_INODE(A,B) ((A).st_ino == (B).inode && (A).st_dev == (B).device)
+/*
+ * Load the specified dynamic-link library file, and look for a function
+ * named funcname in it. If the function is not found, we raise an error
+ * if signalNotFound is true, else return (PGFunction) NULL. Note that
+ * errors in loading the library will provoke elog regardless of
+ * signalNotFound.
+ */
PGFunction
-fmgr_dynamic(Oid functionId)
-{
- HeapTuple procedureTuple;
- Form_pg_proc procedureStruct;
- char *proname,
- *prosrcstring,
- *probinstring;
- Datum prosrcattr,
- probinattr;
- PGFunction user_fn;
- bool isnull;
-
- procedureTuple = SearchSysCache(PROCOID,
- ObjectIdGetDatum(functionId),
- 0, 0, 0);
- if (!HeapTupleIsValid(procedureTuple))
- elog(ERROR, "fmgr_dynamic: function %u: cache lookup failed",
- functionId);
- procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
-
- proname = NameStr(procedureStruct->proname);
-
- prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
- Anum_pg_proc_prosrc, &isnull);
- if (isnull)
- elog(ERROR, "fmgr: Could not extract prosrc for %u from pg_proc",
- functionId);
- prosrcstring = DatumGetCString(DirectFunctionCall1(textout, prosrcattr));
-
- probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
- Anum_pg_proc_probin, &isnull);
- if (isnull)
- elog(ERROR, "fmgr: Could not extract probin for %u from pg_proc",
- functionId);
- probinstring = DatumGetCString(DirectFunctionCall1(textout, probinattr));
-
- user_fn = load_external_function(probinstring, prosrcstring);
-
- pfree(prosrcstring);
- pfree(probinstring);
-
- ReleaseSysCache(procedureTuple);
-
- return user_fn;
-}
-
-PGFunction
-load_external_function(char *filename, char *funcname)
+load_external_function(char *filename, char *funcname,
+ bool signalNotFound)
{
DynamicFileList *file_scanner;
PGFunction retval;
retval = pg_dlsym(file_scanner->handle, funcname);
- if (retval == (PGFunction) NULL)
+ if (retval == (PGFunction) NULL && signalNotFound)
elog(ERROR, "Can't find function %s in file %s", funcname, filename);
return retval;
}
}
- load_external_function(filename, (char *) NULL);
+ load_external_function(filename, (char *) NULL, false);
}
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/fmgr.c,v 1.47 2000/11/16 22:30:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/fmgr.c,v 1.48 2000/11/20 20:36:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "executor/functions.h"
#include "utils/builtins.h"
#include "utils/fmgrtab.h"
+#include "utils/lsyscache.h"
#include "utils/syscache.h"
/*
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;
+
+static void fmgr_info_C_lang(FmgrInfo *finfo, HeapTuple procedureTuple);
+static void fmgr_info_other_lang(FmgrInfo *finfo, HeapTuple procedureTuple);
static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
static Datum fmgr_untrusted(PG_FUNCTION_ARGS);
const FmgrBuiltin *fbp;
HeapTuple procedureTuple;
Form_pg_proc procedureStruct;
- HeapTuple languageTuple;
- Form_pg_language languageStruct;
- Oid language;
char *prosrc;
finfo->fn_oid = functionId;
*/
finfo->fn_nargs = fbp->nargs;
finfo->fn_strict = fbp->strict;
- finfo->fn_retset = false; /* assume no builtins return sets! */
- if (fbp->oldstyle)
- {
- finfo->fn_addr = fmgr_oldstyle;
- finfo->fn_extra = (void *) fbp->func;
- }
- else
- {
- finfo->fn_addr = fbp->func;
- }
+ finfo->fn_retset = fbp->retset;
+ finfo->fn_addr = fbp->func;
return;
}
if (!procedureStruct->proistrusted)
{
+ /* This isn't really supported anymore... */
finfo->fn_addr = fmgr_untrusted;
ReleaseSysCache(procedureTuple);
return;
}
- language = procedureStruct->prolang;
- switch (language)
+ switch (procedureStruct->prolang)
{
case INTERNALlanguageId:
- case NEWINTERNALlanguageId:
/*
* For an ordinary builtin function, we should never get
* here because the isbuiltin() search above will have
elog(ERROR, "fmgr_info: function %s not in internal table",
prosrc);
pfree(prosrc);
- if (fbp->oldstyle)
- {
- finfo->fn_addr = fmgr_oldstyle;
- finfo->fn_extra = (void *) fbp->func;
- }
- else
- {
- finfo->fn_addr = fbp->func;
- }
+ /* Should we check that nargs, strict, retset match the table? */
+ finfo->fn_addr = fbp->func;
break;
case ClanguageId:
- finfo->fn_addr = fmgr_oldstyle;
- finfo->fn_extra = (void *) fmgr_dynamic(functionId);
- break;
-
- case NEWClanguageId:
- finfo->fn_addr = fmgr_dynamic(functionId);
+ fmgr_info_C_lang(finfo, procedureTuple);
break;
case SQLlanguageId:
break;
default:
- /*
- * Might be a created procedural language; try to look it up.
- */
- languageTuple = SearchSysCache(LANGOID,
- ObjectIdGetDatum(language),
- 0, 0, 0);
- if (!HeapTupleIsValid(languageTuple))
- elog(ERROR, "fmgr_info: cache lookup for language %u failed",
- language);
- languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
- if (languageStruct->lanispl)
- {
- FmgrInfo plfinfo;
-
- fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
- 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, "fmgr_info: language %u has old-style handler",
- language);
- }
- else
+ fmgr_info_other_lang(finfo, procedureTuple);
+ break;
+ }
+
+ ReleaseSysCache(procedureTuple);
+}
+
+/*
+ * Special fmgr_info processing for C-language functions
+ */
+static void
+fmgr_info_C_lang(FmgrInfo *finfo, HeapTuple procedureTuple)
+{
+ Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
+ Datum prosrcattr,
+ probinattr;
+ char *prosrcstring,
+ *probinstring;
+ PGFunction user_fn;
+ Pg_finfo_record *inforec;
+ Oldstyle_fnextra *fnextra;
+ bool isnull;
+ int i;
+
+ /* Get prosrc and probin strings (link symbol and library filename) */
+ prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
+ Anum_pg_proc_prosrc, &isnull);
+ if (isnull)
+ elog(ERROR, "fmgr: Could not extract prosrc for %u from pg_proc",
+ finfo->fn_oid);
+ prosrcstring = DatumGetCString(DirectFunctionCall1(textout, prosrcattr));
+
+ probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
+ Anum_pg_proc_probin, &isnull);
+ if (isnull)
+ elog(ERROR, "fmgr: Could not extract probin for %u from pg_proc",
+ finfo->fn_oid);
+ probinstring = DatumGetCString(DirectFunctionCall1(textout, probinattr));
+
+ /* Look up the function itself */
+ user_fn = load_external_function(probinstring, prosrcstring, true);
+
+ /* Get the function information record (real or default) */
+ inforec = fetch_finfo_record(probinstring, prosrcstring);
+
+ switch (inforec->api_version)
+ {
+ case 0:
+ /* Old style: need to use a handler */
+ finfo->fn_addr = fmgr_oldstyle;
+ /* OK to use palloc here because fn_mcxt is CurrentMemoryContext */
+ fnextra = (Oldstyle_fnextra *) palloc(sizeof(Oldstyle_fnextra));
+ finfo->fn_extra = (void *) fnextra;
+ MemSet(fnextra, 0, sizeof(Oldstyle_fnextra));
+ fnextra->func = (func_ptr) user_fn;
+ for (i = 0; i < procedureStruct->pronargs; i++)
{
- elog(ERROR, "fmgr_info: function %u: unsupported language %u",
- functionId, language);
+ fnextra->arg_toastable[i] =
+ TypeIsToastable(procedureStruct->proargtypes[i]);
}
- ReleaseSysCache(languageTuple);
+ break;
+ case 1:
+ /* New style: call directly */
+ finfo->fn_addr = user_fn;
+ break;
+ default:
+ /* Shouldn't get here if fetch_finfo_record did its job */
+ elog(ERROR, "Unknown function API version %d",
+ inforec->api_version);
break;
}
- ReleaseSysCache(procedureTuple);
+ pfree(prosrcstring);
+ pfree(probinstring);
}
+/*
+ * Special fmgr_info processing for other-language functions
+ */
+static void
+fmgr_info_other_lang(FmgrInfo *finfo, HeapTuple procedureTuple)
+{
+ Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
+ Oid language = procedureStruct->prolang;
+ HeapTuple languageTuple;
+ Form_pg_language languageStruct;
+
+ languageTuple = SearchSysCache(LANGOID,
+ ObjectIdGetDatum(language),
+ 0, 0, 0);
+ if (!HeapTupleIsValid(languageTuple))
+ elog(ERROR, "fmgr_info: cache lookup for language %u failed",
+ language);
+ languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);
+ if (languageStruct->lanispl)
+ {
+ FmgrInfo plfinfo;
+
+ fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
+ 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, "fmgr_info: language %u has old-style handler",
+ language);
+ }
+ else
+ {
+ elog(ERROR, "fmgr_info: function %u: unsupported language %u",
+ finfo->fn_oid, language);
+ }
+ ReleaseSysCache(languageTuple);
+}
/*
- * Specialized lookup routine for pg_proc.c: given the alleged name of
- * an internal function, return the OID of the function's language.
- * If the name is not known, return InvalidOid.
+ * Fetch and validate the information record for the given external function.
+ *
+ * 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.
+ *
+ * This function is broken out of fmgr_info_C_lang() so that ProcedureCreate()
+ * can validate the information record for a function not yet entered into
+ * pg_proc.
+ */
+Pg_finfo_record *
+fetch_finfo_record(char *filename, char *funcname)
+{
+ char *infofuncname;
+ PGFInfoFunction infofunc;
+ Pg_finfo_record *inforec;
+ static Pg_finfo_record default_inforec = { 0 };
+
+ /* Compute name of info func */
+ infofuncname = (char *) palloc(strlen(funcname) + 10);
+ sprintf(infofuncname, "pg_finfo_%s", funcname);
+
+ /* Try to look up the info function */
+ infofunc = (PGFInfoFunction) load_external_function(filename,
+ infofuncname,
+ false);
+ if (infofunc == (PGFInfoFunction) NULL)
+ {
+ /* Not found --- assume version 0 */
+ pfree(infofuncname);
+ return &default_inforec;
+ }
+
+ /* Found, so call it */
+ inforec = (*infofunc)();
+
+ /* Validate result as best we can */
+ if (inforec == NULL)
+ elog(ERROR, "Null result from %s", infofuncname);
+ switch (inforec->api_version)
+ {
+ case 0:
+ case 1:
+ /* OK, no additional fields to validate */
+ break;
+ default:
+ elog(ERROR, "Unknown version %d reported by %s",
+ inforec->api_version, infofuncname);
+ break;
+ }
+
+ pfree(infofuncname);
+ return inforec;
+}
+
+
+/*
+ * Specialized lookup routine for ProcedureCreate(): given the alleged name
+ * of an internal function, return the OID of the function.
+ * If the name is not recognized, return InvalidOid.
*/
Oid
-fmgr_internal_language(const char *proname)
+fmgr_internal_function(const char *proname)
{
const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
if (fbp == NULL)
return InvalidOid;
- return fbp->oldstyle ? INTERNALlanguageId : NEWINTERNALlanguageId;
+ return fbp->foid;
}
/*
- * Handler for old-style internal and "C" language functions
- *
- * We expect fmgr_info to have placed the old-style function's address
- * in fn_extra of *flinfo. This is a bit of a hack since fn_extra is really
- * void * which might be a different size than a pointer to function, but
- * it will work on any machine that our old-style call interface works on...
+ * Handler for old-style "C" language functions
*/
static Datum
fmgr_oldstyle(PG_FUNCTION_ARGS)
{
- char *returnValue = NULL;
+ 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, "Internal error: fmgr_oldstyle received NULL function pointer");
+ elog(ERROR, "Internal 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++)
- isnull |= PG_ARGISNULL(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 = (func_ptr) fcinfo->flinfo->fn_extra;
+ user_fn = fnextra->func;
switch (n_arguments)
{
*/
elog(ERROR, "fmgr_oldstyle: function %u: too many arguments (%d > %d)",
fcinfo->flinfo->fn_oid, n_arguments, 16);
+ returnValue = NULL; /* keep compiler quiet */
break;
}
#
#
# IDENTIFICATION
-# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.19 2000/11/13 23:37:53 momjian Exp $
+# $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createlang.sh,v 1.20 2000/11/20 20:36:50 tgl Exp $
#
#-------------------------------------------------------------------------
# ----------
# Create the call handler and the language
# ----------
-$PSQL "CREATE FUNCTION $handler () RETURNS OPAQUE AS '$PGLIB/${object}$DLSUFFIX' LANGUAGE 'newC'"
+$PSQL "CREATE FUNCTION $handler () RETURNS OPAQUE AS '$PGLIB/${object}$DLSUFFIX' LANGUAGE 'C'"
if [ $? -ne 0 ]; then
echo "$CMDNAME: language installation failed" 1>&2
exit 1
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: catversion.h,v 1.61 2000/11/20 05:18:40 vadim Exp $
+ * $Id: catversion.h,v 1.62 2000/11/20 20:36:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 200011191
+#define CATALOG_VERSION_NO 200011201
#endif
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_language.h,v 1.11 2000/05/28 17:56:16 tgl Exp $
+ * $Id: pg_language.h,v 1.12 2000/11/20 20:36:50 tgl Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
* ----------------
*/
-DATA(insert OID = 11 ( internal f f 0 "n/a" ));
-DESCR("old-style built-in functions");
-#define INTERNALlanguageId 11
-DATA(insert OID = 12 ( newinternal f f 0 "n/a" ));
-DESCR("new-style built-in functions");
-#define NEWINTERNALlanguageId 12
+DATA(insert OID = 12 ( internal f f 0 "n/a" ));
+DESCR("Built-in functions");
+#define INTERNALlanguageId 12
DATA(insert OID = 13 ( "C" f f 0 "/bin/cc" ));
-DESCR("Dynamically-loaded old-style C functions");
+DESCR("Dynamically-loaded C functions");
#define ClanguageId 13
-DATA(insert OID = 10 ( "newC" f f 0 "/bin/cc" ));
-DESCR("Dynamically-loaded new-style C functions");
-#define NEWClanguageId 10
DATA(insert OID = 14 ( "sql" f f 0 "postgres"));
DESCR("SQL-language functions");
#define SQLlanguageId 14
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: fmgr.h,v 1.10 2000/08/24 03:29:11 tgl Exp $
+ * $Id: fmgr.h,v 1.11 2000/11/20 20:36:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x)
+/*-------------------------------------------------------------------------
+ * Support for detecting call convention of dynamically-loaded functions
+ *
+ * Dynamically loaded functions may use either the version-1 ("new style")
+ * or version-0 ("old style") calling convention. Version 1 is the call
+ * convention defined in this header file; version 0 is the old "plain C"
+ * convention. A version-1 function must be accompanied by the macro call
+ *
+ * PG_FUNCTION_INFO_V1(function_name);
+ *
+ * Note that internal functions do not need this decoration since they are
+ * assumed to be version-1.
+ *
+ *-------------------------------------------------------------------------
+ */
+
+typedef struct
+{
+ int api_version; /* specifies call convention version number */
+ /* More fields may be added later, for version numbers > 1. */
+} Pg_finfo_record;
+
+/* Expected signature of an info function */
+typedef Pg_finfo_record * (*PGFInfoFunction) (void);
+
+/* Macro to build an info function associated with the given function name */
+
+#define PG_FUNCTION_INFO_V1(funcname) \
+extern Pg_finfo_record * CppConcat(pg_finfo_,funcname) (void); \
+Pg_finfo_record * \
+CppConcat(pg_finfo_,funcname) (void) \
+{ \
+ static Pg_finfo_record my_finfo = { 1 }; \
+ return &my_finfo; \
+}
+
+
/*-------------------------------------------------------------------------
* Support routines and macros for callers of fmgr-compatible functions
*-------------------------------------------------------------------------
/*
* Routines in fmgr.c
*/
-extern Oid fmgr_internal_language(const char *proname);
+extern Pg_finfo_record *fetch_finfo_record(char *filename, char *funcname);
+extern Oid fmgr_internal_function(const char *proname);
/*
* Routines in dfmgr.c
*/
-extern PGFunction fmgr_dynamic(Oid functionId);
-extern PGFunction load_external_function(char *filename, char *funcname);
+extern PGFunction load_external_function(char *filename, char *funcname,
+ bool signalNotFound);
extern void load_file(char *filename);
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: fmgrtab.h,v 1.13 2000/05/28 17:56:20 tgl Exp $
+ * $Id: fmgrtab.h,v 1.14 2000/11/20 20:36:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
const char *funcName; /* C name of the function */
short nargs; /* 0..FUNC_MAX_ARGS, or -1 if variable count */
bool strict; /* T if function is "strict" */
- bool oldstyle; /* T if function uses old fmgr interface */
+ bool retset; /* T if function returns a set */
PGFunction func; /* pointer to compiled function */
} FmgrBuiltin;
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: lsyscache.h,v 1.27 2000/11/16 22:30:49 tgl Exp $
+ * $Id: lsyscache.h,v 1.28 2000/11/20 20:36:51 tgl Exp $
*
*-------------------------------------------------------------------------
*/
extern int16 get_typlen(Oid typid);
extern bool get_typbyval(Oid typid);
extern void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval);
+extern char get_typstorage(Oid typid);
extern Datum get_typdefault(Oid typid);
+#define TypeIsToastable(typid) (get_typstorage(typid) != 'p')
+
#endif /* LSYSCACHE_H */
* ENHANCEMENTS, OR MODIFICATIONS.
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.15 2000/11/16 22:30:49 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.16 2000/11/20 20:36:51 tgl Exp $
*
**********************************************************************/
* call this function for execution of
* perl procedures.
**********************************************************************/
+PG_FUNCTION_INFO_V1(plperl_call_handler);
/* keep non-static */
Datum
* procedural language
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_handler.c,v 1.5 2000/05/29 01:59:14 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_handler.c,v 1.6 2000/11/20 20:36:52 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
* call this function for execution of PL/pgSQL procedures.
* ----------
*/
+PG_FUNCTION_INFO_V1(plpgsql_call_handler);
+
Datum
plpgsql_call_handler(PG_FUNCTION_ARGS)
{
* ENHANCEMENTS, OR MODIFICATIONS.
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.29 2000/11/16 22:30:52 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.30 2000/11/20 20:36:52 tgl Exp $
*
**********************************************************************/
* call this function for execution of
* PL/Tcl procedures.
**********************************************************************/
+PG_FUNCTION_INFO_V1(pltcl_call_handler);
/* keep non-static */
Datum
return retval;
}
+
+/*
+ * Alternate handler for unsafe functions
+ */
+PG_FUNCTION_INFO_V1(pltclu_call_handler);
+
/* keep non-static */
Datum
pltclu_call_handler(PG_FUNCTION_ARGS)
CREATE FUNCTION check_primary_key ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/refint@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION check_foreign_key ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/refint@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION autoinc ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/autoinc@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION funny_dup17 ()
RETURNS opaque
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION ttdummy ()
RETURNS opaque
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION set_ttdummy (int4)
RETURNS int4
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION pt_in_widget(point, widget)
RETURNS bool
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION overpaid(emp)
RETURNS bool
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION boxarea(box)
RETURNS float8
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION interpt_pp(path, path)
RETURNS point
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION reverse_name(name)
RETURNS name
AS '@abs_builddir@/regress@DLSUFFIX@'
LANGUAGE 'c';
+CREATE FUNCTION oldstyle_length(int4, text)
+ RETURNS int4
+ AS '@abs_builddir@/regress@DLSUFFIX@'
+ LANGUAGE 'c';
+
--
-- Function dynamic loading
--
--SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
+--
+-- check that old-style C functions work properly with TOASTed values
+--
+create table oldstyle_test(i int4, t text);
+insert into oldstyle_test values(null,null);
+insert into oldstyle_test values(0,'12');
+insert into oldstyle_test values(1000,'12');
+insert into oldstyle_test values(0, repeat('x', 50000));
+
+select i, length(t), octet_length(t), oldstyle_length(i,t) from oldstyle_test;
+
+drop table oldstyle_test;
+
--
-- functional joins
--
CREATE FUNCTION check_primary_key ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/refint@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION check_foreign_key ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/refint@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION autoinc ()
RETURNS opaque
AS '@abs_builddir@/../../../contrib/spi/autoinc@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION funny_dup17 ()
RETURNS opaque
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION ttdummy ()
RETURNS opaque
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION set_ttdummy (int4)
RETURNS int4
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION pt_in_widget(point, widget)
RETURNS bool
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION overpaid(emp)
RETURNS bool
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION boxarea(box)
RETURNS float8
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION interpt_pp(path, path)
RETURNS point
AS '@abs_builddir@/regress@DLSUFFIX@'
- LANGUAGE 'newC';
+ LANGUAGE 'C';
CREATE FUNCTION reverse_name(name)
RETURNS name
AS '@abs_builddir@/regress@DLSUFFIX@'
LANGUAGE 'c';
+CREATE FUNCTION oldstyle_length(int4, text)
+ RETURNS int4
+ AS '@abs_builddir@/regress@DLSUFFIX@'
+ LANGUAGE 'c';
--
-- Function dynamic loading
--
--SELECT name(equipment(hobby_construct(text 'skywalking', text 'mer'))) AS equip_name;
--
+-- check that old-style C functions work properly with TOASTed values
+--
+create table oldstyle_test(i int4, t text);
+insert into oldstyle_test values(null,null);
+insert into oldstyle_test values(0,'12');
+insert into oldstyle_test values(1000,'12');
+insert into oldstyle_test values(0, repeat('x', 50000));
+select i, length(t), octet_length(t), oldstyle_length(i,t) from oldstyle_test;
+ i | length | octet_length | oldstyle_length
+------+--------+--------------+-----------------
+ | | |
+ 0 | 2 | 2 | 2
+ 1000 | 2 | 2 | 1002
+ 0 | 50000 | 581 | 50000
+(4 rows)
+
+drop table oldstyle_test;
+--
-- functional joins
--
--
/*
- * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.44 2000/08/24 23:34:11 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.45 2000/11/20 20:36:53 tgl Exp $
*/
#include /* faked on sunos */
extern Datum overpaid(PG_FUNCTION_ARGS);
extern Datum boxarea(PG_FUNCTION_ARGS);
extern char *reverse_name(char *string);
+extern int oldstyle_length(int n, text *t);
/*
** Distance from a point to a path
*/
+PG_FUNCTION_INFO_V1(regress_dist_ptpath);
+
Datum
regress_dist_ptpath(PG_FUNCTION_ARGS)
{
/* this essentially does a cartesian product of the lsegs in the
two paths, and finds the min distance between any two lsegs */
+PG_FUNCTION_INFO_V1(regress_path_dist);
+
Datum
regress_path_dist(PG_FUNCTION_ARGS)
{
}
/* return the point where two paths intersect, or NULL if no intersection. */
+PG_FUNCTION_INFO_V1(interpt_pp);
+
Datum
interpt_pp(PG_FUNCTION_ARGS)
{
lseg->m = point_sl(pt1, pt2);
}
+PG_FUNCTION_INFO_V1(overpaid);
+
Datum
overpaid(PG_FUNCTION_ARGS)
{
return result;
}
+PG_FUNCTION_INFO_V1(pt_in_widget);
+
Datum
pt_in_widget(PG_FUNCTION_ARGS)
{
#define ABS(X) ((X) >= 0 ? (X) : -(X))
+PG_FUNCTION_INFO_V1(boxarea);
+
Datum
boxarea(PG_FUNCTION_ARGS)
{
}
char *
-reverse_name(string)
-char *string;
+reverse_name(char *string)
{
int i;
int len;
return new_string;
}
+/* This rather silly function is just to test that oldstyle functions
+ * work correctly on toast-able inputs.
+ */
+int
+oldstyle_length(int n, text *t)
+{
+ int len = 0;
+
+ if (t)
+ len = VARSIZE(t) - VARHDRSZ;
+
+ return n + len;
+}
+
#include "executor/spi.h" /* this is what you need to work with SPI */
#include "commands/trigger.h" /* -"- and triggers */
static bool fd17a_recursion = true;
extern Datum funny_dup17(PG_FUNCTION_ARGS);
+PG_FUNCTION_INFO_V1(funny_dup17);
+
Datum
funny_dup17(PG_FUNCTION_ARGS)
{
static void *splan = NULL;
static bool ttoff = false;
+PG_FUNCTION_INFO_V1(ttdummy);
+
Datum
ttdummy(PG_FUNCTION_ARGS)
{
return PointerGetDatum(rettuple);
}
+PG_FUNCTION_INFO_V1(set_ttdummy);
+
Datum
set_ttdummy(PG_FUNCTION_ARGS)
{
DROP FUNCTION reverse_name(name);
+DROP FUNCTION oldstyle_length(int4, text);
--
-- OPERATOR REMOVAL
/* By Value */
+PG_FUNCTION_INFO_V1(add_one);
+
Datum
add_one(PG_FUNCTION_ARGS)
{
/* By Reference, Fixed Length */
+PG_FUNCTION_INFO_V1(add_one_float8);
+
Datum
add_one_float8(PG_FUNCTION_ARGS)
{
PG_RETURN_FLOAT8(arg + 1.0);
}
+PG_FUNCTION_INFO_V1(makepoint);
+
Datum
makepoint(PG_FUNCTION_ARGS)
{
/* By Reference, Variable Length */
+PG_FUNCTION_INFO_V1(copytext);
+
Datum
copytext(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(new_t);
}
+PG_FUNCTION_INFO_V1(concat_text);
+
Datum
concat_text(PG_FUNCTION_ARGS)
{
/* Composite types */
+PG_FUNCTION_INFO_V1(c_overpaid);
+
Datum
c_overpaid(PG_FUNCTION_ARGS)
{