summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPavan Deolasee2015-06-18 11:54:22 +0000
committerPavan Deolasee2015-06-18 11:54:22 +0000
commit17705af40dfb7aa7861066f4393bc77dc6b47bd4 (patch)
tree58260c87a580dc1504d2fc7f78442f833793b80e /src
parent4b461e8357afee2715f90ed182a18cfe534513c6 (diff)
Handle CREATE TABLE IF NOT EXISTS correctly
In XL we rewrite CTAS as CREATE TABLE followed by INSERT INTO. If the target table already exists, we now issue a notice for the first step and skip the second step completely. The ruleutils also now handles INE correctly for CTAS
Diffstat (limited to 'src')
-rw-r--r--src/backend/rewrite/rewriteHandler.c17
-rw-r--r--src/backend/utils/adt/ruleutils.c5
2 files changed, 20 insertions, 2 deletions
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
index fa1d07777e..48fcd51836 100644
--- a/src/backend/rewrite/rewriteHandler.c
+++ b/src/backend/rewrite/rewriteHandler.c
@@ -3858,10 +3858,27 @@ QueryRewriteCTAS(Query *parsetree)
relation = stmt->into->rel;
+ if (stmt->if_not_exists)
+ {
+ Oid nspid;
+
+ nspid = RangeVarGetCreationNamespace(stmt->into->rel);
+
+ if (get_relname_relid(stmt->into->rel->relname, nspid))
+ {
+ ereport(NOTICE,
+ (errcode(ERRCODE_DUPLICATE_TABLE),
+ errmsg("relation \"%s\" already exists, skipping",
+ stmt->into->rel->relname)));
+ return NIL;
+ }
+ }
+
/* Start building a CreateStmt for creating the target table */
create_stmt = makeNode(CreateStmt);
create_stmt->relation = relation;
create_stmt->islocal = stmt->islocal;
+ create_stmt->if_not_exists = stmt->if_not_exists;
into = stmt->into;
/* Obtain the target list of new table */
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 83c5169430..41e0b35d58 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -6225,10 +6225,11 @@ get_utility_query_def(Query *query, deparse_context *context)
bool istemp = (relation->relpersistence == RELPERSISTENCE_TEMP);
bool isunlogged = (relation->relpersistence == RELPERSISTENCE_UNLOGGED);
- appendStringInfo(buf, "CREATE %s %s %s TABLE ",
+ appendStringInfo(buf, "CREATE %s %s %s TABLE %s ",
stmt->islocal ? "LOCAL" : "",
istemp ? "TEMP" : "",
- isunlogged ? "UNLOGGED" : "");
+ isunlogged ? "UNLOGGED" : "",
+ stmt->if_not_exists ? "IF NOT EXISTS " : "");
if (!istemp && relation->schemaname && relation->schemaname[0])
appendStringInfo(buf, "%s.", relation->schemaname);