Re: Output clause for Upsert aka INSERT...ON CONFLICT

From: Anand Sowmithiran <anandsowmi2(at)gmail(dot)com>
To: Justin Pryzby <pryzby(at)telsasoft(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Output clause for Upsert aka INSERT...ON CONFLICT
Date: 2022-01-27 08:21:10
Message-ID: CAKbQ4s6+Zm_ueHh+rM2oDEzi_Pp0BaMY+Qa6TSAZ=ypsFTEHGg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thanks , that was helpful. I was not framing the right key words during my
searches in SO!
Using the xmax internal column, able to detect if the upsert did an Insert
or Update.
However, the MS SQL server MERGE command also does 'delete' using the 'when
not matched' clause, is there an equivalent ?

thanks,
Anand.

On Thu, Jan 27, 2022 at 11:29 AM Justin Pryzby <pryzby(at)telsasoft(dot)com> wrote:

> On Thu, Jan 27, 2022 at 10:24:14AM +0530, Anand Sowmithiran wrote:
> > The INSERT...ON CONFLICT is used for doing upserts in one of our app.
> > Our app works with both MS SQL and Postgresql, based on customer needs.
> >
> > Unlike the MS SQL MERGE command's OUTPUT clause that gives the $action
> > <
> https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15#output_clause
> >
> > [INSERT / UPDATE /DELETE] that was done during the upsert, the RETURNING
> > clause of the pgsql does not return the action done.
> > We need this so that the application can use that for auditing and UI
> > purposes.
> > Is there any workaround to get this info ?
>
> Thomas already answered about the xmax hack, but I dug these up in the
> meantime.
>
>
> https://www.postgresql.org/message-id/flat/CAA-aLv4d%3DzHnx%2BzFKqoszT8xRFpdeRNph1Z2uhEYA33bzmgtaA%40mail.gmail.com#899e15b8b357c6b29c51d94a0767a601
>
> https://www.postgresql.org/message-id/flat/1565486215.7551.0%40finefun.com.au
>
> https://www.postgresql.org/message-id/flat/20190724232439.lpxzjw2jg3ukgcqn%40alap3.anarazel.de
>
> https://www.postgresql.org/message-id/flat/DE57F14C-DB96-4F17-9254-AD0AABB3F81F%40mackerron.co.uk
>
> https://www.postgresql.org/message-id/CAM3SWZRmkVqmRCs34YtZPOCn%2BHmHqtcdEmo6%3D%3Dnqz1kNA43DVw%40mail.gmail.com
>
>
> https://stackoverflow.com/questions/39058213/postgresql-upsert-differentiate-inserted-and-updated-rows-using-system-columns-x/39204667
>
> https://stackoverflow.com/questions/40878027/detect-if-the-row-was-updated-or-inserted/40880200#40880200
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2022-01-27 08:50:25 Re: pgbench: using prepared BEGIN statement in a pipeline could cause an error
Previous Message Peter Eisentraut 2022-01-27 08:10:46 Re: ICU for global collation