liveBook · Manning

Materialized view

As explained in Just Use Postgres!

A materialized view is a query-defined relation whose result is persisted and remains unchanged until explicitly refreshed.

A materialized view is a query-defined relation whose result is persisted. Unlike an ordinary view, it does not execute the underlying query for every read; clients see the same result until an explicit refresh. This suits resource-intensive queries and reports that do not require current data. section 2.7

Monthly sales report

An e-commerce platform can persist monthly revenue and order totals with `CREATE MATERIALIZED VIEW sales.monthly_sales_summary AS ...`. After a new order, querying the view still returns the previous totals. Running `REFRESH MATERIALIZED VIEW sales.monthly_sales_summary;` recomputes the result, after which the report includes the new order.

How Just Use Postgres! covers it

How deep it goes
Explains persistence, query behavior, creation, querying, stale results, and explicit refreshing through a complete sales example.
Examples
Uses an e-commerce monthly revenue and order-count report that changes only after a refresh.
What it assumes
Assumes familiarity with SQL queries and ordinary views before adding materialization.
What you can do after
Apply it via SQL to create, query, and refresh a persisted aggregate report.
How it connects
Connects views with query execution, aggregation, reporting, and refresh operations.

Also explained in

Compare how 5 books explain materialized view →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.