From 0cba8b13936235cf0025a40743c8ec1849977d3d Mon Sep 17 00:00:00 2001 From: "Zizhuan Liu(X-MAN)" <44973863@qq.com> Date: Thu, 28 May 2026 12:19:41 +0800 Subject: [PATCH v3] Avoid unnecessary matview populated-state updates REFRESH MATERIALIZED VIEW currently calls SetMatViewPopulatedState() even when the materialized view's relispopulated value already matches the state requested by the command. That performs a catalog update of pg_class without changing the visible state of the materialized view. The extra update creates a dead pg_class tuple and causes the usual catalog-update side effects, including relcache invalidation and WAL, for no semantic benefit. The effect is especially visible for REFRESH MATERIALIZED VIEW CONCURRENTLY, where the refresh updates the materialized view contents with DML and otherwise need not change pg_class just to keep relispopulated set to true. Check the current populated state before calling SetMatViewPopulatedState(), and update pg_class only when the value would actually change. This preserves the required state transitions for WITH DATA and WITH NO DATA while avoiding catalog churn for repeated refreshes that leave the populated state unchanged. --- src/backend/commands/matview.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index f7d8007f796..40748958eaf 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -293,10 +293,11 @@ RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData, "REFRESH MATERIALIZED VIEW"); /* - * Tentatively mark the matview as populated or not (this will roll back - * if we fail later). + * Tentatively mark the matview as populated or not, if its state is + * changing (this will roll back if we fail later). */ - SetMatViewPopulatedState(matviewRel, !skipData); + if (RelationIsPopulated(matviewRel) != !skipData) + SetMatViewPopulatedState(matviewRel, !skipData); /* Concurrent refresh builds new data in temp tablespace, and does diff. */ if (concurrent) -- 2.55.0