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 1mlKGK-0003Ub-Bm for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Nov 2021 00:17:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mlKGJ-0008Kn-9P for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Nov 2021 00:17:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mlKGI-0008JB-W9 for pgsql-hackers@lists.postgresql.org; Fri, 12 Nov 2021 00:17:47 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mlKGG-0004zQ-Ug for pgsql-hackers@lists.postgresql.org; Fri, 12 Nov 2021 00:17:46 +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 1AC0Hf791627849; Thu, 11 Nov 2021 19:17:41 -0500 From: Tom Lane To: Alvaro Herrera cc: Amit Langote , vignesh C , Zhihong Yu , Corey Huinker , Kyotaro Horiguchi , pgsql-hackers@lists.postgresql.org Subject: Re: simplifying foreign key/RI checks In-reply-to: <202111112338.3icgjvnzgkbn@alvherre.pgsql> References: <202111112338.3icgjvnzgkbn@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Thu, 11 Nov 2021 20:38:50 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1627847.1636676261.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 11 Nov 2021 19:17:41 -0500 Message-ID: <1627848.1636676261@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alvaro Herrera writes: > I think we (I) should definitely pursue fixing whatever was broken by > DETACH CONCURRENTLY, back to pg14, independently of this patch ... but > I would appreciate some insight into what the problem is. Here's what I'm on about: regression=3D# create table pk (f1 int primary key); CREATE TABLE regression=3D# insert into pk values(1); INSERT 0 1 regression=3D# create table fk (f1 int references pk); CREATE TABLE regression=3D# begin isolation level repeatable read ; BEGIN regression=3D*# select * from pk; -- to establish xact snapshot f1 = ---- 1 (1 row) now, in another session, do: regression=3D# insert into pk values(2); INSERT 0 1 back at the RR transaction, we can't see that: regression=3D*# select * from pk; -- still no row 2 f1 = ---- 1 (1 row) so we get: regression=3D*# insert into fk values(1); INSERT 0 1 regression=3D*# insert into fk values(2); ERROR: insert or update on table "fk" violates foreign key constraint "fk= _f1_fkey" DETAIL: Key (f1)=3D(2) is not present in table "pk". IMO that behavior is correct. If you use READ COMMITTED, then SELECT can see row 2 as soon as it's committed, and so can the FK check, and again that's correct. In v13, the behavior is the same if "pk" is a partitioned table instead of a plain one. In HEAD, it's not: regression=3D# drop table pk, fk; DROP TABLE regression=3D# create table pk (f1 int primary key) partition by list(f1); CREATE TABLE regression=3D# create table pk1 partition of pk for values in (1,2); CREATE TABLE regression=3D# insert into pk values(1); INSERT 0 1 regression=3D# create table fk (f1 int references pk); CREATE TABLE regression=3D# begin isolation level repeatable read ; BEGIN regression=3D*# select * from pk; -- to establish xact snapshot f1 = ---- 1 (1 row) --- now insert row 2 in another session regression=3D*# select * from pk; -- still no row 2 f1 = ---- 1 (1 row) regression=3D*# insert into fk values(1); INSERT 0 1 regression=3D*# insert into fk values(2); INSERT 0 1 regression=3D*# So I say that's busted, and the cause is this hunk from 71f4c8c6f: @@ -392,11 +392,15 @@ RI_FKey_check(TriggerData *trigdata) = /* * Now check that foreign key exists in PK table + * + * XXX detectNewRows must be true when a partitioned table is on the + * referenced side. The reason is that our snapshot must be fresh + * in order for the hack in find_inheritance_children() to work. */ ri_PerformCheck(riinfo, &qkey, qplan, fk_rel, pk_rel, NULL, newslot, - false, + pk_rel->rd_rel->relkind =3D=3D RELKIND_PARTITIONED_TA= BLE, SPI_OK_SELECT); = if (SPI_finish() !=3D SPI_OK_FINISH) I think you need some signalling mechanism that's less global than ActiveSnapshot to tell the partition-lookup machinery what to do in this context. regards, tom lane