summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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);