Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix buffer refcount leak with FDW bulk inserts
authorMichael Paquier <michael@paquier.xyz>
Tue, 25 Apr 2023 00:42:33 +0000 (09:42 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 25 Apr 2023 00:42:33 +0000 (09:42 +0900)
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

contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/sql/postgres_fdw.sql
src/backend/executor/nodeModifyTable.c

index 16bee3dd32cb85921f2a1cd42b1a2601db95022f..ee6d9a9115bac9a09cd67f928c09e9dbebd336a0 100644 (file)
@@ -6867,6 +6867,28 @@ select * from grem1;
 (2 rows)
 
 delete from grem1;
+-- batch insert with foreign partitions.
+-- This schema uses two partitions, one local and one remote with a modulo
+-- to loop across all of them in batches.
+create table tab_batch_local (id int, data text);
+insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
+create table tab_batch_sharded (id int, data text) partition by hash(id);
+create table tab_batch_sharded_p0 partition of tab_batch_sharded
+  for values with (modulus 2, remainder 0);
+create table tab_batch_sharded_p1_remote (id int, data text);
+create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
+  for values with (modulus 2, remainder 1)
+  server loopback options (table_name 'tab_batch_sharded_p1_remote');
+insert into tab_batch_sharded select * from tab_batch_local;
+select count(*) from tab_batch_sharded;
+ count 
+-------
+    45
+(1 row)
+
+drop table tab_batch_local;
+drop table tab_batch_sharded;
+drop table tab_batch_sharded_p1_remote;
 alter server loopback options (drop batch_size);
 -- ===================================================================
 -- test local triggers
index 484304e5832b61a69bb9cac5b54d307962ebad36..8e330f2ea6dee98a6e5120ecc87e197bd60b0a88 100644 (file)
@@ -1619,6 +1619,24 @@ insert into grem1 (a) values (1), (2);
 select * from gloc1;
 select * from grem1;
 delete from grem1;
+-- batch insert with foreign partitions.
+-- This schema uses two partitions, one local and one remote with a modulo
+-- to loop across all of them in batches.
+create table tab_batch_local (id int, data text);
+insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
+create table tab_batch_sharded (id int, data text) partition by hash(id);
+create table tab_batch_sharded_p0 partition of tab_batch_sharded
+  for values with (modulus 2, remainder 0);
+create table tab_batch_sharded_p1_remote (id int, data text);
+create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
+  for values with (modulus 2, remainder 1)
+  server loopback options (table_name 'tab_batch_sharded_p1_remote');
+insert into tab_batch_sharded select * from tab_batch_local;
+select count(*) from tab_batch_sharded;
+drop table tab_batch_local;
+drop table tab_batch_sharded;
+drop table tab_batch_sharded_p1_remote;
+
 alter server loopback options (drop batch_size);
 
 -- ===================================================================
index 2f6e66b6413815a8fb7b570a33a2fcf0654561f5..ea6d7012650a3ca34fd3f996adc515127490d37f 100644 (file)
@@ -837,7 +837,6 @@ ExecInsert(ModifyTableContext *context,
                                resultRelInfo->ri_PlanSlots,
                                resultRelInfo->ri_NumSlots,
                                estate, canSetTag);
-               resultRelInfo->ri_NumSlots = 0;
                flushed = true;
            }
 
@@ -1240,6 +1239,14 @@ ExecBatchInsert(ModifyTableState *mtstate,
 
    if (canSetTag && numInserted > 0)
        estate->es_processed += numInserted;
+
+   /* Clean up all the slots, ready for the next batch */
+   for (i = 0; i < numSlots; i++)
+   {
+       ExecClearTuple(slots[i]);
+       ExecClearTuple(planSlots[i]);
+   }
+   resultRelInfo->ri_NumSlots = 0;
 }
 
 /*
@@ -1263,7 +1270,6 @@ ExecPendingInserts(EState *estate)
                        resultRelInfo->ri_PlanSlots,
                        resultRelInfo->ri_NumSlots,
                        estate, mtstate->canSetTag);
-       resultRelInfo->ri_NumSlots = 0;
    }
 
    list_free(estate->es_insert_pending_result_relations);