Re: [Patch] Add WHERE clause support to REFRESH MATERIALIZED VIEW

From: Kirk Wolak <wolakk(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Adam Brusselback <adambrusselback(at)gmail(dot)com>, Nikolay Samokhvalov <nik(at)postgres(dot)ai>, Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Subject: Re: [Patch] Add WHERE clause support to REFRESH MATERIALIZED VIEW
Date: 2025-12-09 05:08:58
Message-ID: CACLU5mST1LhC3ibaKGNch_=06S2cmbjR4PnoUSupKs+rtgdeyw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Dec 8, 2025 at 3:58 PM Adam Brusselback <adambrusselback(at)gmail(dot)com>
wrote:

> Attached is a patch implementing support for a WHERE clause in REFRESH
> MATERIALIZED VIEW.
>
> The syntax allows for targeted refreshes:
> REFRESH MATERIALIZED VIEW mv WHERE invoice_id = ANY('{1,2,3}');
> REFRESH MATERIALIZED VIEW CONCURRENTLY mv WHERE customer_id = 42;
> REFRESH MATERIALIZED VIEW mv WHERE order_date >= '2023-01-01';
>
> I was inspired to implement this feature after watching the Hacking
> Postgres discussion on the topic:
> https://www.youtube.com/watch?v=6cZvHjDrmlQ
>
> +1 (But I was in that hacking session).

Our situation was a wonderful MV with all the columns we needed (some
hard to calculate) to augment search data done millions of times/day. It
was a thing of beauty. Until we realized we needed to update 1 record
(vendor inventory UPDATE date/time) every time we processed a file
(something we do 24x7, a hundred times each hour!

For that ONE field, we ended up doing REFRESH MV concurrently; OVER
2,000 times per day.

Our understanding is that many people run into this exact issue. The
cache needs small frequent updates.
(After reading the code that handles MVs, we just created our own TABLE,
and maintain it with a scheduler to rebuild HOURLY,
and when we process the file, a Simple UPDATE is issued for the one column).

While this "Works", the CONCEPT of this patch (untested by me, as of
yet), would have fixed this with far less effort,
and would be easier to maintain.

After I review the code, I will add additional comments.

I am curious what others think? (And FWIW, I believe that the larger the
MV, the MORE this feature is needed,
vs refreshing the ENTIRE view).

Regards...

>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2025-12-09 05:21:51 add some errhint for regexp* functions.
Previous Message jian he 2025-12-09 05:07:29 citext_1.out, citext.out confusing comment