Re: a row is not inserted in nested INSERT ON CONFLICT

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Jason Kim <git(at)jasonk(dot)me>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: a row is not inserted in nested INSERT ON CONFLICT
Date: 2024-08-13 19:23:24
Message-ID: CAKFQuwYd5mHWTv373bLLpB2jGDkDq+OTV8sVPMD07B9wog-GCg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Aug 13, 2024 at 10:37 AM Jason Kim <git(at)jasonk(dot)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:

https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-MODIFYING

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

David J.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Cameron Vogt 2024-08-13 20:59:07 Re: TLS session tickets disabled?
Previous Message Alvaro Herrera 2024-08-13 18:32:01 Re: BUG #18582: fixed range of search for empty slot in SLRU