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 1mxfDe-0007Qi-OQ for pgsql-bugs@arkaria.postgresql.org; Thu, 16 Dec 2021 01:06:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mxfDd-00036m-DL for pgsql-bugs@arkaria.postgresql.org; Thu, 16 Dec 2021 01:06:01 +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 1mxfDd-00036c-5F for pgsql-bugs@lists.postgresql.org; Thu, 16 Dec 2021 01:06:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxfDZ-0005Bb-If for pgsql-bugs@lists.postgresql.org; Thu, 16 Dec 2021 01:06:00 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1BG15stf843837; Wed, 15 Dec 2021 20:05:54 -0500 From: Tom Lane To: Nicolas M cc: pgsql-bugs@lists.postgresql.org Subject: Re: Updatable view (where in) with check option doesn't validate data properly In-reply-to: References: Comments: In-reply-to Nicolas M message dated "Wed, 15 Dec 2021 15:09:35 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <843835.1639616754.1@sss.pgh.pa.us> Date: Wed, 15 Dec 2021 20:05:54 -0500 Message-ID: <843836.1639616754@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nicolas M writes: > create or replace view viewSimpleA as select * from tableA where name > like 'a%'with check option; [ behaves as expected ] > create or replace view viewA as select * from tableAwhere db_record_id in ( > select db_record_id from tableA > where name like 'a%' > ) with check option; [ not so much ] I don't think this is a bug. The problem is that we handle WITH CHECK OPTION by seeing whether the proposed new tuple value satisfies the view's WHERE clause. But the new tuple isn't yet stored, or at least isn't yet visible, so that that sub-select still finds the old row contents (or fails to find any row at all, in your first test). We could maybe hack our way to fixing that in the specific case you show here, but I'm not very excited about that, because this usage of WITH CHECK OPTION seems inherently unsafe. For example, if we permit an update based on the fact that there's currently a row with "name like 'a%'", there's no way to be certain that some other transaction hasn't concurrently changed or deleted that row. That'd lead to inconsistency once both transactions commit. In general I think that expecting WITH CHECK OPTION to enforce non-immutable conditions is pretty hazardous, for largely the same reasons that a non-immutable CHECK constraint is hazardous. We don't stop you from declaring such a constraint, but it's likely to bite you in the rear. regards, tom lane