Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How is a materialized view different from a view? In PostgreSQL, what does REFRESH MATERIALIZED VIEW CONCURRENTLY require, and what does it change compared with a normal refresh?

A view stores only its query and runs it on every read, while a materialized view stores the query's result, so reads are fast but the data is as old as the last refresh. A normal REFRESH MATERIALIZED VIEW rebuilds it under an ACCESS EXCLUSIVE lock that blocks readers; REFRESH ... CONCURRENTLY computes the new result and applies only the differences, so readers keep working, but it requires a unique index on the materialized view and is usually slower. PostgreSQL never refreshes on its own, and MySQL has no materialized views at all.

Why this question is asked

Materialized views are the first tool many teams reach for when a dashboard query becomes too slow, and they work well until the refresh itself becomes the problem: a report page that hangs every hour, or a refresh that fails with an error about a missing index. Interviewers ask this to check that we know the trade we are making, speed for staleness, and how the two refresh modes behave under load. In this answer, we will compare a view and a materialized view, look at what each refresh mode locks, see what CONCURRENTLY requires, and finish with scheduling, MySQL, and how to show users how fresh the data is.

1A View Versus a Materialized View

A view is a saved query. Every time we select from it, PostgreSQL runs the query against the base tables, so the answer is always current, and it is exactly as fast or slow as the query. A materialized view runs the query once and stores the rows, much like a table. Reading it is a scan of those stored rows, which can be thousands of times faster than recomputing an aggregate, and we can index it like a table. The price is staleness: changes to the base tables do not appear until we refresh it.

SQL
CREATE MATERIALIZED VIEW daily_sales AS
SELECT date_trunc('day', created_at)::date AS day, region,
count(*) AS orders, sum(total) AS revenue
FROM orders
GROUP BY 1, 2; -- SELECT 7300 (computed now, from 300 million rows)
SELECT * FROM daily_sales WHERE day = '2026-11-04'; -- 2 ms instead of 40 s
INSERT INTO orders (region, total, created_at) VALUES ('EU', 99, now());
SELECT orders FROM daily_sales WHERE day = current_date AND region = 'EU'; -- unchanged until a refresh

2What a Normal Refresh Does

REFRESH MATERIALIZED VIEW daily_sales reruns the query and replaces the stored rows. While it does that, it holds an ACCESS EXCLUSIVE lock on the materialized view, the strongest lock, which conflicts even with a plain SELECT. So for the whole duration of the refresh, every query that reads daily_sales waits. If the refresh takes 90 seconds, the dashboard hangs for 90 seconds.

The upside is simplicity and speed: it writes a fresh copy of the result in one pass and swaps it in, without comparing anything.

3What CONCURRENTLY Changes

REFRESH MATERIALIZED VIEW CONCURRENTLY computes the new result into a temporary table, compares it with the current contents, and applies only the differences as inserts, updates and deletes. Readers keep reading the old rows during the whole refresh. It takes an EXCLUSIVE lock, which still blocks writes and other refreshes of the materialized view but allows SELECT.

To compare old and new rows, PostgreSQL must be able to identify each row, so CONCURRENTLY requires at least one unique index on the materialized view that uses only column names (no expressions) and covers all rows (no WHERE clause). The materialized view must also already be populated. And because it computes the result and then a diff, it is usually slower than a normal refresh, especially when most rows change.

Java
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales;
-- ERROR: cannot refresh materialized view "public.daily_sales" concurrently
-- HINT: Create a unique index with no WHERE clause on one or more columns of the materialized view.
CREATE UNIQUE INDEX daily_sales_key ON daily_sales (day, region);
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales; -- REFRESH MATERIALIZED VIEW; readers never waited
The two refresh modes
REFRESHREFRESH CONCURRENTLY
Lock takenACCESS EXCLUSIVEEXCLUSIVE
Readers during refreshwait until it finisheskeep reading old rows
Needs a unique indexnoyes, plain columns, no WHERE
Speedfaster, one rewriteslower, computes and diffs
Works on an unpopulated viewyesno
Both recompute the whole query; only how the result is applied differs.

So the choice is about who waits. A normal refresh is faster but blocks readers; a concurrent refresh is slower but invisible to them. For anything users look at while it refreshes, CONCURRENTLY is usually right.

