diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 5a853c4..54b65a9 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -36,6 +36,8 @@
 #include "commands/tablecmds.h"
 #include "commands/view.h"
 #include "miscadmin.h"
+#include "nodes/makefuncs.h"
+#include "nodes/nodeFuncs.h"
 #include "parser/parse_clause.h"
 #include "rewrite/rewriteHandler.h"
 #include "storage/smgr.h"
@@ -61,11 +63,161 @@ typedef struct
 /* the address of the created table, for ExecCreateTableAs consumption */
 static ObjectAddress CreateAsReladdr = {InvalidOid, InvalidOid, 0};
 
+/* set of routines for the executor */
 static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
 static bool intorel_receive(TupleTableSlot *slot, DestReceiver *self);
 static void intorel_shutdown(DestReceiver *self);
 static void intorel_destroy(DestReceiver *self);
 
+/* utility functions for CTAS definition creation */
+static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into);
+static ObjectAddress create_ctas_internal(List *colList, IntoClause *into);
+
+
+/*
+ * create_ctas_internal
+ *
+ * Internal utility used for the creation of the definition of a relation
+ * created via CREATE TABLE AS or a materialized view.  Caller needs to
+ * build and provide a list of attributes before calling this routine.
+ */
+static ObjectAddress
+create_ctas_internal(List *attrList, IntoClause *into)
+{
+	CreateStmt *create = makeNode(CreateStmt);
+	bool		is_matview = (into->viewQuery != NULL);
+	char		relkind;
+	Datum		toast_options;
+	static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
+	ObjectAddress intoRelationAddr;
+
+	Assert(into != NULL);		/* else somebody forgot to set it */
+
+	/* This code supports both CREATE TABLE AS and CREATE MATERIALIZED VIEW */
+	is_matview = (into->viewQuery != NULL);
+	relkind = is_matview ? RELKIND_MATVIEW : RELKIND_RELATION;
+
+	/*
+	 * Options for materialized views.
+	 */
+	create->relation = into->rel;
+	create->tableElts = attrList;
+	create->inhRelations = NIL;
+	create->constraints = NIL;
+	create->options = into->options;
+	create->oncommit = into->onCommit;
+	create->tablespacename = into->tableSpaceName;
+	create->if_not_exists = false;
+
+	/*
+	 * finally create the relation (this will error out if there's an
+	 * existing view, so we don't need more code to complain if "replace"
+	 * is false).
+	 */
+	intoRelationAddr = DefineRelation(create, relkind, InvalidOid, NULL);
+
+	/*
+	 * If necessary, create a TOAST table for the target table.  Note that
+	 * NewRelationCreateToastTable ends with CommandCounterIncrement(), so
+	 * that the TOAST table will be visible for insertion.
+	 */
+	CommandCounterIncrement();
+
+	/* parse and validate reloptions for the toast table */
+	toast_options = transformRelOptions((Datum) 0,
+										create->options,
+										"toast",
+										validnsps,
+										true, false);
+
+	(void) heap_reloptions(RELKIND_TOASTVALUE, toast_options, true);
+
+	NewRelationCreateToastTable(intoRelationAddr.objectId, toast_options);
+
+	/* Create the "view" part of a materialized view. */
+	if (is_matview)
+	{
+		/* StoreViewQuery scribbles on tree, so make a copy */
+		Query	   *query = (Query *) copyObject(into->viewQuery);
+
+		StoreViewQuery(intoRelationAddr.objectId, query, false);
+		CommandCounterIncrement();
+	}
+
+	return intoRelationAddr;
+}
+
+
+/*
+ * create_ctas_nodata
+ *
+ * Create CTAS or materialized view when WITH NO DATA is used.  The relation
+ * definition itself and all its underling infrastructure is created.
+ */
+static ObjectAddress
+create_ctas_nodata(List *tlist, IntoClause *into)
+{
+	List	   *attrList;
+	ListCell   *t, *lc;
+
+	/* Build list of attributes */
+	attrList = NIL;
+	lc = list_head(into->colNames);
+	foreach(t, tlist)
+	{
+		TargetEntry *tle = lfirst(t);
+
+		if (!tle->resjunk)
+		{
+			char	   *colname;
+			ColumnDef  *col;
+
+			if (lc)
+			{
+				colname = strVal(lfirst(lc));
+				lc = lnext(lc);
+			}
+			else
+				colname = pstrdup(tle->resname);
+
+			col = makeColumnDef(colname,
+								exprType((Node *) tle->expr),
+								exprTypmod((Node *) tle->expr),
+								exprCollation((Node *) tle->expr));
+
+			pfree(colname);
+
+			/*
+			 * It's possible that the column is of a collatable type but the
+			 * collation could not be resolved, so double-check.  (We must check
+			 * this here because DefineRelation would adopt the type's default
+			 * collation rather than complaining.)
+			 */
+			if (!OidIsValid(col->collOid))
+			{
+				Oid typeOid = col->typeName->typeOid;
+
+				if (type_is_collatable(typeOid))
+					ereport(ERROR,
+							(errcode(ERRCODE_INDETERMINATE_COLLATION),
+							 errmsg("no collation was derived for column \"%s\" with collatable type %s",
+									col->colname, format_type_be(typeOid)),
+							 errhint("Use the COLLATE clause to set the collation explicitly.")));
+			}
+
+			attrList = lappend(attrList, col);
+		}
+	}
+
+	if (lc != NULL)
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("too many column names were specified")));
+
+	/* finally create the relation definition */
+	return create_ctas_internal(attrList, into);
+}
+
 
 /*
  * ExecCreateTableAs -- execute a CREATE TABLE AS command
@@ -157,10 +309,24 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
 
 	/* SELECT should never rewrite to more or less than one SELECT query */
 	if (list_length(rewritten) != 1)
-		elog(ERROR, "unexpected rewrite result for CREATE TABLE AS SELECT");
+	{
+		elog(ERROR, "unexpected rewrite result for %s SELECT",
+			 is_matview ? "CREATE MATERIALIZED VIEW" : "CREATE TABLE AS");
+	}
 	query = (Query *) linitial(rewritten);
 	Assert(query->commandType == CMD_SELECT);
 
+	/*
+	 * For a relation with no data needed, do not go through the parser and
+	 * the executor. Just define the relation with everything it needs and
+	 * leave.
+	 */
+	if (into->skipData)
+	{
+		CreateAsReladdr = create_ctas_nodata(query->targetList, into);
+		goto finish;
+	}
+
 	/* plan the query */
 	plan = pg_plan_query(query, 0, params);
 
@@ -182,14 +348,8 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
 	/* call ExecutorStart to prepare the plan for execution */
 	ExecutorStart(queryDesc, GetIntoRelEFlags(into));
 
-	/*
-	 * Normally, we run the plan to completion; but if skipData is specified,
-	 * just do tuple receiver startup and shutdown.
-	 */
-	if (into->skipData)
-		dir = NoMovementScanDirection;
-	else
-		dir = ForwardScanDirection;
+	/* run the plan to completion */
+	dir = ForwardScanDirection;
 
 	/* run the plan */
 	ExecutorRun(queryDesc, dir, 0L);
