Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8Isy-003JTr-Gl for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Nov 2024 12:42:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t8Isv-00D2uI-Mq for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Nov 2024 12:42:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t84RJ-005RBm-1t for pgsql-hackers@lists.postgresql.org; Mon, 04 Nov 2024 21:16:45 +0000 Received: from smtp.outgoing.loopia.se ([93.188.3.38]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t84RF-000BxR-Ox for pgsql-hackers@lists.postgresql.org; Mon, 04 Nov 2024 21:16:44 +0000 Received: from s807.loopia.se (localhost [127.0.0.1]) by s807.loopia.se (Postfix) with ESMTP id 2C99013BD21 for ; Mon, 04 Nov 2024 22:16:38 +0100 (CET) Received: from s980.loopia.se (unknown [172.22.191.5]) by s807.loopia.se (Postfix) with ESMTP id 1CC6B13C481; Mon, 04 Nov 2024 22:16:38 +0100 (CET) Received: from s471.loopia.se (unknown [172.22.191.6]) by s980.loopia.se (Postfix) with ESMTP id 1890822015A5; Mon, 04 Nov 2024 22:16:38 +0100 (CET) X-Virus-Scanned: amavisd-new at amavis.loopia.se X-Spam-Flag: NO X-Spam-Score: -1.2 X-Spam-Level: X-Spam-Status: No, score=-1.2 tagged_above=-999 required=6.2 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1] autolearn=disabled Authentication-Results: s471.loopia.se (amavisd-new); dkim=pass (2048-bit key) header.d=movsom.se Received: from s981.loopia.se ([172.22.191.6]) by s471.loopia.se (s471.loopia.se [172.22.190.35]) (amavisd-new, port 10024) with LMTP id Yfv7w7GG5yYC; Mon, 4 Nov 2024 22:16:37 +0100 (CET) X-Loopia-Auth: webmail X-Loopia-User: sam@movsom.se Received: from webmail.loopia.se (unknown [172.22.212.10]) (Authenticated sender: sam@movsom.se) by s981.loopia.se (Postfix) with ESMTPA id 789D722B17C1; Mon, 04 Nov 2024 22:16:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=movsom.se; s=loopiadkim1707399282; t=1730754997; bh=GIhSPIeAVdQX1fwPWFQrtX6L6UeRfP7O86MRe3vLA6o=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=HL9R6F1WcmSDpZJpZ+pRiDt18Ko4Ff8AOsjn4Y/GT04LKfrey/hKESFId+nJJ5j2R m4BIpix2uSoM8STOnUFzzPsBk6Ye2xIo/pLMgRww/mGThN8y7QOf7oKnf4e5yKwPDn Qwjk4RM1yfjjHAJ69mJU4Q4KFbcRXzJHKi3pjAMtiX0QoGoQ5+LxC7qp8ORiPaJNni SyTzWEFL5MFRoqKCLDEN7MOF+FiBQ/rGXr4reRfIXjzCsXu1W7YSCjenjihE4GP5sT UoYEfgQN/qNVIQPB8/HN+SO7Nekr+cYhUmsgN68eUbp7kDUc9h05Uj+ZzIhUdnW2/r T7T9Qk7jdNgSw== MIME-Version: 1.0 Date: Mon, 04 Nov 2024 22:16:37 +0100 From: Sam Gabrielsson To: Paul Jungwirth Cc: Peter Eisentraut , jian he , Isaac Morland , Robert Haas , Jeff Davis , PostgreSQL Hackers Subject: Re: SQL:2011 application time In-Reply-To: <57ea0668-5205-426e-b934-efc89f2186c2@illuminatedcomputing.com> References: <30dc3561-d7ab-42be-a1c5-096643423590@illuminatedcomputing.com> <73d09e24-b6b9-4c89-8c0a-8842ca0f81f9@illuminatedcomputing.com> <081aa43c-3a97-4bd5-a498-ba02e0a7570d@illuminatedcomputing.com> <54390149-11d6-4f52-bbe8-f69832ceb3bd@eisentraut.org> <9b7cb814-c78c-489f-85b2-32be4398647f@illuminatedcomputing.com> <95c6d49b-7d60-4ad0-820c-e00ae1b2a3d4@illuminatedcomputing.com> <57ea0668-5205-426e-b934-efc89f2186c2@illuminatedcomputing.com> User-Agent: Loopia Webmail/1.6.9 Message-ID: X-Sender: sam@movsom.se Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Foreign key violation errors are incorrectly raised in a few cases for a temporal foreign key with default ON UPDATE NO ACTION. Test is based on the commited v39 patches (used a snapshot version of PG18 devel available from PGDG). If there exists a single referencing row for a foreign key (with default ON UPDATE NO ACTION) with a range such as: c d |----------| and a single row in the referenced table, and the referenced row's range is updated as in one of the following cases: a b c d e f X>>>>>>>>>>>|==============================| ERROR 1: [a,f) updated to [b,f) or |==============================|<<<<<<<<<<>>>>>>>>>>|================================== ERROR 3: [a,) updated to [b,) then an error is incorrectly raised (also, if the referencing range is [c,) instead of [c,d), then the last case also fails). See SQL-code below for how to reproduce the errors. --- CREATE TABLE temporal_rng ( id int4range, valid_at daterange, CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) ); CREATE TABLE temporal_fk_rng2rng ( id int4range, valid_at daterange, parent_id int4range, CONSTRAINT temporal_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng ); -- ERROR 1 INSERT INTO temporal_rng (id, valid_at) VALUES ('[1,2)', daterange('2018-01-01', '2018-03-01')); INSERT INTO temporal_fk_rng2rng (id, valid_at, parent_id) VALUES ('[2,3)', daterange('2018-01-15', '2018-02-01'), '[1,2)'); -- ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng" -- DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_rng2rng". UPDATE temporal_rng SET valid_at = daterange('2018-01-05', '2018-03-01') WHERE id = '[1,2)' AND valid_at = daterange('2018-01-01', '2018-03-01'); -- ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng" -- DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_rng2rng". UPDATE temporal_rng SET valid_at = daterange('2018-01-01', '2018-02-15') WHERE id = '[1,2)' AND valid_at = daterange('2018-01-01', '2018-03-01'); -- ERROR 2 TRUNCATE temporal_rng, temporal_fk_rng2rng; INSERT INTO temporal_rng (id, valid_at) VALUES ('[1,2)', daterange('2018-01-05', NULL)); INSERT INTO temporal_fk_rng2rng (id, valid_at, parent_id) VALUES ('[2,3)', daterange('2018-01-15', '2018-02-01'), '[1,2)'); -- ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng" -- DETAIL: Key (id, valid_at)=([1,2), [2018-01-05,)) is still referenced from table "temporal_fk_rng2rng". UPDATE temporal_rng SET valid_at = daterange('2018-01-05', '2018-02-15') WHERE id = '[1,2)' AND valid_at = daterange('2018-01-05', NULL); -- ERROR 3 TRUNCATE temporal_rng, temporal_fk_rng2rng; INSERT INTO temporal_rng (id, valid_at) VALUES ('[1,2)', daterange('2018-01-01', NULL)); INSERT INTO temporal_fk_rng2rng (id, valid_at, parent_id) VALUES ('[2,3)', daterange('2018-01-15', '2018-02-01'), '[1,2)'); -- ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_rng2rng_fk" on table "temporal_fk_rng2rng" -- DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,)) is still referenced from table "temporal_fk_rng2rng". UPDATE temporal_rng SET valid_at = daterange('2018-01-05', NULL) WHERE id = '[1,2)' AND valid_at = daterange('2018-01-01', NULL); --- I think the problem is the check in ri_restrict: SELECT 1 FROM [ONLY] x WHERE $1 = fkatt1 [AND ...] FOR KEY SHARE OF x it will be performed in the NO ACTION case when ri_Check_Pk_Match returns false, and it'll then incorrectly assume that the presence of a referencing row in the is an error. However, ri_Check_Pk_Match only tests wheter a temporal primary key's old range is contained by the multirange that includes its new updated range. If that's true, then all references are necessarily still valid. However, even if it is not contained, all references can still be valid. So, only testing for the presence of a referencing row is not enough. For example, for ERROR1, the range [a,f) is updated to [b,f): a b c d f X>>>>>>>>>>>|==============================| Clearly the old range: a c d f |==========================================| is no longer contained by (the multirange returned by range_agg of) the new range: b c d f |==============================| So ri_Check_Pk_Match returns false. Though the row in the referencing table: c d |----------| only specifies the range [c,d), so the temporal referential integrity still holds. However, the ri_restrict test will find a row in the referencing table and because of that raise an error. In the temporal NO ACTION case something similar to this (though with appropriate locks) could perhaps be tested in ri_restrict (when ri_Check_Pk_Match returns false): SELECT 1 FROM (SELECT range_agg(pkperiodatt) AS r FROM WHERE pkatt1 = $1 [AND ...] AND pkperiodatt && $n) AS pktable, (SELECT fkperiodatt AS r FROM WHERE fkatt1 = $1 [AND ...] AND fkperiodatt && $n) AS fktable WHERE NOT fktable.r <@ pktable.r /Sam