Re: Multitable insert syntax support on Postgres?

From: Andres Freund <andres(at)anarazel(dot)de>
To: Alexandre hadjinlian guerra <alexhguerra(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Multitable insert syntax support on Postgres?
Date: 2022-11-15 00:34:10
Message-ID: 20221115003410.zerod7hfttplivpa@awork3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2022-11-14 21:06:09 -0300, Alexandre hadjinlian guerra wrote:
> Hello
> Are there any plans to incorporate a formal syntax multitable/conditional
> insert , similar to the syntax below? snowflake does have the same feature
>
> https://oracle-base.com/articles/9i/multitable-inserts
>
> Today, im resorting to a function that receives the necessary parameters
> from the attributes definition/selection area in a sql select query, called
> by each tuple retrieved. A proper syntax show be real cool

I only skimmed that link, but afaict most of this can be done today in
postgres, with a bit different syntax, using CTEs. Postgres implements this as
an extension to the standard CTE syntax.

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

It's a bit annoying that the last "arm" of the insert looks a bit
difference. OTOH, it's a much more general syntax.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2022-11-15 00:49:26 Re: Meson doesn't define HAVE_LOCALE_T for mscv
Previous Message Andres Freund 2022-11-15 00:25:04 Re: Error on missing Python module in Meson setup