Sub topic 11
Limitations and Alternatives to Built-In Materialized Views
This chapter assumes you know SELECT, JOIN, GROUP BY, aggregate functions, plain views, and basic indexes. You should also know that a PostgreSQL materialized view stores query results and changes only when you refresh it.
A materialized view is useful because it moves work from read time to refresh time. That trade-off creates limits. The right alternative depends on how fresh the data must be, how expensive the query is, and how much update logic your system can safely maintain.
The main limitations
Data can be stale
A materialized view does not automatically see changes in its base tables. A dashboard may show yesterday's totals even though new orders exist.
The delay between a base-table change and the next refresh is the view's staleness window. A five-minute refresh schedule gives an expected delay of up to about five minutes, plus the time needed to run the refresh.
Refreshes can be expensive
A normal refresh runs the defining query again and replaces the stored rows. For a large aggregation, this can require substantial CPU, memory, temporary space, and reads from the base tables.
REFRESH MATERIALIZED VIEW CONCURRENTLY allows readers to continue using the old contents while PostgreSQL builds the new contents. It still performs substantial work. It also requires a unique index covering all rows, and only one refresh can run for a materialized view at a time.
PostgreSQL does not provide built-in incremental refresh
PostgreSQL does not automatically update only the groups affected by new or changed rows. If one order changes, a refresh may still recompute every day and every customer in the result.
You can build incremental maintenance yourself, but that moves complexity into application code, triggers, queues, or batch jobs. Deletes, updates, late-arriving data, and failed jobs must all be handled correctly.
Choosing an alternative
The following options solve different parts of the problem:
| Approach | Freshness | Read cost | Maintenance cost |
|---|---|---|---|
| Plain view | Current at query time | Can be high | Low |
| Materialized view | Refresh-dependent | Usually low | Medium |
| Summary table | Near-current or scheduled | Low | High |
| Application cache | Cache-dependent | Very low | Medium |
| External analytical system | Batch or streaming | Low for reports | High |
Use a plain view for current results
A plain view stores only a query definition. Each read runs the query against current base-table data. It avoids stale results and refresh jobs, but repeated complex joins and aggregates can make every request expensive.
A plain view is often the simplest choice when the query is small, the data set is moderate, or correctness requires current data.
Use a summary table for controlled incremental updates
A summary table is an ordinary table whose rows contain precomputed results. A scheduled job can update it in batches, or application logic can update affected groups as transactions arrive.
For example, an order-processing transaction might update a customer's lifetime total in the same transaction:
INSERT INTO customer_totals (customer_id, lifetime_value)
VALUES (42, 25.00)
ON CONFLICT (customer_id)
DO UPDATE SET lifetime_value =
customer_totals.lifetime_value + EXCLUDED.lifetime_value;This can provide fresher results than a materialized view. However, corrections and deleted orders require compensating updates. Concurrent writes can also create contention or expose bugs in the maintenance code.
Use a cache or external system for specialized workloads
An application cache works well for frequently requested values that can tolerate expiration. It is fast, but invalidation and consistency become application responsibilities.
For large historical reports, a data warehouse, streaming pipeline, or incremental-view extension may be more suitable. These tools can offer incremental processing, partitioning, or distributed computation, but they add operational cost and another data path to monitor.
A practical decision process
Use a materialized view when the query is expensive, reads greatly outnumber refreshes, and delayed data is acceptable. Prefer a plain view when freshness matters more than read speed. Prefer a summary table when you need controlled incremental updates and can test the maintenance rules. Consider an external system when the workload exceeds what one PostgreSQL database should repeatedly aggregate.
Exercise
A report can be 15 minutes old, but its aggregation scans billions of rows. Which approach is a reasonable first choice, and what risk must you address?
Show answer
Start with a materialized view refreshed on a schedule. Address the risk that refreshes may be expensive or fail, leaving users with stale results; monitor refresh duration and failure state.