Unlogged materialized views
Unlogged materialized views
Hello! I like to bring up supporting unlogged materialized views again. I am aware that there were several discussions about them in the past, but all of those seem quite old, the last one is from 7 years ago[1]. There are also two other related discussion with recent activity: * incremental view maintenance[2] * support for WHERE clauses for materialized view refresh[3] The reasoning behind all of these is probably similar as my root cause for this proposal: refresh materialized view on huge views is slow and results in wal churn. The above patches aim to make the changes smaller, while I'd like to revisit the possibility of making materialized views unlogged to avoid wal completely. In some scenarios, crash-safety isn't a requirement for them, as it is okay if certain features aren't available until a materialized view rebuilds, or if queries complete slower until it does. Unlogged materialized views would be perfect for this. The general consensus in the old threads seems to be that unlogged materialized views should become unpopulated after a crash instead of empty, and that's also my assessment. The issue was and is just the how: pg_class currently has a relispopulated field: 0 for unpopulated materialized views, 1 otherwise. With this approach, making them unpopulated at or after recovery would require catalog changes during recovery, which isn't possible. That's why my suggestion is an alternative approach: refactor this field: * rename it to relpopulated to avoid confusion (relispopulated becomes a helper function) * make it an integer: 1 for anything logged that is populated, even normal materialized views, 0 for unpopulated logged materialized views, and a special epoch counter for unlogged materialized views That counter is a "concatenation" of two things: the timelineid, and a newly introduced pg_control field, the unlogged reset generation counter (unloggedResetGen): starts at 0, increases by 1 every time during crash recovery. When an unlogged materialized view is populated, we write the current counter to pg_class.relpopulated. When we use it, we check if the stored epoch counter is the same as the current one for the server - if not, it's empty. We have to also disable access during recovery. With that, standby behavior, replica promotion, crash recovery, and everything else I tested seem to work as expected. In my initial patchset I didn't address two possible improvements, but I want to mention them: * automatically refreshing unlogged views after crash recovery in a background worker * providing a health-check function that tells users if all unlogged materialized views were already refreshed These could be implemented either in core/contrib or as independent extensions. Please see the attached patchset, what do you think about this approach? [1]: https://www.postgresql.org/message-id/CAKLmikNZ6qquFkh_eF9kWm5daTJy0PXhNt-xHT%2BiniYfaKkgZg%40mail.gmail.com [2]: https://www.postgresql.org/message-id/CAE8JnxMPbVtHgApfQ2Gny-a%2BnbVQN%2B%3DZm4-yhsY7EJTFWVm1nQ%40mail.gmail.com [3]: https://www.postgresql.org/message-id/CAMjNa7eFzTQ5%3DoZMQiB2bMkez5KP4A77JC7SRjeVEkOrh7cUHw%40mail.gmail.com
Re: Unlogged materialized views
От:
Dharin Shah <dharinshah95@gmail.com>
Дата:
Hey Zsolt,
+1
Will take a look at the patch
Thanks,
Dharin
+1
Will take a look at the patch
Thanks,
Dharin
On Mon, Aug 10, 2026 at 9:13 PM Adam Brusselback <adambrusselback@gmail.com> wrote:
Hi Zsolt,
> I like to bring up supporting unlogged materialized views again.
+1 from me. Most of the MVs I've built over the years were:
denormalized lookup tables behind a search screen, pre-aggregated
rollups for reporting, cached joins that were too slow to run perrequest.Losing some of those to a crash would have been fine as refreshtime for them wasn't a huge issue upon recovery.
> * automatically refreshing unlogged views after crash recovery in a
> background worker
Something still has to notice and rebuild, and right now that would be on
every user to write themselves. It would be nice to have this built-in IMO.
Thanks,
Adam
Re: Unlogged materialized views
Thank you for the feedback and suggestions! I rebased again; the patch no longer increments the controlfile/catalog version to make conflicts less likely. I included the changes from 0004, and added a few other minor changes/fixes. I also found another issue while testing replication scenarios. To fix that, the counter is now based on the LSN, not the timeline, this is the most significant change compared to v3. Another open question I want to point out is that I found a pg_dump failure with --no-unlogged-table-data. For now I chose the option where this flag doesn't affect materialized views, as they are not tables, but I am not sure if this is the proper solution.
Re: Unlogged materialized views
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
On 09/10/2026 13:40, Zsolt Parragi wrote:
> v5 is a trivial rebase changing tap test numbers.
Today I finally found some time revisit this patch.
== no relispopulated in pg_matviews ==
Client tools like pgAdmin rely on this column to display matview info:
diff --git a/src/backend/catalog/system_views.sql
b/src/backend/catalog/system_views.sql
index ddf801307eb..724a23d559f 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -150,7 +150,7 @@ CREATE VIEW pg_matviews AS
pg_get_userbyid(C.relowner) AS matviewowner,
T.spcname AS tablespace,
C.relhasindex AS hasindexes,
- C.relispopulated AS ispopulated,
+ pg_matview_is_populated(C.oid) AS ispopulated,
pg_get_viewdef(C.oid) AS definition
FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
LEFT JOIN pg_tablespace T ON (T.oid = C.reltablespace)
With the patch applied, pgAdmin can no longer display the properties of
a matview:
column c.relispopulated does not exist
LINE 6: c.relispopulated AS with_data,
^
HINT: Perhaps you meant to reference the column "c.relpopulated".
I will take a closer look at the code in the next few days.
Best, Jim
Re: Unlogged materialized views
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> == no relispopulated in pg_matviews == > > Client tools like pgAdmin rely on this column to display matview info: There is (or rather: there wasn't even before), the column is called pg_matview.ispopulated and it is still there > column c.relispopulated does not exist > LINE 6: c.relispopulated AS with_data, > ^ > HINT: Perhaps you meant to reference the column "c.relpopulated". What is missing is the relispopulated boolean from pg_class, and I don't see how that could stay with this approach. pg_class.relispopulated could only stay if we could update it after recovery, but before accepting connections (as otherwise it could become stale for unlogged matviews), and I don't see an easy way to do that, that's why I implemented this patch in a different way. Otherwise, it could become stale, that's why the patch removes it. But looking at pgadmin, it would be easy to patch it in a way that works with both all earlier versions and this patch.
Re: Unlogged materialized views
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
On 14/10/2026 17:29, Zsolt Parragi wrote:
> What is missing is the relispopulated boolean from pg_class, and I
> don't see how that could stay with this approach.
Right.
> pg_class.relispopulated could only stay if we could update it after
> recovery, but before accepting connections (as otherwise it could
> become stale for unlogged matviews), and I don't see an easy way to do
> that, that's why I implemented this patch in a different way.
> Otherwise, it could become stale, that's why the patch removes it.
>
> But looking at pgadmin, it would be easy to patch it in a way that
> works with both all earlier versions and this patch.
Right. I'm pretty sure things like this already happened in past
releases, I just thought I should bring it up in case it is avoidable --
pgAdmin certainly isn't the only tool relying on it.
I took another look and the only thing that caught my attention some
error messages:
/*
* An unlogged matview has no storage on a standby, though its
* relpopulated may still carry an epoch stamp from the primary. The
* generic recovery guard just below would reject it too, but reporting it
* as unpopulated matches what the same matview does on the primary once a
* crash has invalidated its stamp. Permanent matviews are excluded so
* plan-time behavior is unchanged. No REFRESH hint, as that cannot run
* during recovery.
*/
if (relation->rd_rel->relkind == RELKIND_MATVIEW &&
!RelationIsPermanent(relation) &&
RecoveryInProgress() &&
!RelationIsPopulated(relation))
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("materialized view \"%s\" has not been populated",
RelationGetRelationName(relation))));
I realise it is a matter of taste, but I find the generic error message
more accurate than this new one.
-- from a standby.
-- umv1 is an unlogged matview, and ut1 is an unlogged table
postgres=# select * from umv1;
ERROR: materialized view "umv1" has not been populated
postgres=# select * from ut1;
ERROR: cannot access temporary or unlogged relations during recovery
The hint in copy is not very accurate during a recovery session.
postgres=# copy umv1 to stdout;
ERROR: cannot copy from unpopulated materialized view "umv1"
HINT: Use the REFRESH MATERIALIZED VIEW command.
Other than that, I don't have anything else to add at this point. Thanks
for the patch.
BTW, while testing it I may have found a bug in unlogged tables with copy:
postgres=# copy ut1 to stdout;
ERROR: could not open file "base/5/16420": No such file or directory
.. but I will report it in another thread.
Jim
Re: Unlogged materialized views
> I realise it is a matter of taste, but I find the generic error message > more accurate than this new one. I wanted to improve it, but I agree that doing it differently for matviews might be confusing instead, so I removed that part from the patch, now it gives the same generic recovery message. > The hint in copy is not very accurate during a recovery session. I handled COPY the same way your patch does for unlogged tables, with the same generic message about recovery. v6 contains these changes in 0003, otherwise it's a trivial rebase on current master.