Partition pruning can incorrectly exclude a RANGE default partition

Started by Jacob Brazeal4 months ago10 messageshackers
Jump to latest
#1Jacob Brazeal
jacob.brazeal@gmail.com

Hi hackers,

I found a partition-pruning bug where adding a redundant IS NOT NULL
condition can cause a RANGE table’s default partition to be pruned,
producing wrong results.

This reproduces on current master (13b7a8a0ef5):

CREATE TABLE s2 (a int) PARTITION BY RANGE (a);
CREATE TABLE s2_1 PARTITION OF s2 FOR VALUES FROM (0) TO (10);
CREATE TABLE s2_d PARTITION OF s2 DEFAULT;

INSERT INTO s2 VALUES (5), (15);

SELECT count(*) FROM s2
WHERE a IN (5, 15);

SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);

The first query correctly returns 2. The second returns 1, although a IS
NOT NULL is true for both rows.

The plan for the wrong query contains only the ordinary partition:

Seq Scan on s2_1 s2
Filter: ((a IS NOT NULL) AND
(a = ANY ('{5,15}'::integer[])))

The default partition is absent. Disabling partition pruning, or querying
the two leaf partitions with UNION ALL, returns the correct two rows.

The problem also occurs with equivalent forms that generate multiple
pruning steps, for example:

a IS NOT NULL AND a = ANY (ARRAY[5,15])
a IS NOT NULL AND (a = 5 OR a = 15)
a IS NOT NULL AND (a < 6 OR a > 14)

A simple equality does not reproduce:

WHERE a IS NOT NULL AND a = 15

Run-time pruning is affected as well:

SET plan_cache_mode = force_generic_plan;

PREPARE s2p(int, int) AS
SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN ($1, $2);

EXECUTE s2p(5, 15);

This also returns 1, with the default subplan removed.

The issue appears to be in the nvalues == 0 path of
get_matching_range_bounds(). That path removes bound offsets whose
partindices[] entry is -1 and records the default partition through
scan_default instead.

For this query, the IS NOT NULL pruning step therefore represents the
default partition using scan_default, while the IN step represents it
using a -1 bound offset. The INTERSECT combine step treats those as
independent representations:

result->bound_offsets =
bms_int_members(result->bound_offsets,
step_result->bound_offsets);

if (result->scan_default)
result->scan_default = step_result->scan_default;

The default offset is removed by the bitmap intersection, and
scan_default is then cleared, so the default partition is lost.

Best,
Jacob Brazeal

#2Tender Wang
tndrwang@gmail.com
In reply to: Jacob Brazeal (#1)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Jacob Brazeal <jacob.brazeal@gmail.com> 于2026年7月26日周日 11:14写道:

Hi hackers,

I found a partition-pruning bug where adding a redundant IS NOT NULL condition can cause a RANGE table’s default partition to be pruned, producing wrong results.

This reproduces on current master (13b7a8a0ef5):

CREATE TABLE s2 (a int) PARTITION BY RANGE (a);
CREATE TABLE s2_1 PARTITION OF s2 FOR VALUES FROM (0) TO (10);
CREATE TABLE s2_d PARTITION OF s2 DEFAULT;

INSERT INTO s2 VALUES (5), (15);

SELECT count(*) FROM s2
WHERE a IN (5, 15);

SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);

The first query correctly returns 2. The second returns 1, although a IS NOT NULL is true for both rows.

The plan for the wrong query contains only the ordinary partition:

Seq Scan on s2_1 s2
Filter: ((a IS NOT NULL) AND
(a = ANY ('{5,15}'::integer[])))

The default partition is absent. Disabling partition pruning, or querying the two leaf partitions with UNION ALL, returns the correct two rows.

The problem also occurs with equivalent forms that generate multiple pruning steps, for example:

a IS NOT NULL AND a = ANY (ARRAY[5,15])
a IS NOT NULL AND (a = 5 OR a = 15)
a IS NOT NULL AND (a < 6 OR a > 14)

A simple equality does not reproduce:

WHERE a IS NOT NULL AND a = 15

Run-time pruning is affected as well:

SET plan_cache_mode = force_generic_plan;

PREPARE s2p(int, int) AS
SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN ($1, $2);

EXECUTE s2p(5, 15);

