distinguish update from insert (on conflict)

Started by Justin Pryzbyalmost 7 years ago6 messagesgeneral
Jump to latest
#1Justin Pryzby
pryzby@telsasoft.com

Is it still impossible to distinguish whether a row was inserted vs updated ?

The latest I can see is here:
https://wiki.postgresql.org/wiki/UPSERT#RETURNING_behavior

..but I'm hopeful that the 4 year old wiki page is out of date.

Justin

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Justin Pryzby (#1)
Re: distinguish update from insert (on conflict)

On 5/21/19 6:34 PM, Justin Pryzby wrote:

Is it still impossible to distinguish whether a row was inserted vs updated ?

You will need to be more specific.

On a hunch, see transition relation info here:
https://www.postgresql.org/docs/10/sql-createtrigger.html

The latest I can see is here:
https://wiki.postgresql.org/wiki/UPSERT#RETURNING_behavior

..but I'm hopeful that the 4 year old wiki page is out of date.

Justin

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Justin Pryzby
pryzby@telsasoft.com
In reply to: Adrian Klaver (#2)
Re: distinguish update from insert (on conflict)

On Tue, May 21, 2019 at 06:57:36PM -0700, Adrian Klaver wrote:

On 5/21/19 6:34 PM, Justin Pryzby wrote:

Is it still impossible to distinguish whether a row was inserted vs updated ?

You will need to be more specific.

Sorry, I mean with UPSERT / "INSERT .. ON CONFLICT DO UPDATE", is it possible
to tell whether a row was inserted vs. updated ?

Thanks,
Justin

#4Fabio Ugo Venchiarutti
f.venchiarutti@ocado.com
In reply to: Justin Pryzby (#3)
Re: distinguish update from insert (on conflict)

On 22/05/2019 03:37, Justin Pryzby wrote:

On Tue, May 21, 2019 at 06:57:36PM -0700, Adrian Klaver wrote:

On 5/21/19 6:34 PM, Justin Pryzby wrote:

Is it still impossible to distinguish whether a row was inserted vs updated ?

You will need to be more specific.

Sorry, I mean with UPSERT / "INSERT .. ON CONFLICT DO UPDATE", is it possible
to tell whether a row was inserted vs. updated ?

Thanks,
Justin

Here's my recipe for that:

RETURNING
/* whatever, */
(xmax = 0) AS is_new_record
;

I don't know if any of the hackers thought of a sleeker technique

--
Regards

Fabio Ugo Venchiarutti
OSPCFC Network Engineering Dpt.
Ocado Technology

--

Notice:  This email is confidential and may contain copyright material of
members of the Ocado Group. Opinions and views expressed in this message
may not necessarily reflect the opinions and views of the members of the
Ocado Group. 

 

If you are not the intended recipient, please notify us
immediately and delete all copies of this message. Please note that it is
your responsibility to scan this message for viruses. 

 

Fetch and Sizzle
are trading names of Speciality Stores Limited and Fabled is a trading name
of Marie Claire Beauty Limited, both members of the Ocado Group.

 

References to the “Ocado Group” are to Ocado Group plc (registered in
England and Wales with number 7098618) and its subsidiary undertakings (as
that expression is defined in the Companies Act 2006) from time to time.  
The registered office of Ocado Group plc is Buildings One & Two, Trident
Place, Mosquito Way, Hatfield, Hertfordshire, AL10 9UL.

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Justin Pryzby (#3)
Re: distinguish update from insert (on conflict)

On 5/21/19 7:37 PM, Justin Pryzby wrote:

On Tue, May 21, 2019 at 06:57:36PM -0700, Adrian Klaver wrote:

On 5/21/19 6:34 PM, Justin Pryzby wrote:

Is it still impossible to distinguish whether a row was inserted vs updated ?

You will need to be more specific.

Sorry, I mean with UPSERT / "INSERT .. ON CONFLICT DO UPDATE", is it possible
to tell whether a row was inserted vs. updated ?

In addition to Fabio's suggestion, from my previous post:

https://www.postgresql.org/docs/10/sql-createtrigger.html

"n some cases it is possible for a single SQL command to fire more than
one kind of trigger. For instance an INSERT with an ON CONFLICT DO
UPDATE clause may cause both insert and update operations, so it will
fire both kinds of triggers as needed. The transition relations supplied
to triggers are specific to their event type; thus an INSERT trigger
will see only the inserted rows, while an UPDATE trigger will see only
the updated rows."

So you might want to check out triggers using transition tables.

Thanks,
Justin

--
Adrian Klaver
adrian.klaver@aklaver.com

#6Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Justin Pryzby (#1)
Re: distinguish update from insert (on conflict)

Justin Pryzby wrote:

Is it still impossible to distinguish whether a row was inserted vs updated ?

The latest I can see is here:
https://wiki.postgresql.org/wiki/UPSERT#RETURNING_behavior

..but I'm hopeful that the 4 year old wiki page is out of date.

Maybe this answer can help you:

https://stackoverflow.com/a/39204667/6464308

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com