Skip to content

Commit f038fbf

Browse files
avaminglimy-ship-it
authored andcommitted
Fix writable rules on tables has relative matview.
Though rules are not well supported in GPDB, we have to update matview data status if there were. Some SQL will generate like INSERT into another table which has relative matview. Authored-by: Zhang Mingli avamingli@gmail.com
1 parent 9881448 commit f038fbf

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

src/backend/executor/execMain.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,9 @@ standard_ExecutorRun(QueryDesc *queryDesc,
10441044
* NB: This can't handle well in utility mode, should REFRESH by user
10451045
* after that.
10461046
*/
1047-
if ((GP_ROLE_DISPATCH == Gp_role && es_processed > 0) ||
1048-
(IS_SINGLENODE() && (operation != CMD_SELECT) && estate->es_processed > 0))
1047+
if (IS_QD_OR_SINGLENODE() &&
1048+
((es_processed > 0 || estate->es_processed > 0) ||
1049+
!queryDesc->plannedstmt->canSetTag))
10491050
{
10501051
if (operation == CMD_INSERT ||
10511052
operation == CMD_UPDATE ||

src/test/regress/expected/matview_data.out

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,61 @@ select mvname, datastatus from gp_matview_aux where mvname in ('mv0','mv1', 'mv2
347347
mv3 | e
348348
(1 row)
349349

350+
--
351+
-- test issue https://github.com/cloudberrydb/cloudberrydb/issues/582
352+
-- test rules
353+
begin;
354+
create table t1_issue_582(i int, j int);
355+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
356+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
357+
create table t2_issue_582(i int, j int);
358+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
359+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
360+
create table t3_issue_582(i int, j int);
361+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
362+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
363+
create materialized view mv_t2_issue_582 as select j from t2_issue_582 where i = 1;
364+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'j' as the Cloudberry Database data distribution key for this table.
365+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
366+
create rule r1 as on insert TO t1_issue_582 do also insert into t2_issue_582 values(1,1);
367+
select count(*) from t1_issue_582;
368+
count
369+
-------
370+
0
371+
(1 row)
372+
373+
select count(*) from t2_issue_582;
374+
count
375+
-------
376+
0
377+
(1 row)
378+
379+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
380+
mvname | datastatus
381+
-----------------+------------
382+
mv_t2_issue_582 | u
383+
(1 row)
384+
385+
insert into t1_issue_582 values(1,1);
386+
select count(*) from t1_issue_582;
387+
count
388+
-------
389+
1
390+
(1 row)
391+
392+
select count(*) from t2_issue_582;
393+
count
394+
-------
395+
1
396+
(1 row)
397+
398+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
399+
mvname | datastatus
400+
-----------------+------------
401+
mv_t2_issue_582 | i
402+
(1 row)
403+
404+
abort;
350405
drop schema matview_data_schema cascade;
351406
NOTICE: drop cascades to 3 other objects
352407
DETAIL: drop cascades to table t2

src/test/regress/sql/matview_data.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ drop materialized view mv2;
128128
drop table t1 cascade;
129129
select mvname, datastatus from gp_matview_aux where mvname in ('mv0','mv1', 'mv2', 'mv3');
130130

131+
--
132+
-- test issue https://github.com/cloudberrydb/cloudberrydb/issues/582
133+
-- test rules
134+
begin;
135+
create table t1_issue_582(i int, j int);
136+
create table t2_issue_582(i int, j int);
137+
create table t3_issue_582(i int, j int);
138+
create materialized view mv_t2_issue_582 as select j from t2_issue_582 where i = 1;
139+
create rule r1 as on insert TO t1_issue_582 do also insert into t2_issue_582 values(1,1);
140+
select count(*) from t1_issue_582;
141+
select count(*) from t2_issue_582;
142+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
143+
insert into t1_issue_582 values(1,1);
144+
select count(*) from t1_issue_582;
145+
select count(*) from t2_issue_582;
146+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
147+
abort;
148+
131149
drop schema matview_data_schema cascade;
132150
reset enable_answer_query_using_materialized_views;
133151
reset optimizer;

0 commit comments

Comments
 (0)