summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavan Deolasee2017-05-12 12:06:23 +0000
committerPavan Deolasee2017-05-12 12:12:18 +0000
commit802721867a8d3862eb2ebdb64db1faac013354b8 (patch)
tree619f3dc7a58283ce8e944921627a6945c7ff7d41
parent6dddd57c9a5da1e1192f9455cf5e0a9ad623ea6f (diff)
Fix EXPLAIN ANALYZE SELECT INTO
EXPLAIN ANALYZE SELECT INTO was missing the treatment that we give to a regular SELECT INTO or CREATE TABLE AS SELECT. This patch fixes that such that even when EXPLAIN ANALYZE is used, we first create the target table and then insert the selected rows. The EXPLAIN ANALYZE will only show the plan for the final transformed INSERT INTO statement. This is not very useful right now the EXPLAIN ANALYZE doesn't show anything below Remote Subquery Scan, but that's a separate issue and will be fixed in a separate patch. The regression test's expected output is updated accordingly. This will be backpatched to XL9_5_STABLE.
-rw-r--r--src/backend/commands/explain.c17
-rw-r--r--src/test/regress/expected/select_into.out7
2 files changed, 23 insertions, 1 deletions
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 23496b4f8f..18b2404ebf 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -356,7 +356,22 @@ ExplainOneQuery(Query *query, IntoClause *into, ExplainState *es,
/* planner will not cope with utility statements */
if (query->commandType == CMD_UTILITY)
{
- ExplainOneUtility(query->utilityStmt, into, es, queryString, params);
+ /*
+ * If we are running EXPLAIN ANALYZE, transform the CTAS such that the
+ * target table is created first and select result is inserted into the
+ * table. The EXPLAIN ANALYZE would really just show the plan for the
+ * INSERT INTO generated by QueryRewriteCTAS, but that's OK.
+ */
+ if (es->analyze && IsA(query->utilityStmt, CreateTableAsStmt))
+ {
+ List *rewritten = QueryRewriteCTAS(query);
+ Assert(list_length(rewritten) == 1);
+ ExplainOneQuery((Query *) linitial(rewritten), into, es,
+ queryString, params);
+ }
+ else
+ ExplainOneUtility(query->utilityStmt, into, es,
+ queryString, params);
return;
}
diff --git a/src/test/regress/expected/select_into.out b/src/test/regress/expected/select_into.out
index 8662413656..11b61b592c 100644
--- a/src/test/regress/expected/select_into.out
+++ b/src/test/regress/expected/select_into.out
@@ -107,8 +107,15 @@ SELECT * FROM created_table;
ERROR: relation "created_table" does not exist
LINE 1: SELECT * FROM created_table;
^
+-- Try EXPLAIN ANALYZE SELECT INTO, but hide the output since it won't
+-- be stable.
+DO $$
+BEGIN
+ EXECUTE 'EXPLAIN ANALYZE SELECT * INTO TABLE easi FROM int8_tbl';
+END$$;
DROP TABLE created_table;
ERROR: table "created_table" does not exist
+DROP TABLE easi;
--
-- Disallowed uses of SELECT ... INTO. All should fail
--