Select parser at runtime
Select parser at runtime
От:
Ian Lance Taylor <ian@airs.com>
Дата:
I've been experimenting with using a different parser (one which is
more Oracle compatible). While the parser is not yet ready for prime
time, I thought I would send out the code used to select the parser to
gauge its level of acceptability. If this patch isn't going to fly,
then my approach to an alternate parser isn't going to fly either.
After this patch is applied, you can switch to a new parser by doing
something like this:
create function my_parser(opaque, opaque, opaque) returns opaque
as '..../parser.so' language 'c';
set parser = my_parser;
After you do this, all subsequent input will be interpreted using the
specified parser. Note that you may want to leave yourself an escape
hatch of some sort to set the parser back to Postgres standard.
If this patch is accepted, then some further work needs to be done to
set the parser for SPI calls, so that it is possible for the user to
change the parser while still using ordinary PL/pgSQL.
I would appreciate any feedback.
Ian
Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/tcop/postgres.c,v
retrieving revision 1.230
diff -u -r1.230 postgres.c
--- src/backend/tcop/postgres.c 2001/08/04 00:14:43 1.230
+++ src/backend/tcop/postgres.c 2001/08/11 00:53:08
@@ -58,9 +58,11 @@
#include "tcop/utility.h"
#include "storage/proc.h"
#include "utils/exc.h"
+#include "utils/fcache.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
+#include "utils/syscache.h"
#ifdef MULTIBYTE
#include "mb/pg_wchar.h"
#endif
@@ -118,6 +120,10 @@
*/
int XfuncMode = 0;
+char *parser_function_name = 0;
+static bool update_parser_function_fcache = true;
+static FunctionCachePtr parser_function_fcache = NULL;
+
/* ----------------------------------------------------------------
* decls for routines only used in this file
* ----------------------------------------------------------------
@@ -389,8 +395,23 @@
if (Show_parser_stats)
ResetUsage();
+
+ if (update_parser_function_fcache)
+ assign_parser(parser_function_name);
+
+ if (parser_function_fcache == NULL)
+ raw_parsetree_list = parser(query_string, typev, nargs);
+ else
+ {
+ Datum result;
+
+ parser_function_fcache->fcinfo.arg[0] = PointerGetDatum(query_string);
+ parser_function_fcache->fcinfo.arg[1] = PointerGetDatum(typev);
+ parser_function_fcache->fcinfo.arg[2] = Int32GetDatum(nargs);
- raw_parsetree_list = parser(query_string, typev, nargs);
+ result = FunctionCallInvoke(&parser_function_fcache->fcinfo);
+ raw_parsetree_list = (List *) DatumGetPointer(result);
+ }
if (Show_parser_stats)
{
@@ -399,6 +420,82 @@
}
return raw_parsetree_list;
+}
+
+/*
+ * Check that we can find a parser function. This is called when the
+ * user assigns a value to the `parser' variable.
+ */
+bool
+check_parser(const char *proposed)
+{
+ HeapTuple tup;
+ Oid argtypes[FUNC_MAX_ARGS];
+
+ if (proposed[0] == '\0' || strcmp(proposed, "postgres") == 0)
+ return true;
+
+ /* We can't check this unless we have started running. */
+ if (! IsNormalProcessingMode())
+ return true;
+
+ memset(argtypes, 0, sizeof argtypes);
+ tup = SearchSysCache(PROCNAME,
+ PointerGetDatum(proposed),
+ Int32GetDatum(3),
+ PointerGetDatum(argtypes),
+ 0);
+ if (! HeapTupleIsValid(tup))
+ return false;
+ ReleaseSysCache(tup);
+ return true;
+}
+
+/*
+ * Assign a new parser function.
+ */
+void
+assign_parser(const char *value)
+{
+ FunctionCachePtr fcache;
+ HeapTuple tup;
+ Oid argtypes[FUNC_MAX_ARGS];
+ Oid oid;
+
+ /* We can't update parser_function_fcache until we have started
+ * running.
+ */
+ if (! IsNormalProcessingMode())
+ {
+ update_parser_function_fcache = true;
+ return;
+ }
+
+ if (value[0] == '\0' || strcmp(value, "postgres") == 0)
+ fcache = NULL;
+ else
+ {
+ memset(argtypes, 0, sizeof argtypes);
+ tup = SearchSysCache(PROCNAME,
+ PointerGetDatum(value),
+ Int32GetDatum(3),
+ PointerGetDatum(argtypes),
+ 0);
+ if (! HeapTupleIsValid(tup))
+ elog(ERROR, "parser function %s does not exist", value);
+
+ oid = tup->t_data->t_oid;
+
+ ReleaseSysCache(tup);
+
+ fcache = init_fcache(oid, 3, TopMemoryContext);
+ }
+
+ if (parser_function_fcache != NULL)
+ pfree(parser_function_fcache);
+ parser_function_fcache = fcache;
+
+ update_parser_function_fcache = false;
}
/*
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/backend/utils/misc/guc.c,v
retrieving revision 1.45
diff -u -r1.45 guc.c
--- src/backend/utils/misc/guc.c 2001/07/05 15:19:40 1.45
+++ src/backend/utils/misc/guc.c 2001/08/11 00:53:11
@@ -376,6 +376,9 @@
{"krb_server_keyfile", PGC_POSTMASTER, &pg_krb_server_keyfile,
PG_KRB_SRVTAB, NULL, NULL},
+ {"parser", PGC_USERSET, &parser_function_name, "",
+ check_parser, assign_parser},
+
#ifdef ENABLE_SYSLOG
{"syslog_facility", PGC_POSTMASTER, &Syslog_facility,
"LOCAL0", check_facility, NULL},
Index: src/include/tcop/tcopprot.h
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/include/tcop/tcopprot.h,v
retrieving revision 1.41
diff -u -r1.41 tcopprot.h
--- src/include/tcop/tcopprot.h 2001/06/08 21:16:48 1.41
+++ src/include/tcop/tcopprot.h 2001/08/11 00:53:12
@@ -31,6 +31,8 @@
extern bool HostnameLookup;
extern bool ShowPortNumber;
+extern char *parser_function_name;
+
#ifndef BOOTSTRAP_INCLUDE
extern List *pg_parse_and_rewrite(char *query_string,
@@ -39,6 +41,8 @@
extern void pg_exec_query_string(char *query_string,
CommandDest dest,
MemoryContext parse_context);
+extern bool check_parser(const char *proposed);
+extern void assign_parser(const char *value);
#endif /* BOOTSTRAP_INCLUDE */
Re: [HACKERS] Select parser at runtime
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Ian Lance Taylor writes:
> I've been experimenting with using a different parser (one which is
> more Oracle compatible).
Hmm. What we have here is a mechanism to swap out the entire
backend/parser/ subdirectory --- and nothing else. Somehow this seems
like the wrong granularity. parser/ is an awful lot of code to replace
to make a few random tweaks that don't affect query semantics. Since
you aren't changing the querytree representation nor any of the rewrite/
plan/execute pipeline, it's hard to see how you can do more with this
than very marginal syntax hacks. But if that's all you want to do,
seems like replacing pieces of the parser/semantic analyzer is the right
mechanism, not the whole durn thing.
> Note that you may want to leave yourself an escape
> hatch of some sort to set the parser back to Postgres standard.
> If this patch is accepted, then some further work needs to be done to
> set the parser for SPI calls, so that it is possible for the user to
> change the parser while still using ordinary PL/pgSQL.
I think both of these issues would need to be addressed before, not
after, considering the patch for acceptance. In particular, how do we
cater for both plpgsql and a true "PL/SQL" language that uses the Oracle
parser?
> + {
> + Datum result;
> +
> + parser_function_fcache->fcinfo.arg[0] = PointerGetDatum(query_string);
> + parser_function_fcache->fcinfo.arg[1] = PointerGetDatum(typev);
> + parser_function_fcache->fcinfo.arg[2] = Int32GetDatum(nargs);
> - raw_parsetree_list = parser(query_string, typev, nargs);
> + result = FunctionCallInvoke(&parser_function_fcache->fcinfo);
> + raw_parsetree_list = (List *) DatumGetPointer(result);
> + }
Use FunctionCall3() to hide the cruft here.
> + fcache = init_fcache(oid, 3, TopMemoryContext);
This is a tad odd. Why don't you just do fmgr_info and store an FmgrInfo
structure? You have no use for the rest of an executor fcache
structure.
The update_parser_function_fcache business bothers me, too. I see the
problem: it doesn't work to do this lookup when postgresql.conf is read
in the postmaster. However, I really don't like the notion of disabling
check_parser and allowing a possibly-bogus value to be assigned on
faith. (If the function name *is* bogus, your code can never recover;
at the very least you ought to clear update_parser_function_fcache
before failing.) Given the extent to which the parser is necessarily
tied to the rest of the system, I'm not sure there's any value in
allowing it to be implemented as a dynamic-link function. I'd be more
than half inclined to go with a lower-tech solution wherein you expect
the alternate parser to be permanently linked in and known to the
check_parser and assign_parser subroutines. Then the state variable is
just a C function pointer, and assign_parser looks something like
#ifdef ORACLE_PARSER_SUPPORTED
if (strcasecmp(value, "Oracle") == 0)
parser_fn = oracle_parser;
else
#endif
regards, tom lane
Re: [HACKERS] Select parser at runtime
От:
Ian Lance Taylor <ian@airs.com>
Дата:
Tom Lane writes: > Ian Lance Taylor writes: > > I've been experimenting with using a different parser (one which is > > more Oracle compatible). > > Hmm. What we have here is a mechanism to swap out the entire > backend/parser/ subdirectory --- and nothing else. Somehow this seems > like the wrong granularity. parser/ is an awful lot of code to replace > to make a few random tweaks that don't affect query semantics. Since > you aren't changing the querytree representation nor any of the rewrite/ > plan/execute pipeline, it's hard to see how you can do more with this > than very marginal syntax hacks. But if that's all you want to do, > seems like replacing pieces of the parser/semantic analyzer is the right > mechanism, not the whole durn thing. This patch doesn't actually replace the entire backend/parser subdirectory. It mainly only replaces scan.l and gram.y. This is because the code in postgres.c still passes the result of the replaced parser to pg_analyze_and_rewrite(). You are absolutely correct that at this point this can only do marginal syntax hacks. But since my goal is to support Oracle syntax, marginal syntax hacks are just what is needed. For example, it's hard to tweak the existing parser to support Oracle, because the set of reserved words is different, and because Oracle uses different names for datatypes. Also, CREATE FUNCTION in Oracle doesn't use a literal string, it actually switches to a different parser while parsing the function. It's not impossible to hack these into the Postgres parser; just hard, and a complex maintenance problem. It's true that increased Oracle compatibility will require code changes in other parts of the backend. > > Note that you may want to leave yourself an escape > > hatch of some sort to set the parser back to Postgres standard. > > If this patch is accepted, then some further work needs to be done to > > set the parser for SPI calls, so that it is possible for the user to > > change the parser while still using ordinary PL/pgSQL. > > I think both of these issues would need to be addressed before, not > after, considering the patch for acceptance. In particular, how do we > cater for both plpgsql and a true "PL/SQL" language that uses the Oracle > parser? I agree that these issues need to be addressed before the patch is accepted. I want to get a sense of whether I am on the right track at all. As noted above, PL/SQL needs to be more closely tied to the Oracle parser than PL/pgSQL is to the Postgres parser. I have been thinking in terms of a parser stack. Code which uses SPI could push the correct parser on top of the stack, and pop it off when done. Perhaps SPI would always use the postgres parser unless explicitly directed otherwise. The PL/SQL language interface would direct otherwise. Thanks for your other comments. I went with a dynamic link approach because it seemed minimally intrusive. Using a straight function pointer is certainly easier. Ian
Re: [HACKERS] Select parser at runtime
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Ian Lance Taylor writes: > This patch doesn't actually replace the entire backend/parser > subdirectory. It mainly only replaces scan.l and gram.y. This is > because the code in postgres.c still passes the result of the replaced > parser to pg_analyze_and_rewrite(). Oh, of course, how silly of me. I was thinking that that call did the analyze step too, but you're correct that it does not. Okay, replacing lexer+syntaxer is a more reasonable chunk-size. (AFAIK there's no good way to replace just part of a yacc/bison grammar on the fly, so you couldn't go to a finer grain anyway, could you?) regards, tom lane