Skip to content

Commit 7ecb714

Browse files
committed
Fix improper NULL handling in list partitioning code.
The previous logic was wrong when the value was NULL but there was no partition for NULL. Amit Langote, reviewed by Jeevan Ladhe Discussion: http://postgr.es/m/d64f8498-70eb-3c88-b56d-c54fd3b0500f@lab.ntt.co.jp
1 parent 8355a01 commit 7ecb714

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/backend/catalog/partition.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -1729,10 +1729,14 @@ get_partition_for_tuple(PartitionDispatch *pd,
17291729
errmsg("range partition key of row contains null")));
17301730
}
17311731

1732-
if (partdesc->boundinfo->has_null && isnull[0])
1733-
/* Tuple maps to the null-accepting list partition */
1732+
/*
1733+
* A null partition key is only acceptable if null-accepting list
1734+
* partition exists.
1735+
*/
1736+
cur_index = -1;
1737+
if (isnull[0] && partdesc->boundinfo->has_null)
17341738
cur_index = partdesc->boundinfo->null_index;
1735-
else
1739+
else if (!isnull[0])
17361740
{
17371741
/* Else bsearch in partdesc->boundinfo */
17381742
bool equal = false;

src/test/regress/expected/insert.out

+7
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ DETAIL: Failing row contains (1, 2).
365365
insert into mlparted1 (a, b) values (2, 3);
366366
ERROR: new row for relation "mlparted11" violates partition constraint
367367
DETAIL: Failing row contains (3, 2).
368+
-- check routing error through a list partitioned table when the key is null
369+
create table lparted_nonullpart (a int, b char) partition by list (b);
370+
create table lparted_nonullpart_a partition of lparted_nonullpart for values in ('a');
371+
insert into lparted_nonullpart values (1);
372+
ERROR: no partition of relation "lparted_nonullpart" found for row
373+
DETAIL: Partition key of the failing row contains (b) = (null).
374+
drop table lparted_nonullpart;
368375
-- check that RETURNING works correctly with tuple-routing
369376
alter table mlparted drop constraint check_b;
370377
create table mlparted12 partition of mlparted1 for values from (5) to (10);

src/test/regress/sql/insert.sql

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ insert into mlparted values (1, 2);
226226
-- selected by tuple-routing
227227
insert into mlparted1 (a, b) values (2, 3);
228228

229+
-- check routing error through a list partitioned table when the key is null
230+
create table lparted_nonullpart (a int, b char) partition by list (b);
231+
create table lparted_nonullpart_a partition of lparted_nonullpart for values in ('a');
232+
insert into lparted_nonullpart values (1);
233+
drop table lparted_nonullpart;
234+
229235
-- check that RETURNING works correctly with tuple-routing
230236
alter table mlparted drop constraint check_b;
231237
create table mlparted12 partition of mlparted1 for values from (5) to (10);

0 commit comments

Comments
 (0)