pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Torsten Förtsch <tfoertsch123@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-general@postgresql.org>
Subject: Re: slightly unexpected result
Date: Wed, 10 Jan 2024 10:45:59 -0500
Message-ID: <ZZ67t031h30F84C4@momjian.us> (raw)
In-Reply-To: <CAKkG4_kM1yuqw1oimYM_fAzsSJqQkejWW5yHqw_WfOng+9Y4TQ@mail.gmail.com>
References: <CAKkG4_kM1yuqw1oimYM_fAzsSJqQkejWW5yHqw_WfOng+9Y4TQ@mail.gmail.com>

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  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Only you can decide what is important to you.





view thread (4+ messages)  latest in thread

Message-ID: <ZZ67t031h30F84C4@momjian.us>
Permalink:  ../ZZ67t031h30F84C4@momjian.us/
Also on:    postgresql.org/message-id/ZZ67t031h30F84C4@momjian.us

 · 

reply

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: bruce@momjian.us, tfoertsch123@gmail.com
  Subject: Re: slightly unexpected result
  In-Reply-To: <ZZ67t031h30F84C4@momjian.us>
  References: <CAKkG4_kM1yuqw1oimYM_fAzsSJqQkejWW5yHqw_WfOng+9Y4TQ@mail.gmail.com> <ZZ67t031h30F84C4@momjian.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