diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2015-01-15 13:18:16 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2015-01-15 13:18:16 -0500 |
commit | d25192892d61419278fbb216e695cb070c332092 (patch) | |
tree | e3e6d445319f38594a13469123aeefee65d3a85d /src/backend/commands/explain.c | |
parent | 7b65f194e9ef098e519bb6d4f792af71a4ab5778 (diff) | |
download | postgresql-d25192892d61419278fbb216e695cb070c332092.tar.gz postgresql-d25192892d61419278fbb216e695cb070c332092.zip |
Improve performance of EXPLAIN with large range tables.
As of 9.3, ruleutils.c goes to some lengths to ensure that table and column
aliases used in its output are unique. Of course this takes more time than
was required before, which in itself isn't fatal. However, EXPLAIN was set
up so that recalculation of the unique aliases was repeated for each
subexpression printed in a plan. That results in O(N^2) time and memory
consumption for large plan trees, which did not happen in older branches.
Fortunately, the expensive work is the same across a whole plan tree,
so there is no need to repeat it; we can do most of the initialization
just once per query and re-use it for each subexpression. This buys
back most (not all) of the performance loss since 9.2.
We need an extra ExplainState field to hold the precalculated deparse
context. That's no problem in HEAD, but in the back branches, expanding
sizeof(ExplainState) seems risky because third-party extensions might
have local variables of that struct type. So, in 9.4 and 9.3, introduce
an auxiliary struct to keep sizeof(ExplainState) the same. We should
refactor the APIs to avoid such local variables in future, but that's
material for a separate HEAD-only commit.
Per gripe from Alexey Bashtanov. Back-patch to 9.3 where the issue
was introduced.
Diffstat (limited to 'src/backend/commands/explain.c')
-rw-r--r-- | src/backend/commands/explain.c | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index d99e5a86f0d..032f5a47433 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -33,6 +33,10 @@ #include "utils/xml.h" +/* Crude hack to avoid changing sizeof(ExplainState) in released branches */ +#define grouping_stack extra->groupingstack +#define deparse_cxt extra->deparsecxt + /* Hook for plugins to get control in ExplainOneQuery() */ ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL; @@ -262,6 +266,8 @@ ExplainInitState(ExplainState *es) es->costs = true; /* Prepare output buffer. */ es->str = makeStringInfo(); + /* Kluge to avoid changing sizeof(ExplainState) in released branches. */ + es->extra = (ExplainStateExtra *) palloc0(sizeof(ExplainStateExtra)); } /* @@ -562,6 +568,8 @@ ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc) es->rtable = queryDesc->plannedstmt->rtable; ExplainPreScanNode(queryDesc->planstate, &rels_used); es->rtable_names = select_rtable_names_for_explain(es->rtable, rels_used); + es->deparse_cxt = deparse_context_for_plan_rtable(es->rtable, + es->rtable_names); ExplainNode(queryDesc->planstate, NIL, NULL, NULL, es); } @@ -1653,10 +1661,9 @@ show_plan_tlist(PlanState *planstate, List *ancestors, ExplainState *es) return; /* Set up deparsing context */ - context = deparse_context_for_planstate((Node *) planstate, - ancestors, - es->rtable, - es->rtable_names); + context = set_deparse_context_planstate(es->deparse_cxt, + (Node *) planstate, + ancestors); useprefix = list_length(es->rtable) > 1; /* Deparse each result column (we now include resjunk ones) */ @@ -1685,10 +1692,9 @@ show_expression(Node *node, const char *qlabel, char *exprstr; /* Set up deparsing context */ - context = deparse_context_for_planstate((Node *) planstate, - ancestors, - es->rtable, - es->rtable_names); + context = set_deparse_context_planstate(es->deparse_cxt, + (Node *) planstate, + ancestors); /* Deparse the expression */ exprstr = deparse_expression(node, context, useprefix, false); @@ -1830,10 +1836,9 @@ show_sort_group_keys(PlanState *planstate, const char *qlabel, return; /* Set up deparsing context */ - context = deparse_context_for_planstate((Node *) planstate, - ancestors, - es->rtable, - es->rtable_names); + context = set_deparse_context_planstate(es->deparse_cxt, + (Node *) planstate, + ancestors); useprefix = (list_length(es->rtable) > 1 || es->verbose); for (keyno = 0; keyno < nkeys; keyno++) |