Re: Inserting many rows using "with"

From: Brian Dunavant <dunavant(at)gmail(dot)com>
To: Harmen <harmen(at)lijzij(dot)de>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Inserting many rows using "with"
Date: 2020-09-11 17:43:01
Message-ID: CAJ2+uGU5WvJ2eME+Pppe=wam5Bp-x4=gW_O_WyPL4Lsz3UjWiw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Sep 11, 2020 at 1:31 PM Harmen <harmen(at)lijzij(dot)de> wrote:

> Hi all,
>
> Are there any known problems with this strategy? Are they any other
> methods of
> inserting lots of records in a nicer way?
>

I do this all the time with insert and it's wonderful. It can get tricky
if you need to do UPDATEs.

You can avoid the unnest and just use value rows directly if you want
(which can be easier to read)l

with ids(id) as
( values (1),
(2),
(3)
) select id from ids;

Further, you can use this technique with multiple CTEs to insert into
completely different tables all in the same query saving lots of round
trips.

> Alternatives I know of, and the only options I could find documented:
> - create a huge custom insert statement
> - use COPY, but that doesn't work with 'ON CONFLICT' as far as I can see
>

COPY is great for large amounts of data. If it's a huge amount of data,
you can load it into a temp table with COPY, and then use your ON CONFLICT
insert to implement your logic.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Farber 2020-09-12 20:54:40 How to call JSONB_INSERT with integer as the new to-be-inserted value?
Previous Message Adrian Klaver 2020-09-11 17:20:10 Re: Feature Request pgadmin4 support more storage backends for servers/user data