4Scheduling Refreshes

PostgreSQL has no automatic or incremental refresh for materialized views: nothing happens until someone runs REFRESH, and every refresh recomputes the full query, even if one row changed. So we schedule it, either inside the database with the pg_cron extension or from the application, for example a Spring @Scheduled method. For aggregates that must be both fresh and cheap, teams maintain a summary table instead, updated incrementally by a job or by triggers.

Java
-- With pg_cron, every 5 minutes
SELECT cron.schedule('refresh-daily-sales', '*/5 * * * *',
'REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales');
// Or from Spring
@Scheduled(fixedDelay = 300_000)
void refresh() { jdbc.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY daily_sales"); }

Notice fixedDelay rather than fixedRate: the next refresh starts five minutes after the previous one finishes, so a slow refresh can never overlap the next one. In a cluster of several application instances, only one should run it, for example with a scheduler lock such as ShedLock.

5MySQL Has No Materialized Views

MySQL 8.4 has ordinary views but no materialized views. We get the same effect with a summary table: a normal table that a scheduled event, an application job or triggers keep up to date. A common pattern is to rebuild into a new table and swap it in with RENAME TABLE, which is atomic, so readers never see a half-built result.

SQL
CREATE TABLE daily_sales_new LIKE daily_sales;
INSERT INTO daily_sales_new
SELECT DATE(created_at), region, COUNT(*), SUM(total) FROM orders GROUP BY 1, 2;
RENAME TABLE daily_sales TO daily_sales_old, daily_sales_new TO daily_sales; -- atomic swap
DROP TABLE daily_sales_old;

6Follow-up questions

How would you show users how fresh the data is?

PostgreSQL does not record when a materialized view was last refreshed, so we record it ourselves: the refresh job writes the finish time into a small refresh_log table in the same transaction or right after, and the dashboard shows 'data as of 10:05'. An alternative is to include max(created_at) of the source rows in the materialized view itself. We also alert when the age exceeds the freshness target, for example ten minutes for a five-minute schedule.

Can I index a materialized view?

Yes, like a table, and we usually should, because queries filter it. A unique index is required for CONCURRENTLY anyway. Indexes are maintained during refresh, which adds to its cost.

What happens if two concurrent refreshes start at once?

The second waits for the first, because both need the EXCLUSIVE lock, and then recomputes everything again. Schedulers should prevent overlap with fixed delays or a lock rather than relying on the database to queue them.

7Conclusion

In this answer, we saw that a view runs its query on every read while a materialized view stores the result, trading freshness for fast reads. A normal REFRESH rebuilds it under ACCESS EXCLUSIVE and blocks readers, while CONCURRENTLY builds the new result and applies the differences so reads continue, at the price of a required unique index and a slower refresh. PostgreSQL never refreshes automatically, so we schedule refreshes with pg_cron or the application, and in MySQL we use summary tables instead.

What to say in the interviewAbout 90 seconds aloud

Open with

A normal view stores only the query, so every read runs it and the data is always current. A materialized view stores the result rows, so reads are fast and can use indexes, but the data is stale until we refresh it.

Then, if they want more

In PostgreSQL a plain REFRESH MATERIALIZED VIEW reruns the query and replaces the contents under an ACCESS EXCLUSIVE lock, so every reader waits for the whole refresh. With CONCURRENTLY, PostgreSQL computes the new result separately, diffs it with the current rows and applies only the changes, so readers keep seeing the old data and are never blocked.

It needs a unique index on the materialized view on plain columns with no WHERE clause, so it can match rows, the view must already be populated, and it is usually slower because of the diff.

Either way it is a full recomputation; PostgreSQL has no incremental refresh and never refreshes on its own, so I schedule it with pg_cron or a Spring scheduled job with a fixed delay so refreshes never overlap. If refreshes get too expensive, I switch to a summary table maintained incrementally.

MySQL has no materialized views, so there I use summary tables rebuilt by a job, often swapped in with RENAME TABLE. And I always show users the time of the last refresh.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How is a materialized view different from a view? In PostgreSQL, what does REFRESH...": https://scrivio-interview-library.netlify.app/databases/stored-code/4f3af5516a/