From 434ce09ecae207a1f128eec5593f667b8507f220 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 19 Oct 2012 19:40:49 +0200
Subject: [PATCH] Fix simple oversight causing UtilityContainsQuery not to
 support CREATE TABLE AS ... EXECUTE

UtilityContainsQuery looked in CreateTableAsStmt->query for an ExecuteStmt but
transformCreateTableAsStmt always puts it in ->query->utilityStmt.
---
 src/backend/tcop/utility.c |   19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 97376bb..0d1af0b 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1508,17 +1508,20 @@ UtilityContainsQuery(Node *parsetree)
 			return qry;
 
 		case T_CreateTableAsStmt:
-			/* might or might not contain a Query ... */
 			qry = (Query *) ((CreateTableAsStmt *) parsetree)->query;
-			if (IsA(qry, Query))
-			{
-				/* Recursion currently can't be necessary here */
-				Assert(qry->commandType != CMD_UTILITY);
+			Assert(IsA(qry, Query));
+
+			/* CREATE TABLE .. EXECUTE ...*/
+			if (qry->commandType == CMD_UTILITY &&
+			    IsA(qry->utilityStmt, ExecuteStmt))
+				return NULL;
+			/* CREATE TABLE .. SELECT ... */
+			else if (qry->commandType == CMD_SELECT)
 				return qry;
-			}
-			Assert(IsA(qry, ExecuteStmt));
-			return NULL;
 
+			/* Recursion currently can't be necessary here */
+			elog(ERROR, "unsupported case in CREATE TABLE AS");
+			return NULL; /* silence compiler */
 		default:
 			return NULL;
 	}
-- 
1.7.10.4