This also returns 1, with the default subplan removed.

The issue appears to be in the nvalues == 0 path of
get_matching_range_bounds(). That path removes bound offsets whose
partindices[] entry is -1 and records the default partition through
scan_default instead.

For this query, the IS NOT NULL pruning step therefore represents the
default partition using scan_default, while the IN step represents it
using a -1 bound offset. The INTERSECT combine step treats those as
independent representations:

result->bound_offsets =
bms_int_members(result->bound_offsets,
step_result->bound_offsets);

if (result->scan_default)
result->scan_default = step_result->scan_default;

The default offset is removed by the bitmap intersection, and
scan_default is then cleared, so the default partition is lost.

Yes, it seems that we should use step_result instead of result in
perform_pruning_combine_step().
After replacing it, I get the correct result.
diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..8eedb6cce16 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3685,9 +3685,9 @@
perform_pruning_combine_step(PartitionPruneContext *context,

step_result->bound_offsets);

                                        /* Update whether to scan null
and default partitions. */
-                                       if (result->scan_null)
+                                       if (step_result->scan_null)
                                                result->scan_null =
step_result->scan_null;
-                                       if (result->scan_default)
+                                       if (step_result->scan_default)
                                                result->scan_default =
step_result->scan_default;

postgres=# SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);
count
-------
2
(1 row)

postgres=# SET plan_cache_mode = force_generic_plan;
SET
postgres=# PREPARE s2p(int, int) AS
SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN ($1, $2);
PREPARE
postgres=# execute s2p(5,15);
count
-------
2
(1 row)

See the attached patch. (no test ).

--
Thanks,
Tender Wang

Attachments:

