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

Commit 806fad7

Browse files
committed
Fix buffer refcount leak with FDW bulk inserts
The leak would show up when using batch inserts with foreign tables included in a partition tree, as the slots used in the batch were not reset once processed. In order to fix this problem, some ExecClearTuple() are added to clean up the slots used once a batch is filled and processed, mapping with the number of slots currently in use as tracked by the counter ri_NumSlots. This buffer refcount leak has been introduced in b676ac4 with the addition of the executor facility to improve bulk inserts for FDWs, so backpatch down to 14. Alexander has provided the patch (slightly modified by me). The test for postgres_fdw comes from me, based on the test case that the author has sent in the report. Author: Alexander Pyhalov Discussion: https://postgr.es/m/b035780a740efd38dc30790c76927255@postgrespro.ru Backpatch-through: 14
1 parent 1118cd3 commit 806fad7

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6930,6 +6930,28 @@ select * from grem1;
69306930
(2 rows)
69316931

69326932
delete from grem1;
6933+
-- batch insert with foreign partitions.
6934+
-- This schema uses two partitions, one local and one remote with a modulo
6935+
-- to loop across all of them in batches.
6936+
create table tab_batch_local (id int, data text);
6937+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
6938+
create table tab_batch_sharded (id int, data text) partition by hash(id);
6939+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
6940+
for values with (modulus 2, remainder 0);
6941+
create table tab_batch_sharded_p1_remote (id int, data text);
6942+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
6943+
for values with (modulus 2, remainder 1)
6944+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
6945+
insert into tab_batch_sharded select * from tab_batch_local;
6946+
select count(*) from tab_batch_sharded;
6947+
count
6948+
-------
6949+
45
6950+
(1 row)
6951+
6952+
drop table tab_batch_local;
6953+
drop table tab_batch_sharded;
6954+
drop table tab_batch_sharded_p1_remote;
69336955
alter server loopback options (drop batch_size);
69346956
-- ===================================================================
69356957
-- test local triggers

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,24 @@ insert into grem1 (a) values (1), (2);
16571657
select * from gloc1;
16581658
select * from grem1;
16591659
delete from grem1;
1660+
-- batch insert with foreign partitions.
1661+
-- This schema uses two partitions, one local and one remote with a modulo
1662+
-- to loop across all of them in batches.
1663+
create table tab_batch_local (id int, data text);
1664+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
1665+
create table tab_batch_sharded (id int, data text) partition by hash(id);
1666+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
1667+
for values with (modulus 2, remainder 0);
1668+
create table tab_batch_sharded_p1_remote (id int, data text);
1669+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1670+
for values with (modulus 2, remainder 1)
1671+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
1672+
insert into tab_batch_sharded select * from tab_batch_local;
1673+
select count(*) from tab_batch_sharded;
1674+
drop table tab_batch_local;
1675+
drop table tab_batch_sharded;
1676+
drop table tab_batch_sharded_p1_remote;
1677+
16601678
alter server loopback options (drop batch_size);
16611679

16621680
-- ===================================================================

src/backend/executor/nodeModifyTable.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,6 @@ ExecInsert(ModifyTableContext *context,
856856
resultRelInfo->ri_PlanSlots,
857857
resultRelInfo->ri_NumSlots,
858858
estate, canSetTag);
859-
resultRelInfo->ri_NumSlots = 0;
860859
flushed = true;
861860
}
862861

@@ -1261,6 +1260,14 @@ ExecBatchInsert(ModifyTableState *mtstate,
12611260

12621261
if (canSetTag && numInserted > 0)
12631262
estate->es_processed += numInserted;
1263+
1264+
/* Clean up all the slots, ready for the next batch */
1265+
for (i = 0; i < numSlots; i++)
1266+
{
1267+
ExecClearTuple(slots[i]);
1268+
ExecClearTuple(planSlots[i]);
1269+
}
1270+
resultRelInfo->ri_NumSlots = 0;
12641271
}
12651272

12661273
/*
@@ -1284,7 +1291,6 @@ ExecPendingInserts(EState *estate)
12841291
resultRelInfo->ri_PlanSlots,
12851292
resultRelInfo->ri_NumSlots,
12861293
estate, mtstate->canSetTag);
1287-
resultRelInfo->ri_NumSlots = 0;
12881294
}
12891295

12901296
list_free(estate->es_insert_pending_result_relations);

0 commit comments

Comments
 (0)