Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc83ac02

Browse files
robertmhaastglsfdc
andcommitted
Add ExplainState argument to pg_plan_query() and planner().
This allows extensions to have access to any data they've storedin the ExplainState during planning. Unfortunately, it won't helpwith EXPLAIN EXECUTE is used, but since that case is less common,this still seems like an improvement.Since planner() has quite a few arguments now, also add somedocumentation of those arguments and the return value.Author: Robert Haas <rhaas@postgresql.org>Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us>Reviewed-by: Andrei Lepikhov <lepihov@gmail.com>Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:http://postgr.es/m/CA+TgmoYWKHU2hKr62Toyzh-kTDEnMDeLw7gkOOnjL-TnOUq0kQ@mail.gmail.com
1 parent8e11859 commitc83ac02

File tree

12 files changed

+58
-25
lines changed

12 files changed

+58
-25
lines changed

‎contrib/pg_stat_statements/pg_stat_statements.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ static void pgss_post_parse_analyze(ParseState *pstate, Query *query,
338338
staticPlannedStmt*pgss_planner(Query*parse,
339339
constchar*query_string,
340340
intcursorOptions,
341-
ParamListInfoboundParams);
341+
ParamListInfoboundParams,
342+
ExplainState*es);
342343
staticvoidpgss_ExecutorStart(QueryDesc*queryDesc,inteflags);
343344
staticvoidpgss_ExecutorRun(QueryDesc*queryDesc,
344345
ScanDirectiondirection,
@@ -894,7 +895,8 @@ static PlannedStmt *
894895
pgss_planner(Query*parse,
895896
constchar*query_string,
896897
intcursorOptions,
897-
ParamListInfoboundParams)
898+
ParamListInfoboundParams,
899+
ExplainState*es)
898900
{
899901
PlannedStmt*result;
900902

@@ -929,10 +931,10 @@ pgss_planner(Query *parse,
929931
{
930932
if (prev_planner_hook)
931933
result=prev_planner_hook(parse,query_string,cursorOptions,
932-
boundParams);
934+
boundParams,es);
933935
else
934936
result=standard_planner(parse,query_string,cursorOptions,
935-
boundParams);
937+
boundParams,es);
936938
}
937939
PG_FINALLY();
938940
{
@@ -978,10 +980,10 @@ pgss_planner(Query *parse,
978980
{
979981
if (prev_planner_hook)
980982
result=prev_planner_hook(parse,query_string,cursorOptions,
981-
boundParams);
983+
boundParams,es);
982984
else
983985
result=standard_planner(parse,query_string,cursorOptions,
984-
boundParams);
986+
boundParams,es);
985987
}
986988
PG_FINALLY();
987989
{

‎src/backend/commands/copyto.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ BeginCopyTo(ParseState *pstate,
796796

797797
/* plan the query */
798798
plan=pg_plan_query(query,pstate->p_sourcetext,
799-
CURSOR_OPT_PARALLEL_OK,NULL);
799+
CURSOR_OPT_PARALLEL_OK,NULL,NULL);
800800

801801
/*
802802
* With row-level security and a user using "COPY relation TO", we

‎src/backend/commands/createas.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
321321

322322
/* plan the query */
323323
plan=pg_plan_query(query,pstate->p_sourcetext,
324-
CURSOR_OPT_PARALLEL_OK,params);
324+
CURSOR_OPT_PARALLEL_OK,params,NULL);
325325

326326
/*
327327
* 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
@@ -351,7 +351,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
351351
INSTR_TIME_SET_CURRENT(planstart);
352352

353353
/* plan the query */
354-
plan=pg_plan_query(query,queryString,cursorOptions,params);
354+
plan=pg_plan_query(query,queryString,cursorOptions,params,es);
355355

356356
INSTR_TIME_SET_CURRENT(planduration);
357357
INSTR_TIME_SUBTRACT(planduration,planstart);

‎src/backend/commands/matview.c‎

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

428428
/* Plan the query which will generate data for the refresh. */
429-
plan=pg_plan_query(query,queryString,CURSOR_OPT_PARALLEL_OK,NULL);
429+
plan=pg_plan_query(query,queryString,CURSOR_OPT_PARALLEL_OK,NULL,NULL);
430430

431431
/*
432432
* Use a snapshot with an updated command ID to ensure this query sees

‎src/backend/commands/portalcmds.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
9999
elog(ERROR,"non-SELECT statement in DECLARE CURSOR");
100100

101101
/* Plan the query, applying the specified options */
102-
plan=pg_plan_query(query,pstate->p_sourcetext,cstmt->options,params);
102+
plan=pg_plan_query(query,pstate->p_sourcetext,cstmt->options,params,
103+
NULL);
103104

