mirror of
https://github.com/postgres/postgres.git
synced 2025-11-18 02:02:55 +03:00
Add ExplainState argument to pg_plan_query() and planner().
This allows extensions to have access to any data they've stored in the ExplainState during planning. Unfortunately, it won't help with 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 some documentation 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
This commit is contained in:
@@ -796,7 +796,7 @@ BeginCopyTo(ParseState *pstate,
|
||||
|
||||
/* plan the query */
|
||||
plan = pg_plan_query(query, pstate->p_sourcetext,
|
||||
CURSOR_OPT_PARALLEL_OK, NULL);
|
||||
CURSOR_OPT_PARALLEL_OK, NULL, NULL);
|
||||
|
||||
/*
|
||||
* With row-level security and a user using "COPY relation TO", we
|
||||
|
||||
@@ -321,7 +321,7 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
|
||||
|
||||
/* plan the query */
|
||||
plan = pg_plan_query(query, pstate->p_sourcetext,
|
||||
CURSOR_OPT_PARALLEL_OK, params);
|
||||
CURSOR_OPT_PARALLEL_OK, params, NULL);
|
||||
|
||||
/*
|
||||
* Use a snapshot with an updated command ID to ensure this query sees
|
||||
|
||||
@@ -351,7 +351,7 @@ standard_ExplainOneQuery(Query *query, int cursorOptions,
|
||||
INSTR_TIME_SET_CURRENT(planstart);
|
||||
|
||||
/* plan the query */
|
||||
plan = pg_plan_query(query, queryString, cursorOptions, params);
|
||||
plan = pg_plan_query(query, queryString, cursorOptions, params, es);
|
||||
|
||||
INSTR_TIME_SET_CURRENT(planduration);
|
||||
INSTR_TIME_SUBTRACT(planduration, planstart);
|
||||
|
||||
@@ -426,7 +426,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
|
||||
CHECK_FOR_INTERRUPTS();
|
||||
|
||||
/* Plan the query which will generate data for the refresh. */
|
||||
plan = pg_plan_query(query, queryString, CURSOR_OPT_PARALLEL_OK, NULL);
|
||||
plan = pg_plan_query(query, queryString, CURSOR_OPT_PARALLEL_OK, NULL, NULL);
|
||||
|
||||
/*
|
||||
* Use a snapshot with an updated command ID to ensure this query sees
|
||||
|
||||
@@ -99,7 +99,8 @@ PerformCursorOpen(ParseState *pstate, DeclareCursorStmt *cstmt, ParamListInfo pa
|
||||
elog(ERROR, "non-SELECT statement in DECLARE CURSOR");
|
||||
|
||||
/* Plan the query, applying the specified options */
|
||||
plan = pg_plan_query(query, pstate->p_sourcetext, cstmt->options, params);
|
||||
plan = pg_plan_query(query, pstate->p_sourcetext, cstmt->options, params,
|
||||
NULL);
|
||||
|
||||
/*
|
||||
* Create a portal and copy the plan and query string into its memory.
|
||||
|
||||
@@ -279,6 +279,23 @@ static void create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel
|
||||
*
|
||||
* Query optimizer entry point
|
||||
*
|
||||
* Inputs:
|
||||
* parse: an analyzed-and-rewritten query tree for an optimizable statement
|
||||
* query_string: source text for the query tree (used for error reports)
|
||||
* cursorOptions: bitmask of CURSOR_OPT_XXX flags, see parsenodes.h
|
||||
* boundParams: passed-in parameter values, or NULL if none
|
||||
* es: ExplainState if being called from EXPLAIN, else NULL
|
||||
*
|
||||
* The result is a PlannedStmt tree.
|
||||
*
|
||||
* PARAM_EXTERN Param nodes within the parse tree can be replaced by Consts
|
||||
* using values from boundParams, if those values are marked PARAM_FLAG_CONST.
|
||||
* Parameter values not so marked are still relied on for estimation purposes.
|
||||
*
|
||||
* The ExplainState pointer is not currently used by the core planner, but it
|
||||
* is passed through to some planner hooks so that they can report information
|
||||
* back to EXPLAIN extension hooks.
|
||||
*
|
||||
* To support loadable plugins that monitor or modify planner behavior,
|
||||
* we provide a hook variable that lets a plugin get control before and
|
||||
* 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
|
||||
*****************************************************************************/
|
||||
PlannedStmt *
|
||||
planner(Query *parse, const char *query_string, int cursorOptions,
|
||||
ParamListInfo boundParams)
|
||||
ParamListInfo boundParams, ExplainState *es)
|
||||
{
|
||||
PlannedStmt *result;
|
||||
|
||||
if (planner_hook)
|
||||
result = (*planner_hook) (parse, query_string, cursorOptions, boundParams);
|
||||
result = (*planner_hook) (parse, query_string, cursorOptions,
|
||||
boundParams, es);
|
||||
else
|
||||
result = standard_planner(parse, query_string, cursorOptions, boundParams);
|
||||
result = standard_planner(parse, query_string, cursorOptions,
|
||||
boundParams, es);
|
||||
|
||||
pgstat_report_plan_id(result->planId, false);
|
||||
|
||||
@@ -306,7 +325,7 @@ planner(Query *parse, const char *query_string, int cursorOptions,
|
||||
|
||||
PlannedStmt *
|
||||
standard_planner(Query *parse, const char *query_string, int cursorOptions,
|
||||
ParamListInfo boundParams)
|
||||
ParamListInfo boundParams, ExplainState *es)
|
||||
{
|
||||
PlannedStmt *result;
|
||||
PlannerGlobal *glob;
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/async.h"
|
||||
#include "commands/event_trigger.h"
|
||||
#include "commands/explain_state.h"
|
||||
#include "commands/prepare.h"
|
||||
#include "common/pg_prng.h"
|
||||
#include "jit/jit.h"
|
||||
@@ -884,7 +885,7 @@ pg_rewrite_query(Query *query)
|
||||
*/
|
||||
PlannedStmt *
|
||||
pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
|
||||
ParamListInfo boundParams)
|
||||
ParamListInfo boundParams, ExplainState *es)
|
||||
{
|
||||
PlannedStmt *plan;
|
||||
|
||||
@@ -901,7 +902,7 @@ pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
|
||||
ResetUsage();
|
||||
|
||||
/* call the optimizer */
|
||||
plan = planner(querytree, query_string, cursorOptions, boundParams);
|
||||
plan = planner(querytree, query_string, cursorOptions, boundParams, es);
|
||||
|
||||
if (log_planner_stats)
|
||||
ShowUsage("PLANNER STATISTICS");
|
||||
@@ -997,7 +998,7 @@ pg_plan_queries(List *querytrees, const char *query_string, int cursorOptions,
|
||||
else
|
||||
{
|
||||
stmt = pg_plan_query(query, query_string, cursorOptions,
|
||||
boundParams);
|
||||
boundParams, NULL);
|
||||
}
|
||||
|
||||
stmt_list = lappend(stmt_list, stmt);
|
||||
|
||||
Reference in New Issue
Block a user