Skip to content

Commit 53cf333

Browse files
committed
Create Block Directory for Materialized Views of AO storage.
Fix: apache#865 Implemented a solution that creates a block directory for materialized views using AO/AOCS storage with an index. This fix prevents errors that occur when refreshing materialized views in AO/AOCS storage. Authored-by: Zhang Mingli [email protected]
1 parent 2b31b25 commit 53cf333

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

src/backend/commands/matview.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
410410
ObjectAddress address;
411411
RefreshClause *refreshClause;
412412
bool oldPopulated;
413+
bool ao_has_index;
413414

414415
/* MATERIALIZED_VIEW_FIXME: Refresh MatView is not MPP-fied. */
415416

@@ -649,13 +650,19 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
649650
free_object_addresses(immv_triggers);
650651
}
651652

653+
/*
654+
* Fix issue https://github.com/apache/cloudberry/issues/865
655+
* Create blockdir for Matartalized View of AO/AOCS storage has index.
656+
*/
657+
ao_has_index = matviewRel->rd_rel->relhasindex && RelationIsAppendOptimized(matviewRel);
658+
652659
/*
653660
* Create the transient table that will receive the regenerated data. Lock
654661
* it against access by any other process until commit (by which time it
655662
* will be gone).
656663
*/
657664
OIDNewHeap = make_new_heap(matviewOid, tableSpace, matviewRel->rd_rel->relam, relpersistence,
658-
ExclusiveLock, false, true);
665+
ExclusiveLock, ao_has_index, true);
659666
LockRelationOid(OIDNewHeap, AccessExclusiveLock);
660667
dest = CreateTransientRelDestReceiver(OIDNewHeap, matviewOid, concurrent, relpersistence,
661668
stmt->skipData);
@@ -888,6 +895,7 @@ transientrel_init(QueryDesc *queryDesc)
888895
char relpersistence;
889896
LOCKMODE lockmode;
890897
RefreshClause *refreshClause;
898+
bool ao_has_index;
891899

892900
refreshClause = queryDesc->plannedstmt->refreshClause;
893901
/* Determine strength of lock needed. */
@@ -919,14 +927,17 @@ transientrel_init(QueryDesc *queryDesc)
919927
tableSpace = matviewRel->rd_rel->reltablespace;
920928
relpersistence = matviewRel->rd_rel->relpersistence;
921929
}
930+
931+
ao_has_index = matviewRel->rd_rel->relhasindex && RelationIsAppendOptimized(matviewRel);
932+
922933
/*
923934
* Create the transient table that will receive the regenerated data. Lock
924935
* it against access by any other process until commit (by which time it
925936
* will be gone).
926937
*/
927938
OIDNewHeap = make_new_heap(matviewOid, tableSpace, matviewRel->rd_rel->relam,
928939
relpersistence,
929-
ExclusiveLock, false, false);
940+
ExclusiveLock, ao_has_index, false);
930941
LockRelationOid(OIDNewHeap, AccessExclusiveLock);
931942

