Skip to content

Commit 15572b0

Browse files
committed
[AQUMV] Fix materialized view rename to also update gp_matview_aux
Fix #1116 This addresses issue where renaming a materialized view wasn't properly updating the corresponding entry in gp_matview_aux system table. The fix maintains data integrity between materialized views and their auxiliary metadata by ensuring both are renamed atomically during ALTER MATERIALIZED VIEW RENAME operations. Authored-by: Zhang Mingli avamingli@gmail.com
1 parent 5317c80 commit 15572b0

5 files changed

Lines changed: 83 additions & 1 deletion

File tree

src/backend/catalog/gp_matview_aux.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,44 @@ addRelationMVRefCount(Oid relid, int32 mvrefcount)
601601
heap_freetuple(tuple);
602602

603603
table_close(pgrel, RowExclusiveLock);
604-
}
604+
}
605+
606+
/*
607+
* Rename matview's name in gp_matview_aux.
608+
*/
609+
void
610+
mvaux_rename(Oid mvoid, char* newname)
611+
{
612+
HeapTuple tuple;
613+
HeapTuple newtuple;
614+
Relation mvauxRel;
615+
NameData mvname;
616+
Datum valuesAtt[Natts_gp_matview_aux];
617+
bool nullsAtt[Natts_gp_matview_aux];
618+
bool replacesAtt[Natts_gp_matview_aux];
619+
620+
tuple = SearchSysCacheCopy1(MVAUXOID, ObjectIdGetDatum(mvoid));
621+
622+
if (!HeapTupleIsValid(tuple))
623+
return;
624+
625+
mvauxRel = table_open(GpMatviewAuxId, RowExclusiveLock);
626+
627+
MemSet(valuesAtt, 0, sizeof(valuesAtt));
628+
MemSet(nullsAtt, false, sizeof(nullsAtt));
629+
MemSet(replacesAtt, false, sizeof(replacesAtt));
630+
631+
replacesAtt[Anum_gp_matview_aux_mvname -1] = true;
632+
633+
namestrcpy(&mvname, newname);
634+
valuesAtt[Anum_gp_matview_aux_mvname - 1] = NameGetDatum(&mvname);
635+
636+
newtuple = heap_modify_tuple(tuple, RelationGetDescr(mvauxRel),
637+
valuesAtt, nullsAtt, replacesAtt);
638+
639+
CatalogTupleUpdate(mvauxRel, &newtuple->t_self, newtuple);
640+
heap_freetuple(newtuple);
641+
table_close(mvauxRel, RowExclusiveLock);
642+
643+
CommandCounterIncrement();
644+
}

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,6 +4519,10 @@ RenameRelation(RenameStmt *stmt)
45194519
/* Do the work */
45204520
RenameRelationInternal(relid, stmt->newname, false, is_index_stmt);
45214521

4522+
/* Try to Rename in gp_matview_aux too. */
4523+
if (stmt->renameType == OBJECT_MATVIEW)
4524+
mvaux_rename(relid, stmt->newname);
4525+
45224526
/*
45234527
* if relation is a partitioned table, rename all children tables of it.
45244528
*/

src/include/catalog/gp_matview_aux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ extern bool MatviewIsGeneralyUpToDate(Oid mvoid);
9494

9595
extern bool MatviewIsUpToDate(Oid mvoid);
9696

97+
extern void mvaux_rename(Oid mvoid, char* newname);
98+
9799
#endif /* GP_MATVIEW_AUX_H */

src/test/regress/expected/matview_data.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,32 @@ from gp_segment_configuration where role = 'p' and content = -1;
867867
--
868868
-- End of Maintain materialized views on partitioned tables from bottom to up.
869869
--
870+
-- Test Rename matview.
871+
begin;
872+
create materialized view mv_name1 as
873+
select * from par with no data;
874+
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'a' as the Apache Cloudberry data distribution key for this table.
875+
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
876+
select count(*) from gp_matview_aux where mvname = 'mv_name1';
877+
count
878+
-------
879+
1
880+
(1 row)
881+
882+
alter materialized view mv_name1 rename to mv_name2;
883+
select count(*) from gp_matview_aux where mvname = 'mv_name1';
884+
count
885+
-------
886+
0
887+
(1 row)
888+
889+
select count(*) from gp_matview_aux where mvname = 'mv_name2';
890+
count
891+
-------
892+
1
893+
(1 row)
894+
895+
abort;
870896
--start_ignore
871897
drop schema matview_data_schema cascade;
872898
NOTICE: drop cascades to 3 other objects

src/test/regress/sql/matview_data.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ from gp_segment_configuration where role = 'p' and content = -1;
333333
-- End of Maintain materialized views on partitioned tables from bottom to up.
334334
--
335335

336+
-- Test Rename matview.
337+
begin;
338+
create materialized view mv_name1 as
339+
select * from par with no data;
340+
select count(*) from gp_matview_aux where mvname = 'mv_name1';
341+
342+
alter materialized view mv_name1 rename to mv_name2;
343+
select count(*) from gp_matview_aux where mvname = 'mv_name1';
344+
select count(*) from gp_matview_aux where mvname = 'mv_name2';
345+
abort;
336346

337347
--start_ignore
338348
drop schema matview_data_schema cascade;

0 commit comments

Comments
 (0)