0004-Enable-replication-of-CREATE-MATERIALIZED-VIEW-AS-st.patch

application/octet-stream

Filename: 0004-Enable-replication-of-CREATE-MATERIALIZED-VIEW-AS-st.patch
Type: application/octet-stream
Part: 4
Message: Re: Support logical replication of DDLs

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0004
Subject: Enable replication of CREATE MATERIALIZED VIEW AS stmt. Add tests for both CREATE MATERIALIZED VIEW and CREATE VIEW.
File+
src/backend/tcop/utility.c 40 2
src/test/subscription/t/030_rep_ddls.pl 18 0
From b9b3ca9f7e5749ca0c89a88ab5b5bd990a8598a0 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 04/12] 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 d0bb232af6..79b5faccc0 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