aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2019-02-15 17:12:36 +0900
committerMichael Paquier <michael@paquier.xyz>2019-02-15 17:12:36 +0900
commit3a2923a9bfd8e769d5bafeef4284fbf1ba953cbf (patch)
treeedadec571c4b03b4d444addbb6b34d584b2b3bca /src
parentcb3d674e1709362d84b5e25c94e690e98599a33c (diff)
downloadpostgresql-3a2923a9bfd8e769d5bafeef4284fbf1ba953cbf.tar.gz
postgresql-3a2923a9bfd8e769d5bafeef4284fbf1ba953cbf.zip
Fix support for CREATE TABLE IF NOT EXISTS AS EXECUTE
The grammar IF NOT EXISTS for CTAS is supported since 9.5 and documented as such, however the case of using EXECUTE as query has never been covered as EXECUTE CTAS statements and normal CTAS statements are parsed separately. Author: Andreas Karlsson Discussion: https://postgr.es/m/2ddcc188-e37c-a0be-32bf-a56b07c3559e@proxel.se Backpatch-through: 9.5
Diffstat (limited to 'src')
-rw-r--r--src/backend/parser/gram.y18
-rw-r--r--src/test/regress/expected/create_table.out14
-rw-r--r--src/test/regress/sql/create_table.sql8
3 files changed, 40 insertions, 0 deletions
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index a97f51ce37c..a8ce22f3117 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -10383,11 +10383,29 @@ ExecuteStmt: EXECUTE name execute_param_clause
ctas->into = $4;
ctas->relkind = OBJECT_TABLE;
ctas->is_select_into = false;
+ ctas->if_not_exists = false;
/* cram additional flags into the IntoClause */
$4->rel->relpersistence = $2;
$4->skipData = !($9);
$$ = (Node *) ctas;
}
+ | CREATE OptTemp TABLE IF_P NOT EXISTS create_as_target AS
+ EXECUTE name execute_param_clause opt_with_data
+ {
+ CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
+ ExecuteStmt *n = makeNode(ExecuteStmt);
+ n->name = $10;
+ n->params = $11;
+ ctas->query = (Node *) n;
+ ctas->into = $7;
+ ctas->relkind = OBJECT_TABLE;
+ ctas->is_select_into = false;
+ ctas->if_not_exists = true;
+ /* cram additional flags into the IntoClause */
+ $7->rel->relpersistence = $2;
+ $7->skipData = !($12);
+ $$ = (Node *) ctas;
+ }
;
execute_param_clause: '(' expr_list ')' { $$ = $2; }
diff --git a/src/test/regress/expected/create_table.out b/src/test/regress/expected/create_table.out
index 28c9d2df898..ab82a295606 100644
--- a/src/test/regress/expected/create_table.out
+++ b/src/test/regress/expected/create_table.out
@@ -258,6 +258,20 @@ ERROR: relation "as_select1" already exists
CREATE TABLE IF NOT EXISTS as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
NOTICE: relation "as_select1" already exists, skipping
DROP TABLE as_select1;
+PREPARE select1 AS SELECT 1 as a;
+CREATE TABLE as_select1 AS EXECUTE select1;
+CREATE TABLE as_select1 AS EXECUTE select1;
+ERROR: relation "as_select1" already exists
+SELECT * FROM as_select1;
+ a
+---
+ 1
+(1 row)
+
+CREATE TABLE IF NOT EXISTS as_select1 AS EXECUTE select1;
+NOTICE: relation "as_select1" already exists, skipping
+DROP TABLE as_select1;
+DEALLOCATE select1;
-- check that the oid column is added before the primary key is checked
CREATE TABLE oid_pk (f1 INT, PRIMARY KEY(oid)) WITH OIDS;
DROP TABLE oid_pk;
diff --git a/src/test/regress/sql/create_table.sql b/src/test/regress/sql/create_table.sql
index 7849139ca15..137c0db5b3f 100644
--- a/src/test/regress/sql/create_table.sql
+++ b/src/test/regress/sql/create_table.sql
@@ -274,6 +274,14 @@ CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
CREATE TABLE IF NOT EXISTS as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
DROP TABLE as_select1;
+PREPARE select1 AS SELECT 1 as a;
+CREATE TABLE as_select1 AS EXECUTE select1;
+CREATE TABLE as_select1 AS EXECUTE select1;
+SELECT * FROM as_select1;
+CREATE TABLE IF NOT EXISTS as_select1 AS EXECUTE select1;
+DROP TABLE as_select1;
+DEALLOCATE select1;
+
-- check that the oid column is added before the primary key is checked
CREATE TABLE oid_pk (f1 INT, PRIMARY KEY(oid)) WITH OIDS;
DROP TABLE oid_pk;