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

Commit4cc56f8

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 parent2ba890c commit4cc56f8

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
deletefrom 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+
createtabletab_batch_local (idint, datatext);
1574+
insert into tab_batch_localselect i,'test'|| ifrom generate_series(1,45) i;
1575+
createtabletab_batch_sharded (idint, datatext) partition by hash(id);
1576+
createtabletab_batch_sharded_p0 partition of tab_batch_sharded
1577+
forvalues with (modulus2, remainder0);
1578+
createtabletab_batch_sharded_p1_remote (idint, datatext);
1579+
create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1580+
forvalues with (modulus2, remainder1)
1581+
server loopback options (table_name'tab_batch_sharded_p1_remote');
1582+
insert into tab_batch_shardedselect*from tab_batch_local;
1583+
selectcount(*)from tab_batch_sharded;
1584+
droptable tab_batch_local;
1585+
droptable tab_batch_sharded;
1586+
droptable 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)

[8]ページ先頭

©2009-2025 Movatter.jp