SQL REFRESH MATERIALIZED VIEW
The command that brings a materialized view's stored data back up to date.
What & Why
A materialized view never updates itself — REFRESH MATERIALIZED VIEW re-runs the original query and replaces the stored result. This is usually scheduled (a nightly job, an hourly cron task), not run on every single underlying data change.
See How It Works
BUSINESS QUESTION
Refresh the stored daily event summary created from growth.events, then read its current snapshot.
| id | user_id | event_name | created_at | properties |
|---|---|---|---|---|
| 1001 | 101 | signup | 2024-01-15 09:12:00+00 | {"source":"organic"} |
| 1002 | 101 | activated | 2024-01-16 14:25:00+00 | {"step":"workspace"} |
| 1003 | 102 | signup | 2024-02-10 11:08:00+00 | {"source":"paid"} |
| 1004 | 103 | report_viewed | 2024-03-12 16:40:00+00 | {"report":"retention"} |
EXAMPLE QUERY
-- public.daily_event_summary is defined from growth.events
REFRESH MATERIALIZED VIEW public.daily_event_summary;
SELECT
event_date,
event_name,
events,
users
FROM public.daily_event_summary
ORDER BY event_date, event_name;RESULT — refreshed daily event summary
| event_date | event_name | events | users |
|---|---|---|---|
| 2024-01-15 | signup | 1 | 1 |
| 2024-01-16 | activated | 1 | 1 |
| 2024-02-10 | signup | 1 | 1 |
| 2024-03-12 | report_viewed | 1 | 1 |
REFRESH recomputes public.daily_event_summary from the same four growth.events source rows.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario