@@ -329,18 +329,26 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
329329 /* An existing materialized view can be replaced. */
330330 if (is_matview && into -> replace )
331331 {
332- RefreshMatViewStmt * refresh ;
333-
334332 /* Change the relation to match the new query and other options. */
335- (void ) create_ctas_nodata (query -> targetList , into );
333+ address = create_ctas_nodata (query -> targetList , into );
334+
335+ /*
336+ * Refresh the materialized view with a fake statement unless we
337+ * must keep the old data.
338+ */
339+ if (!into -> keepData )
340+ {
341+ RefreshMatViewStmt * refresh ;
342+
343+ refresh = makeNode (RefreshMatViewStmt );
344+ refresh -> relation = into -> rel ;
345+ refresh -> skipData = into -> skipData ;
346+ refresh -> concurrent = false;
336347
337- /* Refresh the materialized view with a fake statement. */
338- refresh = makeNode (RefreshMatViewStmt );
339- refresh -> relation = into -> rel ;
340- refresh -> skipData = into -> skipData ;
341- refresh -> concurrent = false;
348+ address = ExecRefreshMatView (refresh , pstate -> p_sourcetext , qc );
349+ }
342350
343- return ExecRefreshMatView ( refresh , pstate -> p_sourcetext , qc ) ;
351+ return address ;
344352 }
345353
346354 return InvalidObjectAddress ;
@@ -383,6 +391,9 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
383391 */
384392 if (is_matview )
385393 {
394+ if (into -> keepData )
395+ elog (ERROR , "must not specify WITH OLD DATA when creating a new materialized view" );
396+
386397 do_refresh = !into -> skipData ;
387398 into -> skipData = true;
388399 }
0 commit comments