On 16th of July, Kevin Grittner committed patch:

Add support for REFRESH MATERIALIZED VIEW CONCURRENTLY.
 
This allows reads to continue without any blocking while a REFRESH
runs.  The new data appears atomically as part of transaction
commit.
 
Review questioned the Assert that a matview was not a system
relation.  This will be addressed separately.
 
Reviewed by Hitoshi Harada, Robert Haas, Andres Freund.
Merged after review with security patch f3ab5d4.

Back when Materialized Views where introduced refreshing materialized view data required obtaining AccessExclusiveLock – thus effectively blocking all concurrent access to the view.

Not, luckily, we can do better.

Let's see it:

psql-1> create materialized view test as select now() as refreshed, i from generate_series(1,2) i;
SELECT 2

Then, I can:

psql-1> select now(), * from test;
              now              |           refreshed           | i 
-------------------------------+-------------------------------+---
 2013-07-22 15:31:20.265253+02 | 2013-07-22 15:31:07.814336+02 | 1
 2013-07-22 15:31:20.265253+02 | 2013-07-22 15:31:07.814336+02 | 2
(2 rows)

OK. We can see that the refreshed column is lagging, so it's ok.

Now, let's see previous approach to refresh, with 2nd session trying to get access to the view:

psql-1> begin;
BEGIN
psql-1> refresh materialized view test;
REFRESH MATERIALIZED VIEW

I didn't commit it. In other psql:

psql-2> set statement_timeout = 1000;
SET
psql-2> select * from test;
ERROR:  canceling statement due to statement timeout

As expected – refresh locks the view.

But what about doing it concurrently?

I did rollback to the refresh transaction, and then:

psql-1> begin;
BEGIN
psql-1> refresh materialized view concurrently test;
ERROR:  cannot refresh materialized view "public.test" concurrently
HINT:  Create a UNIQUE index with no WHERE clause on one or more columns of the materialized view.

OK. So, I have to add index, on the view:

psql-1> create unique index some_index on test (i);
CREATE INDEX
psql-1> begin;
BEGIN
psql-1> refresh materialized view concurrently test;
REFRESH MATERIALIZED VIEW

Afterwards, it just works, which is fine. Now – I didn't commit the transaction at this point, so in 2nd psql I tested access:

psql-2> select now(), * from test;
              now              |           refreshed           | i 
-------------------------------+-------------------------------+---
 2013-07-22 15:35:30.611278+02 | 2013-07-22 15:31:07.814336+02 | 1
 2013-07-22 15:35:30.611278+02 | 2013-07-22 15:31:07.814336+02 | 2
(2 rows)

Please note the refreshed is still old. After I did this select, I committed refresh in 1st psql, and re-run the select * from test:

psql-2> select now(), * from test;
              now              |           refreshed           | i 
-------------------------------+-------------------------------+---
 2013-07-22 15:35:35.817046+02 | 2013-07-22 15:35:08.925637+02 | 2
 2013-07-22 15:35:35.817046+02 | 2013-07-22 15:35:08.925637+02 | 1
(2 rows)

So – until transaction with refresh ends – I can query, and will see old data. After it will be committed – I will see new data. Just as expected. All fine.

To be honest, I am not entirely sure why there is requirement for unique index – I thought that refresh would just create new “file" with data, and after finishing, it would just switch which “file" pg reads data from. But, I am by no means Pg hacker, so there have to be some important reason why unique index is needed.

Anyway – it's great addition, and one that will make the materialized views much more functional.

  1. One comment

  2. # Thom Brown
    Jul 23, 2013

    The reason a unique index is required is because when refreshing a materialized view concurrently, it creates a temporary table containing the new materialized data, then performs a full outer join against the old data, and this can’t be done if there are any duplicate rows.

Leave a comment