Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6aba63e

Browse files
committed
Allow the planner-related functions and hook to accept the query string.
This commit adds query_string argument into the planner-related functions and hook and allows us to pass the query string to them. Currently there is no user of the query string passed. But the upcoming patch for the planning counters will add the planning hook function into pg_stat_statements and the function will need the query string. So this change will be necessary for that patch. Also this change is useful for some extensions that want to use the query string in their planner hook function. Author: Pascal Legrand, Julien Rouhaud Reviewed-by: Yoshikazu Imai, Tom Lane, Fujii Masao Discussion: https://postgr.es/m/CAOBaU_bU1m3_XF5qKYtSj1ua4dxd=FWDyh2SH4rSJAUUfsGmAQ@mail.gmail.com Discussion: https://postgr.es/m/1583789487074-0.post@n3.nabble.com
1 parent 4a539a2 commit 6aba63e

File tree

13 files changed

+34
-20
lines changed

13 files changed

+34
-20
lines changed

src/backend/commands/copy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,8 @@ BeginCopy(ParseState *pstate,
15801580
}
15811581

15821582
/* plan the query */
1583-
plan = pg_plan_query(query, CURSOR_OPT_PARALLEL_OK, NULL);
1583+
plan = pg_plan_query(query, pstate->p_sourcetext,
1584+
CURSOR_OPT_PARALLEL_OK, NULL);
15841585

15851586
/*
15861587
* With row level security and a user using "COPY relation TO", we

src/backend/commands/createas.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
329329
Assert(query->commandType == CMD_SELECT);
330330

331331
/* plan the query */
332-
plan = pg_plan_query(query, CURSOR_OPT_PARALLEL_OK, params);
332+
plan = pg_plan_query(query, pstate->p_sourcetext,
333+
CURSOR_OPT_PARALLEL_OK, params);
333334

334335
/*
335336
* Use a snapshot with an updated command ID to ensure this query sees

src/backend/commands/explain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ ExplainOneQuery(Query *query, int cursorOptions,
376376
INSTR_TIME_SET_CURRENT(planstart);
377377

378378
/* plan the query */
379-
plan = pg_plan_query(query, cursorOptions, params);
379+
plan = pg_plan_query(query, queryString, cursorOptions, params);
380380

381381
INSTR_TIME_SET_CURRENT(planduration);
382382
INSTR_TIME_SUBTRACT(planduration, planstart);

src/backend/commands/extension.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ execute_sql_string(const char *sql)
751751
NULL,
752752
0,
753753
NULL);
754-
stmt_list = pg_plan_queries(stmt_list, CURSOR_OPT_PARALLEL_OK, NULL);
754+
stmt_list = pg_plan_queries(stmt_list, sql, CURSOR_OPT_PARALLEL_OK, NULL);
755755

