Parse/analyze function renaming
authorPeter Eisentraut
Fri, 4 Mar 2022 13:49:37 +0000 (14:49 +0100)
committerPeter Eisentraut
Fri, 4 Mar 2022 13:50:22 +0000 (14:50 +0100)
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

17 files changed:
src/backend/catalog/pg_proc.c
src/backend/commands/copyto.c
src/backend/commands/extension.c
src/backend/commands/schemacmds.c
src/backend/commands/tablecmds.c
src/backend/commands/view.c
src/backend/executor/functions.c
src/backend/executor/spi.c
src/backend/optimizer/util/clauses.c
src/backend/parser/analyze.c
src/backend/parser/parse_param.c
src/backend/parser/parse_utilcmd.c
src/backend/tcop/postgres.c
src/backend/utils/cache/plancache.c
src/include/parser/analyze.h
src/include/parser/parse_param.h
src/include/tcop/tcopprot.h

index 12521c77c3cc457ef2af9c059349e23cd582d1e1..ac8aacbd59136b6915f469fdb3003adcc283f1df 100644 (file)
@@ -947,7 +947,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
                    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,
index 3283ef50d06374b5164e6c6f6c52e873bed297f3..55c38b04c45c5f7b58f5c2010731af4acc12f273 100644 (file)
@@ -439,7 +439,7 @@ BeginCopyTo(ParseState *pstate,
         * 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);
 
index 0e04304cb091554128e9eb5d1daeab6ba7e62578..42503ef4543d72cc3791162c3350b520ed0b9e7d 100644 (file)
@@ -757,7 +757,7 @@ execute_sql_string(const char *sql)
        /* 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,
index 984000a5bc9654e22a8af22e10a441a2601e973a..be3925b3b454ca5f28e2e66aeff70fdc7b2539b9 100644 (file)
@@ -172,7 +172,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
    /*
     * 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)
index 3e83f375b553e0add5071cfd54e29d15cc6a0070..dc5872f988c96cd76c7dcf01b7961457dc7f664f 100644 (file)
@@ -13181,7 +13181,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd,
    /*
     * 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);
index 459e9821d08142fb66f901e554c6a894b528d3bf..8690a3f3c6491ee87651a348813806364434c2e1 100644 (file)
@@ -439,7 +439,7 @@ DefineView(ViewStmt *stmt, const char *queryString,
    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.
index 29a68879eede7de5227aabeb1415a1a0faeded23..f9460ae506c05a246879ee01c2d12b4fb40527f1 100644 (file)
@@ -718,7 +718,7 @@ init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
            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,
index 5b353cb93a7652ce29e5cbc2003d3356a11224e7..a82e986667041f57a118890b67ce3e96bd855947 100644 (file)
@@ -2258,7 +2258,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
        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,
@@ -2266,7 +2266,7 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
        }
        else
        {
-           stmt_list = pg_analyze_and_rewrite(parsetree,
+           stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                                               src,
                                               plan->argtypes,
                                               plan->nargs,
@@ -2495,7 +2495,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
            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,
@@ -2503,7 +2503,7 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
            }
            else
            {
-               stmt_list = pg_analyze_and_rewrite(parsetree,
+               stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
                                                   src,
                                                   plan->argtypes,
                                                   plan->nargs,
index a707dc9f26ae92ab6af7cbd2cc829d5ce68fdd6d..413dcac0363aa32fd56d571238439a9c8857dde2 100644 (file)
@@ -5057,7 +5057,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
        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);
index 6ac2e9ce2373c5fba060315e63b78de3bd570a15..19d97fe731be1e2a5f6af5c91c82a91894c037e7 100644 (file)
@@ -96,7 +96,7 @@ static bool test_raw_expression_coverage(Node *node, void *context);
 
 
 /*
- * 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.
@@ -107,8 +107,8 @@ static bool test_raw_expression_coverage(Node *node, void *context);
  * 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);
@@ -120,7 +120,7 @@ parse_analyze(RawStmt *parseTree, const char *sourceText,
    pstate->p_sourcetext = sourceText;
 
    if (numParams > 0)
-       parse_fixed_parameters(pstate, paramTypes, numParams);
+       setup_parse_fixed_parameters(pstate, paramTypes, numParams);
 
    pstate->p_queryEnv = queryEnv;
 
@@ -158,7 +158,7 @@ parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
 
    pstate->p_sourcetext = sourceText;
 
-   parse_variable_parameters(pstate, paramTypes, numParams);
+   setup_parse_variable_parameters(pstate, paramTypes, numParams);
 
    query = transformTopLevelStmt(pstate, parseTree);
 
index 3100d890d21e408dacadf8a9139233e00c7596bb..31a43e034c64448a9d7bb9f66b8ed102e5a59746 100644 (file)
@@ -35,7 +35,7 @@
 
 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;
 
@@ -64,8 +64,8 @@ static bool query_contains_extern_params_walker(Node *node, void *context);
  * 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));
 
@@ -80,7 +80,7 @@ parse_fixed_parameters(ParseState *pstate,
  * 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));
index 99efa26ce4a52fc406fb63ceac97caba2c239ebe..cd946c7692127eb0b57ae96b8226c6ff456087dc 100644 (file)
@@ -3,7 +3,7 @@
  * 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
index 34c13a11138c5c7ccccbff25ae1ffa17fc28ad78..c087db4445696dae854fc239fa7f60c26e3d2f31 100644 (file)
@@ -637,8 +637,8 @@ pg_parse_query(const char *query_string)
  * 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;
@@ -652,7 +652,7 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
    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)
@@ -669,12 +669,13 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
 }
 
 /*
- * 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,
@@ -1125,7 +1126,7 @@ exec_simple_query(const char *query_string)
        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,
index 4a9055e6bb5ced720df7c827a6ad7cfb8f4d4644..4cf6db504ff7d8d8e4eab89507ed3823a1e738fb 100644 (file)
@@ -682,13 +682,13 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
    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,
index 0022184de03b30a593791ff7ad5b097807d392d1..ed989bb1413a6a4be03a9d25e7adc10b33173ec2 100644 (file)
@@ -24,8 +24,8 @@ typedef void (*post_parse_analyze_hook_type) (ParseState *pstate,
 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);
 
index 18e608093cf62a069d6a30a884527956ef949e66..d6f0b656495ab908de8f0eeca9b9b999976668e9 100644 (file)
@@ -15,9 +15,9 @@
 
 #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);
index 15a11bc3ff111bd5c205180f7d0acac0802b698f..00c20966ab89db4ecfbeb8842a8ac799b9cc88f9 100644 (file)
@@ -45,11 +45,11 @@ extern PGDLLIMPORT int log_statement;
 
 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,