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.

Kindcommand
Example LanguageSQL

📝 Syntax

REFRESH MATERIALIZED VIEW [CONCURRENTLY] <materialized view name>
[WITH [NO] DATA]

⚙️ Parameters

CONCURRENTLYRefreshes in the background while old data stays readable
WITH DATAPopulates the materialized view (the default)
WITH NO DATAEmpties 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