From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Insert into on conflict, data size upto 3 billion records
Date: Mon, 15 Feb 2021 12:05:57 -0700
Message-ID: <5ebe9fe2-3546-14f4-cd59-0b7335e26a40@gmail.com> (raw)
In-Reply-To: <CAExg72eOp0cOmaptkO9B09yErd7hEKjxe=PVUcGqX5RoJ0jWKg@mail.gmail.com>
References: <CAD-twtSfABMBH3ODxJiKdh6FHBtB0UuXn4mN-xwnC7tb=Cphjg@mail.gmail.com>
<2a876340-e227-c96a-1d79-f8a74aa45e4a@gmail.com>
<CAExg72eOp0cOmaptkO9B09yErd7hEKjxe=PVUcGqX5RoJ0jWKg@mail.gmail.com>
On 2/15/21 11:41 AM, Karthik K wrote:
> exactly, for now, what I did was, as the table is already partitioned, I
> created 50 different connections and tried updating the target table by
> directly querying from the source partition tables. Are there any other
> techniques that I can use to speed this up? also when we use on conflict
> statement for both insert and update does Postgres uses batching
> internally (committing for every 10000 records etc) or will it update
> all records at once, in that case, does it create a version for each
> record and do swap all at once? I'm wondering how atomicity is
> guaranteed, also if I have to do batching other than selecting from
> individual partitions does doing it batches of 10000 records help?
>
>
What is your ratio of inserts versus update? Can you separate the
inserts and updates? Is the target table indexed other than on primary
key? If so can they be dropped?
Assuming you use \copy to load the batch tables
I've found this strategy to be effective:
index batch on id
--update first
begin
update target t set "all fields" from batch b where t.id = b.id and b.id
between "hi" and "low"
commit
increment hi low, avoid overlap; repeat
--insert
begin;
insert into target as select b.* from from batch b where not exists
(select 1 from target v where b.id = v.id) and b.id between "hi" and "low"
commit
increment hi, low, avoid overlap; repeat
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: robjsargent@gmail.com
Subject: Re: Insert into on conflict, data size upto 3 billion records
In-Reply-To: <5ebe9fe2-3546-14f4-cd59-0b7335e26a40@gmail.com>
References: <CAD-twtSfABMBH3ODxJiKdh6FHBtB0UuXn4mN-xwnC7tb=Cphjg@mail.gmail.com> <2a876340-e227-c96a-1d79-f8a74aa45e4a@gmail.com> <CAExg72eOp0cOmaptkO9B09yErd7hEKjxe=PVUcGqX5RoJ0jWKg@mail.gmail.com> <5ebe9fe2-3546-14f4-cd59-0b7335e26a40@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox