From: Peter Eisentraut <peter_e@gmx.net>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Allow multi-inserts during COPY into a partitioned table
Date: Wed, 01 Aug 2018 08:23:20 +0000
Message-ID: <E1fkmPg-0003xp-Fg@gemulon.postgresql.org> (raw)
Allow multi-inserts during COPY into a partitioned table
CopyFrom allows multi-inserts to be used for non-partitioned tables, but
this was disabled for partitioned tables. The reason for this appeared
to be that the tuple may not belong to the same partition as the
previous tuple did. Not allowing multi-inserts here greatly slowed down
imports into partitioned tables. These could take twice as long as a
copy to an equivalent non-partitioned table. It seems wise to do
something about this, so this change allows the multi-inserts by
flushing the so-far inserted tuples to the partition when the next tuple
does not belong to the same partition, or when the buffer fills. This
improves performance when the next tuple in the stream commonly belongs
to the same partition as the previous tuple.
In cases where the target partition changes on every tuple, using
multi-inserts slightly slows the performance. To get around this we
track the average size of the batches that have been inserted and
adaptively enable or disable multi-inserts based on the size of the
batch. Some testing was done and the regression only seems to exist
when the average size of the insert batch is close to 1, so let's just
enable multi-inserts when the average size is at least 1.3. More
performance testing might reveal a better number for, this, but since
the slowdown was only 1-2% it does not seem critical enough to spend too
much time calculating it. In any case it may depend on other factors
rather than just the size of the batch.
Allowing multi-inserts for partitions required a bit of work around the
per-tuple memory contexts as we must flush the tuples when the next
tuple does not belong the same partition. In which case there is no
good time to reset the per-tuple context, as we've already built the new
tuple by this time. In order to work around this we maintain two
per-tuple contexts and just switch between them every time the partition
changes and reset the old one. This does mean that the first of each
batch of tuples is not allocated in the same memory context as the
others, but that does not matter since we only reset the context once
the previous batch has been inserted.
Author: David Rowley <david.rowley@2ndquadrant.com>
Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/0d5f05cde011512e605bb2688d9b1fbb5b3ae152
Modified Files
--------------
src/backend/commands/copy.c | 347 +++++++++++++++++++++++++--------
src/backend/executor/execPartition.c | 5 +-
src/backend/executor/nodeModifyTable.c | 6 +-
src/include/executor/execPartition.h | 3 +-
src/test/regress/input/copy.source | 30 +++
src/test/regress/output/copy.source | 27 +++
6 files changed, 330 insertions(+), 88 deletions(-)
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-committers@postgresql.org
Cc: peter_e@gmx.net
Subject: Re: pgsql: Allow multi-inserts during COPY into a partitioned table
In-Reply-To: <E1fkmPg-0003xp-Fg@gemulon.postgresql.org>
References: <E1fkmPg-0003xp-Fg@gemulon.postgresql.org>
* 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