Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86b8504

Browse files
committed
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
1 parent 7bac3ac commit 86b8504

File tree

9 files changed

+587
-366
lines changed

9 files changed

+587
-366
lines changed

src/backend/access/heap/heapam.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid,
21062106
* temporary context before calling this, if that's a problem.
21072107
*/
21082108
void
2109-
heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
2109+
heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
21102110
CommandId cid, int options, BulkInsertState bistate)
21112111
{
21122112
TransactionId xid = GetCurrentTransactionId();
@@ -2127,11 +2127,18 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
21272127
saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
21282128
HEAP_DEFAULT_FILLFACTOR);
21292129

2130-
/* Toast and set header data in all the tuples */
2130+
/* Toast and set header data in all the slots */
21312131
heaptuples = palloc(ntuples * sizeof(HeapTuple));
21322132
for (i = 0; i < ntuples; i++)
2133-
heaptuples[i] = heap_prepare_insert(relation, tuples[i],
2134-
xid, cid, options);
2133+
{
2134+
HeapTuple tuple;
2135+
2136+
tuple = ExecFetchSlotHeapTuple(slots[i], true, NULL);
2137+
slots[i]->tts_tableOid = RelationGetRelid(relation);
2138+
tuple->t_tableOid = slots[i]->tts_tableOid;
2139+
heaptuples[i] = heap_prepare_insert(relation, tuple, xid, cid,
2140+
options);
2141+
}
21352142

21362143
/*
21372144
* We're about to do the actual inserts -- but check for conflict first,
@@ -2361,13 +2368,9 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
23612368
CacheInvalidateHeapTuple(relation, heaptuples[i], NULL);
23622369
}
23632370

2364-
/*
2365-
* Copy t_self fields back to the caller's original tuples. This does
2366-
* nothing for untoasted tuples (tuples[i] == heaptuples[i)], but it's
2367-
* probably faster to always copy than check.
2368-
*/
2371+
/* copy t_self fields back to the caller's slots */
23692372
for (i = 0; i < ntuples; i++)
2370-
tuples[i]->t_self = heaptuples[i]->t_self;
2373+
slots[i]->tts_tid = heaptuples[i]->t_self;
23712374

23722375
pgstat_count_heap_insert(relation, ntuples);
23732376
}

src/backend/access/heap/heapam_handler.c

+1
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,7 @@ static const TableAmRoutine heapam_methods = {
25162516
.tuple_insert = heapam_tuple_insert,
25172517
.tuple_insert_speculative = heapam_tuple_insert_speculative,
25182518
.tuple_complete_speculative = heapam_tuple_complete_speculative,
2519+
.multi_insert = heap_multi_insert,
25192520
.tuple_delete = heapam_tuple_delete,
25202521
.tuple_update = heapam_tuple_update,
25212522
.tuple_lock = heapam_tuple_lock,

0 commit comments

Comments
 (0)