Re: a row is not inserted in nested INSERT ON CONFLICT - Mailing list pgsql-bugs

From David G. Johnston
Subject Re: a row is not inserted in nested INSERT ON CONFLICT
Date
Msg-id CAKFQuwYd5mHWTv373bLLpB2jGDkDq+OTV8sVPMD07B9wog-GCg@mail.gmail.com
Whole thread Raw
In response to a row is not inserted in nested INSERT ON CONFLICT  (Jason Kim <git@jasonk.me>)
List pgsql-bugs
On Tue, Aug 13, 2024 at 10:37 AM Jason Kim <git@jasonk.me> wrote:
    create table a (i int, j int, k int, t timestamp default (clock_timestamp()), unique (i));
    insert into a values (1, 1), (2, 2), (3, 3);
    with w(i) as (
        insert into a values (1, 100), (2, 200), (3, 300) on conflict (i) do update set i = excluded.i + 10, t = clock_timestamp(), k = 0 returning i
    ) insert into a values (1, 10), (2, 20) on conflict (i) do update set i = (select (i - 10) from w order by i desc limit 1), t = clock_timestamp(), k = 1;
    table a;

I find it unusual that the (1, 10) insert seems to be ignored.


I find it unusual that at no point did that produce an error.  Something like:

ERROR:  ON CONFLICT DO UPDATE command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

(though because it is two inserts that exact error doesn't happen)

However, this non-behavior is documented:


In short, your command is broken but the system presently is incapable of telling you that and instead produces undefined behavior.

David J.

pgsql-bugs by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: BUG #18582: fixed range of search for empty slot in SLRU
Next
From: Peter Smith
Date:
Subject: Re: BUG #18558: ALTER PUBLICATION fails with unhelpful error on attempt to use system column