t253192_2
0001-Fix-partition_prue.patchapplication/octet-stream; name=0001-Fix-partition_prue.patchDownload+2-3
#3Tender Wang
tndrwang@gmail.com
In reply to: Tender Wang (#2)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Tender Wang <tndrwang@gmail.com> 于2026年7月27日周一 17:48写道:

Yes, it seems that we should use step_result instead of result in
perform_pruning_combine_step().
After replacing it, I get the correct result.
diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..8eedb6cce16 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3685,9 +3685,9 @@
perform_pruning_combine_step(PartitionPruneContext *context,

step_result->bound_offsets);

/* Update whether to scan null
and default partitions. */
-                                       if (result->scan_null)
+                                       if (step_result->scan_null)
result->scan_null =
step_result->scan_null;
-                                       if (result->scan_default)
+                                       if (step_result->scan_default)
result->scan_default =
step_result->scan_default;

postgres=# SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);
count
-------
2
(1 row)

postgres=# SET plan_cache_mode = force_generic_plan;
SET
postgres=# PREPARE s2p(int, int) AS
SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN ($1, $2);
PREPARE
postgres=# execute s2p(5,15);
count
-------
2
(1 row)

See the attached patch. (no test ).

The attached patch I wrote in the last email may be wrong. Because
some cases in the regression failed.
After digging deep into this issue, the problem seems to be with
processing (5,15); to be more precise, it is processing 15.
See the debug info:
PartitionPruneStepOp [step_id=0]
[opstrategy=3]
[exprs]
Const [consttype=23 constlen=4 constvalue=5
constisnull=false constbyval=true]
[cmpfns] OidList: [351]
(gdb) n
(gdb) pgprint argsteps
PartitionPruneStepOp [step_id=1]
[opstrategy=3]
[exprs]
Const [consttype=23 constlen=4 constvalue=15
constisnull=false constbyval=true]
[cmpfns] OidList: [351]

The above steps are processing (5,15). When we process 15 in (5,15),
the result is :

PruneStepResult [scan_default=false scan_null=false]
[bound_offsets] Bitmapset [2]

We should scan the default partition, and perform_pruning_base_step()
returned the above result.
But the scan_default flag is false; I think it should be true here.

When we process "a is not null and a in (5,15)", we first process the
result of "(5,15), in perform_pruning_combine_step() : case
PARTPRUNE_COMBINE_INTERSECT.

As we said above, the result->scan_default is false when processing "a
is not null", because firststep is false, so we enter :

....
else
{
/* Record datum indexes common to both steps */
result->bound_offsets =
bms_int_members(result->bound_offsets,
step_result->bound_offsets);

/* Update whether to scan null and default partitions. */
if (result->scan_null)
result->scan_null = step_result->scan_null;
if (result->scan_default)
result->scan_default = step_result->scan_default;
}
....
The result->scan_default is finally false, even though
step_result->scan_default is true.

--
Thanks,
Tender Wang

#4Tender Wang
tndrwang@gmail.com
In reply to: Tender Wang (#3)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Tender Wang <tndrwang@gmail.com> 于2026年7月27日周一 18:41写道:

Tender Wang <tndrwang@gmail.com> 于2026年7月27日周一 17:48写道:

Yes, it seems that we should use step_result instead of result in
perform_pruning_combine_step().
After replacing it, I get the correct result.
diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..8eedb6cce16 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3685,9 +3685,9 @@
perform_pruning_combine_step(PartitionPruneContext *context,

step_result->bound_offsets);

/* Update whether to scan null
and default partitions. */
-                                       if (result->scan_null)
+                                       if (step_result->scan_null)
result->scan_null =
step_result->scan_null;
-                                       if (result->scan_default)
+                                       if (step_result->scan_default)
result->scan_default =
step_result->scan_default;

postgres=# SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);
count
-------
2
(1 row)

postgres=# SET plan_cache_mode = force_generic_plan;
SET
postgres=# PREPARE s2p(int, int) AS
SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN ($1, $2);
PREPARE
postgres=# execute s2p(5,15);
count
-------
2
(1 row)

See the attached patch. (no test ).

The attached patch I wrote in the last email may be wrong. Because
some cases in the regression failed.
After digging deep into this issue, the problem seems to be with
processing (5,15); to be more precise, it is processing 15.
See the debug info:
PartitionPruneStepOp [step_id=0]
[opstrategy=3]
[exprs]
Const [consttype=23 constlen=4 constvalue=5
constisnull=false constbyval=true]
[cmpfns] OidList: [351]
(gdb) n
(gdb) pgprint argsteps
PartitionPruneStepOp [step_id=1]
[opstrategy=3]
[exprs]
Const [consttype=23 constlen=4 constvalue=15
constisnull=false constbyval=true]
[cmpfns] OidList: [351]

The above steps are processing (5,15). When we process 15 in (5,15),
the result is :

PruneStepResult [scan_default=false scan_null=false]
[bound_offsets] Bitmapset [2]

We should scan the default partition, and perform_pruning_base_step()
returned the above result.
But the scan_default flag is false; I think it should be true here.

I tried the following:

diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..96649580e6f 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3170,6 +3170,8 @@ get_matching_range_bounds(PartitionPruneContext *context,
                                 * end up adding the first bound's
offset, that is, 0.
                                 */
                                result->bound_offsets =
bms_make_singleton(off + 1);
+                               if (off == boundinfo->default_index)
+                                       result->scan_default = true;
                        }

return result;

This time all cases in regression passed.
Any thoughts?

--
Thanks,
Tender Wang

#5Chengpeng Yan
chengpeng_yan@outlook.com
In reply to: Tender Wang (#4)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Hi Tender,

On Jul 27, 2026, at 19:34, Tender Wang <tndrwang@gmail.com> wrote:

I tried the following:

diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..96649580e6f 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3170,6 +3170,8 @@ get_matching_range_bounds(PartitionPruneContext *context,
* end up adding the first bound's
offset, that is, 0.
*/
result->bound_offsets =
bms_make_singleton(off + 1);
+                               if (off == boundinfo->default_index)
+                                       result->scan_default = true;
}

return result;

This time all cases in regression passed.
Any thoughts?

--
Thanks,
Tender Wang

Thanks for working on this. I may be missing something, but `off`
indexes range-bound datums while `default_index` is a partition index,
so I am not sure the two can be compared. With that patch I still get a
wrong result:

```sql
CREATE TABLE r (a int) PARTITION BY RANGE (a);
CREATE TABLE r1 PARTITION OF r FOR VALUES FROM (0) TO (10);
CREATE TABLE r2 PARTITION OF r FOR VALUES FROM (20) TO (30);
CREATE TABLE rd PARTITION OF r DEFAULT;
INSERT INTO r VALUES (5), (35);

SELECT count(*) FROM r WHERE a IS NOT NULL AND a IN (5, 35);
```

Here `default_index` is 2 but `off` is 3 for 35, so `scan_default` stays
false, `rd` is pruned and the query returns 1 instead of 2.

I agree with Jacob that the problem is in the `nvalues == 0` path. For
35, the `IN` step keeps a `-1` offset, but the `IS NOT NULL` step
removes that same offset. Their intersection therefore loses 35 and
prunes the default partition. If the `IS NOT NULL` step keeps all
offsets, `get_matching_partitions()` can later turn the surviving `-1`
offset into a default-partition scan:

```diff
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index e7c318bbcac..48d16ebb4fb 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3031,15 +3031,7 @@ 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);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
return result;
```

The reported cases and the example above behave as expected with this,
and `make check` passes.

--
Best regards,
Chengpeng Yan

#6Tender Wang
tndrwang@gmail.com
In reply to: Chengpeng Yan (#5)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Chengpeng Yan <chengpeng_yan@outlook.com> 于2026年7月27日周一 22:53写道:

Thanks for working on this. I may be missing something, but `off`
indexes range-bound datums while `default_index` is a partition index,
so I am not sure the two can be compared. With that patch I still get a
wrong result:

```sql
CREATE TABLE r (a int) PARTITION BY RANGE (a);
CREATE TABLE r1 PARTITION OF r FOR VALUES FROM (0) TO (10);
CREATE TABLE r2 PARTITION OF r FOR VALUES FROM (20) TO (30);
CREATE TABLE rd PARTITION OF r DEFAULT;
INSERT INTO r VALUES (5), (35);

SELECT count(*) FROM r WHERE a IS NOT NULL AND a IN (5, 35);
```

Here `default_index` is 2 but `off` is 3 for 35, so `scan_default` stays
false, `rd` is pruned and the query returns 1 instead of 2.

Yes, I misunderstood the meaning of 'off'. When I saw the code in
create_range_bounds():

if (spec->is_default)
{
default_index = i;
continue;
}

I mistakenly thought that off and default_index were related in some way.

I agree with Jacob that the problem is in the `nvalues == 0` path. For
35, the `IN` step keeps a `-1` offset, but the `IS NOT NULL` step
removes that same offset. Their intersection therefore loses 35 and
prunes the default partition. If the `IS NOT NULL` step keeps all
offsets, `get_matching_partitions()` can later turn the surviving `-1`
offset into a default-partition scan:

```diff
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index e7c318bbcac..48d16ebb4fb 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3031,15 +3031,7 @@ 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);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
return result;
```

I looked back at how the default partition were added to the final result.
In the last of get_matching_partitions(), we have:
...
while ((i = bms_next_member(final_result->bound_offsets, i)) >= 0)
{
int partindex;

Assert(i < context->boundinfo->nindexes);
partindex = context->boundinfo->indexes[i];

if (partindex < 0)
{
/*
* In range partitioning cases, if a partition index is -1 it
* means that the bound at the offset is the upper bound for a
* range not covered by any partition (other than a possible
* default partition). In hash partitioning, the same means no
* partition has been defined for the corresponding remainder
* value.
*
* In either case, the value is still part of the queried range of
* values, so mark to scan the default partition if one exists.
*/
scan_default |= partition_bound_has_default(context->boundinfo);
continue;
}
...
The code above can retrieve the default partition.
Removing the code in "nvalues ==0" can cause the logic to enter "if
(partindex<0)" in get_matching_partitions().
But I still think that we should make the prune result of '15'
prune_step correct.

diff --git a/src/backend/partitioning/partprune.c
b/src/backend/partitioning/partprune.c
index e7c318bbcac..4641941f597 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3170,6 +3170,8 @@ get_matching_range_bounds(PartitionPruneContext *context,
                                 * end up adding the first bound's
offset, that is, 0.
                                 */
                                result->bound_offsets =
bms_make_singleton(off + 1);
+                               if (partindices[off + 1] < 0)
+                                       result->scan_default |=
partition_bound_has_default(boundinfo);
                        }

The above diff can do the same work as the code at the end of
get_matching_partitions().

--
Thanks,
Tender Wang

#7Tender Wang
tndrwang@gmail.com
In reply to: Chengpeng Yan (#5)
Re: Partition pruning can incorrectly exclude a RANGE default partition

Chengpeng Yan <chengpeng_yan@outlook.com> 于2026年7月27日周一 22:53写道:

```diff
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index e7c318bbcac..48d16ebb4fb 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -3031,15 +3031,7 @@ 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);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
return result;
```

postgres=# create table s2_2 partition of s2 for values from (20) to (30);
CREATE TABLE
postgres=# explain SELECT count(*) FROM s2
WHERE a IS NOT NULL
AND a IN (5, 15);
QUERY PLAN
-----------------------------------------------------------------------------
Aggregate (cost=84.12..84.14 rows=1 width=8)
-> Append (cost=0.00..84.00 rows=50 width=0)
-> Seq Scan on s2_1 (cost=0.00..41.88 rows=25 width=0)
Filter: ((a IS NOT NULL) AND (a = ANY ('{5,15}'::integer[])))
-> Seq Scan on s2_d s2_2 (cost=0.00..41.88 rows=25 width=0)
Filter: ((a IS NOT NULL) AND (a = ANY ('{5,15}'::integer[])))
(6 rows)

The above query is OK.
If the value in the IN clause is larger than the max datum in
boundinfo or lower than the min datum in boundinfo,
per the if (nvalues == 0) logic, the min and max offset will finally
be excluded. So the default partition is pruned.
It seems that the current logic ignores the reported case.

Before this email, I thought that as long as every prune_step is
correct, then the final result is correct.
But that way, there seem to be more places where we should consider
the result->scan_default flag in get_matching_range_bounds().

Your fix seems simpler.
I added Amit to the CC list; the original author seems to be him. He
should know more than I do.

--
Thanks,
Tender Wang

#8David Rowley
dgrowleyml@gmail.com
In reply to: Chengpeng Yan (#5)
Re: Partition pruning can incorrectly exclude a RANGE default partition

On Tue, 28 Jul 2026 at 02:53, Chengpeng Yan <chengpeng_yan@outlook.com> wrote:

@@ -3031,15 +3031,7 @@ 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);
result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
return result;

(Much of this has been said already, but I wanted to summarise my
independent analysis)

The problem is down to there being two distinct ways to mark that the
DEFAULT partition should be scanned with RANGE partitioned tables. I
wrote the attached debug_part_prune.diff patch to highlight this. This
provides additional debug output during pruning.

CREATE TABLE s2 (a int) PARTITION BY RANGE (a);
CREATE TABLE s2_1 PARTITION OF s2 FOR VALUES FROM (0) TO (10);
CREATE TABLE s2_d PARTITION OF s2 DEFAULT;

SELECT * FROM s2 postgres-# WHERE a IN (5, 15);
NOTICE: PartitionPruneStepOp: 0 N-exprs = 1, offsets = 1 (1),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepOp: 1 N-exprs = 1, offsets = 1 (2),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepCombine: 2 (UNION) N-inputs = 2 (0 1),
offsets = 2 (1 2), scan_default = NO, scan_null = NO

In the reported working case (without "a IS NOT NULL"), you can see
that "scan_default = NO", even though getting a = 15 requires that the
default partition is scanned. Instead, scanning the default is
represented with the offset = 2 bit being set. It's only later in
get_matching_partitions() that the boundinfo->indexes sees that the
indexes element is set to -1 and then marks the default to be scanned.

The problem occurs during PartitionPruneStepCombine INTERSECT steps
when the "a IS NOT NULL" clause is added. The results in a
miscalculation of the common partitions that must be scanned. If some
PruneStepResults use the bound_offsets way of marking that the DEFAULT
partition is to be scanned, and another step uses the
scan_default=true method, then once the intersected results are
calculated, neither representation remains. Consider the problem case:

SELECT * FROM s2 WHERE a IS NOT NULL and a IN (5,15);
NOTICE: PartitionPruneStepOp: 0 N-exprs = 1, offsets = 1 (1),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepOp: 1 N-exprs = 1, offsets = 1 (2),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepCombine: 2 (UNION) N-inputs = 2 (0 1),
offsets = 2 (1 2), scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepOp: 3 N-exprs = 0, offsets = 1 (1),
scan_default = YES, scan_null = NO
NOTICE: PartitionPruneStepCombine: 4 (INTERSECT) N-inputs = 2 (2 3),
offsets = 1 (1), scan_default = *NO*, scan_null = NO

Here you can see that the 2nd step (step_id=1) has offset=2 set. The
following step UNIONs those and ends up with 1+2. All good so far
(this is the IN (5,15) part). The step_id=3 step is for "a IS NOT
NULL" and has markings for s2_1 to be scanned (offset=1) and uses
scan_default=true to mark that the default must be scanned. It's easy
to see that the final INTERSECT step accidentally removes the offset=2
and scan_default=true markings, which is incorrect.

If we look at a similar case with a LIST partitioned table:

CREATE TABLE lp (a INT) PARTITION BY LIST(a);
CREATE TABLE lp_5 PARTITION OF lp FOR VALUES IN (5);
CREATE TABLE lp_d PARTITION OF lp DEFAULT;

SELECT * FROM lp WHERE a IS NOT NULL AND a IN(5,15);

NOTICE: PartitionPruneStepOp: 0 N-exprs = 1, offsets = 1 (0),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepOp: 1 N-exprs = 1, offsets = 0 (),
scan_default = YES, scan_null = NO
NOTICE: PartitionPruneStepCombine: 2 (UNION) N-inputs = 2 (0 1),
offsets = 1 (0), scan_default = YES, scan_null = NO
NOTICE: PartitionPruneStepOp: 3 N-exprs = 0, offsets = 1 (0),
scan_default = YES, scan_null = NO
NOTICE: PartitionPruneStepCombine: 4 (INTERSECT) N-inputs = 2 (2 3),
offsets = 1 (0), scan_default = YES, scan_null = NO

You can see step_id=2 uses scan_default=true and since step_id=3 has
that set too, that continues into the final intersected step's result.
No bugs there.

The reason RANGE uses this alternative way to represent DEFAULTs is so
that gaps in the range are easier to detect. If you have:

CREATE TABLE rp (a INT) PARTITION BY RANGE(a);
CREATE TABLE rp1 PARTITION OF rp FOR VALUES FROM (0) TO (10);
CREATE TABLE rp2 PARTITION OF rp FOR VALUES FROM (20) TO (30);
CREATE TABLE rp_d PARTITION OF rp DEFAULT;

SELECT * FROM rp WHERE a BETWEEN 0 AND 25;

Then the PartitionBoundInfoData .indexes will be = { -1, 0, -1, 1, -1}
and we'll land on the middle -1 when binary searching the
PartitionBoundInfoData.datums array. That middle -1 wouldn't be there
if there was no gap between rp1 and rp2.

I see a couple of ways to fix this:

1. Modify PartitionBoundInfoData to add a new Bitmapset which marks
which of the indexes[] array maps to -1. Then in
get_matching_range_bounds(), once we've set bound_offsets, check if we
have a bms_overlap with the new Bitmapset and set scan_default=true if
there is; or
2. Modify get_matching_range_bounds() so that we mark *all*
bound_offsets for the "if (nvalues == 0)" path.

#1 is more invasive as it requires a new field in
PartitionBoundInfoData, then some extra processing when we execute
each RANGE pruning step. This can happen often for run-time pruning
and this might slow down that enough to notice.

The concern I had with #2 is that we also mark the default partition
to be scanned in the "if (nvalues < partnatts)" case. This case is hit
when we RANGE partition by multiple columns and only have a pruning
step for some leading subset of those. Consider:

CREATE TABLE rb (a INT, b INT) PARTITION BY RANGE(a,b);
CREATE TABLE rb1 PARTITION OF rb FOR VALUES FROM (0,0) TO (0,10);
CREATE TABLE rb2 PARTITION OF rb FOR VALUES FROM (0,10) TO (0,20);
CREATE TABLE rb_d PARTITION OF rb DEFAULT;

SELECT * FROM rb WHERE a = 0;
NOTICE: PartitionPruneStepOp: 0 N-exprs = 1, offsets = 4 (0 1 2 3),
scan_default = YES, scan_null = NO

As expected, we have scan_default=true. To have a problem here, we
need to produce an INTERSECT combine step that combines the above step
with one with the bound_offset way of marking that we scan the default
partition. I think it might not be possible to trigger the same issue
in this scenario without having quals that conflict and result in
pruning partitions anyway. Consider:

SELECT * FROM rb WHERE (a = 0 AND b = 9) AND (a = 0 AND b>30);
NOTICE: PartitionPruneStepOp: 0 N-exprs = 1, offsets = 4 (0 1 2 3),
scan_default = YES, scan_null = NO
NOTICE: PartitionPruneStepOp: 1 N-exprs = 2, offsets = 1 (1),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepOp: 2 N-exprs = 2, offsets = 1 (3),
scan_default = NO, scan_null = NO
NOTICE: PartitionPruneStepCombine: 3 (INTERSECT) N-inputs = 3 (0 1
2), offsets = 0 (), scan_default = NO, scan_null = NO

Here, step_id=0 has scan_default=true and step_id=2 marks the default
to be scanned with offset = 3. That results in the INTERSECT step
matching no partitions! But in this case, that's valid since we can't
have b=9 AND b>30 rows. So, if we did fix this with method #1,
step_id=0 would mark the bound_offsets for all
PartitionBoundInfoData.indexes that map to the DEFAULT partition, and
that would result in the final pruning step mistakenly thinking that
the default needed to be scanned.

In other words, I think #2 is the correct fix. The attached v1-0001
patchifies that method.

Another alternative fix would be to modify the
PARTPRUNE_COMBINE_INTERSECT code to special-case RANGE partitioned
tables to check for any bound_offsets that map to the default
partition and set scan_default=true when it finds some. I don't like
this method as that code is currently unaware of different
partitioning methods and to fix it this way would mean adding a
special case for RANGE partitioned tables.

David

Attachments:

t253192_8
debug_part_prune.diffapplication/octet-stream; name=debug_part_prune.diffDownload+38-0
v1-0001-Fix-issue-with-RANGE-partition-pruning-being-too-.patchapplication/octet-stream; name=v1-0001-Fix-issue-with-RANGE-partition-pruning-being-too-.patchDownload+15-9
#9Tender Wang
tndrwang@gmail.com
In reply to: David Rowley (#8)
Re: Partition pruning can incorrectly exclude a RANGE default partition

David Rowley <dgrowleyml@gmail.com> 于2026年7月30日周四 11:47写道:

(Much of this has been said already, but I wanted to summarise my
independent analysis)

Thanks for the detailed write-up. Your analysis is very thorough, and
I think it accurately captures the root cause of this issue.

I see a couple of ways to fix this:

1. Modify PartitionBoundInfoData to add a new Bitmapset which marks
which of the indexes[] array maps to -1. Then in
get_matching_range_bounds(), once we've set bound_offsets, check if we
have a bms_overlap with the new Bitmapset and set scan_default=true if
there is; or
2. Modify get_matching_range_bounds() so that we mark *all*
bound_offsets for the "if (nvalues == 0)" path.

#1 is more invasive as it requires a new field in
PartitionBoundInfoData, then some extra processing when we execute
each RANGE pruning step. This can happen often for run-time pruning
and this might slow down that enough to notice.

Initially, I tried to pursue approach #1 for the fix. However, as I
dug deeper, I found that it required changes in many places, as you
pointed out.

In other words, I think #2 is the correct fix. The attached v1-0001
patchifies that method.

Another alternative fix would be to modify the
PARTPRUNE_COMBINE_INTERSECT code to special-case RANGE partitioned
tables to check for any bound_offsets that map to the default
partition and set scan_default=true when it finds some. I don't like
this method as that code is currently unaware of different
partitioning methods and to fix it this way would mean adding a
special case for RANGE partitioned tables.

Yes, I agree that #2 is the better approach. It is simpler, more
straightforward, and has less impact on the existing code.

--
Thanks,
Tender Wang

#10David Rowley
dgrowleyml@gmail.com
In reply to: Tender Wang (#9)
Re: Partition pruning can incorrectly exclude a RANGE default partition

On Thu, 30 Jul 2026 at 17:29, Tender Wang <tndrwang@gmail.com> wrote:

David Rowley <dgrowleyml@gmail.com> 于2026年7月30日周四 11:47写道:

In other words, I think #2 is the correct fix. The attached v1-0001
patchifies that method.

Yes, I agree that #2 is the better approach. It is simpler, more
straightforward, and has less impact on the existing code.

I made some test adjustments and pushed.

Thanks for the report, Jacob.

David