Sub topic 1
Views, Materialized Views, and Tables: What Is Stored and What Is Not
Prerequisites
This chapter assumes you know basic SQL, including SELECT, JOIN, GROUP BY, and aggregate functions. You should also understand PostgreSQL tables, plain views, and indexes.
A relation is a database object that can be queried like a table. A table stores rows directly. A plain view stores a query definition. A materialized view stores the rows produced by a query.
Three Ways to Represent Queryable Data
Suppose an application needs daily sales totals. PostgreSQL can represent this result in three different ways:
| Object | Stores query definition | Stores result rows | Automatically reflects base-table changes |
|---|---|---|---|
| Plain view | Yes | No | Yes |
| Materialized view | Yes | Yes | No |
| Table | No, unless maintained separately | Yes | No |
A plain view is a named query. When you select from it, PostgreSQL runs the view's underlying query against the current base tables. The view usually does not store a separate copy of the result.
A materialized view is a named query whose result is stored on disk. A later SELECT reads those stored rows. The stored result remains unchanged until you refresh the materialized view.
A table stores rows directly and can be changed with INSERT, UPDATE, and DELETE. PostgreSQL does not know which query should produce or maintain those rows. Your application, a job, or a trigger must maintain them.
Plain Views Read Current Data
A plain view is useful when you want a reusable query, a stable interface, or controlled access to columns and rows.
CREATE VIEW sales_daily_live AS
SELECT order_date AS day,
count(*) AS orders,
sum(total) AS revenue
FROM orders
GROUP BY order_date;
SELECT *
FROM sales_daily_live
WHERE day >= CURRENT_DATE - 7
ORDER BY day;The query inside sales_daily_live runs when you select from the view. If a new order is committed, the next query can include it immediately. The cost of this freshness is that PostgreSQL must perform the aggregation when the view is queried. An index on orders may help, but the database still computes the result as part of the query.
A plain view does not normally have its own rows or indexes. You index the underlying tables instead.
Materialized Views Read a Stored Snapshot
A materialized view stores the result of its defining query. This can make repeated, expensive reports much faster.
CREATE MATERIALIZED VIEW sales_daily AS
SELECT order_date AS day,
count(*) AS orders,
sum(total) AS revenue
FROM orders
GROUP BY order_date;
SELECT *
FROM sales_daily
WHERE day >= CURRENT_DATE - 7
ORDER BY day;The CREATE MATERIALIZED VIEW statement runs the aggregate query and stores its output. Later reads use sales_daily as a relation. You can add indexes to it, just as you can with a table.
However, changes to orders do not change sales_daily automatically. The materialized view is a snapshot from the time it was created or last refreshed.
INSERT INTO orders (customer_id, order_date, total)
VALUES (42, CURRENT_DATE, 75.00);
-- This may still show the old result.
SELECT *
FROM sales_daily
WHERE day = CURRENT_DATE;
REFRESH MATERIALIZED VIEW sales_daily;
-- This now includes the inserted order.
SELECT *
FROM sales_daily
WHERE day = CURRENT_DATE;The refresh reruns the defining query and replaces the stored contents. Refreshing can consume CPU, memory, and I/O, and it can take time for large source tables.
Choosing Between the Objects
Use a plain view when the result must reflect committed source data immediately and the query cost is acceptable. Use a materialized view when reads are frequent or expensive, the result can be slightly old, and a refresh process is practical. Use a table when the result needs independent edits, custom incremental maintenance, or a data lifecycle that does not match one defining query.
The choice is about both storage and responsibility. A materialized view stores the result for you, but you must decide when to refresh it. A table gives you more control, but you must maintain its correctness yourself.
Exercise
A dashboard can tolerate data that is 15 minutes old, but its aggregate query scans millions of rows and runs hundreds of times per minute. Which object is a strong candidate, and what responsibility does it add?
Show answer
A materialized view is a strong candidate because it stores the aggregate result for fast reads. It adds the responsibility of refreshing the view often enough to meet the dashboard's freshness requirement.