104105
/*
105106
* Create a portal and copy the plan and query string into its memory.

‎src/backend/optimizer/plan/planner.c‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,23 @@ static void create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel
279279
*
280280
* Query optimizer entry point
281281
*
282+
* Inputs:
283+
*parse: an analyzed-and-rewritten query tree for an optimizable statement
284+
*query_string: source text for the query tree (used for error reports)
285+
*cursorOptions: bitmask of CURSOR_OPT_XXX flags, see parsenodes.h
286+
*boundParams: passed-in parameter values, or NULL if none
287+
*es: ExplainState if being called from EXPLAIN, else NULL
288+
*
289+
* The result is a PlannedStmt tree.
290+
*
291+
* PARAM_EXTERN Param nodes within the parse tree can be replaced by Consts
292+
* using values from boundParams, if those values are marked PARAM_FLAG_CONST.
293+
* Parameter values not so marked are still relied on for estimation purposes.
294+
*
295+
* The ExplainState pointer is not currently used by the core planner, but it
296+
* is passed through to some planner hooks so that they can report information
297+
* back to EXPLAIN extension hooks.
298+
*
282299
* To support loadable plugins that monitor or modify planner behavior,
283300
* we provide a hook variable that lets a plugin get control before and
284301
* after the standard planning process. The plugin would normally call
@@ -290,14 +307,16 @@ static void create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel
290307
*****************************************************************************/
291308
PlannedStmt*
292309
planner(Query*parse,constchar*query_string,intcursorOptions,
293-
ParamListInfoboundParams)
310+
ParamListInfoboundParams,ExplainState*es)
294311
{
295312
PlannedStmt*result;
296313

297314
if (planner_hook)
298-
result= (*planner_hook) (parse,query_string,cursorOptions,boundParams);
315+
result= (*planner_hook) (parse,query_string,cursorOptions,
316+
boundParams,es);
299317
else
300-
result=standard_planner(parse,query_string,cursorOptions,boundParams);
318+
result=standard_planner(parse,query_string,cursorOptions,
319+
boundParams,es);
301320

302321
pgstat_report_plan_id(result->planId, false);
303322

@@ -306,7 +325,7 @@ planner(Query *parse, const char *query_string, int cursorOptions,
306325

307326
PlannedStmt*
308327
standard_planner(Query*parse,constchar*query_string,intcursorOptions,
309-
ParamListInfoboundParams)
328+
ParamListInfoboundParams,ExplainState*es)
310329
{
311330
PlannedStmt*result;
312331
PlannerGlobal*glob;

‎src/backend/tcop/postgres.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include"catalog/pg_type.h"
3838
#include"commands/async.h"
3939
#include"commands/event_trigger.h"
40+
#include"commands/explain_state.h"
4041
#include"commands/prepare.h"
4142
#include"common/pg_prng.h"
4243
#include"jit/jit.h"
@@ -884,7 +885,7 @@ pg_rewrite_query(Query *query)
884885
*/
885886
PlannedStmt*
886887
pg_plan_query(Query*querytree,constchar*query_string,intcursorOptions,
887-
ParamListInfoboundParams)
888+
ParamListInfoboundParams,ExplainState*es)
888889
{
889890
PlannedStmt*plan;
890891

@@ -901,7 +902,7 @@ pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
901902
ResetUsage();
902903

903904
/* call the optimizer */
904-
plan=planner(querytree,query_string,cursorOptions,boundParams);
905+
plan=planner(querytree,query_string,cursorOptions,boundParams,es);
905906

906907
if (log_planner_stats)
907908
ShowUsage("PLANNER STATISTICS");
@@ -997,7 +998,7 @@ pg_plan_queries(List *querytrees, const char *query_string, int cursorOptions,
997998
else
998999
{
9991000
stmt=pg_plan_query(query,query_string,cursorOptions,
1000-
boundParams);
1001+
boundParams,NULL);
10011002
}
10021003

10031004
stmt_list=lappend(stmt_list,stmt);

‎src/include/optimizer/optimizer.h‎

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

2525
#include"nodes/parsenodes.h"
2626

27+
typedefstructExplainStateExplainState;/* defined in explain_state.h */
28+
2729
/*
2830
* We don't want to include nodes/pathnodes.h here, because non-planner
2931
* code should generally treat PlannerInfo as an opaque typedef.
@@ -104,7 +106,8 @@ extern PGDLLIMPORT bool enable_distinct_reordering;
104106

105107
externPlannedStmt*planner(Query*parse,constchar*query_string,
106108
intcursorOptions,
107-
ParamListInfoboundParams);
109+
ParamListInfoboundParams,
110+
ExplainState*es);
108111

109112
externExpr*expression_planner(Expr*expr);
110113
externExpr*expression_planner_with_deps(Expr*expr,

‎src/include/optimizer/planner.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
#include"nodes/plannodes.h"
2323

2424

25+
typedefstructExplainStateExplainState;/* defined in explain_state.h */
26+
2527
/* Hook for plugins to get control in planner() */
2628
typedefPlannedStmt*(*planner_hook_type) (Query*parse,
2729
constchar*query_string,
2830
intcursorOptions,
29-
ParamListInfoboundParams);
31+
ParamListInfoboundParams,
32+
ExplainState*es);
3033
externPGDLLIMPORTplanner_hook_typeplanner_hook;
3134

3235
/* Hook for plugins to get control when grouping_planner() plans upper rels */
@@ -40,7 +43,8 @@ extern PGDLLIMPORT create_upper_paths_hook_type create_upper_paths_hook;
4043

4144
externPlannedStmt*standard_planner(Query*parse,constchar*query_string,
4245
intcursorOptions,
43-
ParamListInfoboundParams);
46+
ParamListInfoboundParams,
47+
ExplainState*es);
4448

4549
externPlannerInfo*subquery_planner(PlannerGlobal*glob,Query*parse,
4650
char*plan_name,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp