From abe5b204d5473488f4861eed5b6acd783a84123f Mon Sep 17 00:00:00 2001
From: "Zheng (Zane) Li" <zhelli@amazon.com>
Date: Tue, 22 Mar 2022 23:47:52 +0000
Subject: [PATCH 4/6] Enable replication of CREATE MATERIALIZED VIEW AS stmt.
 Add tests for both CREATE MATERIALIZED VIEW and CREATE VIEW.

---
 src/backend/tcop/utility.c              | 42 +++++++++++++++++++++++--
 src/test/subscription/t/030_rep_ddls.pl | 18 +++++++++++
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 1c8dae4f6b..b764d9df3d 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1140,6 +1140,45 @@ LogLogicalDDLCommand(Node *parsetree, const char *queryString)
 			}
 			break;
 
+		/*
+		 * CreateTableAsStmt can create either a table a materialized view
+		 * and they are handled differently.
+		 */
+		case T_CreateTableAsStmt:
+		{
+			CreateTableAsStmt *stmt = (CreateTableAsStmt *) parsetree;
+			switch(stmt->objtype)
+			{
+				case OBJECT_TABLE:
+					/*
+					 * FIXME CREATE TABLE AS stmt needs to be broken down into two parts
+					 * 1. A normal CREATE TABLE string that get's logged and replicated via
+					 *	  DDL replication.
+					 * 2. Insertions that get replicated by DML replication.
+					 */
+					break;
+				case OBJECT_MATVIEW:
+					/*
+					 * Log CREATE MATERIALIZED VIEW AS stmt for logical replication if
+					 * there is any FOR ALL TABLES publication with pubddl_database on.
+					 * i.e. Database level DDL replication is on for some publication.
+					 */
+					if (ddl_need_xlog(InvalidOid, true, true))
+					{
+						bool transactional = true;
+						const char* prefix = "";
+						LogLogicalDDLMessage(prefix,
+											 GetUserId(),
+											 queryString,
+											 strlen(queryString),
+											 transactional);
+					}
+				default:
+					break;
+			}
+			break;
+		}
+
 		/*
 		 * Secondly, commands that may be allowed in Table level DDL replication.
 		 * These are currently handled in the later execution path of the command.
@@ -1155,7 +1194,7 @@ LogLogicalDDLCommand(Node *parsetree, const char *queryString)
 		/* DropStmt depends on the removeType */
 		case T_DropStmt:
 		{
-			DropStmt* stmt = (DropStmt*) parsetree;
+			DropStmt *stmt = (DropStmt *) parsetree;
 			switch (stmt->removeType)
 			{
 				/* Maybe allowed in Table level DDL replication, handled in later code path */
@@ -1206,7 +1245,6 @@ LogLogicalDDLCommand(Node *parsetree, const char *queryString)
 		case T_RuleStmt:
 		case T_CreateSeqStmt:
 		case T_AlterSeqStmt:
-		case T_CreateTableAsStmt:
 		case T_RefreshMatViewStmt:
 		case T_CreatePLangStmt:
 		case T_CreateConversionStmt:
diff --git a/src/test/subscription/t/030_rep_ddls.pl b/src/test/subscription/t/030_rep_ddls.pl
index c88c4ea1c0..562efe2cf7 100644
--- a/src/test/subscription/t/030_rep_ddls.pl
+++ b/src/test/subscription/t/030_rep_ddls.pl
@@ -226,6 +226,24 @@ $node_publisher->wait_for_catchup('mysub');
 $result = $node_subscriber->safe_psql('postgres', "SELECT tableowner from pg_catalog.pg_tables where tablename = 't5';");
 is($result, qq(ddl_replication_user), 'Owner of t5 is correct');
 
+# Test CREATE MATERIALIZED VIEW stmt is replicated
+$node_publisher->safe_psql('postgres', "CREATE MATERIALIZED VIEW s1.matview1 AS SELECT a, b from s1.t1;");
+$result = $node_publisher->safe_psql('postgres', "SELECT count(*) from s1.matview1;");
+
+$node_publisher->wait_for_catchup('mysub');
+
+$result_sub = $node_subscriber->safe_psql('postgres', "SELECT count(*) from s1.matview1;");
+is($result, qq($result_sub), 'CREATE of s1.matview1 is replicated');
+
+# Test CREATE VIEW stmt is replicated
+$node_publisher->safe_psql('postgres', "CREATE VIEW s1.view1 AS SELECT a, b from s1.t1;");
+$result = $node_publisher->safe_psql('postgres', "SELECT count(*) from s1.view1;");
+
+$node_publisher->wait_for_catchup('mysub');
+
+$result_sub = $node_subscriber->safe_psql('postgres', "SELECT count(*) from s1.view1;");
+is($result, qq($result_sub), 'CREATE of s1.view1 is replicated');
+
 #TODO TEST certain DDLs are not replicated
 
 pass "DDL replication tests passed!";
-- 
2.32.0

