aboutsummaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-01-15 13:18:19 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-01-15 13:18:19 -0500
commit939f0fb6765ef16874a4bd268efeb27cbc965e43 (patch)
tree7fd163b6e230596c7b0de1b5de955746db10e66a /src/include
parentebbef4f3959501f65041739759ea6c5b34437091 (diff)
downloadpostgresql-939f0fb6765ef16874a4bd268efeb27cbc965e43.tar.gz
postgresql-939f0fb6765ef16874a4bd268efeb27cbc965e43.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/include')
-rw-r--r--src/include/commands/explain.h9
-rw-r--r--src/include/utils/builtins.h5
2 files changed, 11 insertions, 3 deletions
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index ca213d7f704..664499c2d35 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -24,6 +24,13 @@ typedef enum ExplainFormat
EXPLAIN_FORMAT_YAML
} ExplainFormat;
+/* Crude hack to avoid changing sizeof(ExplainState) in released branches */
+typedef struct ExplainStateExtra
+{
+ List *groupingstack; /* format-specific grouping state */
+ List *deparsecxt; /* context list for deparsing expressions */
+} ExplainStateExtra;
+
typedef struct ExplainState
{
StringInfo str; /* output buffer */
@@ -39,7 +46,7 @@ typedef struct ExplainState
List *rtable; /* range table */
List *rtable_names; /* alias names for RTEs */
int indent; /* current indentation level */
- List *grouping_stack; /* format-specific grouping state */
+ ExplainStateExtra *extra; /* pointer to additional data */
} ExplainState;
/* Hook for plugins to get control in ExplainOneQuery() */
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index c5071c87b73..929a728106d 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -660,8 +660,9 @@ extern Datum pg_get_function_result(PG_FUNCTION_ARGS);
extern char *deparse_expression(Node *expr, List *dpcontext,
bool forceprefix, bool showimplicit);
extern List *deparse_context_for(const char *aliasname, Oid relid);
-extern List *deparse_context_for_planstate(Node *planstate, List *ancestors,
- List *rtable, List *rtable_names);
+extern List *deparse_context_for_plan_rtable(List *rtable, List *rtable_names);
+extern List *set_deparse_context_planstate(List *dpcontext,
+ Node *planstate, List *ancestors);
extern List *select_rtable_names_for_explain(List *rtable,
Bitmapset *rels_used);
extern const char *quote_identifier(const char *ident);