932943
queryDesc->dest = CreateTransientRelDestReceiver(OIDNewHeap, matviewOid, concurrent,

src/test/regress/expected/matview.out

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,50 @@ ERROR: materialized view "mat_view_twn" has not been populated
809809
HINT: Use the REFRESH MATERIALIZED VIEW command.
810810
DROP MATERIALIZED VIEW mat_view_twn;
811811
DROP TABLE mvtest_twn;
812+
--
813+
-- https://github.com/apache/cloudberry/issues/865
814+
--
815+
set default_table_access_method TO AO_ROW;
816+
CREATE TABLE t_issue_865_ao
817+
(
818+
id bigint NOT NULL,
819+
user_id bigint
820+
);
821+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Cloudberry Database data distribution key for this table.
822+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
823+
insert into t_issue_865_ao values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
824+
CREATE MATERIALIZED VIEW matview_issue_865_ao AS SELECT * FROM t_issue_865_ao WHERE id < 6;
825+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause. Creating a NULL policy entry.
826+
CREATE INDEX idx_matview_issue_865_ao ON matview_issue_865_ao USING btree (user_id);
827+
BEGIN;
828+
UPDATE t_issue_865_ao SET id = id WHERE id = 1;
829+
UPDATE t_issue_865_ao SET id = id WHERE id = 2;
830+
UPDATE t_issue_865_ao SET id = id WHERE id = 3;
831+
COMMIT;
832+
VACUUM t_issue_865_ao;
833+
REFRESH MATERIALIZED VIEW matview_issue_865_ao;
834+
-- AOCS
835+
set default_table_access_method TO AO_COLUMN;
836+
CREATE TABLE t_issue_865_aocs
837+
(
838+
id bigint NOT NULL,
839+
user_id bigint
840+
);
841+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Cloudberry Database data distribution key for this table.
842+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
843+
insert into t_issue_865_aocs values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
844+
CREATE MATERIALIZED VIEW matview_issue_865_aocs AS SELECT * FROM t_issue_865_aocs WHERE id < 6;
845+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause. Creating a NULL policy entry.
846+
CREATE INDEX idx_matview_issue_865_aocs ON matview_issue_865_aocs USING btree (user_id);
847+
BEGIN;
848+
UPDATE t_issue_865_aocs SET id = id WHERE id = 1;
849+
UPDATE t_issue_865_aocs SET id = id WHERE id = 2;
850+
UPDATE t_issue_865_aocs SET id = id WHERE id = 3;
851+
COMMIT;
852+
VACUUM t_issue_865_aocs;
853+
REFRESH MATERIALIZED VIEW matview_issue_865_aocs;
854+
RESET default_table_access_method;
855+
DROP TABLE t_issue_865_ao CASCADE;
856+
NOTICE: drop cascades to materialized view matview_issue_865_ao
857+
DROP TABLE t_issue_865_aocs CASCADE;
858+
NOTICE: drop cascades to materialized view matview_issue_865_aocs

src/test/regress/expected/matview_optimizer.out

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,50 @@ ERROR: materialized view "mat_view_twn" has not been populated
826826
HINT: Use the REFRESH MATERIALIZED VIEW command.
827827
DROP MATERIALIZED VIEW mat_view_twn;
828828
DROP TABLE mvtest_twn;
829+
--
830+
-- https://github.com/apache/cloudberry/issues/865
831+
--
832+
set default_table_access_method TO AO_ROW;
833+
CREATE TABLE t_issue_865_ao
834+
(
835+
id bigint NOT NULL,
836+
user_id bigint
837+
);
838+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Cloudberry Database data distribution key for this table.
839+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
840+
insert into t_issue_865_ao values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
841+
CREATE MATERIALIZED VIEW matview_issue_865_ao AS SELECT * FROM t_issue_865_ao WHERE id < 6;
842+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause. Creating a NULL policy entry.
843+
CREATE INDEX idx_matview_issue_865_ao ON matview_issue_865_ao USING btree (user_id);
844+
BEGIN;
845+
UPDATE t_issue_865_ao SET id = id WHERE id = 1;
846+
UPDATE t_issue_865_ao SET id = id WHERE id = 2;
847+
UPDATE t_issue_865_ao SET id = id WHERE id = 3;
848+
COMMIT;
849+
VACUUM t_issue_865_ao;
850+
REFRESH MATERIALIZED VIEW matview_issue_865_ao;
851+
-- AOCS
852+
set default_table_access_method TO AO_COLUMN;
853+
CREATE TABLE t_issue_865_aocs
854+
(
855+
id bigint NOT NULL,
856+
user_id bigint
857+
);
858+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Cloudberry Database data distribution key for this table.
859+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
860+
insert into t_issue_865_aocs values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
861+
CREATE MATERIALIZED VIEW matview_issue_865_aocs AS SELECT * FROM t_issue_865_aocs WHERE id < 6;
862+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause. Creating a NULL policy entry.
863+
CREATE INDEX idx_matview_issue_865_aocs ON matview_issue_865_aocs USING btree (user_id);
864+
BEGIN;
865+
UPDATE t_issue_865_aocs SET id = id WHERE id = 1;
866+
UPDATE t_issue_865_aocs SET id = id WHERE id = 2;
867+
UPDATE t_issue_865_aocs SET id = id WHERE id = 3;
868+
COMMIT;
869+
VACUUM t_issue_865_aocs;
870+
REFRESH MATERIALIZED VIEW matview_issue_865_aocs;
871+
RESET default_table_access_method;
872+
DROP TABLE t_issue_865_ao CASCADE;
873+
NOTICE: drop cascades to materialized view matview_issue_865_ao
874+
DROP TABLE t_issue_865_aocs CASCADE;
875+
NOTICE: drop cascades to materialized view matview_issue_865_aocs

src/test/regress/sql/matview.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,55 @@ SELECT * FROM mat_view_twn;
358358

359359
DROP MATERIALIZED VIEW mat_view_twn;
360360
DROP TABLE mvtest_twn;
361+
362+
--
363+
-- https://github.com/apache/cloudberry/issues/865
364+
--
365+
set default_table_access_method TO AO_ROW;
366+
367+
CREATE TABLE t_issue_865_ao
368+
(
369+
id bigint NOT NULL,
370+
user_id bigint
371+
);
372+
insert into t_issue_865_ao values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
373+
374+
CREATE MATERIALIZED VIEW matview_issue_865_ao AS SELECT * FROM t_issue_865_ao WHERE id < 6;
375+
CREATE INDEX idx_matview_issue_865_ao ON matview_issue_865_ao USING btree (user_id);
376+
377+
BEGIN;
378+
UPDATE t_issue_865_ao SET id = id WHERE id = 1;
379+
UPDATE t_issue_865_ao SET id = id WHERE id = 2;
380+
UPDATE t_issue_865_ao SET id = id WHERE id = 3;
381+
COMMIT;
382+
383+
VACUUM t_issue_865_ao;
384+
385+
REFRESH MATERIALIZED VIEW matview_issue_865_ao;
386+
387+
-- AOCS
388+
set default_table_access_method TO AO_COLUMN;
389+
390+
CREATE TABLE t_issue_865_aocs
391+
(
392+
id bigint NOT NULL,
393+
user_id bigint
394+
);
395+
insert into t_issue_865_aocs values (1, 1), (2, 1), (3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (8, 4), (9, 5), (10, 5);
396+
397+
CREATE MATERIALIZED VIEW matview_issue_865_aocs AS SELECT * FROM t_issue_865_aocs WHERE id < 6;
398+
CREATE INDEX idx_matview_issue_865_aocs ON matview_issue_865_aocs USING btree (user_id);
399+
400+
BEGIN;
401+
UPDATE t_issue_865_aocs SET id = id WHERE id = 1;
402+
UPDATE t_issue_865_aocs SET id = id WHERE id = 2;
403+
UPDATE t_issue_865_aocs SET id = id WHERE id = 3;
404+
COMMIT;
405+
406+
VACUUM t_issue_865_aocs;
407+
408+
REFRESH MATERIALIZED VIEW matview_issue_865_aocs;
409+
410+
RESET default_table_access_method;
411+
DROP TABLE t_issue_865_ao CASCADE;
412+
DROP TABLE t_issue_865_aocs CASCADE;

0 commit comments

Comments
 (0)