From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tom Kazimiers <tom@voodoo-arts.net>
Cc: pgsql-general@postgresql.org
Subject: Re: Updating PK and all FKs to it in CTE
Date: Thu, 29 Jul 2021 10:51:09 -0400
Message-ID: <713691.1627570269@sss.pgh.pa.us> (raw)
In-Reply-To: <YQKef3h1sWgnvnx+@dewberry.localdomain>
References: <YQKef3h1sWgnvnx+@dewberry.localdomain>
Tom Kazimiers <tom@voodoo-arts.net> writes:
> I am on Postgres 13 and have a problem with updates in a CTE. While certainly
> not generally recommended, I need to update the primary key in a table that is
> referenced by a few other tables. The table definition is attached to the end
> of this email [2]. I'd like to avoid dropping and recreating the constraints or
> even columns, because these tables can become quite large. While I could define
> the FK constraints as ON UPDATE CASCADE, I wonder why an alternative solution
> using a CTE doesn't work:
I tried to reproduce your problem and failed; the attached script
acts as I'd expect. So there must be some moving part you've not
mentioned. Can yo create a similar self-contained example that
fails?
regards, tom lane
Attachments:fktest.sql (text/plain, 645B) download
drop table if exists pt, c1, c2;
create table pt (id int primary key);
create table c1 (id int primary key, ref int references pt
deferrable initially deferred);
create table c2 (id int primary key, ref int references pt
deferrable initially deferred);
insert into pt values(11);
insert into c1 values(1, 11);
insert into c2 values(2, 11);
update pt set id = 12 where id = 11; -- fails
with u1 as (
update c1 set ref = 12 where ref = 11
)
update pt set id = 12 where id = 11; -- fails
with u1 as (
update c1 set ref = 12 where ref = 11
), u2 as (
update c2 set ref = 12 where ref = 11
)
update pt set id = 12 where id = 11; -- ok
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, tom@voodoo-arts.net
Subject: Re: Updating PK and all FKs to it in CTE
In-Reply-To: <713691.1627570269@sss.pgh.pa.us>
References: <YQKef3h1sWgnvnx+@dewberry.localdomain> <713691.1627570269@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