diff options
author | Bruce Momjian <bruce@momjian.us> | 1999-09-28 04:34:56 +0000 |
---|---|---|
committer | Bruce Momjian <bruce@momjian.us> | 1999-09-28 04:34:56 +0000 |
commit | 9394d62c73e35329d886f96b6b000080b20132f3 (patch) | |
tree | f410842b474e1ffff7cea0f89d442e8172c5c36c /src | |
parent | 63a85082e32a64e2da0f3dde4e4426d016fb749e (diff) | |
download | postgresql-9394d62c73e35329d886f96b6b000080b20132f3.tar.gz postgresql-9394d62c73e35329d886f96b6b000080b20132f3.zip |
I have been working with user defined types and user defined c
functions. One problem that I have encountered with the function
manager is that it does not allow the user to define type conversion
functions that convert between user types. For instance if mytype1,
mytype2, and mytype3 are three Postgresql user types, and if I wish to
define Postgresql conversion functions like
I run into problems, because the Postgresql dynamic loader would look
for a single link symbol, mytype3, for both pieces of object code. If
I just change the name of one of the Postgresql functions (to make the
symbols distinct), the automatic type conversion that Postgresql uses,
for example, when matching operators to arguments no longer finds the
type conversion function.
The solution that I propose, and have implemented in the attatched
patch extends the CREATE FUNCTION syntax as follows. In the first case
above I use the link symbol mytype2_to_mytype3 for the link object
that implements the first conversion function, and define the
Postgresql operator with the following syntax
The patch includes changes to the parser to include the altered
syntax, changes to the ProcedureStmt node in nodes/parsenodes.h,
changes to commands/define.c to handle the extra information in the AS
clause, and changes to utils/fmgr/dfmgr.c that alter the way that the
dynamic loader figures out what link symbol to use. I store the
string for the link symbol in the prosrc text attribute of the pg_proc
table which is currently unused in rows that reference dynamically
loaded
functions.
Bernie Frankpitt
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/commands/define.c | 36 | ||||
-rw-r--r-- | src/backend/parser/gram.y | 12 | ||||
-rw-r--r-- | src/backend/utils/fmgr/dfmgr.c | 34 | ||||
-rw-r--r-- | src/bin/psql/psqlHelp.h | 12 | ||||
-rw-r--r-- | src/include/nodes/parsenodes.h | 14 | ||||
-rw-r--r-- | src/interfaces/ecpg/preproc/preproc.y | 11 | ||||
-rw-r--r-- | src/tools/backend/index.html | 36 |
7 files changed, 112 insertions, 43 deletions
diff --git a/src/backend/commands/define.c b/src/backend/commands/define.c index c7dc7d3ad1c..509d5026d30 100644 --- a/src/backend/commands/define.c +++ b/src/backend/commands/define.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/define.c,v 1.34 1999/07/17 20:16:52 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/define.c,v 1.35 1999/09/28 04:34:40 momjian Exp $ * * DESCRIPTION * The "DefineFoo" routines take the parse tree and pick out the @@ -178,23 +178,45 @@ compute_full_attributes(const List *parameters, int32 *byte_pct_p, } +/* + * For a dynamically linked C language object, the form of the clause is + * + * AS <object file name> [, <link symbol name> ] + * + * In all other cases + * + * AS <object reference, or sql code> + * + */ static void -interpret_AS_clause(const char *languageName, const char *as, +interpret_AS_clause(const char *languageName, const List *as, char **prosrc_str_p, char **probin_str_p) { + Assert(as != NIL); if (strcmp(languageName, "C") == 0) { - /* For "C" language, store the given string in probin */ - *prosrc_str_p = "-"; - *probin_str_p = (char *) as; + + /* + * For "C" language, store the file name in probin and, when + * given, the link symbol name in prosrc. + */ + *probin_str_p = strVal(lfirst(as)); + if (lnext(as) == NULL) + *prosrc_str_p = "-"; + else + *prosrc_str_p = strVal(lsecond(as)); } else { - /* Everything else wants the given string in prosrc */ - *prosrc_str_p = (char *) as; + /* Everything else wants the given string in prosrc. */ + *prosrc_str_p = strVal(lfirst(as)); *probin_str_p = "-"; + + if (lnext(as) != NULL) + elog(ERROR, "CREATE FUNCTION: parse error in 'AS %s, %s'.", + strVal(lfirst(as)), strVal(lsecond(as))); } } diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 32c46e05d1c..67d7ac00cd1 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.99 1999/09/23 17:02:46 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.100 1999/09/28 04:34:44 momjian Exp $ * * HISTORY * AUTHOR DATE MAJOR EVENT @@ -163,7 +163,7 @@ Oid param_type(int t); /* used in parse_expr.c */ %type <list> stmtblock, stmtmulti, result, relation_name_list, OptTableElementList, OptInherit, definition, - opt_with, func_args, func_args_list, + opt_with, func_args, func_args_list, func_as, oper_argtypes, RuleActionList, RuleActionBlock, RuleActionMulti, opt_column_list, columnList, opt_va_list, va_list, sort_clause, sortby_list, index_params, index_list, name_list, @@ -1923,7 +1923,7 @@ RecipeStmt: EXECUTE RECIPE recipe_name *****************************************************************************/ ProcedureStmt: CREATE FUNCTION func_name func_args - RETURNS func_return opt_with AS Sconst LANGUAGE Sconst + RETURNS func_return opt_with AS func_as LANGUAGE Sconst { ProcedureStmt *n = makeNode(ProcedureStmt); n->funcname = $3; @@ -1949,6 +1949,12 @@ func_args_list: TypeId { $$ = lappend($1,makeString($3)); } ; +func_as: Sconst + { $$ = lcons(makeString($1),NIL); } + | Sconst ',' Sconst + { $$ = lappend(lcons(makeString($1),NIL), makeString($3)); } + ; + func_return: set_opt TypeId { TypeName *n = makeNode(TypeName); diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c index dcf2814171b..171f269127d 100644 --- a/src/backend/utils/fmgr/dfmgr.c +++ b/src/backend/utils/fmgr/dfmgr.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.32 1999/09/18 19:08:01 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.33 1999/09/28 04:34:46 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -42,8 +42,11 @@ fmgr_dynamic(Oid procedureId, int *pronargs) HeapTuple procedureTuple; Form_pg_proc procedureStruct; char *proname, - *probinstring; + *probinstring, + *prosrcstring, + *linksymbol; Datum probinattr; + Datum prosrcattr; func_ptr user_fn; Relation rel; bool isnull; @@ -90,7 +93,32 @@ fmgr_dynamic(Oid procedureId, int *pronargs) heap_close(rel, AccessShareLock); - user_fn = handle_load(probinstring, proname); + prosrcattr = heap_getattr(procedureTuple, + Anum_pg_proc_prosrc, + RelationGetDescr(rel), &isnull); + + if (isnull) + { /* Use the proname for the link symbol */ + linksymbol = proname; + } + else if (!PointerIsValid(prosrcattr)) + { /* pg_proc must be messed up! */ + heap_close(rel); + elog(ERROR, "fmgr: Could not extract prosrc for %u from %s", + procedureId, ProcedureRelationName); + return (func_ptr) NULL; + } + else + { /* The text in prosrcattr is either "-" or + * a link symbol */ + prosrcstring = textout((struct varlena *) prosrcattr); + if (strcmp(prosrcstring, "-") == 0) + linksymbol = proname; + else + linksymbol = prosrcstring; + } + + user_fn = handle_load(probinstring, linksymbol); pfree(probinstring); diff --git a/src/bin/psql/psqlHelp.h b/src/bin/psql/psqlHelp.h index 020d8e450e1..63268c2cbd9 100644 --- a/src/bin/psql/psqlHelp.h +++ b/src/bin/psql/psqlHelp.h @@ -5,7 +5,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: psqlHelp.h,v 1.75 1999/09/27 20:27:20 momjian Exp $ + * $Id: psqlHelp.h,v 1.76 1999/09/28 04:34:48 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -88,8 +88,14 @@ static struct _helpStruct QL_HELP[] = { "create a user-defined function", "\ \tCREATE FUNCTION function_name ([type1, ...typeN]) RETURNS return_type\n\ -\tAS 'object_filename'|'sql-queries'|'builtin_function_name'\n\ -\tLANGUAGE 'c'|'sql'|'internal';"}, +\tAS 'sql-queries'|'builtin_function_name'|'object_filename'\n\ +\tLANGUAGE 'sql'|'internal'|'c';\n\ +\n\ +OR\n\ +\n\ +\tCREATE FUNCTION function_name ([type1, ...typeN]) RETURNS return_type\n\ +\tAS 'object_filename', 'link_symbol'\n\ +\tLANGUAGE 'c';"}, {"create index", "construct an index", "\ diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 66c5a9132aa..4bf879137ac 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: parsenodes.h,v 1.79 1999/09/23 17:03:22 momjian Exp $ + * $Id: parsenodes.h,v 1.80 1999/09/28 04:34:50 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -70,7 +70,7 @@ typedef struct Query /* internal to planner */ List *base_rel_list; /* list of base-relation RelOptInfos */ List *join_rel_list; /* list of join-relation RelOptInfos */ - List *query_pathkeys; /* pathkeys for query_planner()'s result */ + List *query_pathkeys; /* pathkeys for query_planner()'s result */ } Query; @@ -361,7 +361,7 @@ typedef struct ProcedureStmt Node *returnType; /* the return type (as a string or a * TypeName (ie.setof) */ List *withClause; /* a list of ParamString */ - char *as; /* the SQL statement or filename */ + List *as; /* the SQL statement or filename */ char *language; /* C or SQL */ } ProcedureStmt; @@ -836,8 +836,10 @@ typedef struct ResTarget { NodeTag type; char *name; /* column name or NULL */ - List *indirection; /* subscripts for destination column, or NIL */ - Node *val; /* the value expression to compute or assign */ + List *indirection; /* subscripts for destination column, or + * NIL */ + Node *val; /* the value expression to compute or + * assign */ } ResTarget; /* @@ -970,7 +972,7 @@ typedef struct RangeTblEntry typedef struct SortClause { NodeTag type; - Index tleSortGroupRef; /* reference into targetlist */ + Index tleSortGroupRef;/* reference into targetlist */ Oid sortop; /* the sort operator to use */ } SortClause; diff --git a/src/interfaces/ecpg/preproc/preproc.y b/src/interfaces/ecpg/preproc/preproc.y index 6151a6328d8..9ba3daacb64 100644 --- a/src/interfaces/ecpg/preproc/preproc.y +++ b/src/interfaces/ecpg/preproc/preproc.y @@ -799,7 +799,7 @@ adjust_array(enum ECPGttype type_enum, int *dimension, int *length, int type_dim %type <str> opt_analyze opt_va_list va_list ExplainStmt index_params %type <str> index_list func_index index_elem opt_type opt_class access_method_clause %type <str> index_opt_unique IndexStmt set_opt func_return def_rest -%type <str> func_args_list func_args opt_with ProcedureStmt def_arg +%type <str> func_as func_args_list func_args opt_with ProcedureStmt def_arg %type <str> def_elem def_list definition def_name def_type DefineStmt %type <str> opt_instead event event_object RuleActionList, %type <str> RuleActionBlock RuleActionMulti join_list @@ -2208,11 +2208,12 @@ RecipeStmt: EXECUTE RECIPE recipe_name * [, iscachable]) * [arg is (<type-1> { , <type-n>})] * as <filename or code in language as appropriate> + * [, <link name for dynamic loader>] * *****************************************************************************/ ProcedureStmt: CREATE FUNCTION func_name func_args - RETURNS func_return opt_with AS Sconst LANGUAGE Sconst + RETURNS func_return opt_with AS func_as LANGUAGE Sconst { $$ = cat2_str(cat5_str(cat5_str(make1_str("create function"), $3, $4, make1_str("returns"), $6), $7, make1_str("as"), $9, make1_str("language")), $11); } @@ -2230,6 +2231,12 @@ func_args_list: TypeId { $$ = $1; } { $$ = cat3_str($1, make1_str(","), $3); } ; +func_as: Sconst + { $$ = $1; } + | Sconst ',' Sconst + { $$ = cat3_str($1, make1_str(","), $3); } + ; + func_return: set_opt TypeId { $$ = cat2_str($1, $2); diff --git a/src/tools/backend/index.html b/src/tools/backend/index.html index e21efb71c19..499698412c8 100644 --- a/src/tools/backend/index.html +++ b/src/tools/backend/index.html @@ -11,28 +11,26 @@ by Bruce Momjian </H2> <P> <CENTER> -<BR> -<BR> <IMG src="flow.gif" usemap="#flowmap" alt="flowchart" border=0> </CENTER> <MAP name="flowmap"> -<AREA COORDS="70,0,230,40" HREF="backend_dirs.html#main"> -<AREA COORDS="70,80,230,120" HREF="backend_dirs.html#postmaster"> -<AREA COORDS="330,40,490,80" HREF="backend_dirs.html#libpq"> -<AREA COORDS="70,160,230,200" HREF="backend_dirs.html#tcop"> -<AREA COORDS="330,160,490,200" HREF="backend_dirs.html#tcop"> -<AREA COORDS="70,260,230,300" HREF="backend_dirs.html#parser"> -<AREA COORDS="70,340,230,380" HREF="backend_dirs.html#tcop"> -<AREA COORDS="70,420,230,460" HREF="backend_dirs.html#optimizer"> -<AREA COORDS="70,400,230,540" HREF="backend_dirs.html#optimizer/plan"> -<AREA COORDS="70,580,230,620" HREF="backend_dirs.html#executor"> -<AREA COORDS="330,340,490,380" HREF="backend_dirs.html#commands"> -<AREA COORDS="0,690,160,740" HREF="backend_dirs.html#utils"> -<AREA COORDS="210,690,370,730" HREF="backend_dirs.html#catalog"> -<AREA COORDS="420,690,590,740" HREF="backend_dirs.html#storage"> -<AREA COORDS="100,770,270,820" HREF="backend_dirs.html#access"> -<AREA COORDS="330,770,490,820" HREF="backend_dirs.html#nodes"> -<AREA COORDS="10,860,170,900" HREF="backend_dirs.html#bootstrap"> +<AREA COORDS="125,35,245,65" HREF="backend_dirs.html#main"> +<AREA COORDS="125,100,245,125" HREF="backend_dirs.html#postmaster"> +<AREA COORDS="325,65,450,95" HREF="backend_dirs.html#libpq"> +<AREA COORDS="125,160,245,190" HREF="backend_dirs.html#tcop"> +<AREA COORDS="325,160,450,190" HREF="backend_dirs.html#tcop"> +<AREA COORDS="125,240,245,265" HREF="backend_dirs.html#parser"> +<AREA COORDS="125,300,250,330" HREF="backend_dirs.html#tcop"> +<AREA COORDS="125,360,250,390" HREF="backend_dirs.html#optimizer"> +<AREA COORDS="125,425,245,455" HREF="backend_dirs.html#optimizer/plan"> +<AREA COORDS="125,490,245,515" HREF="backend_dirs.html#executor"> +<AREA COORDS="325,300,450,330" HREF="backend_dirs.html#commands"> +<AREA COORDS="75,575,195,605" HREF="backend_dirs.html#utils"> +<AREA COORDS="235,575,360,605" HREF="backend_dirs.html#catalog"> +<AREA COORDS="405,575,525,605" HREF="backend_dirs.html#storage"> +<AREA COORDS="155,635,275,665" HREF="backend_dirs.html#access"> +<AREA COORDS="325,635,450,665" HREF="backend_dirs.html#nodes"> +<AREA COORDS="75,705,200,730" HREF="backend_dirs.html#bootstrap"> </MAP> <CENTER><EM> Click on an item to see more detail or look at the full |