diff options
author | Andres Freund <andres@anarazel.de> | 2019-04-04 15:47:19 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2019-04-04 16:28:18 -0700 |
commit | 86b85044e823a304d2a265abc030254d39efe7df (patch) | |
tree | 7a4e236f6a73a38db638c561a238f1d29d0f436b /src/include | |
parent | 7bac3acab4d5c3f2c35aa3a7bea08411d83fd5bc (diff) | |
download | postgresql-86b85044e823a304d2a265abc030254d39efe7df.tar.gz postgresql-86b85044e823a304d2a265abc030254d39efe7df.zip |
tableam: Add table_multi_insert() and revamp/speed-up COPY FROM buffering.
This adds table_multi_insert(), and converts COPY FROM, the only user
of heap_multi_insert, to it.
A simple conversion of COPY FROM use slots would have yielded a
slowdown when inserting into a partitioned table for some
workloads. Different partitions might need different slots (both slot
types and their descriptors), and dropping / creating slots when
there's constant partition changes is measurable.
Thus instead revamp the COPY FROM buffering for partitioned tables to
allow to buffer inserts into multiple tables, flushing only when
limits are reached across all partition buffers. By only dropping
slots when there've been inserts into too many different partitions,
the aforementioned overhead is gone. By allowing larger batches, even
when there are frequent partition changes, we actuall speed such cases
up significantly.
By using slots COPY of very narrow rows into unlogged / temporary
might slow down very slightly (due to the indirect function calls).
Author: David Rowley, Andres Freund, Haribabu Kommi
Discussion:
https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.de
https://postgr.es/m/20190327054923.t3epfuewxfqdt22e@alap3.anarazel.de
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/access/heapam.h | 6 | ||||
-rw-r--r-- | src/include/access/tableam.h | 26 | ||||
-rw-r--r-- | src/include/nodes/execnodes.h | 4 |
3 files changed, 34 insertions, 2 deletions
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h index 4c077755d54..77e5e603b03 100644 --- a/src/include/access/heapam.h +++ b/src/include/access/heapam.h @@ -36,6 +36,7 @@ #define HEAP_INSERT_SPECULATIVE 0x0010 typedef struct BulkInsertStateData *BulkInsertState; +struct TupleTableSlot; #define MaxLockTupleMode LockTupleExclusive @@ -143,8 +144,9 @@ extern void ReleaseBulkInsertStatePin(BulkInsertState bistate); extern void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate); -extern void heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples, - CommandId cid, int options, BulkInsertState bistate); +extern void heap_multi_insert(Relation relation, struct TupleTableSlot **slots, + int ntuples, CommandId cid, int options, + BulkInsertState bistate); extern TM_Result heap_delete(Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, struct TM_FailureData *tmfd, bool changingPart); diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h index 42e2ba68bf9..90c329a88d3 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -350,6 +350,10 @@ typedef struct TableAmRoutine uint32 specToken, bool succeeded); + /* see table_multi_insert() for reference about parameters */ + void (*multi_insert) (Relation rel, TupleTableSlot **slots, int nslots, + CommandId cid, int options, struct BulkInsertStateData *bistate); + /* see table_delete() for reference about parameters */ TM_Result (*tuple_delete) (Relation rel, ItemPointer tid, @@ -1078,6 +1082,28 @@ table_complete_speculative(Relation rel, TupleTableSlot *slot, } /* + * Insert multiple tuple into a table. + * + * This is like table_insert(), but inserts multiple tuples in one + * operation. That's often faster than calling table_insert() in a loop, + * because e.g. the AM can reduce WAL logging and page locking overhead. + * + * Except for taking `nslots` tuples as input, as an array of TupleTableSlots + * in `slots`, the parameters for table_multi_insert() are the same as for + * table_insert(). + * + * Note: this leaks memory into the current memory context. You can create a + * temporary context before calling this, if that's a problem. + */ +static inline void +table_multi_insert(Relation rel, TupleTableSlot **slots, int nslots, + CommandId cid, int options, struct BulkInsertStateData *bistate) +{ + rel->rd_tableam->multi_insert(rel, slots, nslots, + cid, options, bistate); +} + +/* * Delete a tuple. * * NB: do not call this directly unless prepared to deal with diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 5b4ea6c2355..a5e4b7ef2e0 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -40,6 +40,7 @@ struct ExprState; struct ExprContext; struct RangeTblEntry; /* avoid including parsenodes.h here */ struct ExprEvalStep; /* avoid including execExpr.h everywhere */ +struct CopyMultiInsertBuffer; /* ---------------- @@ -481,6 +482,9 @@ typedef struct ResultRelInfo /* Additional information specific to partition tuple routing */ struct PartitionRoutingInfo *ri_PartitionInfo; + + /* For use by copy.c when performing multi-inserts */ + struct CopyMultiInsertBuffer *ri_CopyMultiInsertBuffer; } ResultRelInfo; /* ---------------- |