From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Mats Julian Olsen <mats@duneanalytics.com>
Cc: pgsql-general@lists.postgresql.org
Cc: wilhelm@duneanalytics.com
Subject: Re: postgres_fdw insert extremely slow
Date: Thu, 26 Nov 2020 09:07:41 -0800
Message-ID: <c44acaac-ed91-2b1d-043e-a6570d392caa@aklaver.com> (raw)
In-Reply-To: <500774.1606331937@sss.pgh.pa.us>
References: <CAARtqpE-7mZY8WKKMq2fWeHBcrAJu41_GnKgKqdvkidk7g+M3A@mail.gmail.com>
<3862aa4d-01c7-c76b-4497-ebce1af702cc@aklaver.com>
<10df210c-a1bb-f909-9a48-61ac54880df0@duneanalytics.com>
<493978.1606323509@sss.pgh.pa.us>
<a5d9aeaa-83bf-457a-e04c-b6a66bee306f@duneanalytics.com>
<494596.1606324345@sss.pgh.pa.us>
<56a2c3cf-374d-893b-2feb-964805ff0884@duneanalytics.com>
<500774.1606331937@sss.pgh.pa.us>
On 11/25/20 11:18 AM, Tom Lane wrote:
> Mats Julian Olsen <mats@duneanalytics.com> writes:
>> I've got some more numbers here:
>> ...
>> To me this does indicate some sort of networking issue, but I'm
>> wondering if INSERTs are treated differently than SELECTs in
>> postgres_fdw? The only feasibly explanation I have is that postgres_fdw
>> does many more network calls for INSERT than for SELECT, e.g. something
>> like 1 for SELECT and `n` for INSERT?
>
> I don't have the code in front of me, but from memory, postgres_fdw
> will issue an INSERT statement to the remote for each row it has to
> insert. Maybe you are indeed just dealing with spectacularly bad
> network round trip times.
>
> You could try turning on log_statement and/or log_duration on the
> remote to see if that sheds any more light about how much time is
> spent executing each insertion vs. the network delay.
>
> There's been some recent discussion about teaching postgres_fdw to
> batch insertions, which would likely be helpful in your situation.
> I don't know how close that is to committable, but in any case
> it couldn't see the light of day earlier than v14. In the meantime,
> if you're sufficiently desperate maybe you could switch to using
> dblink with manually-issued multi-row INSERTs. (This needn't
> preclude continuing to access the table with postgres_fdw when
> that's suitable.)
So even if Mats where to break this query:
INSERT INTO foreign.labels (address, labels)
SELECT address_id, ARRAY_AGG(name) AS labels
FROM labels
GROUP BY 1
LIMIT 100;
down into something like this:
INSERT INTO foreign.labels (address, labels)
VALUES (), (), (), ();
postgres_fdw would send it as individual INSERTs?
>
> regards, tom lane
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, tgl@sss.pgh.pa.us, mats@duneanalytics.com, wilhelm@duneanalytics.com
Subject: Re: postgres_fdw insert extremely slow
In-Reply-To: <c44acaac-ed91-2b1d-043e-a6570d392caa@aklaver.com>
References: <CAARtqpE-7mZY8WKKMq2fWeHBcrAJu41_GnKgKqdvkidk7g+M3A@mail.gmail.com> <3862aa4d-01c7-c76b-4497-ebce1af702cc@aklaver.com> <10df210c-a1bb-f909-9a48-61ac54880df0@duneanalytics.com> <493978.1606323509@sss.pgh.pa.us> <a5d9aeaa-83bf-457a-e04c-b6a66bee306f@duneanalytics.com> <494596.1606324345@sss.pgh.pa.us> <56a2c3cf-374d-893b-2feb-964805ff0884@duneanalytics.com> <500774.1606331937@sss.pgh.pa.us> <c44acaac-ed91-2b1d-043e-a6570d392caa@aklaver.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