Sub topic 12
Refreshing a Materialized View
Assume you know how to create materialized views and run basic SELECT statements. This chapter uses PostgreSQL syntax. Other database systems provide similar features, but refresh commands, locking behavior, and scheduling options can differ.
Why a materialized view needs refreshing
A regular view runs its defining query whenever you read from it. A materialized view stores the query result, usually on disk. That stored result does not change automatically when the source tables change.
Refreshing a materialized view runs its defining query again and replaces the stored result with new data.
REFRESH MATERIALIZED VIEW department_salary_totals;Suppose department_salary_totals contains the total salary for each department. If an employee receives a raise after the materialized view was created, a query against the view still returns the old total until you refresh it.
Choosing when to refresh
The right schedule depends on how much staleness your application can accept.
| Refresh strategy | Suitable when | Main trade-off |
|---|---|---|
| On demand | Data is read occasionally | Results can become stale |
| Scheduled | Reports need predictable freshness | Requires a job scheduler |
| After a data load | A batch process defines the update boundary | Refresh may take time after each batch |
| Frequent refreshes | Users need near-current summaries | More database work |
A refresh recomputes the result, so it can consume substantial CPU, memory, disk, and I/O. Measure the defining query before choosing a short interval.
Blocking and concurrent refreshes
In PostgreSQL, the normal refresh can block reads from the materialized view while the stored result is being replaced:
REFRESH MATERIALIZED VIEW department_salary_totals;PostgreSQL also supports a concurrent refresh:
REFRESH MATERIALIZED VIEW CONCURRENTLY department_salary_totals;A concurrent refresh allows other sessions to read the existing materialized view during much of the refresh. It has additional requirements:
- The materialized view must already contain data. You cannot use
CONCURRENTLYfor its first population. - The view must have a suitable unique index covering all rows.
- PostgreSQL does not allow this command inside a transaction block.
For example:
CREATE UNIQUE INDEX department_salary_totals_department_id_uq
ON department_salary_totals (department_id);
REFRESH MATERIALIZED VIEW CONCURRENTLY department_salary_totals;The unique index is not just an optimization. PostgreSQL uses it to match rows while updating the materialized view.
Running refreshes safely
A production refresh process should answer these questions:
- How old may the data be?
- How long does a refresh normally take?
- What happens if a refresh fails?
- Can two refresh jobs run at the same time?
- How will operators detect stale data?
Avoid overlapping refresh jobs. A scheduler can use a lock, job state, or another coordination mechanism so that a slow refresh does not start a second copy.
After a refresh, check the result and record the completion time. You can also expose that time in a separate table or monitoring system. A successful command does not prove that the underlying data was loaded correctly.
Database-specific syntax
REFRESH MATERIALIZED VIEW is common in PostgreSQL, but it is not universal SQL syntax. Oracle uses its own materialized-view refresh mechanisms, and other systems may refresh views through procedures or scheduled jobs.
Check the documentation for your database before copying options such as CONCURRENTLY. Also verify whether refreshes are atomic, whether readers are blocked, and whether the system supports incremental refresh. Incremental refresh updates only affected portions, but it requires database-specific rules and extra tracking.
Exercise
A materialized view was created yesterday, and its source table changed today. What must you do for a normal query against the materialized view to see the new source data?
Show answer
Refresh the materialized view. For PostgreSQL, run REFRESH MATERIALIZED VIEW view_name;, or use CONCURRENTLY when its requirements are satisfied.