Very cool 9.5 feature

From: Michael Moore <michaeljmoore(at)gmail(dot)com>
To: postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Very cool 9.5 feature
Date: 2016-01-11 18:47:42
Message-ID: CACpWLjN_DouuG11hL8gNtLYZu+uLNOQxPpKWEzk5w2rOrHB_qA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I was impressed by the ability of PostgresSQL to do this so I though I'd
share it with the group.

--CREATE TABLE db2 (a INT PRIMARY KEY, b TEXT,c text);

INSERT into db2 as current
SELECT * FROM json_populate_record(null::db2,
(SELECT '{"a":3,"b":"test3.2","c":"ctest3.2"}'::json))
on conflict (a) do update set b = coalesce(EXCLUDED.b,current.b),
c = coalesce(EXCLUDED.c,current.c)
;

INSERT into db2 as current
SELECT * FROM json_populate_record(null::db2,
(SELECT '{"a":3,"b":"test99"}'::json))
on conflict (a) do update set b = coalesce(EXCLUDED.b,current.b),
c = coalesce(EXCLUDED.c,current.c)
;

Note that the second insert will not UPDATE the value of column C to NULL
due to the "coalesce".
Very cool; maybe the next release will let us do:
"on conflict (a) DO UPDATE set ROW from NEW-VALUES".

Mike

Browse pgsql-sql by date

  From Date Subject
Next Message Eugene Yin 2016-01-12 01:32:46 Re: BLOBs
Previous Message Andreas Joseph Krogh 2016-01-11 17:48:17 Re: BLOBs