agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Fujii Masao <fujii@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid unnecessary matview populated-state updates
Date: Wed, 12 Aug 2026 05:44:35 +0000
Message-ID: <E1wu1lS-00000000DSh-1vmh@gemulon.postgresql.org> (raw)

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.

Author: ChangAo Chen <cca5507@qq.com>
Reviewed-by: Zizhuan Liu <44973863@qq.com>
Reviewed-by: Adam Brusselback <adambrusselback@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/tencent_DF2D8533B5412908326F7806075B7637A108@qq.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/33c77cfad3e4d5749a5827f7c8c6394e7cab0b30

Modified Files
--------------
src/backend/commands/matview.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)



Message-ID: <E1wu1lS-00000000DSh-1vmh@gemulon.postgresql.org>
Permalink:  ../E1wu1lS-00000000DSh-1vmh@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wu1lS-00000000DSh-1vmh@gemulon.postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: fujii@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Avoid unnecessary matview populated-state updates
  In-Reply-To: <E1wu1lS-00000000DSh-1vmh@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox