From ec9b36b392a3b56ce31340f537accb116c70d6dd Mon Sep 17 00:00:00 2001 From: Rachel Phan Date: Mon, 26 Sep 2022 08:47:00 -0400 Subject: [PATCH] [#10512] YSQL: Adding support for ALTER MATERIALIZED VIEW MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: - Alter a MV’s owner: `ALTER MATERIALIZED VIEW OWNER TO { new_owner | CURRENT_USER | SESSION_USER }` - Rename MV: `ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME TO new_name` - Rename MV’s column: `ALTER MATERIALIZED VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO new_column_name` Test Plan: `TestPgRegressMatview` Reviewers: fizaa, plee Reviewed By: fizaa, plee Subscribers: jenkins-bot, yql Differential Revision: https://phabricator.dev.yugabyte.com/D19508 --- src/postgres/src/backend/commands/ybccmds.c | 20 +++-- src/postgres/src/backend/parser/gram.y | 6 -- .../src/test/regress/expected/yb_matview.out | 73 ++++++++++++++++++- .../src/test/regress/sql/yb_matview.sql | 51 ++++++++++++- 4 files changed, 137 insertions(+), 13 deletions(-) diff --git a/src/postgres/src/backend/commands/ybccmds.c b/src/postgres/src/backend/commands/ybccmds.c index 0a84469262ae..e6ec15b058cc 100644 --- a/src/postgres/src/backend/commands/ybccmds.c +++ b/src/postgres/src/backend/commands/ybccmds.c @@ -916,7 +916,7 @@ YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles, return handles; } } - Oid relationId = RelationGetRelid(rel); + Oid relationId = YbGetStorageRelid(rel); switch (cmd->subtype) { case AT_AddColumn: @@ -1252,7 +1252,7 @@ YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles, HandleYBStatus( YBCPgNewAlterTable( YBCGetDatabaseOidByRelid(relationId), - relationId, + YbGetStorageRelid(dependent_rel), &alter_cmd_handle)); HandleYBStatus( YBCPgAlterTableIncrementSchemaVersion(alter_cmd_handle)); @@ -1290,7 +1290,7 @@ YBCPrepareAlterTable(List** subcmds, List *handles = NIL; YBCPgStatement db_handle = NULL; HandleYBStatus(YBCPgNewAlterTable(YBCGetDatabaseOidByRelid(relationId), - relationId, + YbGetStorageRelid(rel), &db_handle)); handles = lappend(handles, db_handle); ListCell *lcmd; @@ -1334,9 +1334,18 @@ YBCRename(RenameStmt *stmt, Oid relationId) YBCPgStatement handle = NULL; Oid databaseId = YBCGetDatabaseOidByRelid(relationId); char *db_name = get_database_name(databaseId); + Relation rel; switch (stmt->renameType) { + case OBJECT_MATVIEW: + rel = RelationIdGetRelation(relationId); + HandleYBStatus(YBCPgNewAlterTable(databaseId, + YbGetStorageRelid(rel), + &handle)); + HandleYBStatus(YBCPgAlterTableRenameTable(handle, db_name, stmt->newname)); + RelationClose(rel); + break; case OBJECT_INDEX: case OBJECT_TABLE: HandleYBStatus(YBCPgNewAlterTable(databaseId, @@ -1347,12 +1356,13 @@ YBCRename(RenameStmt *stmt, Oid relationId) case OBJECT_COLUMN: case OBJECT_ATTRIBUTE: - + rel = RelationIdGetRelation(relationId); HandleYBStatus(YBCPgNewAlterTable(databaseId, - relationId, + YbGetStorageRelid(rel), &handle)); HandleYBStatus(YBCPgAlterTableRenameColumn(handle, stmt->subname, stmt->newname)); + RelationClose(rel); break; default: diff --git a/src/postgres/src/backend/parser/gram.y b/src/postgres/src/backend/parser/gram.y index b3f0f08d1c6c..869260539b1e 100644 --- a/src/postgres/src/backend/parser/gram.y +++ b/src/postgres/src/backend/parser/gram.y @@ -2054,7 +2054,6 @@ AlterTableStmt: } | ALTER MATERIALIZED VIEW qualified_name alter_table_cmds { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); AlterTableStmt *n = makeNode(AlterTableStmt); n->relation = $4; n->cmds = $5; @@ -2064,7 +2063,6 @@ AlterTableStmt: } | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name alter_table_cmds { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); AlterTableStmt *n = makeNode(AlterTableStmt); n->relation = $6; n->cmds = $7; @@ -9467,7 +9465,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name } | ALTER MATERIALIZED VIEW qualified_name RENAME TO name { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); RenameStmt *n = makeNode(RenameStmt); n->renameType = OBJECT_MATVIEW; n->relation = $4; @@ -9478,7 +9475,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name } | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME TO name { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); RenameStmt *n = makeNode(RenameStmt); n->renameType = OBJECT_MATVIEW; n->relation = $6; @@ -9552,7 +9548,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name } | ALTER MATERIALIZED VIEW qualified_name RENAME opt_column name TO name { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); RenameStmt *n = makeNode(RenameStmt); n->renameType = OBJECT_COLUMN; n->relationType = OBJECT_MATVIEW; @@ -9564,7 +9559,6 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name } | ALTER MATERIALIZED VIEW IF_P EXISTS qualified_name RENAME opt_column name TO name { - parser_ybc_signal_unsupported(@1, "ALTER MATERIALIZED VIEW", 1131); RenameStmt *n = makeNode(RenameStmt); n->renameType = OBJECT_COLUMN; n->relationType = OBJECT_MATVIEW; diff --git a/src/postgres/src/test/regress/expected/yb_matview.out b/src/postgres/src/test/regress/expected/yb_matview.out index 63f112908f4b..cb74202d7064 100644 --- a/src/postgres/src/test/regress/expected/yb_matview.out +++ b/src/postgres/src/test/regress/expected/yb_matview.out @@ -27,6 +27,77 @@ SELECT * from pg_temp__123; DROP TABLE test_yb CASCADE; NOTICE: drop cascades to materialized view mtest_yb +-- Alter materialized view - rename matview and rename columns +CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL); +INSERT INTO test_yb VALUES (1, 'xyz', 2); +CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb; +CREATE UNIQUE INDEX unique_IDX ON mtest_YB(id); +ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1; +SELECT * FROM mtest_yb; -- error +ERROR: relation "mtest_yb" does not exist +LINE 1: SELECT * FROM mtest_yb; + ^ +SELECT * from mtest_yb1; -- ok + id | type | val +----+------+----- + 1 | xyz | 2 +(1 row) + +REFRESH MATERIALIZED VIEW mtest_yb1; +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; +ALTER MATERIALIZED VIEW mtest_yb1 RENAME TO mtest_yb2; +SELECT * from mtest_yb2; + id | type | val +----+------+----- + 1 | xyz | 2 +(1 row) + +REFRESH MATERIALIZED VIEW mtest_yb2; +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb2; +ALTER MATERIALIZED VIEW mtest_yb2 RENAME val TO total; -- test Alter Rename Column +SELECT * FROM mtest_yb2; + id | type | total +----+------+------- + 1 | xyz | 2 +(1 row) + +DROP TABLE test_yb CASCADE; +NOTICE: drop cascades to materialized view mtest_yb2 +-- Alter materialized view - change owner +CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL); +INSERT INTO test_yb VALUES (1, 'xyz', 2); +CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb; +CREATE UNIQUE INDEX unique_IDX ON mtest_yb(id); +CREATE ROLE test_mv_user; +SET ROLE test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- error +ERROR: must be owner of materialized view mtest_yb +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error +ERROR: must be owner of materialized view mtest_yb +SET ROLE yugabyte; +ALTER MATERIALIZED VIEW mtest_yb OWNER TO test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- error +ERROR: permission denied for table test_yb +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error +ERROR: permission denied for table test_yb +ALTER TABLE test_yb OWNER TO test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok +ALTER MATERIALIZED VIEW mtest_yb OWNER TO SESSION_USER; +ALTER TABLE test_yb OWNER TO SESSION_USER; +REFRESH MATERIALIZED VIEW mtest_yb; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok +ALTER MATERIALIZED VIEW mtest_yb RENAME val TO amt; +ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1; +CREATE ROLE test_mv_superuser SUPERUSER; +ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO test_mv_superuser; +REFRESH MATERIALIZED VIEW mtest_yb1; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; -- ok +ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO CURRENT_USER; +DROP ROLE test_mv_user; +DROP ROLE test_mv_superuser; +DROP TABLE test_yb CASCADE; +NOTICE: drop cascades to materialized view mtest_yb1 -- Test special characters in an attribute's name CREATE TABLE test_yb ("xyzID''\\b" int NOT NULL, "y" int); INSERT INTO test_yb VALUES (1); @@ -277,4 +348,4 @@ DROP MATERIALIZED VIEW mv; SELECT * FROM mv; ERROR: relation "mv" does not exist LINE 1: SELECT * FROM mv; - ^ + ^ \ No newline at end of file diff --git a/src/postgres/src/test/regress/sql/yb_matview.sql b/src/postgres/src/test/regress/sql/yb_matview.sql index 66fb95fcb2ae..3881a0e7f05f 100644 --- a/src/postgres/src/test/regress/sql/yb_matview.sql +++ b/src/postgres/src/test/regress/sql/yb_matview.sql @@ -16,6 +16,55 @@ INSERT INTO pg_temp__123 values (1); SELECT * from pg_temp__123; DROP TABLE test_yb CASCADE; +-- Alter materialized view - rename matview and rename columns +CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL); +INSERT INTO test_yb VALUES (1, 'xyz', 2); +CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb; +CREATE UNIQUE INDEX unique_IDX ON mtest_YB(id); +ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1; +SELECT * FROM mtest_yb; -- error +SELECT * from mtest_yb1; -- ok +REFRESH MATERIALIZED VIEW mtest_yb1; +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; +ALTER MATERIALIZED VIEW mtest_yb1 RENAME TO mtest_yb2; +SELECT * from mtest_yb2; +REFRESH MATERIALIZED VIEW mtest_yb2; +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb2; +ALTER MATERIALIZED VIEW mtest_yb2 RENAME val TO total; -- test Alter Rename Column +SELECT * FROM mtest_yb2; +DROP TABLE test_yb CASCADE; + +-- Alter materialized view - change owner +CREATE TABLE test_yb (id int NOT NULL PRIMARY KEY, type text NOT NULL, val numeric NOT NULL); +INSERT INTO test_yb VALUES (1, 'xyz', 2); +CREATE MATERIALIZED VIEW mtest_yb AS SELECT * FROM test_yb; +CREATE UNIQUE INDEX unique_IDX ON mtest_yb(id); +CREATE ROLE test_mv_user; +SET ROLE test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- error +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error +SET ROLE yugabyte; +ALTER MATERIALIZED VIEW mtest_yb OWNER TO test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- error +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- error +ALTER TABLE test_yb OWNER TO test_mv_user; +REFRESH MATERIALIZED VIEW mtest_yb; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok +ALTER MATERIALIZED VIEW mtest_yb OWNER TO SESSION_USER; +ALTER TABLE test_yb OWNER TO SESSION_USER; +REFRESH MATERIALIZED VIEW mtest_yb; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb; -- ok +ALTER MATERIALIZED VIEW mtest_yb RENAME val TO amt; +ALTER MATERIALIZED VIEW mtest_yb RENAME TO mtest_yb1; +CREATE ROLE test_mv_superuser SUPERUSER; +ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO test_mv_superuser; +REFRESH MATERIALIZED VIEW mtest_yb1; -- ok +REFRESH MATERIALIZED VIEW CONCURRENTLY mtest_yb1; -- ok +ALTER MATERIALIZED VIEW mtest_yb1 OWNER TO CURRENT_USER; +DROP ROLE test_mv_user; +DROP ROLE test_mv_superuser; +DROP TABLE test_yb CASCADE; + -- Test special characters in an attribute's name CREATE TABLE test_yb ("xyzID''\\b" int NOT NULL, "y" int); INSERT INTO test_yb VALUES (1); @@ -167,4 +216,4 @@ CREATE UNIQUE INDEX ON mv(col); REFRESH MATERIALIZED VIEW CONCURRENTLY mv; SELECT * FROM mv ORDER BY col; DROP MATERIALIZED VIEW mv; -SELECT * FROM mv; +SELECT * FROM mv; \ No newline at end of file