PostgreSQL Materialized Views
Summary: in this tutorial, you will learn about PostgreSQL materialized views that store the result of a query physically and refresh the data from base tables periodically.
Introduction to the PostgreSQL materialized views
In PostgreSQL, views are virtual tables that represent data of the underlying tables. Simple views can be updatable.
PostgreSQL extends the view concept to the next level which allows views to store data physically. These views are called materialized views.
Materialized views cache the result set of an expensive query and allow you to refresh data periodically.
The materialized views can be useful in many cases that require fast data access. Therefore, you often find them in data warehouses and business intelligence applications.
Creating materialized views
To create a materialized view, you use the CREATE MATERIALIZED VIEW
statement as follows:
How it works.
- First, specify the
view_name
after theCREATE MATERIALIZED VIEW
clause - Second, add the query that retrieves data from the underlying tables after the
AS
keyword. - Third, if you want to load data into the materialized view at the creation time, use the
WITH DATA
option; otherwise, you useWITH NO DATA
option. If you use theWITH NO DATA
option, the view is flagged as unreadable. It means that you cannot query data from the view until you load data into it. - Finally, use the IF NOT EXISTS option to conditionally create a view only if it does not exist.
Refreshing data for materialized views
To load data into a materialized view, you use the REFRESH MATERIALIZED VIEW
statement:
When you refresh data for a materialized view, PostgreSQL locks the underlying tables. Consequently, you will not be able to retrieve data from underlying tables while data is loading into the view.
To avoid this, you can use the CONCURRENTLY
option.
With the CONCURRENTLY
option, PostgreSQL creates a temporary updated version of the materialized view, compares two versions, and performs INSERT and UPDATE only the differences.
PostgreSQL allows you to retrieve data from a materialized view while it is being updated. One requirement for using CONCURRENTLY
option is that the materialized view must have a UNIQUE
index.
Notice that CONCURRENTLY
option is only available in PostgreSQL 9.4 or later.
Removing materialized views
To remove a materialized view, you use the DROP MATERIALIZED VIEW
statement:
In this syntax, you specify the name of the materialized view that you want to drop after the DROP MATERIALIZED VIEW
keywords.
PostgreSQL materialized views example
We’ll use the tables in the sample database for creating a materialized view.
First, create a materialized view named rental_by_category
using the CREATE MATERIALIZED VIEW
statement:
Because of the WITH NO DATA
option, you cannot query data from the view. If you attempt to do so, you’ll get the following error message:
Output:
PostgreSQL is helpful to give you a hint to ask for loading data into the view.
Second, load data into the materialized view using the REFRESH MATERIALIZED VIEW
statement:
Third, retrieve data from the materialized view:
Output:
From now on, you can refresh the data in the rental_by_category
view using the REFRESH MATERIALIZED VIEW
statement.
However, to refresh it with CONCURRENTLY
option, you need to create a UNIQUE
index for the view first.
Let’s refresh data concurrently for the rental_by_category
view.
Summary
- A materialized view is a view that stores data that comes from the base tables.
- Use the
CREATE MATERIALIZED VIEW
statement to create a materialized view. - Use the
REFRESH MATERIALIZED VIEW
statement to load data from the base tables into the view. - Use the
DROP MATERIALIZED VIEW
statement to drop a materialized view.