From eff8446b04ec6640045a4d92efec4ca09e0ff5b4 Mon Sep 17 00:00:00 2001
From: Andreas Karlsson <andreas@proxel.se>
Date: Wed, 6 Feb 2019 03:17:57 +0100
Subject: [PATCH] Add missing IF NOT EXIST support for CTAS with prepared
 statments

---
 src/backend/parser/gram.y | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index c1faf4152c..524c5b762a 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -10696,11 +10696,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; }
-- 
2.11.0

