Sub topic 5
Refreshing Without Blocking Readers Using `CONCURRENTLY`
This chapter assumes you know basic SELECT, GROUP BY, aggregate functions, tables, views, and indexes. You also need access to a PostgreSQL database where sales_daily is an existing, populated materialized view.
A normal refresh replaces the stored result while holding a strong lock on the materialized view. Readers can wait until the refresh finishes. REFRESH MATERIALIZED VIEW CONCURRENTLY uses a different process. PostgreSQL builds the new result separately, then makes it visible as one change. Readers can continue querying the old result during most of the refresh.
Create the required unique index
A concurrent refresh requires at least one unique index on the materialized view. The index must:
- Use only plain columns from the materialized view.
- Have no
WHEREcondition. - Identify every row uniquely.
For a daily aggregate, day is unique because the view has one row per day.
CREATE UNIQUE INDEX sales_daily_day_idx
ON sales_daily (day);If two rows could have the same day, this index would fail. In that case, choose a combination of columns that uniquely identifies each result row.
The index is also useful for ordinary queries:
SELECT day, orders, revenue
FROM sales_daily
WHERE day >= CURRENT_DATE - INTERVAL '30 days'
ORDER BY day;Run a concurrent refresh
After the materialized view is populated and has a suitable unique index, run:
REFRESH MATERIALIZED VIEW CONCURRENTLY sales_daily;PostgreSQL reads the base tables, computes a new result, and compares it with the stored result. It then applies the differences while preserving a readable version for queries that overlap the refresh.
The query result may change during the refresh boundary, but a single statement sees a consistent snapshot. A reader does not receive half of the old aggregate and half of the new aggregate.
The word “concurrently” does not mean that every operation runs without locks. PostgreSQL still takes brief locks when it starts and finishes the refresh. Those locks are normally short enough that ordinary reads do not wait for the full query computation.
Compare normal and concurrent refreshes
| Refresh command | Reader behavior | Main requirement |
|---|---|---|
| `REFRESH MATERIALIZED VIEW sales_daily` | Can block readers for the refresh duration | No unique index required |
| `REFRESH MATERIALIZED VIEW CONCURRENTLY sales_daily` | Readers can use the existing contents during the refresh | A qualifying unique index is required |
A concurrent refresh usually uses more work and temporary storage than a normal refresh because PostgreSQL must maintain old and new contents while it finds the changes. It can also take longer. Choose it when uninterrupted reads matter more than the extra refresh cost.
Important limitations
A concurrent refresh cannot populate an unpopulated materialized view. If you created a view with WITH NO DATA, perform an ordinary first refresh:
REFRESH MATERIALIZED VIEW customer_totals;
CREATE UNIQUE INDEX customer_totals_customer_id_idx
ON customer_totals (customer_id);
REFRESH MATERIALIZED VIEW CONCURRENTLY customer_totals;The first command fills the view. The index then makes later concurrent refreshes possible.
Only one refresh can run for a materialized view at a time. If your scheduler starts a second refresh before the first ends, the second job must wait or fail according to how your scheduler handles database locks. Make the schedule longer than the usual refresh duration, or add job-level locking.
Decide whether to use it
Use CONCURRENTLY when:
- Applications must read the materialized view throughout the refresh.
- The view has a stable unique key.
- The additional refresh time and storage are acceptable.
Use a normal refresh when:
- A maintenance window is available.
- The view is small.
- The simpler and often faster operation is more important than uninterrupted reads.
- You are performing the first refresh of a view created with
WITH NO DATA.
Exercise
What must you add before running REFRESH MATERIALIZED VIEW CONCURRENTLY sales_daily, and why?
Show answer
Add a unique index whose plain columns uniquely identify every row, such as CREATE UNIQUE INDEX ... ON sales_daily (day). PostgreSQL uses it to match old and new rows while keeping the existing contents available to readers.