1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Fix issue with RANGE's DEFAULT partition pruning

Partition pruning for RANGE-partitioned tables could mistakenly prune
the DEFAULT partition in some cases when it was not valid to do so,
which could lead to rows missing from query results.

The only known cases where this could happen is when combining pruning
steps from an IS NOT NULL clause with other steps that matched to the
DEFAULT partition.  This could occur due to RANGE partitioned tables
having two distinct internal representations for marking if the DEFAULT
partition should be scanned.  The IS NOT NULL steps would mark the
"scan_default" boolean, but other steps created for different purposes
could mark a bound_offset Bitmapset, which would ultimately translate into
also scanning the default partition.  This could all fail after multiple
steps were combined with a combine intersect operator, as that will
intersect the bound_offset bits and only set scan_default if all pruning
steps have that flag set.  When both input steps to the intersect operator
had different representations of whether to scan the DEFAULT partition,
the resulting intersect step result would contain neither representation.

Here, we fix this by having the IS NOT NULL pruning result mark the
bound_offsets so that it uses both representations to mark that the
DEFAULT partition must be scanned.

Reported-by: Jacob Brazeal <jacob.brazeal@gmail.com>
Diagnosed-by: Jacob Brazeal <jacob.brazeal@gmail.com>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CA+COZaDXrfTaBjLE=Z79MTaH6Xun1V4PeKxLvCNv8mXS8wn0rw@mail.gmail.com
Backpatch-through: 14
This commit is contained in:
David Rowley
2026-07-31 15:36:31 +12:00
parent d64c805dea
commit 709dfd27f1
3 changed files with 68 additions and 8 deletions

View File

@@ -3031,15 +3031,9 @@ get_matching_range_bounds(PartitionPruneContext *context,
*/
if (nvalues == 0)
{
/* ignore key space not covered by any partitions */
if (partindices[minoff] < 0)
minoff++;
if (partindices[maxoff] < 0)
maxoff--;
result->scan_default = partition_bound_has_default(boundinfo);
Assert(partindices[minoff] >= 0 &&
partindices[maxoff] >= 0);
Assert(partindices[minoff] >= -1 &&
partindices[maxoff] >= -1);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
return result;

View File

@@ -691,6 +691,52 @@ explain (costs off) select * from rlp where (a = 1 and a = 3) or (a > 1 and a =
Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
(11 rows)
-- Test cases for range partitioned tables with IN clauses.
create table rangepart (a int) partition by range (a);
create table rangepart1 partition of rangepart for values from (0) to (10);
create table rangepart2 partition of rangepart for values from (10) to (20);
create table rangepart_def partition of rangepart default;
-- Ensure we scan all apart from the default partition
explain (costs off) select * from rangepart where a in(5,15);
QUERY PLAN
-------------------------------------------------
Append
-> Seq Scan on rangepart1 rangepart_1
Filter: (a = ANY ('{5,15}'::integer[]))
-> Seq Scan on rangepart2 rangepart_2
Filter: (a = ANY ('{5,15}'::integer[]))
(5 rows)
-- Ensure we scan only the default
explain (costs off) select * from rangepart where a in(20,21);
QUERY PLAN
--------------------------------------------
Seq Scan on rangepart_def rangepart
Filter: (a = ANY ('{20,21}'::integer[]))
(2 rows)
-- Ensure we scan only the default
explain (costs off) select * from rangepart where a in(-1,20);
QUERY PLAN
--------------------------------------------
Seq Scan on rangepart_def rangepart
Filter: (a = ANY ('{-1,20}'::integer[]))
(2 rows)
-- Ensure we scan all partitions
explain (costs off) select * from rangepart where a is not null and a in(-1,5,15,20);
QUERY PLAN
-----------------------------------------------------------------------------
Append
-> Seq Scan on rangepart1 rangepart_1
Filter: ((a IS NOT NULL) AND (a = ANY ('{-1,5,15,20}'::integer[])))
-> Seq Scan on rangepart2 rangepart_2
Filter: ((a IS NOT NULL) AND (a = ANY ('{-1,5,15,20}'::integer[])))
-> Seq Scan on rangepart_def rangepart_3
Filter: ((a IS NOT NULL) AND (a = ANY ('{-1,5,15,20}'::integer[])))
(7 rows)
drop table rangepart;
-- multi-column keys
create table mc3p (a int, b int, c int) partition by range (a, abs(b), c);
create table mc3p_default partition of mc3p default;

View File

@@ -118,6 +118,26 @@ explain (costs off) select * from rlp where a > 1 and a >=15; /* rlp3 onwards, i
explain (costs off) select * from rlp where a = 1 and a = 3; /* empty */
explain (costs off) select * from rlp where (a = 1 and a = 3) or (a > 1 and a = 15);
-- Test cases for range partitioned tables with IN clauses.
create table rangepart (a int) partition by range (a);
create table rangepart1 partition of rangepart for values from (0) to (10);
create table rangepart2 partition of rangepart for values from (10) to (20);
create table rangepart_def partition of rangepart default;
-- Ensure we scan all apart from the default partition
explain (costs off) select * from rangepart where a in(5,15);
-- Ensure we scan only the default
explain (costs off) select * from rangepart where a in(20,21);
-- Ensure we scan only the default
explain (costs off) select * from rangepart where a in(-1,20);
-- Ensure we scan all partitions
explain (costs off) select * from rangepart where a is not null and a in(-1,5,15,20);
drop table rangepart;
-- multi-column keys
create table mc3p (a int, b int, c int) partition by range (a, abs(b), c);
create table mc3p_default partition of mc3p default;