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 1rNamL-00DeOY-OX for pgsql-general@arkaria.postgresql.org; Wed, 10 Jan 2024 15:46:06 +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 1rNamJ-0059vM-LY for pgsql-general@arkaria.postgresql.org; Wed, 10 Jan 2024 15:46:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rNamJ-0059vE-5K for pgsql-general@lists.postgresql.org; Wed, 10 Jan 2024 15:46:03 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rNamG-000rWd-Gy for pgsql-general@postgresql.org; Wed, 10 Jan 2024 15:46:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2024010820; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-ID:Content-Description; bh=GZQLkboWrlKT5H7nbetadRvRJNtC/kBk0idZ+cUlydM=; b=U/OIC8JgRjUZBPxb3zSzo/Fnio DRROsLGiG2iyczxZ48fmXjAuWzgnPMYwSz25oEuG3cbuLKtOfCslB8XZigKbxaWjougAG/eo+pGjg 8ZPtWBJn5OkkIXySXxAMl2SYBJW7Ge8qgEAyGBRFD59E00IRFHiVDIHPNf86o+yboOQ7TZRsJj14N PVaL1Hf/DVXTyeLROEDTdnKSg1TDgYMoWzmuF4i175jMJz31r9zjupfUrGPh2pfjz7T0jXFOd51ce FQK5WHmevS7apPVa3C3dYCiEHkDlpZynswVxtbSjkgOw5eG21KVB3teSFrlRZM2V3gq6T3YCjDzd9 gAbk93PA==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1rNamF-002LiG-0T; Wed, 10 Jan 2024 10:45:59 -0500 Date: Wed, 10 Jan 2024 10:45:59 -0500 From: Bruce Momjian To: Torsten =?utf-8?Q?F=C3=B6rtsch?= Cc: PostgreSQL mailing lists Subject: Re: slightly unexpected result Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jan 10, 2024 at 12:29:54PM +0100, Torsten Förtsch wrote: > Hi, > > imagine a simple table with 1 row > > =# table tf; >  i | x   > ---+---- >  1 | xx > (1 row) > > And this query: > > with x as (update tf set i=i+1 returning *) > , y as (update tf set x=x||'yy' returning *) > select * from x,y; > > My PG14 gives this result > >  i | x | i | x > ---+---+---+--- > (0 rows) > > To me that was a bit surprising. I would have expected it to fail with > something like "can't update the same row twice in the same command". > > If I check the table content after the query I see the i=i+1 part was executed. > > Is this expected behavior? Yes, this surprised me too. Here is a reproducible case: CREATE TABLE tf (i INT, x TEXT); INSERT INTO tf VALUES (1, 'x'); WITH x AS (UPDATE tf SET i=i+1 RETURNING *), y AS (UPDATE tf SET x=x||'yy' RETURNING *) SELECT * FROM x,y; i | x | i | x ---+---+---+--- TABLE tf; i | x ---+--- 2 | x I know you can cascade the returning of one table into the update of another table, but maybe it doesn't work into the same table. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Only you can decide what is important to you.