Sub topic 7
Creating an Unpopulated Materialized View with `WITH NO DATA`
This chapter assumes you know basic SELECT, GROUP BY, aggregate functions, and PostgreSQL materialized views. A materialized view stores query results as rows that can be indexed and queried. Unlike a plain view, it does not calculate its query every time you read it.
WITH NO DATA creates the materialized view's definition without running its query. PostgreSQL creates the relation, but it leaves the relation empty and marks it as not populated.
Why create a materialized view without data?
Creating a materialized view normally runs its defining query immediately:
CREATE MATERIALIZED VIEW customer_totals AS
SELECT customer_id, sum(total) AS lifetime_value
FROM orders
GROUP BY customer_id;If orders contains millions of rows, this statement may take a long time. It also needs enough resources to perform the aggregation and write the result.
Adding WITH NO DATA separates definition from population:
CREATE MATERIALIZED VIEW customer_totals AS
SELECT customer_id, sum(total) AS lifetime_value
FROM orders
GROUP BY customer_id
WITH NO DATA;This statement creates the materialized view quickly because PostgreSQL does not execute the SELECT yet. The view is ready for a later refresh, perhaps during a maintenance window.
Check whether the view is populated
PostgreSQL exposes materialized views through the pg_matviews catalog view. Its ispopulated column reports whether the materialized view currently contains a usable result.
SELECT schemaname,
matviewname,
ispopulated
FROM pg_matviews
WHERE matviewname = 'customer_totals';The result is similar to this:
| schemaname | matviewname | ispopulated |
|---|---|---|
| public | customer_totals | false |
The false value means that PostgreSQL has not yet run the defining query successfully.
You can inspect the stored definition as well:
SELECT definition
FROM pg_matviews
WHERE matviewname = 'customer_totals';The definition remains available even while the materialized view is unpopulated.
Populate the materialized view
Run REFRESH MATERIALIZED VIEW to execute the defining query and store its result:
REFRESH MATERIALIZED VIEW customer_totals;After the refresh succeeds, you can query the materialized view:
SELECT customer_id, lifetime_value
FROM customer_totals
ORDER BY lifetime_value DESC;You can confirm the state changed:
SELECT matviewname, ispopulated
FROM pg_matviews
WHERE matviewname = 'customer_totals';This time, ispopulated is true.
The first refresh must be a regular refresh. REFRESH MATERIALIZED VIEW CONCURRENTLY cannot populate a materialized view that has never been populated. After the first successful regular refresh, concurrent refreshes may be available if the required unique index exists.
flowchart TD
A[CREATE MATERIALIZED VIEW WITH NO DATA] --> B[Definition exists]
B --> C[ispopulated is false]
C --> D[Regular REFRESH MATERIALIZED VIEW]
D --> E[Stored rows are created]
E --> F[ispopulated is true]
F --> G[Query the materialized view]Add an index before or after the first refresh
An empty materialized view can have indexes. Adding an index before the first refresh can make later reads faster, and it can prepare the view for a concurrent refresh:
CREATE UNIQUE INDEX customer_totals_customer_id_idx
ON customer_totals (customer_id);The indexed column must be unique for every row. Here, the query groups by customer_id, so each customer appears once. That makes customer_id suitable for a unique index.
Then populate the view:
REFRESH MATERIALIZED VIEW customer_totals;The index remains in place when PostgreSQL replaces the materialized view's stored contents during a refresh.
When `WITH NO DATA` is useful
Use WITH NO DATA when:
- The initial query is expensive and should run later.
- You are deploying database objects before loading production data.
- You need to create indexes or permissions before the first population.
- A maintenance process will perform the first refresh.
- You want schema creation and data generation to happen in separate steps.
Remember that permissions to query the materialized view do not make an unpopulated view usable. The view must first receive a successful refresh.
Exercise
What happens if you run SELECT * FROM customer_totals immediately after creating it with WITH NO DATA, and which command makes the query possible?
Show answer
The query fails because the materialized view is not populated. Run REFRESH MATERIALIZED VIEW customer_totals first.