From: Tom Lane <tgl@sss.pgh.pa.us>
To: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Louis Tian <louis.tian@aquamonix.com.au>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: PostgreSQL Rule does not work with deferred constraint.
Date: Wed, 21 Sep 2022 11:12:48 -0400
Message-ID: <1078305.1663773168@sss.pgh.pa.us> (raw)
In-Reply-To: <0d52121503f0babc9ce3eeb0753067edb56d6e8e.camel@cybertec.at>
References: <SY4P282MB1052134AB3D38592D4BB7A0BA64F9@SY4P282MB1052.AUSP282.PROD.OUTLOOK.COM>
<0d52121503f0babc9ce3eeb0753067edb56d6e8e.camel@cybertec.at>
Laurenz Albe <laurenz.albe@cybertec.at> writes:
> I just tried your commands, and it works as you expect on my PostgreSQL v15 database.
It does fail for me, but I think it's a well-known trap rather than
a bug (or at least, it's not something that anyone wishes to redesign
the rule system to change). The problem is that *a rule is a macro*
and therefore it's subject to multiple-evaluation hazards. Your
volatile default expression does not play nice with that.
Initially you have:
insert into "children" values (default);
Replacement of the "default" produces:
insert into "children" values (gen_random_uuid());
Now the DO ALSO rule produces:
insert into "parent" (id) values (gen_random_uuid());
The two insertions will compute different random UUIDs,
and kaboom.
We tend to recommend using triggers not rules to implement this
sort of behavior; they are less prone to surprises.
regards, tom lane
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: tgl@sss.pgh.pa.us, laurenz.albe@cybertec.at, louis.tian@aquamonix.com.au
Subject: Re: PostgreSQL Rule does not work with deferred constraint.
In-Reply-To: <1078305.1663773168@sss.pgh.pa.us>
References: <SY4P282MB1052134AB3D38592D4BB7A0BA64F9@SY4P282MB1052.AUSP282.PROD.OUTLOOK.COM> <0d52121503f0babc9ce3eeb0753067edb56d6e8e.camel@cybertec.at> <1078305.1663773168@sss.pgh.pa.us>
* 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