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 1u6Wb0-00HBVQ-47 for pgsql-general@arkaria.postgresql.org; Sun, 20 Apr 2025 15:28:38 +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 1u6Wav-00Csno-Cn for pgsql-general@arkaria.postgresql.org; Sun, 20 Apr 2025 15:28:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u6Wau-00Csng-1H for pgsql-general@lists.postgresql.org; Sun, 20 Apr 2025 15:28:33 +0000 Received: from fhigh-a4-smtp.messagingengine.com ([103.168.172.155]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u6Wan-0015HO-2p for pgsql-general@postgresql.org; Sun, 20 Apr 2025 15:28:31 +0000 Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 865051140168; Sun, 20 Apr 2025 11:28:24 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Sun, 20 Apr 2025 11:28:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1745162904; x=1745249304; bh=7rpCYsrSm1qrSivG9tq1qbpX6ueU4uKhj7bjI0LtaWY=; b= eqH6dMr/iSwlBkWzreNXe8hT+xMbXXdb7LMAWDI5lU1GtA3hPvbla8qMNTr5YY9P a4iw2QeqzoFXoK5t8JryoIytjvddPpxUwc6+0XR9b7wa0vsE1ctqaVQbymAqPmwX 8cBQhd6Vkne7x8F9hBfveBILaCKBpaEAfGAlWzcPAyS2w3WhNP8Lz1TaHzEaapag V8brEq7vQ0ZhhjoBdLikCzZs++6/S+qEApeMsjm1T8WUl3rNcOehzRaN/jEEyomE a9aX46Hpa0aLinkwyZr1+atmom6SufDnntX9kTTt6ngX7/4/NzE6HV2Mcz1PBS+M JX0l+V73yCKIPh1ElKVwNw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1745162904; x=1745249304; bh=7 rpCYsrSm1qrSivG9tq1qbpX6ueU4uKhj7bjI0LtaWY=; b=Qbcdc8I3UPnX/XX3d jNZ3qd9+gfEsc2tI87NCH83LjHV6Ck6ojisEK4W9o86SRnDfXBN4H9DI0ZdPRp8k gjBIVeTjn0O4WAhPaOo6DcaLbRbkmMIru8a1S/tSUzfMcJQtuGYdKx+I4/i03DI9 5mzhjZUj9ryMh16Ff7u3JOQ5PfbYokXy/iJn3JdqYZijzgoAzPPCdoGACAq96bLN iBhC1ZKCIjQoCFvjk/FOeuvDaHoGEf3rTBrhydrNvmj6D1jziv3rpC+ADHP1Ynn4 2HXCj9xqhMmbJwm5rx1j9YAiGNG4+B2aMp8FkrlgYCD94xY419rt9kjLLRIOpFX1 zSWgQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvfeekvdekucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkff ggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghv vghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrf grthhtvghrnhepgefggeevhfetfeekhfeuvddufeekgeelffeghedthfeghedujefgkeej vddtjeffnecuffhomhgrihhnpegthigsvghrthgvtgdqphhoshhtghhrvghsqhhlrdgtoh hmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggu rhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpthhtohepvd dpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohephhhjphdqphhgshhqlheshhhjphdr rghtpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlhesphhoshhtghhrvghsqhhlrd horhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 20 Apr 2025 11:28:23 -0400 (EDT) Message-ID: Date: Sun, 20 Apr 2025 08:28:22 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Order of update To: "Peter J. Holzer" , pgsql-general@postgresql.org References: <20250420091033.n437fdrkihtjrncd@hjp.at> Content-Language: en-US From: Adrian Klaver In-Reply-To: <20250420091033.n437fdrkihtjrncd@hjp.at> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/20/25 02:10, Peter J. Holzer wrote: > I've just read Laurenz' blog post about the differences between Oracle > and PostgreSQL[1]. > > One of the differences is that something like > > UPDATE tab SET id = id + 1; > > tends to fail on PostgreSQL because the the primary key constraint is > checked for every row, so it will stumble over the temporary conflicts. > > The solution is to define the constraint as deferrable. > > But that got me to thinking about different ways ... > > There won't be a conflict if the ids are updated in descending order. > Is there a way to force PostgreSQL to update the rows in a specific > order? > > I came up with > > with a as (select id from t where id > 50 order by id desc) > update t set id = a.id+1 from a where t.id = a.id; > > which works in my simple test case, but it doesn't look like it's > guaranteed to work. The implicit join in «update t ... from a» could > produce rows in any order, especially for large tables. My read of this is that for the duration of the query a temporary table a is create that is ordered on `id desc` and that '... from a where t.id = a.id' will apply that order to the selection of t.id. As example: create table id_update(id integer primary key); insert into id_update select a from generate_series(1, 100000) as t(a); INSERT 0 100000 -- id(s) are temporarily in order. update id_update set id = id where id between 50000 and 60000; UPDATE 10001 -- The above move the 10001 values to 'end' of id_update with a as (select id from id_update where id > 100 order by id desc) update id_update as t set id = a.id + 1 from a where t.id = a.id; UPDATE 99900 -- The UPDATE works even though the t.id(s) in id_update are not ordered -- by id > > So, is there a better way? > > hjp > > > [1] https://www.cybertec-postgresql.com/en/comparison-of-the-transaction-systems-of-oracle-and-postgresql/ > -- Adrian Klaver adrian.klaver@aklaver.com