756756
foreach(lc2, stmt_list)
757757
{

src/backend/commands/matview.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
391391
CHECK_FOR_INTERRUPTS();
392392

393393
/* Plan the query which will generate data for the refresh. */
394-
plan = pg_plan_query(query, 0, NULL);
394+
plan = pg_plan_query(query, queryString, 0, NULL);
395395

396396
/*
397397
* Use a snapshot with an updated command ID to ensure this query sees

src/backend/commands/portalcmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
9090
elog(ERROR, "non-SELECT statement in DECLARE CURSOR");
9191

9292
/* Plan the query, applying the specified options */
93-
plan = pg_plan_query(query, cstmt->options, params);
93+
plan = pg_plan_query(query, pstate->p_sourcetext, cstmt->options, params);
9494

9595
/*
9696
* Create a portal and copy the plan and query string into its memory.

src/backend/executor/functions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ init_execution_state(List *queryTree_list,
510510
}
511511
else
512512
stmt = pg_plan_query(queryTree,
513+
fcache->src,
513514
CURSOR_OPT_PARALLEL_OK,
514515
NULL);
515516

src/backend/optimizer/plan/planner.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,21 @@ static int common_prefix_cmp(const void *a, const void *b);
264264
*
265265
*****************************************************************************/
266266
PlannedStmt *
267-
planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
267+
planner(Query *parse, const char *query_string, int cursorOptions,
268+
ParamListInfo boundParams)
268269
{
269270
PlannedStmt *result;
270271

271272
if (planner_hook)
272-
result = (*planner_hook) (parse, cursorOptions, boundParams);
273+
result = (*planner_hook) (parse, query_string, cursorOptions, boundParams);
273274
else
274-
result = standard_planner(parse, cursorOptions, boundParams);
275+
result = standard_planner(parse, query_string, cursorOptions, boundParams);
275276
return result;
276277
}
277278

278279
PlannedStmt *
279-
standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
280+
standard_planner(Query *parse, const char *query_string, int cursorOptions,
281+
ParamListInfo boundParams)
280282
{
281283
PlannedStmt *result;
282284
PlannerGlobal *glob;

src/backend/tcop/postgres.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ pg_rewrite_query(Query *query)
854854
* This is a thin wrapper around planner() and takes the same parameters.
855855
*/
856856
PlannedStmt *
857-
pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
857+
pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
858+
ParamListInfo boundParams)
858859
{
859860
PlannedStmt *plan;
860861

@@ -871,7 +872,7 @@ pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
871872
ResetUsage();
872873

873874
/* call the optimizer */
874-
plan = planner(querytree, cursorOptions, boundParams);
875+
plan = planner(querytree, query_string, cursorOptions, boundParams);
875876

876877
if (log_planner_stats)
877878
ShowUsage("PLANNER STATISTICS");
@@ -939,7 +940,8 @@ pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
939940
* The result is a list of PlannedStmt nodes.
940941
*/
941942
List *
942-
pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams)
943+
pg_plan_queries(List *querytrees, const char *query_string, int cursorOptions,
944+
ParamListInfo boundParams)
943945
{
944946
List *stmt_list = NIL;
945947
ListCell *query_list;
@@ -961,7 +963,8 @@ pg_plan_queries(List *querytrees, int cursorOptions, ParamListInfo boundParams)
961963
}
962964
else
963965
{
964-
stmt = pg_plan_query(query, cursorOptions, boundParams);
966+
stmt = pg_plan_query(query, query_string, cursorOptions,
967+
boundParams);
965968
}
966969

967970
stmt_list = lappend(stmt_list, stmt);
@@ -1152,7 +1155,7 @@ exec_simple_query(const char *query_string)
11521155
querytree_list = pg_analyze_and_rewrite(parsetree, query_string,
11531156
NULL, 0, NULL);
11541157

1155-
plantree_list = pg_plan_queries(querytree_list,
1158+
plantree_list = pg_plan_queries(querytree_list, query_string,
11561159
CURSOR_OPT_PARALLEL_OK, NULL);
11571160

11581161
/*

src/backend/utils/cache/plancache.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,8 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
930930
/*
931931
* Generate the plan.
932932
*/
933-
plist = pg_plan_queries(qlist, plansource->cursor_options, boundParams);
933+
plist = pg_plan_queries(qlist, plansource->query_string,
934+
plansource->cursor_options, boundParams);
934935

935936
/* Release snapshot if we got one */
936937
if (snapshot_set)

src/include/optimizer/optimizer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ typedef enum
102102
extern int force_parallel_mode;
103103
extern bool parallel_leader_participation;
104104

105-
extern struct PlannedStmt *planner(Query *parse, int cursorOptions,
105+
extern struct PlannedStmt *planner(Query *parse, const char *query_string,
106+
int cursorOptions,
106107
struct ParamListInfoData *boundParams);
107108

108109
extern Expr *expression_planner(Expr *expr);

src/include/optimizer/planner.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/* Hook for plugins to get control in planner() */
2626
typedef PlannedStmt *(*planner_hook_type) (Query *parse,
27+
const char *query_string,
2728
int cursorOptions,
2829
ParamListInfo boundParams);
2930
extern PGDLLIMPORT planner_hook_type planner_hook;
@@ -37,7 +38,8 @@ typedef void (*create_upper_paths_hook_type) (PlannerInfo *root,
3738
extern PGDLLIMPORT create_upper_paths_hook_type create_upper_paths_hook;
3839

3940

40-
extern PlannedStmt *standard_planner(Query *parse, int cursorOptions,
41+
extern PlannedStmt *standard_planner(Query *parse, const char *query_string,
42+
int cursorOptions,
4143
ParamListInfo boundParams);
4244

4345
extern PlannerInfo *subquery_planner(PlannerGlobal *glob, Query *parse,

src/include/tcop/tcopprot.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ extern List *pg_analyze_and_rewrite_params(RawStmt *parsetree,
5252
ParserSetupHook parserSetup,
5353
void *parserSetupArg,
5454
QueryEnvironment *queryEnv);
55-
extern PlannedStmt *pg_plan_query(Query *querytree, int cursorOptions,
55+
extern PlannedStmt *pg_plan_query(Query *querytree, const char *query_string,
56+
int cursorOptions,
5657
ParamListInfo boundParams);
57-
extern List *pg_plan_queries(List *querytrees, int cursorOptions,
58+
extern List *pg_plan_queries(List *querytrees, const char *query_string,
59+
int cursorOptions,
5860
ParamListInfo boundParams);
5961

6062
extern bool check_max_stack_depth(int *newval, void **extra, GucSource source);

0 commit comments

Comments
 (0)