There are three parallel ways to call parse/analyze: with fixed
parameters, with variable parameters, and by supplying your own parser
callback. Some of the involved functions were confusingly named and
made this API structure more confusing. This patch renames some
functions to make this clearer:
parse_analyze() -> parse_analyze_fixedparams()
pg_analyze_and_rewrite() -> pg_analyze_and_rewrite_fixedparams()
(Otherwise one might think this variant doesn't accept parameters, but
in fact all three ways accept parameters.)
pg_analyze_and_rewrite_params() -> pg_analyze_and_rewrite_withcb()
(Before, and also when considering pg_analyze_and_rewrite(), one might
think this is the only way to pass parameters. Moreover, the parser
callback doesn't necessarily need to parse only parameters, it's just
one of the things it could do.)
parse_fixed_parameters() -> setup_parse_fixed_parameters()
parse_variable_parameters() -> setup_parse_variable_parameters()
(These functions don't actually do any parsing, they just set up
callbacks to use during parsing later.)
This patch also adds some const decorations to the fixed-parameters
API, so the distinction from the variable-parameters API is more
clear.
Reviewed-by: Nathan Bossart
Discussion: https://www.postgresql.org/message-id/flat/
c67ce276-52b4-0239-dc0e-
39875bf81840@enterprisedb.com
RawStmt *parsetree = lfirst_node(RawStmt, lc);
List *querytree_sublist;
- querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
+ querytree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
prosrc,
(ParserSetupHook) sql_fn_parser_setup,
pinfo,
* Run parse analysis and rewrite. Note this also acquires sufficient
* locks on the source table(s).
*/
- rewritten = pg_analyze_and_rewrite(raw_query,
+ rewritten = pg_analyze_and_rewrite_fixedparams(raw_query,
pstate->p_sourcetext, NULL, 0,
NULL);
/* Be sure parser can see any DDL done so far */
CommandCounterIncrement();
- stmt_list = pg_analyze_and_rewrite(parsetree,
+ stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
sql,
NULL,
0,
/*
* Execute each command contained in the CREATE SCHEMA. Since the grammar
* allows only utility commands in CREATE SCHEMA, there is no need to pass
- * them through parse_analyze() or the rewriter; we can just hand them
+ * them through parse_analyze_*() or the rewriter; we can just hand them
* straight to ProcessUtility.
*/
foreach(parsetree_item, parsetree_list)
/*
* We expect that we will get only ALTER TABLE and CREATE INDEX
* statements. Hence, there is no need to pass them through
- * parse_analyze() or the rewriter, but instead we need to pass them
+ * parse_analyze_*() or the rewriter, but instead we need to pass them
* through parse_utilcmd.c to make them ready for execution.
*/
raw_parsetree_list = raw_parser(cmd, RAW_PARSE_DEFAULT);
rawstmt->stmt_location = stmt_location;
rawstmt->stmt_len = stmt_len;
- viewParse = parse_analyze(rawstmt, queryString, NULL, 0, NULL);
+ viewParse = parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, NULL);
/*
* The grammar should ensure that the result is a single SELECT Query.
RawStmt *parsetree = lfirst_node(RawStmt, lc);
List *queryTree_sublist;
- queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
+ queryTree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
fcache->src,
(ParserSetupHook) sql_fn_parser_setup,
fcache->pinfo,
if (plan->parserSetup != NULL)
{
Assert(plan->nargs == 0);
- stmt_list = pg_analyze_and_rewrite_params(parsetree,
+ stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
src,
plan->parserSetup,
plan->parserSetupArg,
}
else
{
- stmt_list = pg_analyze_and_rewrite(parsetree,
+ stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
src,
plan->argtypes,
plan->nargs,
else if (plan->parserSetup != NULL)
{
Assert(plan->nargs == 0);
- stmt_list = pg_analyze_and_rewrite_params(parsetree,
+ stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
src,
plan->parserSetup,
plan->parserSetupArg,
}
else
{
- stmt_list = pg_analyze_and_rewrite(parsetree,
+ stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
src,
plan->argtypes,
plan->nargs,
if (list_length(raw_parsetree_list) != 1)
goto fail;
- querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
+ querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
src,
(ParserSetupHook) sql_fn_parser_setup,
pinfo, NULL);
/*
- * parse_analyze
+ * parse_analyze_fixedparams
* Analyze a raw parse tree and transform it to Query form.
*
* Optionally, information about $n parameter types can be supplied.
* a dummy CMD_UTILITY Query node.
*/
Query *
-parse_analyze(RawStmt *parseTree, const char *sourceText,
- Oid *paramTypes, int numParams,
+parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
+ const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv)
{
ParseState *pstate = make_parsestate(NULL);
pstate->p_sourcetext = sourceText;
if (numParams > 0)
- parse_fixed_parameters(pstate, paramTypes, numParams);
+ setup_parse_fixed_parameters(pstate, paramTypes, numParams);
pstate->p_queryEnv = queryEnv;
pstate->p_sourcetext = sourceText;
- parse_variable_parameters(pstate, paramTypes, numParams);
+ setup_parse_variable_parameters(pstate, paramTypes, numParams);
query = transformTopLevelStmt(pstate, parseTree);
typedef struct FixedParamState
{
- Oid *paramTypes; /* array of parameter type OIDs */
+ const Oid *paramTypes; /* array of parameter type OIDs */
int numParams; /* number of array entries */
} FixedParamState;
* Set up to process a query containing references to fixed parameters.
*/
void
-parse_fixed_parameters(ParseState *pstate,
- Oid *paramTypes, int numParams)
+setup_parse_fixed_parameters(ParseState *pstate,
+ const Oid *paramTypes, int numParams)
{
FixedParamState *parstate = palloc(sizeof(FixedParamState));
* Set up to process a query containing references to variable parameters.
*/
void
-parse_variable_parameters(ParseState *pstate,
+setup_parse_variable_parameters(ParseState *pstate,
Oid **paramTypes, int *numParams)
{
VarParamState *parstate = palloc(sizeof(VarParamState));
* parse_utilcmd.c
* Perform parse analysis work for various utility commands
*
- * Formerly we did this work during parse_analyze() in analyze.c. However
+ * Formerly we did this work during parse_analyze_*() in analyze.c. However
* that is fairly unsafe in the presence of querytree caching, since any
* database state that we depend on in making the transformations might be
* obsolete by the time the utility command is executed; and utility commands
* NOTE: for reasons mentioned above, this must be separate from raw parsing.
*/
List *
-pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
- Oid *paramTypes, int numParams,
+pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string,
+ const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv)
{
Query *query;
if (log_parser_stats)
ResetUsage();
- query = parse_analyze(parsetree, query_string, paramTypes, numParams,
+ query = parse_analyze_fixedparams(parsetree, query_string, paramTypes, numParams,
queryEnv);
if (log_parser_stats)
}
/*
- * Do parse analysis and rewriting. This is the same as pg_analyze_and_rewrite
- * except that external-parameter resolution is determined by parser callback
- * hooks instead of a fixed list of parameter datatypes.
+ * Do parse analysis and rewriting. This is the same as
+ * pg_analyze_and_rewrite_fixedparams except that, instead of a fixed list of
+ * parameter datatypes, a parser callback is supplied that can do
+ * external-parameter resolution and possibly other things.
*/
List *
-pg_analyze_and_rewrite_params(RawStmt *parsetree,
+pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
const char *query_string,
ParserSetupHook parserSetup,
void *parserSetupArg,
else
oldcontext = MemoryContextSwitchTo(MessageContext);
- querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
+ querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree, query_string,
NULL, 0, NULL);
plantree_list = pg_plan_queries(querytree_list, query_string,
if (rawtree == NULL)
tlist = NIL;
else if (plansource->parserSetup != NULL)
- tlist = pg_analyze_and_rewrite_params(rawtree,
+ tlist = pg_analyze_and_rewrite_withcb(rawtree,
plansource->query_string,
plansource->parserSetup,
plansource->parserSetupArg,
queryEnv);
else
- tlist = pg_analyze_and_rewrite(rawtree,
+ tlist = pg_analyze_and_rewrite_fixedparams(rawtree,
plansource->query_string,
plansource->param_types,
plansource->num_params,
extern PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook;
-extern Query *parse_analyze(RawStmt *parseTree, const char *sourceText,
- Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
+extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
+ const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
extern Query *parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
Oid **paramTypes, int *numParams);
#include "parser/parse_node.h"
-extern void parse_fixed_parameters(ParseState *pstate,
- Oid *paramTypes, int numParams);
-extern void parse_variable_parameters(ParseState *pstate,
+extern void setup_parse_fixed_parameters(ParseState *pstate,
+ const Oid *paramTypes, int numParams);
+extern void setup_parse_variable_parameters(ParseState *pstate,
Oid **paramTypes, int *numParams);
extern void check_variable_parameters(ParseState *pstate, Query *query);
extern bool query_contains_extern_params(Query *query);
extern List *pg_parse_query(const char *query_string);
extern List *pg_rewrite_query(Query *query);
-extern List *pg_analyze_and_rewrite(RawStmt *parsetree,
+extern List *pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree,
const char *query_string,
- Oid *paramTypes, int numParams,
+ const Oid *paramTypes, int numParams,
QueryEnvironment *queryEnv);
-extern List *pg_analyze_and_rewrite_params(RawStmt *parsetree,
+extern List *pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
const char *query_string,
ParserSetupHook parserSetup,
void *parserSetupArg,