This is an automated email from the ASF dual-hosted git repository. maxyang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/cloudberry.git
commit f6c7e55051d90fed1234c3998111753f6cc56c17 Author: Zhang Mingli <[email protected]> AuthorDate: Thu Jul 24 21:38:45 2025 +0800 Fix MV status handling for partitioned tables. The reference counting optimization introduced in 77863a64c (Optimize MV invalidation overhead using reference counting) incorrectly skips materialized view status checks for partitioned tables. While the optimization properly handles regular tables by skipping unnecessary invalidations when no MVs exist, this logic breaks down for partitioned tables because child partitions may have no MVs while their parent tables do. Authored-by: Zhang Mingli [email protected] --- src/backend/catalog/gp_matview_aux.c | 3 ++- src/backend/executor/nodeModifyTable.c | 3 +++ src/test/regress/expected/matview_data.out | 26 +++++++++++++++++----- src/test/regress/sql/matview_data.sql | 12 ++++++---- .../singlenode_regress/expected/matview_data.out | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/backend/catalog/gp_matview_aux.c b/src/backend/catalog/gp_matview_aux.c index 9e1c224019c..a119f86b6e5 100644 --- a/src/backend/catalog/gp_matview_aux.c +++ b/src/backend/catalog/gp_matview_aux.c @@ -338,7 +338,8 @@ SetRelativeMatviewAuxStatus(Oid relid, char status, char direction) /* * Do a quick check if relation has relative materialized views. */ - if (get_rel_relmvrefcount(relid) <= 0) + if (get_rel_relmvrefcount(relid) <= 0 && + !get_rel_relispartition(relid)) return; mvauxRel = table_open(GpMatviewAuxId, RowExclusiveLock); diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index dc12ffb47c5..1d6bd2e8a93 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -1091,6 +1091,7 @@ ExecInsert(ModifyTableState *mtstate, { ModifiedLeafRelidsKey key; + MemSet(&key, 0, sizeof(key)); key.cmd = CMD_INSERT; key.relid = RelationGetRelid(resultRelationDesc); @@ -1562,6 +1563,7 @@ ldelete:; { ModifiedLeafRelidsKey key; + MemSet(&key, 0, sizeof(key)); key.cmd = CMD_DELETE; key.relid = RelationGetRelid(resultRelationDesc); @@ -2214,6 +2216,7 @@ lreplace:; { ModifiedLeafRelidsKey key; + MemSet(&key, 0, sizeof(key)); key.cmd = CMD_UPDATE; key.relid = RelationGetRelid(resultRelationDesc); diff --git a/src/test/regress/expected/matview_data.out b/src/test/regress/expected/matview_data.out index 14ffe538c55..91a06d6cbf9 100644 --- a/src/test/regress/expected/matview_data.out +++ b/src/test/regress/expected/matview_data.out @@ -491,7 +491,7 @@ select mvname, datastatus from gp_matview_aux where mvname like 'mv_par%'; mv_par1_1 | i mv_par1 | i mv_par | i - mv_par2 | u + mv_par2 | i (6 rows) refresh materialized view mv_par; @@ -897,16 +897,16 @@ abort; CREATE EXTENSION IF NOT EXISTS gp_inject_fault; NOTICE: extension "gp_inject_fault" already exists, skipping -- end_ignore -create table par1(a int, b int) partition by range(a) using ao_row distributed randomly; +create table par_normal_oid(a int, b int) partition by range(a) using ao_row distributed randomly; select gp_inject_fault('bump_oid', 'skip', dbid) from gp_segment_configuration where role = 'p' and content = -1; gp_inject_fault ----------------- Success: (1 row) -create table sub_par1 partition of par1 for values from (1) to (2) using ao_row; +create table sub_par1_large_oid partition of par_normal_oid for values from (1) to (2) using ao_row; NOTICE: table has parent, setting distribution columns to match parent table -select 'sub_par1'::regclass::oid > x'7FFFFFFF'::bigint; +select 'sub_par1_large_oid'::regclass::oid > x'7FFFFFFF'::bigint; ?column? ---------- t @@ -918,7 +918,23 @@ select gp_inject_fault('bump_oid', 'reset', dbid) from gp_segment_configuration Success: (1 row) -insert into par1 values(1, 2); +create materialized view mv_par_normal_oid as + select count(*) from par_normal_oid; +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'count' as the Apache Cloudberry data distribution key for this table. +HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. +select mvname, datastatus from gp_matview_aux where mvname = 'mv_par_normal_oid'; + mvname | datastatus +-------------------+------------ + mv_par_normal_oid | u +(1 row) + +insert into par_normal_oid values(1, 2); +select mvname, datastatus from gp_matview_aux where mvname = 'mv_par_normal_oid'; + mvname | datastatus +-------------------+------------ + mv_par_normal_oid | i +(1 row) + --start_ignore drop schema matview_data_schema cascade; NOTICE: drop cascades to 3 other objects diff --git a/src/test/regress/sql/matview_data.sql b/src/test/regress/sql/matview_data.sql index 7b6759477a2..d1d7af4c7fc 100644 --- a/src/test/regress/sql/matview_data.sql +++ b/src/test/regress/sql/matview_data.sql @@ -347,12 +347,16 @@ abort; -- start_ignore CREATE EXTENSION IF NOT EXISTS gp_inject_fault; -- end_ignore -create table par1(a int, b int) partition by range(a) using ao_row distributed randomly; +create table par_normal_oid(a int, b int) partition by range(a) using ao_row distributed randomly; select gp_inject_fault('bump_oid', 'skip', dbid) from gp_segment_configuration where role = 'p' and content = -1; -create table sub_par1 partition of par1 for values from (1) to (2) using ao_row; -select 'sub_par1'::regclass::oid > x'7FFFFFFF'::bigint; +create table sub_par1_large_oid partition of par_normal_oid for values from (1) to (2) using ao_row; +select 'sub_par1_large_oid'::regclass::oid > x'7FFFFFFF'::bigint; select gp_inject_fault('bump_oid', 'reset', dbid) from gp_segment_configuration where role = 'p' and content = -1; -insert into par1 values(1, 2); +create materialized view mv_par_normal_oid as + select count(*) from par_normal_oid; +select mvname, datastatus from gp_matview_aux where mvname = 'mv_par_normal_oid'; +insert into par_normal_oid values(1, 2); +select mvname, datastatus from gp_matview_aux where mvname = 'mv_par_normal_oid'; --start_ignore drop schema matview_data_schema cascade; diff --git a/src/test/singlenode_regress/expected/matview_data.out b/src/test/singlenode_regress/expected/matview_data.out index 4139b96f5f3..14c7fc48f45 100644 --- a/src/test/singlenode_regress/expected/matview_data.out +++ b/src/test/singlenode_regress/expected/matview_data.out @@ -462,7 +462,7 @@ select mvname, datastatus from gp_matview_aux where mvname like 'mv_par%'; mv_par1_1 | i mv_par1 | i mv_par | i - mv_par2 | u + mv_par2 | i (6 rows) refresh materialized view mv_par; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
