Re: MERGE SQL Statement for PG11

From: Nico Williams <nico(at)cryptonector(dot)com>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Peter Geoghegan <pg(at)bowt(dot)ie>, Robert Haas <robertmhaas(at)gmail(dot)com>, Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: MERGE SQL Statement for PG11
Date: 2017-11-02 19:39:33
Message-ID: 20171102193932.GS4496@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

If nothing else, anyone needing MERGE can port their MERGE statements to
a DML with DML-containing CTEs...

The generic mapping would be something like this, I think:

WITH
rows AS (SELECT <target> FROM <target> WHERE <condition>)
, updated AS (
UPDATE <target>
SET ...
WHERE <key> IN (SELECT <key> FROM rows) /* matched */
RETURNING <target>
)
, inserted AS (
INSERT INTO <target>
SELECT ...
WHERE <key> NOT IN (SELECT <key> FROM rows) /* not matched */
RETURNING <target>
)
DELETE FROM <target>
WHERE (...) AND
<key> NOT IN (SELECT <key> FROM updated UNION
SELECT <key> FROM inserted);

Nico
--

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2017-11-02 19:51:45 Re: MERGE SQL Statement for PG11
Previous Message Tom Lane 2017-11-02 19:19:52 Re: Re: PANIC: invalid index offnum: 186 when processing BRIN indexes in VACUUM