Re: how to return data from insert into ... on conflict ... returning ... into

From: Gurjeet Singh <gurjeet(at)singh(dot)im>
To: Les <nagylzs(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: how to return data from insert into ... on conflict ... returning ... into
Date: 2023-07-11 07:27:49
Message-ID: CABwTF4Uimi2s7zc5VbNb_vLb_LBU=o449dct3uWOSc4r3yZGTQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jun 20, 2023 at 1:26 PM Les <nagylzs(at)gmail(dot)com> wrote:
>
> https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT
>
> and the only possible actions are "do nothing" and "do update". The "do nothing" does not update the record, that is clear. But it also does not return any data, and that was not obvious to me. Why can't it return the data from the conflicted record? After the instruction is completed, "GET DIAGNOSTICS" could still be used to check the number of updates.

Would it be fair to summarize that you'd like a feature where, upon
conflict, the command behaves as SELECT, and returns row that caused
the conflict?

The docs say: "If the INSERT command contains a RETURNING clause, the
result will be similar to that of a SELECT statement containing the
columns and values defined in the RETURNING list, computed over the
row(s) inserted or updated by the command."

So in your desired behaviour, the RETURNING list would be computed
over the rows that cause the conflict.

> I understand that changing this behaviour may break backward compatibility. Would it be possible to introduce a "DO RETURN" clause that returns the data, even when there was a conflict?

If this behaviour is introduced with a new syntax, then there won't be
any fears of breaking backwards compatibility. I think DO SELECT as
the new syntax will make the intent clear. Taking one of your queries
as an example, after the new syntax it will look as follows:

insert into tbl(d) values ('1') on conflict(h) DO SELECT returning id into aid;

Best regards,
Gurjeet
http://Gurje.et

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kyotaro Horiguchi 2023-07-11 07:30:29 Re: Problematic enforcement of "ERROR: functions in index predicate must be marked IMMUTABLE"
Previous Message Gurjeet Singh 2023-07-11 06:47:49 Re: pgbouncer best practices