Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khuWj-0001Vp-9Z for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Nov 2020 13:08:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khuWi-0005vn-8o for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Nov 2020 13:08:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khuWi-0005vg-2T for pgsql-hackers@lists.postgresql.org; Wed, 25 Nov 2020 13:08:04 +0000 Received: from mx0a-0050f201.pphosted.com ([148.163.145.86]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khuWd-0006aR-VL for pgsql-hackers@lists.postgresql.org; Wed, 25 Nov 2020 13:08:03 +0000 Received: from pps.filterd (m0203369.ppops.net [127.0.0.1]) by mx0b-0050f201.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 0APD5nq2008734 for ; Wed, 25 Nov 2020 22:07:56 +0900 Received: from sraihe.sra.co.jp (sraihe.sra.co.jp [221.255.117.38]) by mx0b-0050f201.pphosted.com with ESMTP id 34xtehugae-1 for ; Wed, 25 Nov 2020 22:07:56 +0900 Received: from srascb.sra.co.jp (srascb2 [133.137.8.63]) by sraihe.sra.co.jp (Postfix) with ESMTP id 6DAB52A684A for ; Wed, 25 Nov 2020 22:07:55 +0900 (JST) Received: from sranhm.sra.co.jp (osspc25 [133.137.174.97]) by srascb.sra.co.jp (Postfix) with ESMTP id 5BE8F580219 for ; Wed, 25 Nov 2020 22:07:55 +0900 (JST) Received: from yugon-CFSV7-1 (sraihb-hub.sra.co.jp [133.137.8.6]) by sranhm.sra.co.jp (Postfix) with SMTP id 2EEACA13BC; Wed, 25 Nov 2020 22:07:55 +0900 (JST) Date: Wed, 25 Nov 2020 22:06:28 +0900 From: Yugo NAGATA To: Konstantin Knizhnik Cc: Tatsuo Ishii , pgsql-hackers@lists.postgresql.org Subject: Re: Implementing Incremental View Maintenance Message-Id: <20201125220628.fd738d85b8db150ffd7254b5@sraoss.co.jp> In-Reply-To: <172440c3-d7cf-7642-97d1-2996c70ab9fc@postgrespro.ru> References: <20201001140627.1f8fafe595f545e026d110c6@sraoss.co.jp> <20201005181618.31f2baf8583358fcfbed26cd@sraoss.co.jp> <024ba056-6b4f-b26c-af17-297c1bbca65a@postgrespro.ru> <20201112.205355.393498768557598337.t-ishii@sraoss.co.jp> <89729da8-9042-7ea0-95af-e415df6da14d@postgrespro.ru> <20201124182133.2b7d2ee8767f4bb946226ecc@sraoss.co.jp> <5663f5f0-48af-686c-bf3c-62d279567e2a@postgrespro.ru> <20201124191138.f57e0955622f09e333538d7b@sraoss.co.jp> <172440c3-d7cf-7642-97d1-2996c70ab9fc@postgrespro.ru> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9815 signatures=668682 X-Proofpoint-Spam-Details: rule=spam_low_notspam policy=spam_low score=0 phishscore=0 bulkscore=0 impostorscore=0 malwarescore=0 clxscore=1034 priorityscore=1501 adultscore=0 spamscore=0 suspectscore=18 mlxlogscore=738 mlxscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011250080 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 25 Nov 2020 15:16:05 +0300 Konstantin Knizhnik wrote: > > > On 24.11.2020 13:11, Yugo NAGATA wrote: > > > >> I wonder if it is possible to somehow use predicate locking mechanism of > >> Postgres to avoid this anomalies without global lock? > > You mean that, ,instead of using any table lock, if any possibility of the > > anomaly is detected using predlock mechanism then abort the transaction? > > Yes. If both transactions are using serializable isolation level, then > lock is not needed, isn't it? > So at least you can add yet another simple optimization: if transaction > has serializable isolation level, > then exclusive lock is not required. As long as we use the trigger approach, we can't handle concurrent view maintenance in either repeatable read or serializable isolation level. It is because one transaction (R= R+dR) cannot see changes occurred in another transaction (S'= S+dS) in such cases, and we cannot get the incremental change on the view (dV=dR*dS). Therefore, in the current implementation, the transaction is aborted when the concurrent view maintenance happens in repeatable read or serializable. > But I wonder if we can go further so that even if transaction is using > read-committed or repeatable-read isolation level, > we still can replace exclusive table lock with predicate locks. > > The main problem with this approach (from my point of view) is the > predicate locks are able to detect conflict but not able to prevent it. > I.e. if such conflict is detected then transaction has to be aborted. > And it is not always desirable, especially because user doesn't expect > it: how can insertion of single record with unique keys in a table cause > transaction conflict? > And this is what will happen in your example with transactions T1 and T2 > inserting records in R and S tables. Yes. I wonder that either aborting transaction or waiting on locks is unavoidable when a view is incrementally updated concurrently (at least in the immediate maintenance where a view is update in the same transaction that updates the base table). > And what do you think about backrgound update of materialized view? > On update/insert trigger will just add record to some "delta" table and > then some background worker will update view. > Certainly in this case we loose synchronization between main table and > materialized view (last one may contain slightly deteriorated data). > But in this case no exclusive lock is needed, isn't it? Of course, we are considering this type of view maintenance. This is deferred maintenance where a view is update after the transaction that updates the base tables is committed. Views can be updated in bacground in a appropreate timing or as a response of a user command. To implement this, we needs a mechanism to maintain change logs which records changes of base tables. We think that implementing this infrastructure is not trivial work, so, in the first patch proposal, we decided to start from immediate approach which needs less code. -- Yugo NAGATA