Skip to content

Commit e769711

Browse files
committed
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 [email protected]
1 parent 978bab5 commit e769711

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/backend/executor/execMain.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,10 @@ 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+
(operation != CMD_SELECT) &&
1049+
((es_processed > 0 || estate->es_processed > 0) ||
1050+
!queryDesc->plannedstmt->canSetTag))
10491051
{
10501052
if (operation == CMD_INSERT ||
10511053
operation == CMD_UPDATE ||

src/test/regress/expected/matview_data.out

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

327+
--
328+
-- test issue https://github.com/cloudberrydb/cloudberrydb/issues/582
329+
-- test rules
330+
begin;
331+
create table t1_issue_582(i int, j int);
332+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
333+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
334+
create table t2_issue_582(i int, j int);
335+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
336+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
337+
create table t3_issue_582(i int, j int);
338+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database data distribution key for this table.
339+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
340+
create materialized view mv_t2_issue_582 as select j from t2_issue_582 where i = 1;
341+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'j' as the Cloudberry Database data distribution key for this table.
342+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
343+
create rule r1 as on insert TO t1_issue_582 do also insert into t2_issue_582 values(1,1);
344+
select count(*) from t1_issue_582;
345+
count
346+
-------
347+
0
348+
(1 row)
349+
350+
select count(*) from t2_issue_582;
351+
count
352+
-------
353+
0
354+
(1 row)
355+
356+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
357+
mvname | datastatus
358+
-----------------+------------
359+
mv_t2_issue_582 | u
360+
(1 row)
361+
362+
insert into t1_issue_582 values(1,1);
363+
select count(*) from t1_issue_582;
364+
count
365+
-------
366+
1
367+
(1 row)
368+
369+
select count(*) from t2_issue_582;
370+
count
371+
-------
372+
1
373+
(1 row)
374+
375+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
376+
mvname | datastatus
377+
-----------------+------------
378+
mv_t2_issue_582 | i
379+
(1 row)
380+
381+
abort;
327382
drop schema matview_data_schema cascade;
328383
NOTICE: drop cascades to 3 other objects
329384
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
@@ -114,6 +114,24 @@ drop materialized view mv2;
114114
drop table t1 cascade;
115115
select mvname, datastatus from gp_matview_aux where mvname in ('mv0','mv1', 'mv2', 'mv3');
116116

117+
--
118+
-- test issue https://github.com/cloudberrydb/cloudberrydb/issues/582
119+
-- test rules
120+
begin;
121+
create table t1_issue_582(i int, j int);
122+
create table t2_issue_582(i int, j int);
123+
create table t3_issue_582(i int, j int);
124+
create materialized view mv_t2_issue_582 as select j from t2_issue_582 where i = 1;
125+
create rule r1 as on insert TO t1_issue_582 do also insert into t2_issue_582 values(1,1);
126+
select count(*) from t1_issue_582;
127+
select count(*) from t2_issue_582;
128+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
129+
insert into t1_issue_582 values(1,1);
130+
select count(*) from t1_issue_582;
131+
select count(*) from t2_issue_582;
132+
select mvname, datastatus from gp_matview_aux where mvname = 'mv_t2_issue_582';
133+
abort;
134+
117135
drop schema matview_data_schema cascade;
118136
reset enable_answer_query_using_materialized_views;
119137
reset optimizer;

0 commit comments

Comments
 (0)