Sub topic 3
Creating an unpopulated view with WITH NO DATA
This chapter assumes you know basic SQL, can create materialized views, can run REFRESH MATERIALIZED VIEW, and have access to a PostgreSQL server with psql.
Creating an unpopulated view
WITH NO DATA creates the view definition without running the query. The view exists in the catalog but holds no rows. This is useful when you want to create the object in a migration and fill it later, during a maintenance window.
CREATE MATERIALIZED VIEW customer_totals AS
SELECT customer_id, sum(total) AS lifetime_value
FROM orders
GROUP BY customer_id
WITH NO DATA;Querying an unpopulated view fails with the error "has not been populated". Run REFRESH MATERIALIZED VIEW customer_totals; to fill it.
Running refreshes on a schedule
PostgreSQL does not refresh materialized views on its own. You must schedule the refresh. The pg_cron extension can do this inside the database. It must be installed and listed in shared_preload_libraries.
CREATE EXTENSION IF NOT EXISTS pg_cron;
SELECT cron.schedule(
'refresh-customer-totals',
'*/15 * * * *',
'REFRESH MATERIALIZED VIEW CONCURRENTLY customer_totals'
);You can also run psql -c "REFRESH MATERIALIZED VIEW customer_totals" from an operating system cron job.
Checking state with pg_matviews
The pg_matviews catalog view lists every materialized view. Its ispopulated column shows whether a refresh has run.
SELECT matviewname, ispopulated, hasindexes
FROM pg_matviews
WHERE matviewname = 'customer_totals';Altering, renaming, and dropping
You can rename or move a view without rebuilding it:
ALTER MATERIALIZED VIEW customer_totals RENAME TO customer_lifetime;
ALTER MATERIALIZED VIEW customer_lifetime SET SCHEMA reporting;
DROP MATERIALIZED VIEW reporting.customer_lifetime;You cannot change the defining query with ALTER. To change the query or its columns, drop the view and create it again. DROP fails if other objects depend on the view unless you add CASCADE.
Limitations and alternatives
- Data goes stale between refreshes.
- A plain
REFRESHlocks the view and blocks readers.CONCURRENTLYavoids this but needs a unique index. - Refreshes recompute the whole query. PostgreSQL has no built-in incremental refresh.
Alternatives include a regular summary table kept current by triggers or scheduled jobs, or an extension that maintains views incrementally.
Deciding when to use one
Use a materialized view when the query is expensive, the results can be a few minutes or hours old, and many readers need the same answer. Use a plain view when you need current data and the query is cheap. Use a table with your own update logic when you need row-level freshness.
Exercise
You create a view with WITH NO DATA, then query it before any refresh. What happens, and how do you check beforehand whether it is populated?
Show answer
The query fails with an error saying the view has not been populated. Check with SELECT matviewname, ispopulated FROM pgmatviews WHERE matviewname = 'customertotals'; If ispopulated is false, run REFRESH MATERIALIZED VIEW customer_totals; first.