Re: Per row status during INSERT .. ON CONFLICT UPDATE?

From: Marko Tiikkaja <marko(at)joh(dot)to>
To: Thom Brown <thom(at)linux(dot)com>, Robins Tharakan <tharakan(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Per row status during INSERT .. ON CONFLICT UPDATE?
Date: 2015-05-19 13:14:18
Message-ID: 555B372A.1040409@joh.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 5/19/15 3:04 PM, Thom Brown wrote:
> If you want the delta, you'll have to resort to a CTE:
>
> e.g.
>
> # WITH newvals AS (
> INSERT INTO test (name, age) VALUES ('James', 45)
> ON CONFLICT (name)
> DO UPDATE SET age = EXCLUDED.age
> RETURNING *)
> SELECT n.name, o.age as "old.age", n.age as "new.age"
> FROM test o RIGHT JOIN newvals n on o.name = n.name;
>
> name | old.age | new.age
> -------+---------+---------
> James | 44 | 45
> (1 row)

Also note that the old value is not the actual value right before the
update, but one according to a snapshot taken at the beginning of the
query. So if you instead did SET age = age + 1, you could see an old
value of 44 and a new value of 46 (or any similarly weird combination of
values).

.m

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2015-05-19 13:52:41 Re: Run pgindent now?
Previous Message Andrew Dunstan 2015-05-19 13:08:45 Re: Run pgindent now?