From: Yugo NAGATA <nagata@sraoss.co.jp>
To: pgsql-hackers@postgresql.org
Subject: SI-read predicate locks on materialized views
Date: Tue, 26 Jul 2022 16:44:34 +0900
Message-ID: <20220726164434.42d4e33911b4b4fcf751c4e7@sraoss.co.jp> (raw)
Hi,
I propose to acquire SI-read predicate locks on materialized views
as the attached patch.
Currently, materialized views do not participate in predicate locking,
but I think this causes a serialization anomaly when `REFRESH
MATERIALIZED VIEW CONCURRENTLY` is used.
For example, supporse that there is a table "orders" which contains
order information and a materialized view "order_summary" which contains
summary of the order information.
CREATE TABLE orders (date date, item text, num int);
CREATE MATERIALIZED VIEW order_summary AS
SELECT date, item, sum(num) FROM orders GROUP BY date, item;
"order_summary" is refreshed once per day in the following transaction.
T1:
REFRESH MATERIALIZED VIEW CONCURRENTLY order_summary;
"orders" has a date column, and when a new item is inserted, the date
value is determined as the next day of the last date recorded in
"order_summary" as in the following transaction.
T2:
SELECT max(date) + 1 INTO today FROM order_summary;
INSERT INTO orders(date, item, num) VALUES (today, 'apple', 1);
If such two transactions run concurrently, a write skew anomaly occurs,
and the result of order_summary refreshed in T1 will not contain the
record inserted in T2.
On the other hand, if the materialized view participates in predicate
locking and the transaction isolation level is SELIALIZABLE, this
anomaly can be avoided; one of the transaction will be aborted and
suggested to be retried.
The problem doesn't occur when we use REFRESH MATERIALIZED VIEW
(not CONCURRENTLY) because it acquires the strongest lock and
any concurrent transactions are prevent from reading the materialized view.
I think this is the reason why materialized views didn't have to
participate in predicate locking. However, this is no longer the case
because now we support REFRESH ... CONCURRENTLY which refreshes the
materialized view using DELETE and INSERT and also allow to read it
from concurrent transactions. I think we can regard them as same as
DELETE, INSERT, and SELECT on regular tables and acquire predicate
locks on materialized views as well.
What do you think about it?
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attachments:predicate_locks_on_matview.patch (text/x-diff, 723B) downloaddiff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.cindex 5136da6ea3..6d414bfcc9 100644--- a/src/backend/storage/lmgr/predicate.c+++ b/src/backend/storage/lmgr/predicate.c@@ -489,14 +489,13 @@ static void ReleasePredicateLocksLocal(void);
/*
* Does this relation participate in predicate locking? Temporary and system
- * relations are exempt, as are materialized views.+ * relations are exempt.
*/
static inline bool
PredicateLockingNeededForRelation(Relation relation)
{
return !(relation->rd_id < FirstUnpinnedObjectId ||
- RelationUsesLocalBuffers(relation) ||- relation->rd_rel->relkind == RELKIND_MATVIEW);+ RelationUsesLocalBuffers(relation));
}
/*
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: nagata@sraoss.co.jp
Subject: Re: SI-read predicate locks on materialized views
In-Reply-To: <20220726164434.42d4e33911b4b4fcf751c4e7@sraoss.co.jp>
References: <20220726164434.42d4e33911b4b4fcf751c4e7@sraoss.co.jp>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox