Re: Multitable insert syntax support on Postgres? - Mailing list pgsql-hackers

From Corey Huinker
Subject Re: Multitable insert syntax support on Postgres?
Date
Msg-id CADkLM=fRPe2ftSM_iUYDPfih1qQRRMTzxOw7=C3gWDhXSpOe0g@mail.gmail.com
Whole thread Raw
In response to Re: Multitable insert syntax support on Postgres?  (Andres Freund <andres@anarazel.de>)
List 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;

pgsql-hackers by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: Reducing power consumption on idle servers
Next
From: Corey Huinker
Date:
Subject: Re: Error-safe user functions