In Postgresql, when inserting rows with an on conflict
and a returning
clause, what is returned? The row that was inserted? The row before insertion? What if no row was inserted?
Consider the following statement using ... on conflict do nothing returning ...
:
insert into test
values (:id, :name)
on conflict do nothing
returning id;
and the following statement ... on conflict (...) do update ... returning ...
:
insert into test
values (:id, :name)
on conflict (name) do update
set id = EXCLUDED.id + 10
returning id;
and assume both id
and name
columns to be unique. What will the above statements return?
Closely related, how could I find the ID of the existing row that causes a conflict?
PS: Yes, I could easily just try it out. In fact, that's what I did (see the answer). It's easy to make a wrong guess, hard to remember the answer and expensive to reproduce the experiment, so I thought this question would be useful to others.