Re: slightly unexpected result

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Torsten Förtsch <tfoertsch123(at)gmail(dot)com>
Cc: PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org>
Subject: Re: slightly unexpected result
Date: 2024-01-10 15:45:59
Message-ID: ZZ67t031h30F84C4@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(at)momjian(dot)us> https://momjian.us
EDB https://enterprisedb.com

Only you can decide what is important to you.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message kaido vaikla 2024-01-10 15:57:15 Re: failed to setup barman backup when Posgres is running in Podman Container
Previous Message duc hiep ha 2024-01-10 13:58:24 failed to setup barman backup when Posgres is running in Podman Container