mirror of
https://github.com/postgres/postgres.git
synced 2025-06-17 17:02:08 +03:00
Add pg_analyze_and_rewrite_varparams()
This new function extracts common code from PrepareQuery() and exec_parse_message(). It is then exactly analogous to the existing pg_analyze_and_rewrite_fixedparams() and pg_analyze_and_rewrite_withcb(). To unify these two code paths, this makes PrepareQuery() now subject to log_parser_stats. Also, both paths now invoke TRACE_POSTGRESQL_QUERY_REWRITE_START(). PrepareQuery() no longer checks whether a utility statement was specified. The grammar doesn't allow that anyway, and exec_parse_message() supports it, so restricting it doesn't seem necessary. This also adds QueryEnvironment support to the *varparams functions, for consistency with its cousins, even though it is not used right now. Reviewed-by: Nathan Bossart <bossartn@amazon.com> Discussion: https://www.postgresql.org/message-id/flat/c67ce276-52b4-0239-dc0e-39875bf81840@enterprisedb.com
This commit is contained in:
@ -637,9 +637,11 @@ pg_parse_query(const char *query_string)
|
||||
* NOTE: for reasons mentioned above, this must be separate from raw parsing.
|
||||
*/
|
||||
List *
|
||||
pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string,
|
||||
const Oid *paramTypes, int numParams,
|
||||
QueryEnvironment *queryEnv)
|
||||
pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree,
|
||||
const char *query_string,
|
||||
const Oid *paramTypes,
|
||||
int numParams,
|
||||
QueryEnvironment *queryEnv)
|
||||
{
|
||||
Query *query;
|
||||
List *querytree_list;
|
||||
@ -668,6 +670,59 @@ pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string,
|
||||
return querytree_list;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do parse analysis and rewriting. This is the same as
|
||||
* pg_analyze_and_rewrite_fixedparams except that it's okay to deduce
|
||||
* information about $n symbol datatypes from context.
|
||||
*/
|
||||
List *
|
||||
pg_analyze_and_rewrite_varparams(RawStmt *parsetree,
|
||||
const char *query_string,
|
||||
Oid **paramTypes,
|
||||
int *numParams,
|
||||
QueryEnvironment *queryEnv)
|
||||
{
|
||||
Query *query;
|
||||
List *querytree_list;
|
||||
|
||||
TRACE_POSTGRESQL_QUERY_REWRITE_START(query_string);
|
||||
|
||||
/*
|
||||
* (1) Perform parse analysis.
|
||||
*/
|
||||
if (log_parser_stats)
|
||||
ResetUsage();
|
||||
|
||||
query = parse_analyze_varparams(parsetree, query_string, paramTypes, numParams,
|
||||
queryEnv);
|
||||
|
||||
/*
|
||||
* Check all parameter types got determined.
|
||||
*/
|
||||
for (int i = 0; i < *numParams; i++)
|
||||
{
|
||||
Oid ptype = (*paramTypes)[i];
|
||||
|
||||
if (ptype == InvalidOid || ptype == UNKNOWNOID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INDETERMINATE_DATATYPE),
|
||||
errmsg("could not determine data type of parameter $%d",
|
||||
i + 1)));
|
||||
}
|
||||
|
||||
if (log_parser_stats)
|
||||
ShowUsage("PARSE ANALYSIS STATISTICS");
|
||||
|
||||
/*
|
||||
* (2) Rewrite the queries, as necessary
|
||||
*/
|
||||
querytree_list = pg_rewrite_query(query);
|
||||
|
||||
TRACE_POSTGRESQL_QUERY_REWRITE_DONE(query_string);
|
||||
|
||||
return querytree_list;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do parse analysis and rewriting. This is the same as
|
||||
* pg_analyze_and_rewrite_fixedparams except that, instead of a fixed list of
|
||||
@ -1409,7 +1464,6 @@ exec_parse_message(const char *query_string, /* string to execute */
|
||||
|
||||
if (parsetree_list != NIL)
|
||||
{
|
||||
Query *query;
|
||||
bool snapshot_set = false;
|
||||
|
||||
raw_parse_tree = linitial_node(RawStmt, parsetree_list);
|
||||
@ -1449,34 +1503,13 @@ exec_parse_message(const char *query_string, /* string to execute */
|
||||
/*
|
||||
* Analyze and rewrite the query. Note that the originally specified
|
||||
* parameter set is not required to be complete, so we have to use
|
||||
* parse_analyze_varparams().
|
||||
* pg_analyze_and_rewrite_varparams().
|
||||
*/
|
||||
if (log_parser_stats)
|
||||
ResetUsage();
|
||||
|
||||
query = parse_analyze_varparams(raw_parse_tree,
|
||||
query_string,
|
||||
¶mTypes,
|
||||
&numParams);
|
||||
|
||||
/*
|
||||
* Check all parameter types got determined.
|
||||
*/
|
||||
for (int i = 0; i < numParams; i++)
|
||||
{
|
||||
Oid ptype = paramTypes[i];
|
||||
|
||||
if (ptype == InvalidOid || ptype == UNKNOWNOID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INDETERMINATE_DATATYPE),
|
||||
errmsg("could not determine data type of parameter $%d",
|
||||
i + 1)));
|
||||
}
|
||||
|
||||
if (log_parser_stats)
|
||||
ShowUsage("PARSE ANALYSIS STATISTICS");
|
||||
|
||||
querytree_list = pg_rewrite_query(query);
|
||||
querytree_list = pg_analyze_and_rewrite_varparams(raw_parse_tree,
|
||||
query_string,
|
||||
¶mTypes,
|
||||
&numParams,
|
||||
NULL);
|
||||
|
||||
/* Done with the snapshot used for parsing */
|
||||
if (snapshot_set)
|
||||
|
Reference in New Issue
Block a user