From 99cea49d6525e30bc3768e4ffb95377e7584dea1 Mon Sep 17 00:00:00 2001 From: Tomas Vondra Date: Wed, 16 Jun 2021 22:53:31 +0200 Subject: Fix copying data into slots with FDW batching Commit b676ac443b optimized handling of tuple slots with bulk inserts into foreign tables, so that the slots are initialized only once and reused for all batches. The data was however copied into the slots only after the initialization, inserting duplicate values when the slot gets reused. Fixed by moving the ExecCopySlot outside the init branch. The existing postgres_fdw tests failed to catch this due to inserting data into foreign tables without unique indexes, and then checking only the number of inserted rows. This adds a new test with both a unique index and a check of inserted values. Reported-by: Alexander Pyhalov Discussion: https://postgr.es/m/7a8cf8d56b3d18e5c0bccd6cd42d04ac%40postgrespro.ru --- src/backend/executor/nodeModifyTable.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/backend/executor/nodeModifyTable.c') diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 88c479c6da3..143517bc760 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -717,18 +717,20 @@ ExecInsert(ModifyTableState *mtstate, resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] = MakeSingleTupleTableSlot(tdesc, slot->tts_ops); - ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots], - slot); resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] = MakeSingleTupleTableSlot(tdesc, planSlot->tts_ops); - ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots], - planSlot); /* remember how many batch slots we initialized */ resultRelInfo->ri_NumSlotsInitialized++; } + ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots], + slot); + + ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots], + planSlot); + resultRelInfo->ri_NumSlots++; MemoryContextSwitchTo(oldContext); -- cgit v1.2.3