@@ -207,6 +367,8 @@ ExecCreateTableAs(CreateTableAsStmt *stmt, const char *queryString,
 
 	PopActiveSnapshot();
 
+finish:
+
 	if (is_matview)
 	{
 		/* Roll back any GUC changes */
@@ -287,14 +449,12 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 	IntoClause *into = myState->into;
 	bool		is_matview;
 	char		relkind;
-	CreateStmt *create;
 	ObjectAddress intoRelationAddr;
 	Relation	intoRelationDesc;
+	List	   *attrList;
 	RangeTblEntry *rte;
-	Datum		toast_options;
 	ListCell   *lc;
 	int			attnum;
-	static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
 
 	Assert(into != NULL);		/* else somebody forgot to set it */
 
@@ -303,62 +463,30 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 	relkind = is_matview ? RELKIND_MATVIEW : RELKIND_RELATION;
 
 	/*
-	 * Create the target relation by faking up a CREATE TABLE parsetree and
-	 * passing it to DefineRelation.
-	 */
-	create = makeNode(CreateStmt);
-	create->relation = into->rel;
-	create->tableElts = NIL;	/* will fill below */
-	create->inhRelations = NIL;
-	create->ofTypename = NULL;
-	create->constraints = NIL;
-	create->options = into->options;
-	create->oncommit = into->onCommit;
-	create->tablespacename = into->tableSpaceName;
-	create->if_not_exists = false;
-
-	/*
 	 * Build column definitions using "pre-cooked" type and collation info. If
 	 * a column name list was specified in CREATE TABLE AS, override the
 	 * column names derived from the query.  (Too few column names are OK, too
 	 * many are not.)
 	 */
+	attrList = NIL;
 	lc = list_head(into->colNames);
 	for (attnum = 0; attnum < typeinfo->natts; attnum++)
 	{
 		Form_pg_attribute attribute = typeinfo->attrs[attnum];
-		ColumnDef  *col = makeNode(ColumnDef);
-		TypeName   *coltype = makeNode(TypeName);
+		ColumnDef  *col;
+		char	   *colname;
 
 		if (lc)
 		{
-			col->colname = strVal(lfirst(lc));
+			colname = strVal(lfirst(lc));
 			lc = lnext(lc);
 		}
 		else
-			col->colname = NameStr(attribute->attname);
-		col->typeName = coltype;
-		col->inhcount = 0;
-		col->is_local = true;
-		col->is_not_null = false;
-		col->is_from_type = false;
-		col->storage = 0;
-		col->raw_default = NULL;
-		col->cooked_default = NULL;
-		col->collClause = NULL;
-		col->collOid = attribute->attcollation;
-		col->constraints = NIL;
-		col->fdwoptions = NIL;
-		col->location = -1;
-
-		coltype->names = NIL;
-		coltype->typeOid = attribute->atttypid;
-		coltype->setof = false;
-		coltype->pct_type = false;
-		coltype->typmods = NIL;
-		coltype->typemod = attribute->atttypmod;
-		coltype->arrayBounds = NIL;
-		coltype->location = -1;
+			colname = NameStr(attribute->attname);
+
+		col = makeColumnDef(colname, attribute->atttypid,
+							attribute->atttypmod,
+							attribute->attcollation);
 
 		/*
 		 * It's possible that the column is of a collatable type but the
@@ -366,15 +494,19 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 		 * this here because DefineRelation would adopt the type's default
 		 * collation rather than complaining.)
 		 */
-		if (!OidIsValid(col->collOid) &&
-			type_is_collatable(coltype->typeOid))
-			ereport(ERROR,
-					(errcode(ERRCODE_INDETERMINATE_COLLATION),
-					 errmsg("no collation was derived for column \"%s\" with collatable type %s",
-							col->colname, format_type_be(coltype->typeOid)),
-					 errhint("Use the COLLATE clause to set the collation explicitly.")));
-
-		create->tableElts = lappend(create->tableElts, col);
+		if (!OidIsValid(col->collOid))
+		{
+			Oid typeOid = col->typeName->typeOid;
+
+			if (type_is_collatable(typeOid))
+				ereport(ERROR,
+						(errcode(ERRCODE_INDETERMINATE_COLLATION),
+						 errmsg("no collation was derived for column \"%s\" with collatable type %s",
+								col->colname, format_type_be(typeOid)),
+						 errhint("Use the COLLATE clause to set the collation explicitly.")));
+		}
+
+		attrList = lappend(attrList, col);
 	}
 
 	if (lc != NULL)
@@ -382,38 +514,8 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 				(errcode(ERRCODE_SYNTAX_ERROR),
 				 errmsg("too many column names were specified")));
 
-	/*
-	 * Actually create the target table
-	 */
-	intoRelationAddr = DefineRelation(create, relkind, InvalidOid, NULL);
-
-	/*
-	 * If necessary, create a TOAST table for the target table.  Note that
-	 * NewRelationCreateToastTable ends with CommandCounterIncrement(), so
-	 * that the TOAST table will be visible for insertion.
-	 */
-	CommandCounterIncrement();
-
-	/* parse and validate reloptions for the toast table */
-	toast_options = transformRelOptions((Datum) 0,
-										create->options,
-										"toast",
-										validnsps,
-										true, false);
-
-	(void) heap_reloptions(RELKIND_TOASTVALUE, toast_options, true);
-
-	NewRelationCreateToastTable(intoRelationAddr.objectId, toast_options);
-
-	/* Create the "view" part of a materialized view. */
-	if (is_matview)
-	{
-		/* StoreViewQuery scribbles on tree, so make a copy */
-		Query	   *query = (Query *) copyObject(into->viewQuery);
-
-		StoreViewQuery(intoRelationAddr.objectId, query, false);
-		CommandCounterIncrement();
-	}
+	/* create the relation definition */
+	intoRelationAddr = create_ctas_internal(attrList, into);
 
 	/*
 	 * Finally we can open the target table
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c
index e9d9ba2..929c490 100644
--- a/src/backend/commands/view.c
+++ b/src/backend/commands/view.c
@@ -86,21 +86,10 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
 
 		if (!tle->resjunk)
 		{
-			ColumnDef  *def = makeNode(ColumnDef);
-
-			def->colname = pstrdup(tle->resname);
-			def->typeName = makeTypeNameFromOid(exprType((Node *) tle->expr),
-											 exprTypmod((Node *) tle->expr));
-			def->inhcount = 0;
-			def->is_local = true;
-			def->is_not_null = false;
-			def->is_from_type = false;
-			def->storage = 0;
-			def->raw_default = NULL;
-			def->cooked_default = NULL;
-			def->collClause = NULL;
-			def->collOid = exprCollation((Node *) tle->expr);
-			def->location = -1;
+			ColumnDef  *def = makeColumnDef(tle->resname,
+											exprType((Node *) tle->expr),
+											exprTypmod((Node *) tle->expr),
+											exprCollation((Node *) tle->expr));
 
 			/*
 			 * It's possible that the column is of a collatable type but the
@@ -117,7 +106,6 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
 			}
 			else
 				Assert(!OidIsValid(def->collOid));
-			def->constraints = NIL;
 
 			attrList = lappend(attrList, def);
 		}
diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
index f625c32..f930f57 100644
--- a/src/backend/nodes/makefuncs.c
+++ b/src/backend/nodes/makefuncs.c
@@ -477,6 +477,33 @@ makeTypeNameFromOid(Oid typeOid, int32 typmod)
 }
 
 /*
+ * makeColumnDef -
+ *  build a ColumnDef node to represent a column definition.
+ */
+ColumnDef *
+makeColumnDef(char *colname, Oid typeOid, int32 typmod, Oid collOid)
+{
+	ColumnDef   *n = makeNode(ColumnDef);
+
+	n->colname = pstrdup(colname);
+	n->typeName = makeTypeNameFromOid(typeOid, typmod);
+	n->inhcount = 0;
+	n->is_local = true;
+	n->is_not_null = false;
+	n->is_from_type = false;
+	n->storage = 0;
+	n->raw_default = NULL;
+	n->cooked_default = NULL;
+	n->collClause = NULL;
+	n->collOid = collOid;
+	n->location = -1;
+	n->constraints = NIL;
+	n->fdwoptions = NIL;
+
+	return n;
+}
+
+/*
  * makeFuncExpr -
  *	build an expression tree representing a function call.
  *
diff --git a/src/include/nodes/makefuncs.h b/src/include/nodes/makefuncs.h
index ef34a6e..042ee7c 100644
--- a/src/include/nodes/makefuncs.h
+++ b/src/include/nodes/makefuncs.h
@@ -43,6 +43,9 @@ extern TargetEntry *makeTargetEntry(Expr *expr,
 				char *resname,
 				bool resjunk);
 
+extern ColumnDef *makeColumnDef(char *colname, Oid typeOid,
+				int32 typmod, Oid collOid);
+
 extern TargetEntry *flatCopyTargetEntry(TargetEntry *src_tle);
 
 extern FromExpr *makeFromExpr(List *fromlist, Node *quals);
diff --git a/src/test/regress/expected/matview.out b/src/test/regress/expected/matview.out
index 7f9741e..bb62d4b 100644
--- a/src/test/regress/expected/matview.out
+++ b/src/test/regress/expected/matview.out
@@ -455,13 +455,23 @@ DROP TABLE mvtest_boxes CASCADE;
 NOTICE:  drop cascades to materialized view mvtest_boxmv
 -- make sure that column names are handled correctly
 CREATE TABLE mvtest_v (i int, j int);
-CREATE MATERIALIZED VIEW mvtest_mv_v (ii) AS SELECT i, j AS jj FROM mvtest_v;
+CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj, kk) AS SELECT i, j FROM mvtest_v; -- error
+ERROR:  too many column names were specified
+CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj) AS SELECT i, j FROM mvtest_v; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_2 (ii) AS SELECT i, j FROM mvtest_v; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj, kk) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- error
+ERROR:  too many column names were specified
+CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_4 (ii) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
 ALTER TABLE mvtest_v RENAME COLUMN i TO x;
 INSERT INTO mvtest_v values (1, 2);
 CREATE UNIQUE INDEX mvtest_mv_v_ii ON mvtest_mv_v (ii);
 REFRESH MATERIALIZED VIEW mvtest_mv_v;
 UPDATE mvtest_v SET j = 3 WHERE x = 1;
 REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv_v;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_2;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_3;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_4;
 SELECT * FROM mvtest_v;
  x | j 
 ---+---
@@ -474,8 +484,30 @@ SELECT * FROM mvtest_mv_v;
   1 |  3
 (1 row)
 
+SELECT * FROM mvtest_mv_v_2;
+ ii | j 
+----+---
+  1 | 3
+(1 row)
+
+SELECT * FROM mvtest_mv_v_3;
+ ii | jj 
+----+----
+  1 |  3
+(1 row)
+
+SELECT * FROM mvtest_mv_v_4;
+ ii | j 
+----+---
+  1 | 3
+(1 row)
+
 DROP TABLE mvtest_v CASCADE;
-NOTICE:  drop cascades to materialized view mvtest_mv_v
+NOTICE:  drop cascades to 4 other objects
+DETAIL:  drop cascades to materialized view mvtest_mv_v
+drop cascades to materialized view mvtest_mv_v_2
+drop cascades to materialized view mvtest_mv_v_3
+drop cascades to materialized view mvtest_mv_v_4
 -- make sure that matview rows can be referenced as source rows (bug #9398)
 CREATE TABLE mvtest_v AS SELECT generate_series(1,10) AS a;
 CREATE MATERIALIZED VIEW mvtest_mv_v AS SELECT a FROM mvtest_v WHERE a <= 5;
diff --git a/src/test/regress/expected/select_into.out b/src/test/regress/expected/select_into.out
index b577d1b..cee77e7 100644
--- a/src/test/regress/expected/select_into.out
+++ b/src/test/regress/expected/select_into.out
@@ -50,6 +50,44 @@ DETAIL:  drop cascades to table selinto_schema.tmp1
 drop cascades to table selinto_schema.tmp2
 drop cascades to table selinto_schema.tmp3
 DROP USER selinto_user;
+-- Tests for WITH NO DATA and column name consistency
+CREATE TABLE ctas_base (i int, j int);
+INSERT INTO ctas_base VALUES (1, 2);
+CREATE TABLE ctas_nodata (ii, jj, kk) AS SELECT i, j FROM ctas_base; -- Error
+ERROR:  too many column names were specified
+CREATE TABLE ctas_nodata (ii, jj, kk) AS SELECT i, j FROM ctas_base WITH NO DATA; -- Error
+ERROR:  too many column names were specified
+CREATE TABLE ctas_nodata (ii, jj) AS SELECT i, j FROM ctas_base; -- OK
+CREATE TABLE ctas_nodata_2 (ii, jj) AS SELECT i, j FROM ctas_base WITH NO DATA; -- OK
+CREATE TABLE ctas_nodata_3 (ii) AS SELECT i, j FROM ctas_base; -- OK
+CREATE TABLE ctas_nodata_4 (ii) AS SELECT i, j FROM ctas_base WITH NO DATA; -- OK
+SELECT * FROM ctas_nodata;
+ ii | jj 
+----+----
+  1 |  2
+(1 row)
+
+SELECT * FROM ctas_nodata_2;
+ ii | jj 
+----+----
+(0 rows)
+
+SELECT * FROM ctas_nodata_3;
+ ii | j 
+----+---
+  1 | 2
+(1 row)
+
+SELECT * FROM ctas_nodata_4;
+ ii | j 
+----+---
+(0 rows)
+
+DROP TABLE ctas_base;
+DROP TABLE ctas_nodata;
+DROP TABLE ctas_nodata_2;
+DROP TABLE ctas_nodata_3;
+DROP TABLE ctas_nodata_4;
 --
 -- CREATE TABLE AS/SELECT INTO as last command in a SQL function
 -- have been known to cause problems
diff --git a/src/test/regress/sql/matview.sql b/src/test/regress/sql/matview.sql
index 002698a..722393b 100644
--- a/src/test/regress/sql/matview.sql
+++ b/src/test/regress/sql/matview.sql
@@ -176,15 +176,26 @@ DROP TABLE mvtest_boxes CASCADE;
 
 -- make sure that column names are handled correctly
 CREATE TABLE mvtest_v (i int, j int);
-CREATE MATERIALIZED VIEW mvtest_mv_v (ii) AS SELECT i, j AS jj FROM mvtest_v;
+CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj, kk) AS SELECT i, j FROM mvtest_v; -- error
+CREATE MATERIALIZED VIEW mvtest_mv_v (ii, jj) AS SELECT i, j FROM mvtest_v; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_2 (ii) AS SELECT i, j FROM mvtest_v; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj, kk) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- error
+CREATE MATERIALIZED VIEW mvtest_mv_v_3 (ii, jj) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
+CREATE MATERIALIZED VIEW mvtest_mv_v_4 (ii) AS SELECT i, j FROM mvtest_v WITH NO DATA; -- ok
 ALTER TABLE mvtest_v RENAME COLUMN i TO x;
 INSERT INTO mvtest_v values (1, 2);
 CREATE UNIQUE INDEX mvtest_mv_v_ii ON mvtest_mv_v (ii);
 REFRESH MATERIALIZED VIEW mvtest_mv_v;
 UPDATE mvtest_v SET j = 3 WHERE x = 1;
 REFRESH MATERIALIZED VIEW CONCURRENTLY mvtest_mv_v;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_2;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_3;
+REFRESH MATERIALIZED VIEW mvtest_mv_v_4;
 SELECT * FROM mvtest_v;
 SELECT * FROM mvtest_mv_v;
+SELECT * FROM mvtest_mv_v_2;
+SELECT * FROM mvtest_mv_v_3;
+SELECT * FROM mvtest_mv_v_4;
 DROP TABLE mvtest_v CASCADE;
 
 -- make sure that matview rows can be referenced as source rows (bug #9398)
diff --git a/src/test/regress/sql/select_into.sql b/src/test/regress/sql/select_into.sql
index e4460ae..632077c 100644
--- a/src/test/regress/sql/select_into.sql
+++ b/src/test/regress/sql/select_into.sql
@@ -53,6 +53,25 @@ RESET SESSION AUTHORIZATION;
 DROP SCHEMA selinto_schema CASCADE;
 DROP USER selinto_user;
 
+-- Tests for WITH NO DATA and column name consistency
+CREATE TABLE ctas_base (i int, j int);
+INSERT INTO ctas_base VALUES (1, 2);
+CREATE TABLE ctas_nodata (ii, jj, kk) AS SELECT i, j FROM ctas_base; -- Error
+CREATE TABLE ctas_nodata (ii, jj, kk) AS SELECT i, j FROM ctas_base WITH NO DATA; -- Error
+CREATE TABLE ctas_nodata (ii, jj) AS SELECT i, j FROM ctas_base; -- OK
+CREATE TABLE ctas_nodata_2 (ii, jj) AS SELECT i, j FROM ctas_base WITH NO DATA; -- OK
+CREATE TABLE ctas_nodata_3 (ii) AS SELECT i, j FROM ctas_base; -- OK
+CREATE TABLE ctas_nodata_4 (ii) AS SELECT i, j FROM ctas_base WITH NO DATA; -- OK
+SELECT * FROM ctas_nodata;
+SELECT * FROM ctas_nodata_2;
+SELECT * FROM ctas_nodata_3;
+SELECT * FROM ctas_nodata_4;
+DROP TABLE ctas_base;
+DROP TABLE ctas_nodata;
+DROP TABLE ctas_nodata_2;
+DROP TABLE ctas_nodata_3;
+DROP TABLE ctas_nodata_4;
+
 --
 -- CREATE TABLE AS/SELECT INTO as last command in a SQL function
 -- have been known to cause problems
