From: | Nico Williams <nico(at)cryptonector(dot)com> |
---|---|
To: | Simon Riggs <simon(at)2ndquadrant(dot)com> |
Cc: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Mapping MERGE onto CTEs (Re: MERGE SQL Statement for PG11) |
Date: | 2017-11-01 16:56:20 |
Message-ID: | 20171101165618.GN4496@localhost |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Is it possible to map MERGE onto a query with CTEs that does the the
various DMLs, with all but the last RETURNING? Here's a sketch:
WITH matched_rows AS (
SELECT FROM <target> t WHERE <condition>
),
updated_rows AS (
UPDATE <target> t
SET ...
WHERE ... AND t in (SELECT j FROM matched_rows j)
RETURNING t
),
inserted_rows AS (
INSERT INTO <target> t
SELECT ...
WHERE ... AND t NOT IN (SELECT j FROM matched_rows j)
RETURNING t
),
DELETE FROM <target> t
WHERE ...;
Now, one issue is that in PG CTEs are basically like temp tables, and
also like optimizer barriers, so this construction is not online, and if
matched_rows is very large, that would be a problem.
As an aside, I'd like to be able to control which CTEs are view-like and
which are table-like. In SQLite3, for example, they are all view-like,
and the optimizer will act accordingly, whereas in PG they are all
table-like, and thus optimizer barriers.
Nico
--
From | Date | Subject | |
---|---|---|---|
Next Message | Alvaro Herrera | 2017-11-01 17:14:03 | Re: Mapping MERGE onto CTEs (Re: MERGE SQL Statement for PG11) |
Previous Message | Alvaro Herrera | 2017-11-01 16:15:47 | Re: Re: PANIC: invalid index offnum: 186 when processing BRIN indexes in VACUUM |