From e5dac00c23e834463252e0c6b5669743c023ee40 Mon Sep 17 00:00:00 2001
From: Onder Kalaci <onderkalaci@gmail.com>
Date: Tue, 9 Aug 2022 14:28:14 +0200
Subject: [PATCH] Allow MATERIALIZED VIEW Rewrite when event triggers exists

It looks like we forgot to allow table rewrite
on PG_CMDTAG. This commit fixes that.
---
 src/include/tcop/cmdtaglist.h               |  2 +-
 src/test/regress/expected/event_trigger.out |  9 +++++++++
 src/test/regress/sql/event_trigger.sql      | 12 ++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/include/tcop/cmdtaglist.h b/src/include/tcop/cmdtaglist.h
index 2b1163ce33..9e94f44c5f 100644
--- a/src/include/tcop/cmdtaglist.h
+++ b/src/include/tcop/cmdtaglist.h
@@ -42,7 +42,7 @@ PG_CMDTAG(CMDTAG_ALTER_FUNCTION, "ALTER FUNCTION", true, false, false)
 PG_CMDTAG(CMDTAG_ALTER_INDEX, "ALTER INDEX", true, false, false)
 PG_CMDTAG(CMDTAG_ALTER_LANGUAGE, "ALTER LANGUAGE", true, false, false)
 PG_CMDTAG(CMDTAG_ALTER_LARGE_OBJECT, "ALTER LARGE OBJECT", true, false, false)
-PG_CMDTAG(CMDTAG_ALTER_MATERIALIZED_VIEW, "ALTER MATERIALIZED VIEW", true, false, false)
+PG_CMDTAG(CMDTAG_ALTER_MATERIALIZED_VIEW, "ALTER MATERIALIZED VIEW", true, true, false)
 PG_CMDTAG(CMDTAG_ALTER_OPERATOR, "ALTER OPERATOR", true, false, false)
 PG_CMDTAG(CMDTAG_ALTER_OPERATOR_CLASS, "ALTER OPERATOR CLASS", true, false, false)
 PG_CMDTAG(CMDTAG_ALTER_OPERATOR_FAMILY, "ALTER OPERATOR FAMILY", true, false, false)
diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out
index c95c30b314..df8b5f4f2e 100644
--- a/src/test/regress/expected/event_trigger.out
+++ b/src/test/regress/expected/event_trigger.out
@@ -606,6 +606,15 @@ SELECT
  start_rls_command | event trigger start_rls_command | event trigger | {start_rls_command} | {}          | ("event trigger",,start_rls_command,start_rls_command)
 (3 rows)
 
+-- Create a heap2 table am handler with heapam handler
+CREATE TABLE heaptable USING heap AS
+SELECT a, repeat(a::text, 100) FROM generate_series(1,9) AS a;
+CREATE MATERIALIZED VIEW heapmv USING heap AS SELECT * FROM heaptable;
+-- we can rewrite materialized views when there are event triggers
+ALTER MATERIALIZED VIEW heapmv SET ACCESS METHOD heap2;
+-- clean up objects created for testing rewrite mat. view when event triggers exist
+DROP MATERIALIZED VIEW heapmv;
+DROP TABLE heaptable;
 DROP EVENT TRIGGER start_rls_command;
 DROP EVENT TRIGGER end_rls_command;
 DROP EVENT TRIGGER sql_drop_command;
diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql
index 5e45e3f190..f2d656010c 100644
--- a/src/test/regress/sql/event_trigger.sql
+++ b/src/test/regress/sql/event_trigger.sql
@@ -463,6 +463,18 @@ SELECT
     LATERAL pg_get_object_address(b.type, b.object_names, b.object_args) as a
   ORDER BY e.evtname;
 
+-- Create a heap2 table am handler with heapam handler
+CREATE TABLE heaptable USING heap AS
+SELECT a, repeat(a::text, 100) FROM generate_series(1,9) AS a;
+CREATE MATERIALIZED VIEW heapmv USING heap AS SELECT * FROM heaptable;
+
+-- we can rewrite materialized views when there are event triggers
+ALTER MATERIALIZED VIEW heapmv SET ACCESS METHOD heap2;
+
+-- clean up objects created for testing rewrite mat. view when event triggers exist
+DROP MATERIALIZED VIEW heapmv;
+DROP TABLE heaptable;
+
 DROP EVENT TRIGGER start_rls_command;
 DROP EVENT TRIGGER end_rls_command;
 DROP EVENT TRIGGER sql_drop_command;
-- 
2.34.1

