aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/explain.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-01-15 13:18:12 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-01-15 13:18:12 -0500
commita5cd70dcbc268381e13cb0b2973b5732856d186f (patch)
treebfcd1f5690ac2b656bdea4b5ee21f14f368e224b /src/backend/commands/explain.c
parent0b49642b99ca2818bb8bfcaddf522b2e36a5b350 (diff)
downloadpostgresql-a5cd70dcbc268381e13cb0b2973b5732856d186f.tar.gz
postgresql-a5cd70dcbc268381e13cb0b2973b5732856d186f.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.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 8a0be5df0bf..39ceaf26639 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -563,6 +563,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);
}
@@ -1678,10 +1680,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) */
@@ -1710,10 +1711,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);
@@ -1855,10 +1855,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++)