diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index fc132759377..9d9b66ead95 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -3301,6 +3301,22 @@ clean_up_IVM_hash_entry(MV_TriggerHashEntry *entry, bool is_abort) list_free(table->new_tuplestores); table->new_tuplestores = NIL; } + if (!is_abort) + { + if (CurrentResourceOwner == entry->resowner) + { + if (table->slot) + { + ExecDropSingleTupleTableSlot(table->slot); + table->slot = NULL; + } + if (table->rel) + { + table_close(table->rel, NoLock); + table->rel = NULL; + } + } + } } if (entry->tables) diff --git a/src/test/regress/expected/incremental_matview.out b/src/test/regress/expected/incremental_matview.out index 2c5aa00007d..8cacaa7b422 100644 --- a/src/test/regress/expected/incremental_matview.out +++ b/src/test/regress/expected/incremental_matview.out @@ -336,6 +336,16 @@ SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a; (1 row) ROLLBACK; +-- TRUNCATE a base table without transaction block. +CREATE TABLE mv_base_simple(i int); +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Cloudberry Database 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. +CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm_simple AS SELECT * FROM mv_base_simple; +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'i' as the Cloudberry Database 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. +TRUNCATE mv_base_simple; +DROP TABLE mv_base_simple CASCADE; +NOTICE: drop cascades to materialized view mv_ivm_simple -- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect. BEGIN; CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm_avg_bug AS SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i; diff --git a/src/test/regress/sql/incremental_matview.sql b/src/test/regress/sql/incremental_matview.sql index a7342e13fbd..b30376de381 100644 --- a/src/test/regress/sql/incremental_matview.sql +++ b/src/test/regress/sql/incremental_matview.sql @@ -141,6 +141,12 @@ SELECT sum, count, avg FROM mv_ivm_group; SELECT SUM(j), COUNT(j), AVG(j) FROM mv_base_a; ROLLBACK; +-- TRUNCATE a base table without transaction block. +CREATE TABLE mv_base_simple(i int); +CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm_simple AS SELECT * FROM mv_base_simple; +TRUNCATE mv_base_simple; +DROP TABLE mv_base_simple CASCADE; + -- resolved issue: When use AVG() function and values is indivisible, result of AVG() is incorrect. BEGIN; CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm_avg_bug AS SELECT i, SUM(j), COUNT(j), AVG(j) FROM mv_base_A GROUP BY i;