From: Louis Tian <louis.tian@aquamonix.com.au>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: PostgreSQL Rule does not work with deferred constraint.
Date: Wed, 21 Sep 2022 01:19:15 +0000
Message-ID: <SY4P282MB1052134AB3D38592D4BB7A0BA64F9@SY4P282MB1052.AUSP282.PROD.OUTLOOK.COM> (raw)
Hi all,
Encountered a possible bug today. In short, it appears that the rule system is not deferring the constraint checking as specified. Here is an example.
create table "parent" (
"id" uuid primary key
);
create table "children" (
"id" uuid primary key
default gen_random_uuid()
references parent("id")
deferrable
initially deferred
);
-- this works as expected.
begin;
insert into "children" values ('74c247be-2fc3-43e5-94f8-d5e1af147a9b');
insert into "parent" values ('74c247be-2fc3-43e5-94f8-d5e1af147a9b');
commit;
-- doing the same with a rule
create rule on_insert as on insert to "children" do also (
insert into "parent" (id) values (new."id");
);
-- this fails with:
-- ERROR: insert or update on table "children" violates foreign key constraint "children_id_fkey"
-- DETAIL: Key (id)=(40513132-f9fa-4e5a-aa46-20c8185b3e58) is not present in table "parent".
-- SQL state: 23503
insert into "children" values (default);
The rules system supposedly transforms the insert statement and executed the rule action in the same transaction. So I think it should work.
But I got the same error on both pg13 and pg14.
Is there something I missed here? or is my understanding of the rule system just simply wrong?
Regards,
Louis Tian
louis.tian@aquamonix.com.au
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: louis.tian@aquamonix.com.au
Subject: Re: PostgreSQL Rule does not work with deferred constraint.
In-Reply-To: <SY4P282MB1052134AB3D38592D4BB7A0BA64F9@SY4P282MB1052.AUSP282.PROD.OUTLOOK.COM>
References: <SY4P282MB1052134AB3D38592D4BB7A0BA64F9@SY4P282MB1052.AUSP282.PROD.OUTLOOK.COM>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox