]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix issue with RANGE's DEFAULT partition pruning
authorDavid Rowley <drowley@postgresql.org>
Fri, 31 Jul 2026 03:36:31 +0000 (15:36 +1200)
committerDavid Rowley <drowley@postgresql.org>
Fri, 31 Jul 2026 03:36:31 +0000 (15:36 +1200)
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

src/backend/partitioning/partprune.c
src/test/regress/expected/partition_prune.out
src/test/regress/sql/partition_prune.sql

index e7c318bbcacc90ccd0f9f1b47586c477613db379..afe57ac297db7c89c9c750d43aac603a441bbfee 100644 (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;
index 849049f9c51a4d5124967052253cf32cc2e15bf2..0d21a2d027cf11db99a7fd17f87d0aa8f5777cda 100644 (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;
index 359a92080562166085cdd879cef7d5d327e49c31..dac673ef80a7b83e95125e0767f1a89752ddfd6c 100644 (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;