Skip to content

Commit aa6177c

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 c1598d8 commit aa6177c

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
@@ -6867,6 +6867,28 @@ select * from grem1;
68676867
(2 rows)
68686868

68696869
delete from grem1;
6870+
-- batch insert with foreign partitions.
6871+
-- This schema uses two partitions, one local and one remote with a modulo
6872+
-- to loop across all of them in batches.
6873+
create table tab_batch_local (id int, data text);
6874+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
6875+
create table tab_batch_sharded (id int, data text) partition by hash(id);
6876+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
6877+
for values with (modulus 2, remainder 0);
6878+
create table tab_batch_sharded_p1_remote (id int, data text);
6879+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
6880+
for values with (modulus 2, remainder 1)
6881+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
6882+
insert into tab_batch_sharded select * from tab_batch_local;
6883+
select count(*) from tab_batch_sharded;
6884+
count
6885+
-------
6886+
45
6887+
(1 row)
6888+
6889+
drop table tab_batch_local;
6890+
drop table tab_batch_sharded;
6891+
drop table tab_batch_sharded_p1_remote;
68706892
alter server loopback options (drop batch_size);
68716893
-- ===================================================================
68726894
-- test local triggers

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,24 @@ insert into grem1 (a) values (1), (2);
16191619
select * from gloc1;
16201620
select * from grem1;
16211621
delete from grem1;
1622+
-- batch insert with foreign partitions.
1623+
-- This schema uses two partitions, one local and one remote with a modulo
1624+
-- to loop across all of them in batches.
1625+
create table tab_batch_local (id int, data text);
1626+
insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
1627+
create table tab_batch_sharded (id int, data text) partition by hash(id);
1628+
create table tab_batch_sharded_p0 partition of tab_batch_sharded
1629+
for values with (modulus 2, remainder 0);
1630+
create table tab_batch_sharded_p1_remote (id int, data text);
1631+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1632+
for values with (modulus 2, remainder 1)
1633+
server loopback options (table_name 'tab_batch_sharded_p1_remote');
1634+
insert into tab_batch_sharded select * from tab_batch_local;
1635+
select count(*) from tab_batch_sharded;
1636+
drop table tab_batch_local;
1637+
drop table tab_batch_sharded;
1638+
drop table tab_batch_sharded_p1_remote;
1639+
16221640
alter server loopback options (drop batch_size);
16231641

16241642
-- ===================================================================

src/backend/executor/nodeModifyTable.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,6 @@ ExecInsert(ModifyTableContext *context,
837837
resultRelInfo->ri_PlanSlots,
838838
resultRelInfo->ri_NumSlots,
839839
estate, canSetTag);
840-
resultRelInfo->ri_NumSlots = 0;
841840
flushed = true;
842841
}
843842

@@ -1240,6 +1239,14 @@ ExecBatchInsert(ModifyTableState *mtstate,
12401239

12411240
if (canSetTag && numInserted > 0)
12421241
estate->es_processed += numInserted;
1242+
1243+
/* Clean up all the slots, ready for the next batch */
1244+
for (i = 0; i < numSlots; i++)
1245+
{
1246+
ExecClearTuple(slots[i]);
1247+
ExecClearTuple(planSlots[i]);
1248+
}
1249+
resultRelInfo->ri_NumSlots = 0;
12431250
}
12441251

12451252
/*
@@ -1263,7 +1270,6 @@ ExecPendingInserts(EState *estate)
12631270
resultRelInfo->ri_PlanSlots,
12641271
resultRelInfo->ri_NumSlots,
12651272
estate, mtstate->canSetTag);
1266-
resultRelInfo->ri_NumSlots = 0;
12671273
}
12681274

12691275
list_free(estate->es_insert_pending_result_relations);

0 commit comments

Comments
 (0)