Re: Multitable insert syntax support on Postgres?

From: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: Alexandre hadjinlian guerra <alexhguerra(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Multitable insert syntax support on Postgres?
Date: 2022-11-21 05:09:08
Message-ID: CADkLM=fRPe2ftSM_iUYDPfih1qQRRMTzxOw7=C3gWDhXSpOe0g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
> WITH data_src AS (SELECT * FROM source_tbl),
> insert_a AS (INSERT INTO a SELECT * FROM data_src WHERE d < 5),
> insert_b AS (INSERT INTO b SELECT * FROM data_src WHERE d >= 5)
> INSERT INTO c SELECT * FROM data_src WHERE d < 5
>

I suppose you could just do a dummy SELECT at the bottom to make it look
more symmetrical

WITH data_src AS (SELECT * FROM source_tbl),
insert_a AS (INSERT INTO a SELECT * FROM data_src WHERE d < 5),
insert_b AS (INSERT INTO b SELECT * FROM data_src WHERE d >= 5)
insert_c AS (INSERT INTO c SELECT * FROM data_src WHERE d < 5)
SELECT true AS inserts_complete;

Or maybe get some diagnostics out of it:

WITH data_src AS (SELECT * FROM source_tbl),
insert_a AS (INSERT INTO a SELECT * FROM data_src WHERE d < 5 RETURNING
NULL),
insert_b AS (INSERT INTO b SELECT * FROM data_src WHERE d >= 5 RETURNING
NULL),
insert_c AS (INSERT INTO c SELECT * FROM data_src WHERE d < 5 RETURNING
NULL)
SELECT
(SELECT COUNT(*) FROM insert_a) AS new_a_rows,
(SELECT COUNT(*) FROM insert_b) AS new_b_rows,
(SELECT COUNT(*) FROM insert_c) AS new_c_rows;

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Corey Huinker 2022-11-21 05:17:12 Re: Error-safe user functions
Previous Message Laurenz Albe 2022-11-21 05:07:04 Re: Reducing power consumption on idle servers