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 1m97NS-0000Eq-P8 for pgsql-general@arkaria.postgresql.org; Thu, 29 Jul 2021 14:51:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m97NR-00066w-Fg for pgsql-general@arkaria.postgresql.org; Thu, 29 Jul 2021 14:51:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m97NR-00066o-6a for pgsql-general@lists.postgresql.org; Thu, 29 Jul 2021 14:51:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m97NP-0004O4-2O for pgsql-general@postgresql.org; Thu, 29 Jul 2021 14:51:12 +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 16TEp9Bf713692; Thu, 29 Jul 2021 10:51:09 -0400 From: Tom Lane To: Tom Kazimiers cc: pgsql-general@postgresql.org Subject: Re: Updating PK and all FKs to it in CTE In-reply-to: References: Comments: In-reply-to Tom Kazimiers message dated "Thu, 29 Jul 2021 14:26:39 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <713657.1627570230.0@sss.pgh.pa.us> Date: Thu, 29 Jul 2021 10:51:09 -0400 Message-ID: <713691.1627570269@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <713657.1627570230.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Tom Kazimiers writes: > I am on Postgres 13 and have a problem with updates in a CTE. While cert= ainly > not generally recommended, I need to update the primary key in a table t= hat is > referenced by a few other tables. The table definition is attached to th= e end > of this email [2]. I'd like to avoid dropping and recreating the constra= ints 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 sol= ution > 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 ------- =_aaaaaaaaaa0 Content-Type: text/plain; name="fktest.sql"; charset="us-ascii" Content-ID: <713657.1627570230.2@sss.pgh.pa.us> Content-Description: fktest.sql 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 ------- =_aaaaaaaaaa0--