The idea · Lab 3.4.4A materialized view stores the actual result of a query as real, physical data, rather than recomputing it on every read the way a plain view does — a 90-second aggregation becomes an instant lookup, at the cost of the result going stale the moment underlying data changes, until something explicitly refreshes it. Keeping that view fresh is where a genuine tradeoff lives: a plain REFRESH MATERIALIZED VIEW recomputes the entire result from scratch and swaps it in atomically, but takes a lock strong enough to block every reader for the refresh's full duration — unacceptable for a materialized view meant to serve live traffic continuously.
REFRESH MATERIALIZED VIEW CONCURRENTLY computes the new result separately, then merges only the actual differences into the existing view in place — slower overall, but under a genuinely weaker lock that does not conflict with ordinary reads. This lab proves both halves of that claim directly against pg_locks rather than citing it from documentation: the exact lock mode each command takes, checked live while it is actually running.
A Materialized View, and Its Unique-Index Requirement
CREATE MATERIALIZED VIEW sql_mv_daily_totals AS
SELECT sale_date, sum(amount) AS total, count(*) AS n FROM sql_mv_sales GROUP BY sale_date;
REFRESH MATERIALIZED VIEW CONCURRENTLY sql_mv_daily_totals;
ERROR: cannot refresh materialized view "public.sql_mv_daily_totals" concurrently
HINT: Create a unique index with no WHERE clause on one or more columns of the materialized view.
CONCURRENTLY needs a way to uniquely identify each row to compute a precise diff between old and new versions — without a unique index, there is no such identity to compare by.
CREATE UNIQUE INDEX ON sql_mv_daily_totals(sale_date);
Stale Data, Confirmed and Fixed
INSERT INTO sql_mv_sales VALUES (999999, '2025-01-01', 99999);
SELECT total FROM sql_mv_daily_totals WHERE sale_date = '2025-01-01'; -- 424990, stale
REFRESH MATERIALIZED VIEW CONCURRENTLY sql_mv_daily_totals;
SELECT total FROM sql_mv_daily_totals WHERE sale_date = '2025-01-01'; -- 524989, correct
The view genuinely did not see the new row until explicitly refreshed — and genuinely did afterward.
The Real Lock Difference, Checked Live
-- Session A (held open with a 3-second sleep to observe it):
BEGIN; REFRESH MATERIALIZED VIEW sql_mv_daily_totals; SELECT pg_sleep(10); COMMIT;
-- Session B, checked mid-refresh:
SELECT mode, granted FROM pg_locks WHERE relation = 'sql_mv_daily_totals'::regclass;
mode | granted
---------------------+---------
AccessExclusiveLock | t
A plain REFRESH genuinely holds AccessExclusiveLock — the strongest lock PostgreSQL has, conflicting with every other lock mode including a plain SELECT's AccessShareLock. Every reader is genuinely blocked for the refresh's entire duration.
-- REFRESH MATERIALIZED VIEW CONCURRENTLY, checked mid-refresh:
SELECT mode, granted FROM pg_locks WHERE relation = 'sql_mv_daily_totals'::regclass;
mode | granted
---------------+---------
ExclusiveLock | t
ExclusiveLock — genuinely weaker. It conflicts with other writers, but not with a plain SELECT's AccessShareLock, which is exactly why readers are never blocked during a CONCURRENTLY refresh.
Key concepts
AccessExclusiveLock vs ExclusiveLock
PostgreSQL's strongest lock mode, AccessExclusiveLock, conflicts with every other lock mode including a plain SELECT's AccessShareLock — nothing else can access the relation at all while it is held. ExclusiveLock is one level weaker: it conflicts with other writers and with itself, but is compatible with AccessShareLock, meaning ordinary reads can proceed normally while it is held. A plain REFRESH MATERIALIZED VIEW takes the former; REFRESH MATERIALIZED VIEW CONCURRENTLY takes only the latter.
REFRESH MATERIALIZED VIEW CONCURRENTLY
Computes the view's new result into a separate temporary structure, then merges only the actual differences into the existing view using its required unique index to match up rows — an UPDATE/INSERT/DELETE-style diff rather than a full replace. This lets it hold a much weaker lock than a plain REFRESH, at the cost of typically taking longer overall and requiring a unique index to exist on the view first.
What to remember
- A materialized view stores a query's actual result physically — instant to read, but only as fresh as its last refresh
- REFRESH MATERIALIZED VIEW CONCURRENTLY requires a unique index because it computes a precise row-by-row diff, which needs a reliable way to match old rows to new ones
- A plain REFRESH genuinely takes AccessExclusiveLock, blocking every reader for its full duration — proven directly via pg_locks, not assumed from documentation
- REFRESH MATERIALIZED VIEW CONCURRENTLY genuinely takes only ExclusiveLock, which is compatible with ordinary reads — proven the same direct way
- Any materialized view serving live read traffic should always be refreshed with CONCURRENTLY, whether scheduled by pg_cron or a plain OS crontab entry