Skip to content

Commit a7a1b44

Browse files
committed
Fix crash in multi-insert COPY
A bug introduced in 0d5f05c considered the *previous* partition's triggers when deciding whether multi-insert can be used. Rearrange the code so that the current partition is considered. Author: Ashutosh Sharma <ashu.coek88@gmail.com>
1 parent d8cc161 commit a7a1b44

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/backend/commands/copy.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,21 +2783,7 @@ CopyFrom(CopyState cstate)
27832783
lastPartitionSampleLineNo = cstate->cur_lineno;
27842784
nPartitionChanges = 0;
27852785
}
2786-
2787-
/*
2788-
* Tests have shown that using multi-inserts when the
2789-
* partition changes on every tuple slightly decreases the
2790-
* performance, however, there are benefits even when only
2791-
* some batches have just 2 tuples, so let's enable
2792-
* multi-inserts even when the average is quite low.
2793-
*/
2794-
leafpart_use_multi_insert = avgTuplesPerPartChange >= 1.3 &&
2795-
!has_before_insert_row_trig &&
2796-
!has_instead_insert_row_trig &&
2797-
resultRelInfo->ri_FdwRoutine == NULL;
27982786
}
2799-
else
2800-
leafpart_use_multi_insert = false;
28012787

28022788
/*
28032789
* Overwrite resultRelInfo with the corresponding partition's
@@ -2821,6 +2807,19 @@ CopyFrom(CopyState cstate)
28212807
has_instead_insert_row_trig = (resultRelInfo->ri_TrigDesc &&
28222808
resultRelInfo->ri_TrigDesc->trig_insert_instead_row);
28232809

2810+
/*
2811+
* Tests have shown that using multi-inserts when the
2812+
* partition changes on every tuple slightly decreases the
2813+
* performance, however, there are benefits even when only
2814+
* some batches have just 2 tuples, so let's enable
2815+
* multi-inserts even when the average is quite low.
2816+
*/
2817+
leafpart_use_multi_insert = insertMethod == CIM_MULTI_CONDITIONAL &&
2818+
avgTuplesPerPartChange >= 1.3 &&
2819+
!has_before_insert_row_trig &&
2820+
!has_instead_insert_row_trig &&
2821+
resultRelInfo->ri_FdwRoutine == NULL;
2822+
28242823
/*
28252824
* We'd better make the bulk insert mechanism gets a new
28262825
* buffer when the partition being inserted into changes.

src/test/regress/input/copy.source

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,23 @@ copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv';
162162
select tableoid::regclass,count(*),sum(a) from parted_copytest
163163
group by tableoid order by tableoid::regclass::name;
164164

165+
truncate parted_copytest;
166+
167+
-- create before insert row trigger on parted_copytest_a2
168+
create function part_ins_func() returns trigger language plpgsql as $$
169+
begin
170+
return new;
171+
end;
172+
$$;
173+
174+
create trigger part_ins_trig
175+
before insert on parted_copytest_a2
176+
for each row
177+
execute procedure part_ins_func();
178+
179+
copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv';
180+
181+
select tableoid::regclass,count(*),sum(a) from parted_copytest
182+
group by tableoid order by tableoid::regclass::name;
183+
165184
drop table parted_copytest;

src/test/regress/output/copy.source

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,24 @@ group by tableoid order by tableoid::regclass::name;
121121
parted_copytest_a2 | 10 | 10055
122122
(2 rows)
123123

124+
truncate parted_copytest;
125+
-- create before insert row trigger on parted_copytest_a2
126+
create function part_ins_func() returns trigger language plpgsql as $$
127+
begin
128+
return new;
129+
end;
130+
$$;
131+
create trigger part_ins_trig
132+
before insert on parted_copytest_a2
133+
for each row
134+
execute procedure part_ins_func();
135+
copy parted_copytest from '@abs_builddir@/results/parted_copytest.csv';
136+
select tableoid::regclass,count(*),sum(a) from parted_copytest
137+
group by tableoid order by tableoid::regclass::name;
138+
tableoid | count | sum
139+
--------------------+-------+--------
140+
parted_copytest_a1 | 1010 | 510655
141+
parted_copytest_a2 | 10 | 10055
142+
(2 rows)
143+
124144
drop table parted_copytest;

0 commit comments

Comments
 (0)