From: Pavan Deolasee Date: Fri, 12 May 2017 12:06:23 +0000 (+0530) Subject: Fix EXPLAIN ANALYZE SELECT INTO X-Git-Tag: XL_10_R1BETA1~295 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=802721867a8d3862eb2ebdb64db1faac013354b8;p=postgres-xl.git 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. --- 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 --