Skip to content

Commit 4cc56f8

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 2ba890c commit 4cc56f8

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
@@ -6724,6 +6724,28 @@ select * from grem1;
67246724
(2 rows)
67256725

67266726
delete from grem1;
6727+
-- batch insert with foreign partitions.
6728+
-- This schema uses two partitions, one local and one remote with a modulo
6729+
-- to loop across all of them in batches.
6730+
create table tab_batch_local (id int, data text);
6731+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
6732+
create table tab_batch_sharded (id int, data text) partition by hash(id);
6733+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
6734+
for values with (modulus 2, remainder 0);
6735+
create table tab_batch_sharded_p1_remote (id int, data text);
6736+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
6737+
for values with (modulus 2, remainder 1)
6738+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
6739+
insert into tab_batch_sharded select * from tab_batch_local;
6740+
select count(*) from tab_batch_sharded;
6741+
count
6742+
-------
6743+
45
6744+
(1 row)
6745+
6746+
drop table tab_batch_local;
6747+
drop table tab_batch_sharded;
6748+
drop table tab_batch_sharded_p1_remote;
67276749
alter server loopback options (drop batch_size);
67286750
-- ===================================================================
67296751
-- test local triggers

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,24 @@ insert into grem1 (a) values (1), (2);
15671567
select * from gloc1;
15681568
select * from grem1;
15691569
delete from grem1;
1570+
-- batch insert with foreign partitions.
1571+
-- This schema uses two partitions, one local and one remote with a modulo
1572+
-- to loop across all of them in batches.
1573+
create table tab_batch_local (id int, data text);
1574+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
1575+
create table tab_batch_sharded (id int, data text) partition by hash(id);
1576+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
1577+
for values with (modulus 2, remainder 0);
1578+
create table tab_batch_sharded_p1_remote (id int, data text);
1579+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1580+
for values with (modulus 2, remainder 1)
1581+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
1582+
insert into tab_batch_sharded select * from tab_batch_local;
1583+
select count(*) from tab_batch_sharded;
1584+
drop table tab_batch_local;
1585+
drop table tab_batch_sharded;
1586+
drop table tab_batch_sharded_p1_remote;
1587+
15701588
alter server loopback options (drop batch_size);
15711589

15721590
-- ===================================================================

src/backend/executor/nodeModifyTable.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,6 @@ ExecInsert(ModifyTableState *mtstate,
755755
resultRelInfo->ri_PlanSlots,
756756
resultRelInfo->ri_NumSlots,
757757
estate, canSetTag);
758-
resultRelInfo->ri_NumSlots = 0;
759758
flushed = true;
760759
}
761760

@@ -1142,6 +1141,14 @@ ExecBatchInsert(ModifyTableState *mtstate,
11421141

11431142
if (canSetTag && numInserted > 0)
11441143
estate->es_processed += numInserted;
1144+
1145+
/* Clean up all the slots, ready for the next batch */
1146+
for (i = 0; i < numSlots; i++)
1147+
{
1148+
ExecClearTuple(slots[i]);
1149+
ExecClearTuple(planSlots[i]);
1150+
}
1151+
resultRelInfo->ri_NumSlots = 0;
11451152
}
11461153

11471154
/* ----------------------------------------------------------------
@@ -1523,7 +1530,6 @@ ExecPendingInserts(EState *estate)
15231530
resultRelInfo->ri_PlanSlots,
15241531
resultRelInfo->ri_NumSlots,
15251532
estate, mtstate->canSetTag);
1526-
resultRelInfo->ri_NumSlots = 0;
15271533
}
15281534

15291535
list_free(estate->es_insert_pending_result_relations);

0 commit comments

Comments
 (0)