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 1kjZJt-0005Db-Rb for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Nov 2020 02:53:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kjZJr-00076v-S1 for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Nov 2020 02:53:39 +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 1kjZJr-00076o-Ko for pgsql-hackers@lists.postgresql.org; Mon, 30 Nov 2020 02:53:39 +0000 Received: from mx0b-0050f201.pphosted.com ([148.163.141.54]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kjZJp-0005eL-6X for pgsql-hackers@lists.postgresql.org; Mon, 30 Nov 2020 02:53:39 +0000 Received: from pps.filterd (m0203370.ppops.net [127.0.0.1]) by mx0b-0050f201.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 0AU2mF8M027223 for ; Mon, 30 Nov 2020 11:53:34 +0900 Received: from sraihe.sra.co.jp (sraihe.sra.co.jp [221.255.117.38]) by mx0b-0050f201.pphosted.com with ESMTP id 3534arhjwr-1 for ; Mon, 30 Nov 2020 11:53:34 +0900 Received: from srascb.sra.co.jp (srascb2 [133.137.8.63]) by sraihe.sra.co.jp (Postfix) with ESMTP id 7E1852A684B for ; Mon, 30 Nov 2020 11:53:33 +0900 (JST) Received: from sranhm.sra.co.jp (osspc25 [133.137.174.97]) by srascb.sra.co.jp (Postfix) with ESMTP id 6848558024D for ; Mon, 30 Nov 2020 11:53:33 +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 37D9EA163A; Mon, 30 Nov 2020 11:53:33 +0900 (JST) Date: Mon, 30 Nov 2020 11:52:05 +0900 From: Yugo NAGATA To: Konstantin Knizhnik Cc: Tatsuo Ishii , pgsql-hackers@lists.postgresql.org Subject: Re: Implementing Incremental View Maintenance Message-Id: <20201130115205.1d2ca8a69bf02e8334a4a8fc@sraoss.co.jp> In-Reply-To: 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> <20201125220628.fd738d85b8db150ffd7254b5@sraoss.co.jp> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9820 signatures=668682 X-Proofpoint-Spam-Details: rule=spam_low_notspam policy=spam_low score=0 adultscore=0 mlxscore=0 priorityscore=1501 clxscore=1034 impostorscore=0 spamscore=0 bulkscore=0 phishscore=0 lowpriorityscore=0 suspectscore=18 malwarescore=0 mlxlogscore=500 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011300016 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, 25 Nov 2020 18:00:16 +0300 Konstantin Knizhnik wrote: > > > On 25.11.2020 16:06, Yugo NAGATA wrote: > > 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. > > Sorry, may be I do not correctly understand you or you do not understand me. > Lets consider two serializable transactions (I do not use view or > triggers, but perform correspondent updates manually): > > > > create table t(pk integer, val int); > create table mat_view(gby_key integer primary key, total bigint); > insert into t values (1,0),(2,0); > insert into mat_view values (1,0),(2,0); > > Session 1: Session 2: > > begin isolation level serializable; > begin isolation level serializable; > insert into t values (1,200);                         insert into t > values (1,300); > update mat_view set total=total+200  where gby_key=1; > update mat_view set total=total+300 where gby_key=1; > > commit; > ERROR:  could not serialize access due to concurrent update > > So both transactions are aborted. > It is expected behavior for serializable transactions. > But if transactions updating different records of mat_view, then them > can be executed concurrently: > > Session 1: Session 2: > > begin isolation level serializable; > begin isolation level serializable; > insert into t values (1,200);                         insert into t > values (2,300); > update mat_view set total=total+200  where gby_key=1; > update mat_view set total=total+300 where gby_key=2; > commit;                                                      commit; > > So, if transactions are using serializable isolation level, then we can > update mat view without exclusive lock > and if there is not conflict, this transaction can be executed concurrently. > > Please notice, that exclusive lock doesn't prevent conflict in first case: > > Session 1: Session 2: > > begin isolation level serializable; > begin isolation level serializable; > insert into t values (1,200);                         insert into t > values (1,300); > lock table mat_view; > update mat_view set total=total+200  where gby_key=1; > lock table mat_view; > > commit; > update mat_view set total=total+300 where gby_key=1; > commit; > ERROR:  could not serialize access due to concurrent update > > > So do you agree that there are no reasons for using explicit lock for > serializable transactions? Yes, I agree. I said an anomaly could occur in repeatable read and serializable isolation level, but it was wrong. In serializable, the transaction will be aborted in programmable cases due to predicate locks, and we don't need the lock. However, in repeatable read, the anomaly still could occurs when the table is defined on more than one base tables even if we lock the view. To prevent it, the only way I found is aborting the transaction forcedly in such cases for now. Regards, Yugo Nagata -- Yugo NAGATA