REFRESH MATERIALIZED VIEW (Refresh Materialized View)
Last updated: 2026-11-04
REFRESH MATERIALIZED VIEW (Refresh Materialized View)
REFRESH MATERIALIZED VIEW recomputes the defining query and replaces the stored result. A plain REFRESH blocks reads of the view while it runs; REFRESH ... CONCURRENTLY refreshes in the background while old data stays readable, but it requires a unique index on the materialized view.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
REFRESH MATERIALIZED VIEW [CONCURRENTLY] <materialized view name>
[WITH [NO] DATA]
⚙️ Parameters
| CONCURRENTLY | Refreshes in the background while old data stays readable |
|---|---|
| WITH DATA | Populates the materialized view (the default) |
| WITH NO DATA | Empties the materialized view |
Returns:No return value. After a successful refresh the contents match the base data.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
DROP MATERIALIZED VIEW
CREATE TABLE
INSERT 0 3
CREATE MATERIALIZED VIEW
CREATE INDEX
region | orders | total
-------+--------+-------
east | 2 | 350.50
west | 1 | 300.00
INSERT 0 1
REFRESH MATERIALIZED VIEW
region | orders | total
-------+--------+-------
east | 2 | 350.50
west | 2 | 420.00
REFRESH MATERIALIZED VIEW
📌 Key Points
- CONCURRENTLY requires a unique index on the materialized view; without it you get cannot refresh materialized view ... concurrently.
- During a CONCURRENTLY refresh the previous data stays readable, so readers never see an empty view.
- A plain REFRESH recomputes and swaps the whole view, costing about as much as rebuilding it and making the view unavailable while it runs.
- Statistics on the materialized view are stale after a refresh — run ANALYZE again so the planner sees the new distribution.
- A CONCURRENTLY refresh must diff old and new contents, so it is considerably slower than a plain refresh.
- REFRESH ... WITH NO DATA empties the view, which pairs with the create-then-fill workflow.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QCan I read the old data during a CONCURRENTLY refresh?
AYes — that is the main advantage over a plain REFRESH: readers always see complete data, just possibly an older version.
QHow do I refresh a materialized view automatically?
APostgreSQL has no built-in auto-refresh; the usual approach is the pg_cron extension or an external scheduler running REFRESH MATERIALIZED VIEW on a timer.