Partition pruning can incorrectly exclude a RANGE default partition

Поиск
Список
Период
Сортировка
Искать

Partition pruning can incorrectly exclude a RANGE default partition

От:
Jacob 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

Re: Partition pruning can incorrectly exclude a RANGE default partition

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Chengpeng Yan  于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


Re: Partition pruning can incorrectly exclude a RANGE default partition

От:
Tender Wang <tndrwang@gmail.com>
Дата:
Chengpeng Yan  于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


FAQ