Skip to content

Commit 791b1b7

Browse files
committed
Parse/analyze function renaming
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 <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent d816f36 commit 791b1b7

File tree

17 files changed

+41
-40
lines changed

17 files changed

+41
-40
lines changed

src/backend/catalog/pg_proc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
947947
RawStmt *parsetree = lfirst_node(RawStmt, lc);
948948
List *querytree_sublist;
949949

950-
querytree_sublist = pg_analyze_and_rewrite_params(parsetree,
950+
querytree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
951951
prosrc,
952952
(ParserSetupHook) sql_fn_parser_setup,
953953
pinfo,

src/backend/commands/copyto.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ BeginCopyTo(ParseState *pstate,
439439
* Run parse analysis and rewrite. Note this also acquires sufficient
440440
* locks on the source table(s).
441441
*/
442-
rewritten = pg_analyze_and_rewrite(raw_query,
442+
rewritten = pg_analyze_and_rewrite_fixedparams(raw_query,
443443
pstate->p_sourcetext, NULL, 0,
444444
NULL);
445445

src/backend/commands/extension.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ execute_sql_string(const char *sql)
757757
/* Be sure parser can see any DDL done so far */
758758
CommandCounterIncrement();
759759

760-
stmt_list = pg_analyze_and_rewrite(parsetree,
760+
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
761761
sql,
762762
NULL,
763763
0,

src/backend/commands/schemacmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
172172
/*
173173
* Execute each command contained in the CREATE SCHEMA. Since the grammar
174174
* allows only utility commands in CREATE SCHEMA, there is no need to pass
175-
* them through parse_analyze() or the rewriter; we can just hand them
175+
* them through parse_analyze_*() or the rewriter; we can just hand them
176176
* straight to ProcessUtility.
177177
*/
178178
foreach(parsetree_item, parsetree_list)

src/backend/commands/tablecmds.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13181,7 +13181,7 @@ ATPostAlterTypeParse(Oid oldId, Oid oldRelId, Oid refRelId, char *cmd,
1318113181
/*
1318213182
* We expect that we will get only ALTER TABLE and CREATE INDEX
1318313183
* statements. Hence, there is no need to pass them through
13184-
* parse_analyze() or the rewriter, but instead we need to pass them
13184+
* parse_analyze_*() or the rewriter, but instead we need to pass them
1318513185
* through parse_utilcmd.c to make them ready for execution.
1318613186
*/
1318713187
raw_parsetree_list = raw_parser(cmd, RAW_PARSE_DEFAULT);

src/backend/commands/view.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ DefineView(ViewStmt *stmt, const char *queryString,
439439
rawstmt->stmt_location = stmt_location;
440440
rawstmt->stmt_len = stmt_len;
441441

442-
viewParse = parse_analyze(rawstmt, queryString, NULL, 0, NULL);
442+
viewParse = parse_analyze_fixedparams(rawstmt, queryString, NULL, 0, NULL);
443443

444444
/*
445445
* The grammar should ensure that the result is a single SELECT Query.

src/backend/executor/functions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
718718
RawStmt *parsetree = lfirst_node(RawStmt, lc);
719719
List *queryTree_sublist;
720720

721-
queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
721+
queryTree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
722722
fcache->src,
723723
(ParserSetupHook) sql_fn_parser_setup,
724724
fcache->pinfo,

src/backend/executor/spi.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -2258,15 +2258,15 @@ _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
22582258
if (plan->parserSetup != NULL)
22592259
{
22602260
Assert(plan->nargs == 0);
2261-
stmt_list = pg_analyze_and_rewrite_params(parsetree,
2261+
stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
22622262
src,
22632263
plan->parserSetup,
22642264
plan->parserSetupArg,
22652265
_SPI_current->queryEnv);
22662266
}
22672267
else
22682268
{
2269-
stmt_list = pg_analyze_and_rewrite(parsetree,
2269+
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
22702270
src,
22712271
plan->argtypes,
22722272
plan->nargs,
@@ -2495,15 +2495,15 @@ _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options,
24952495
else if (plan->parserSetup != NULL)
24962496
{
24972497
Assert(plan->nargs == 0);
2498-
stmt_list = pg_analyze_and_rewrite_params(parsetree,
2498+
stmt_list = pg_analyze_and_rewrite_withcb(parsetree,
24992499
src,
25002500
plan->parserSetup,
25012501
plan->parserSetupArg,
25022502
_SPI_current->queryEnv);
25032503
}
25042504
else
25052505
{
2506-
stmt_list = pg_analyze_and_rewrite(parsetree,
2506+
stmt_list = pg_analyze_and_rewrite_fixedparams(parsetree,
25072507
src,
25082508
plan->argtypes,
25092509
plan->nargs,

src/backend/optimizer/util/clauses.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5057,7 +5057,7 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
50575057
if (list_length(raw_parsetree_list) != 1)
50585058
goto fail;
50595059

5060-
querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
5060+
querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
50615061
src,
50625062
(ParserSetupHook) sql_fn_parser_setup,
50635063
pinfo, NULL);

src/backend/parser/analyze.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static bool test_raw_expression_coverage(Node *node, void *context);
9696

9797

9898
/*
99-
* parse_analyze
99+
* parse_analyze_fixedparams
100100
* Analyze a raw parse tree and transform it to Query form.
101101
*
102102
* Optionally, information about $n parameter types can be supplied.
@@ -107,8 +107,8 @@ static bool test_raw_expression_coverage(Node *node, void *context);
107107
* a dummy CMD_UTILITY Query node.
108108
*/
109109
Query *
110-
parse_analyze(RawStmt *parseTree, const char *sourceText,
111-
Oid *paramTypes, int numParams,
110+
parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
111+
const Oid *paramTypes, int numParams,
112112
QueryEnvironment *queryEnv)
113113
{
114114
ParseState *pstate = make_parsestate(NULL);
@@ -120,7 +120,7 @@ parse_analyze(RawStmt *parseTree, const char *sourceText,
120120
pstate->p_sourcetext = sourceText;
121121

122122
if (numParams > 0)
123-
parse_fixed_parameters(pstate, paramTypes, numParams);
123+
setup_parse_fixed_parameters(pstate, paramTypes, numParams);
124124

125125
pstate->p_queryEnv = queryEnv;
126126

@@ -158,7 +158,7 @@ parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
158158

159159
pstate->p_sourcetext = sourceText;
160160

161-
parse_variable_parameters(pstate, paramTypes, numParams);
161+
setup_parse_variable_parameters(pstate, paramTypes, numParams);
162162

163163
query = transformTopLevelStmt(pstate, parseTree);
164164

src/backend/parser/parse_param.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
typedef struct FixedParamState
3737
{
38-
Oid *paramTypes; /* array of parameter type OIDs */
38+
const Oid *paramTypes; /* array of parameter type OIDs */
3939
int numParams; /* number of array entries */
4040
} FixedParamState;
4141

@@ -64,8 +64,8 @@ static bool query_contains_extern_params_walker(Node *node, void *context);
6464
* Set up to process a query containing references to fixed parameters.
6565
*/
6666
void
67-
parse_fixed_parameters(ParseState *pstate,
68-
Oid *paramTypes, int numParams)
67+
setup_parse_fixed_parameters(ParseState *pstate,
68+
const Oid *paramTypes, int numParams)
6969
{
7070
FixedParamState *parstate = palloc(sizeof(FixedParamState));
7171

@@ -80,7 +80,7 @@ parse_fixed_parameters(ParseState *pstate,
8080
* Set up to process a query containing references to variable parameters.
8181
*/
8282
void
83-
parse_variable_parameters(ParseState *pstate,
83+
setup_parse_variable_parameters(ParseState *pstate,
8484
Oid **paramTypes, int *numParams)
8585
{
8686
VarParamState *parstate = palloc(sizeof(VarParamState));

src/backend/parser/parse_utilcmd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* parse_utilcmd.c
44
* Perform parse analysis work for various utility commands
55
*
6-
* Formerly we did this work during parse_analyze() in analyze.c. However
6+
* Formerly we did this work during parse_analyze_*() in analyze.c. However
77
* that is fairly unsafe in the presence of querytree caching, since any
88
* database state that we depend on in making the transformations might be
99
* obsolete by the time the utility command is executed; and utility commands

src/backend/tcop/postgres.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ pg_parse_query(const char *query_string)
637637
* NOTE: for reasons mentioned above, this must be separate from raw parsing.
638638
*/
639639
List *
640-
pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
641-
Oid *paramTypes, int numParams,
640+
pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree, const char *query_string,
641+
const Oid *paramTypes, int numParams,
642642
QueryEnvironment *queryEnv)
643643
{
644644
Query *query;
@@ -652,7 +652,7 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
652652
if (log_parser_stats)
653653
ResetUsage();
654654

655-
query = parse_analyze(parsetree, query_string, paramTypes, numParams,
655+
query = parse_analyze_fixedparams(parsetree, query_string, paramTypes, numParams,
656656
queryEnv);
657657

658658
if (log_parser_stats)
@@ -669,12 +669,13 @@ pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string,
669669
}
670670

671671
/*
672-
* Do parse analysis and rewriting. This is the same as pg_analyze_and_rewrite
673-
* except that external-parameter resolution is determined by parser callback
674-
* hooks instead of a fixed list of parameter datatypes.
672+
* Do parse analysis and rewriting. This is the same as
673+
* pg_analyze_and_rewrite_fixedparams except that, instead of a fixed list of
674+
* parameter datatypes, a parser callback is supplied that can do
675+
* external-parameter resolution and possibly other things.
675676
*/
676677
List *
677-
pg_analyze_and_rewrite_params(RawStmt *parsetree,
678+
pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
678679
const char *query_string,
679680
ParserSetupHook parserSetup,
680681
void *parserSetupArg,
@@ -1125,7 +1126,7 @@ exec_simple_query(const char *query_string)
11251126
else
11261127
oldcontext = MemoryContextSwitchTo(MessageContext);
11271128

1128-
querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
1129+
querytree_list = pg_analyze_and_rewrite_fixedparams(parsetree, query_string,
11291130
NULL, 0, NULL);
11301131

11311132
plantree_list = pg_plan_queries(querytree_list, query_string,

src/backend/utils/cache/plancache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,13 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
682682
if (rawtree == NULL)
683683
tlist = NIL;
684684
else if (plansource->parserSetup != NULL)
685-
tlist = pg_analyze_and_rewrite_params(rawtree,
685+
tlist = pg_analyze_and_rewrite_withcb(rawtree,
686686
plansource->query_string,
687687
plansource->parserSetup,
688688
plansource->parserSetupArg,
689689
queryEnv);
690690
else
691-
tlist = pg_analyze_and_rewrite(rawtree,
691+
tlist = pg_analyze_and_rewrite_fixedparams(rawtree,
692692
plansource->query_string,
693693
plansource->param_types,
694694
plansource->num_params,

src/include/parser/analyze.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ typedef void (*post_parse_analyze_hook_type) (ParseState *pstate,
2424
extern PGDLLIMPORT post_parse_analyze_hook_type post_parse_analyze_hook;
2525

2626

27-
extern Query *parse_analyze(RawStmt *parseTree, const char *sourceText,
28-
Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
27+
extern Query *parse_analyze_fixedparams(RawStmt *parseTree, const char *sourceText,
28+
const Oid *paramTypes, int numParams, QueryEnvironment *queryEnv);
2929
extern Query *parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
3030
Oid **paramTypes, int *numParams);
3131

src/include/parser/parse_param.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
#include "parser/parse_node.h"
1717

18-
extern void parse_fixed_parameters(ParseState *pstate,
19-
Oid *paramTypes, int numParams);
20-
extern void parse_variable_parameters(ParseState *pstate,
18+
extern void setup_parse_fixed_parameters(ParseState *pstate,
19+
const Oid *paramTypes, int numParams);
20+
extern void setup_parse_variable_parameters(ParseState *pstate,
2121
Oid **paramTypes, int *numParams);
2222
extern void check_variable_parameters(ParseState *pstate, Query *query);
2323
extern bool query_contains_extern_params(Query *query);

src/include/tcop/tcopprot.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ extern PGDLLIMPORT int log_statement;
4545

4646
extern List *pg_parse_query(const char *query_string);
4747
extern List *pg_rewrite_query(Query *query);
48-
extern List *pg_analyze_and_rewrite(RawStmt *parsetree,
48+
extern List *pg_analyze_and_rewrite_fixedparams(RawStmt *parsetree,
4949
const char *query_string,
50-
Oid *paramTypes, int numParams,
50+
const Oid *paramTypes, int numParams,
5151
QueryEnvironment *queryEnv);
52-
extern List *pg_analyze_and_rewrite_params(RawStmt *parsetree,
52+
extern List *pg_analyze_and_rewrite_withcb(RawStmt *parsetree,
5353
const char *query_string,
5454
ParserSetupHook parserSetup,
5555
void *parserSetupArg,

0 commit comments

Comments
 (0)