pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Merlin Moncure <mmoncure@gmail.com>
Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Subject: Re: data modifying WITH seems to drop rows in cascading updates -- bug?
Date: Fri, 23 Aug 2019 13:54:18 -0400
Message-ID: <10109.1566582858@sss.pgh.pa.us> (raw)
In-Reply-To: <CAHyXU0xibzCj+zOGF87-mcpTpD=tBYoHFyKZimRio3pM8YZsuw@mail.gmail.com>
References: <CAHyXU0xibzCj+zOGF87-mcpTpD=tBYoHFyKZimRio3pM8YZsuw@mail.gmail.com>

Merlin Moncure <mmoncure@gmail.com> writes:
> Trying to figure out if this is undefined behavior of a bug. It's
> confusing, and I'm aware of certain oddities in the fringes of the
> data modifying with queries where the query dependencies are not
> really clear.  Why does the query only return one row?

> postgres=# create table foo(id int);
> CREATE TABLE
> postgres=# insert into foo values(1);
> INSERT 0 1
> postgres=# with a as (update foo set id = id + 1 returning *), b
> as(update foo set id = id + 1 returning * ) select * from a union all
> select id from b;
>  id
> ────
>   2
> (1 row)

FWIW, I think it's intentional.  The two UPDATEs execute against the
same snapshot, so only one of them can update the row --- the other
one is going to see it as already-updated-by-self.  It's undefined
only to the extent that it's not completely clear which one gets
there first.  In this formulation of the outer query, I think it's
pretty safe to assume that "a" will get there first, but if you'd
joined "a" and "b" in some other fashion, conceivably "b" would.

Note that the fine manual (sec. 7.8.2) says

    Trying to update the same row twice in a single statement is not
    supported. Only one of the modifications takes place, but it is not
    easy (and sometimes not possible) to reliably predict which one. This
    also applies to deleting a row that was already updated in the same
    statement: only the update is performed. Therefore you should
    generally avoid trying to modify a single row twice in a single
    statement. In particular avoid writing WITH sub-statements that could
    affect the same rows changed by the main statement or a sibling
    sub-statement. The effects of such a statement will not be
    predictable.

			regards, tom lane





view thread (5+ messages)  latest in thread

Message-ID: <10109.1566582858@sss.pgh.pa.us>
Permalink:  ../10109.1566582858@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/10109.1566582858@sss.pgh.pa.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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, mmoncure@gmail.com
  Subject: Re: data modifying WITH seems to drop rows in cascading updates -- bug?
  In-Reply-To: <10109.1566582858@sss.pgh.pa.us>
  References: <CAHyXU0xibzCj+zOGF87-mcpTpD=tBYoHFyKZimRio3pM8YZsuw@mail.gmail.com> <10109.1566582858@sss.pgh.pa.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