Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit806fad7

Browse files
committed
Fix buffer refcount leak with FDW bulk inserts
The leak would show up when using batch inserts with foreign tablesincluded in a partition tree, as the slots used in the batch were notreset once processed. In order to fix this problem, someExecClearTuple() are added to clean up the slots used once a batch isfilled and processed, mapping with the number of slots currently in useas tracked by the counter ri_NumSlots.This buffer refcount leak has been introduced inb676ac4 with theaddition of the executor facility to improve bulk inserts for FDWs, sobackpatch down to 14.Alexander has provided the patch (slightly modified by me). The testfor postgres_fdw comes from me, based on the test case that the authorhas sent in the report.Author: Alexander PyhalovDiscussion:https://postgr.es/m/b035780a740efd38dc30790c76927255@postgrespro.ruBackpatch-through: 14
1 parent1118cd3 commit806fad7

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
deletefrom 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+
createtabletab_batch_local (idint, datatext);
1664+
insert into tab_batch_localselect i,'test'|| ifrom generate_series(1,45) i;
1665+
createtabletab_batch_sharded (idint, datatext) partition by hash(id);
1666+
createtabletab_batch_sharded_p0 partition of tab_batch_sharded
1667+
forvalues with (modulus2, remainder0);
1668+
createtabletab_batch_sharded_p1_remote (idint, datatext);
1669+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1670+
forvalues with (modulus2, remainder1)
1671+
server loopback options (table_name'tab_batch_sharded_p1_remote');
1672+
insert into tab_batch_shardedselect*from tab_batch_local;
1673+
selectcount(*)from tab_batch_sharded;
1674+
droptable tab_batch_local;
1675+
droptable tab_batch_sharded;
1676+
droptable 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)

[8]ページ先頭

©2009-2025 Movatter.jp