More partition pruning bugs with multi-column RANGE partitions

Started by David Rowley3 months ago8 messageshackers
Jump to latest
#1David Rowley
dgrowleyml@gmail.com

There is still a pending bug report in [1]/messages/by-id/CAON2xHO=sqdqp=z8zWkybnWp0AuvefnAi2ez2vOYWrhXB6hHWQ@mail.gmail.com about the DEFAULT partition
still being pruned incorrectly in some cases. This one isn't the same
issue, but I did find this one as a result of looking into Ewan's
report (which I'm still looking at).

This is the reproducer:

create table mc2ap (a int, b int) partition by range (a, b);
create table mc2ap1 partition of mc2ap for values from (1, 4) to (1, 7);
create table mc2ap2 partition of mc2ap for values from (1, 7) to (3, 8);
create table mc2ap3 partition of mc2ap for values from (4, 8) to (6, 9);
create table mc2ap_def partition of mc2ap default;

insert into mc2ap values(1,7);

explain select * from mc2ap where a <= 1;

set enable_partition_pruning=1;
select * from mc2ap where a <= 1; -- 0 rows (!)
set enable_partition_pruning=0;
select * from mc2ap where a <= 1; -- 1 row

mc2ap2 gets pruned by mistake due to an incorrectly coded loop bound.
In this scenario, before the loop, off == 0, so we never perform any
loops to look for other matching bounds. I've moved the condition
check for the loop until after nextoff has been set (according to the
inclusive variable) and breaking out the loop if nextoff is out of
bounds.

There is another similar loop in the BTGreaterStrategyNumber case. I
didn't change that one as I can't find anything wrong with it.

This problem was found using a fuzz testing script I asked Claude Code
to write. See attached.

\i partprune_fuzz.sql
SELECT * FROM pp_fuzz.regression_cases();
CALL pp_fuzz.run(100000); -- about 30 mins of processing
SELECT * FROM pp_fuzz.report();

It'll still find mismatches after applying this attached patch, but
shouldn't if you apply the fix in [1]/messages/by-id/CAON2xHO=sqdqp=z8zWkybnWp0AuvefnAi2ez2vOYWrhXB6hHWQ@mail.gmail.com. If it does, then there are yet
more bugs.

David

[1]: /messages/by-id/CAON2xHO=sqdqp=z8zWkybnWp0AuvefnAi2ez2vOYWrhXB6hHWQ@mail.gmail.com

Attachments:

t253545_1
v1-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchapplication/octet-stream; name=v1-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchDownload+38-3
partprune_fuzz.sqlapplication/octet-stream; name=partprune_fuzz.sqlDownload
#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: David Rowley (#1)
Re: More partition pruning bugs with multi-column RANGE partitions

Hi,

On Tue, 25 Aug 2026 at 16:32, David Rowley <dgrowleyml@gmail.com> wrote:

There is still a pending bug report in [1] about the DEFAULT partition
still being pruned incorrectly in some cases. This one isn't the same
issue, but I did find this one as a result of looking into Ewan's
report (which I'm still looking at).

This is the reproducer:

create table mc2ap (a int, b int) partition by range (a, b);
create table mc2ap1 partition of mc2ap for values from (1, 4) to (1, 7);
create table mc2ap2 partition of mc2ap for values from (1, 7) to (3, 8);
create table mc2ap3 partition of mc2ap for values from (4, 8) to (6, 9);
create table mc2ap_def partition of mc2ap default;

insert into mc2ap values(1,7);

explain select * from mc2ap where a <= 1;

set enable_partition_pruning=1;
select * from mc2ap where a <= 1; -- 0 rows (!)
set enable_partition_pruning=0;
select * from mc2ap where a <= 1; -- 1 row

mc2ap2 gets pruned by mistake due to an incorrectly coded loop bound.
In this scenario, before the loop, off == 0, so we never perform any
loops to look for other matching bounds. I've moved the condition
check for the loop until after nextoff has been set (according to the
inclusive variable) and breaking out the loop if nextoff is out of
bounds.

I tested v1 and it fixes the reported wrong-result case.

Using the same partition layout, I also tried:

EXPLAIN (COSTS OFF) SELECT * FROM mc2ap WHERE a < 1;

Master scans only mc2ap_def, while v1 scans both mc2ap1 and
mc2ap_def. The result is unchanged, but it seems that the new check
rejects nextoff == 0 even though that is a valid index into
boundinfo->datums.

Should the check use the actual array bounds instead?

if (nextoff < 0 || nextoff >= boundinfo->ndatums)

Would the same apply to the similar loop in the
BTGreaterStrategyNumber case?

Regards,
Ayush

#3Tender Wang
tndrwang@gmail.com
In reply to: Ayush Tiwari (#2)
Re: More partition pruning bugs with multi-column RANGE partitions

Ayush Tiwari <ayushtiwari.slg01@gmail.com> 于2026年8月25日周二 22:24写道:

I tested v1 and it fixes the reported wrong-result case.

Using the same partition layout, I also tried:

EXPLAIN (COSTS OFF) SELECT * FROM mc2ap WHERE a < 1;

Master scans only mc2ap_def, while v1 scans both mc2ap1 and
mc2ap_def. The result is unchanged, but it seems that the new check
rejects nextoff == 0 even though that is a valid index into
boundinfo->datums.

Should the check use the actual array bounds instead?

if (nextoff < 0 || nextoff >= boundinfo->ndatums)

Would the same apply to the similar loop in the
BTGreaterStrategyNumber case?

Hi,

I tested the similar loop in the BTGreaterStrategyNumber case and
confirmed that it has the same issue. Here is a small example:

```
CREATE TABLE r2 (a int, b int) PARTITION BY RANGE (a, b);

CREATE TABLE r2_p1 PARTITION OF r2
FOR VALUES FROM (1, 10) TO (1, 20);

CREATE TABLE r2_def PARTITION OF r2 DEFAULT;

EXPLAIN (COSTS OFF)
SELECT * FROM r2 WHERE a > 1;
```

With v1, both `r2_p1` and `r2_def` are scanned:

```
Append
-> Seq Scan on r2_p1 r2_1
Filter: (a > 1)
-> Seq Scan on r2_def r2_2
Filter: (a > 1)
```

`r2_p1` cannot contain any rows satisfying `a > 1`, so it should be
pruned. In this case, the two range bounds `(1, 10)` and `(1, 20)`
both compare equal to the lookup prefix `{1}`. Walking to
`nextoff == boundinfo->ndatums - 1` is therefore necessary to find
the appropriate edge of the matching bounds.

So I agree that the bounds check should be against the actual
`boundinfo->datums` array bounds, and that the same change is needed
for the similar loop in the BTGreaterStrategyNumber case.

I wrote the code to cover >= cases, as in the attached v2 (including
David's v1 code), and added the test case.

While looking at this, I also found the comment above the
BTLessStrategyNumber case a little misleading for prefix lookups:

```
/*
* Look for the greatest bound that is < or <= lookup value and
* set maxoff to its offset.
*/
```

When `nvalues < partnatts`, multiple range bounds can compare equal
to the lookup prefix, and `partition_range_datum_bsearch()` may
return one of those matching bounds rather than the greatest one.
The following loop then walks the adjacent bounds to find the
appropriate edge of that group.

Perhaps the comment could mention this distinction. For example,
something along the lines of:

```
/*
* Locate a bound at or below the lookup value. If the lookup
* contains only a prefix of the partition keys, multiple bounds
* may compare equal to it, so adjust off below to find the
* appropriate edge of the matching bounds.
*/
```

Similar wording might also be useful for the corresponding
BTGreaterStrategyNumber case.
The comment adjustments are not in the patch. I'm not sure there is a
need to adjust it.

--
Thanks,
Tender Wang

Attachments:

t253545_3
v2-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchtext/plain; charset=US-ASCII; name=v2-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchDownload+79-4
#4David Rowley
dgrowleyml@gmail.com
In reply to: Ayush Tiwari (#2)
Re: More partition pruning bugs with multi-column RANGE partitions

On Wed, 26 Aug 2026 at 02:24, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Using the same partition layout, I also tried:

EXPLAIN (COSTS OFF) SELECT * FROM mc2ap WHERE a < 1;

Master scans only mc2ap_def, while v1 scans both mc2ap1 and
mc2ap_def. The result is unchanged, but it seems that the new check
rejects nextoff == 0 even though that is a valid index into
boundinfo->datums.

I can't recreate this. Are you sure you tested with partition pruning enabled?

Should the check use the actual array bounds instead?

if (nextoff < 0 || nextoff >= boundinfo->ndatums)

I modified the fuzzer tool to record the plans of each query tested,
and it seems that not visiting the end elements could result in
partitions *not* being pruned that could be pruned, so making that the
condition seems correct.

Would the same apply to the similar loop in the
BTGreaterStrategyNumber case?

The fuzzer highlighted that that's the case with this one too. More
partitions could be pruned that are not pruned today.

David

#5David Rowley
dgrowleyml@gmail.com
In reply to: David Rowley (#4)
Re: More partition pruning bugs with multi-column RANGE partitions

On Wed, 26 Aug 2026 at 23:09, David Rowley <dgrowleyml@gmail.com> wrote:

On Wed, 26 Aug 2026 at 02:24, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Should the check use the actual array bounds instead?

if (nextoff < 0 || nextoff >= boundinfo->ndatums)

I modified the fuzzer tool to record the plans of each query tested,
and it seems that not visiting the end elements could result in
partitions *not* being pruned that could be pruned, so making that the
condition seems correct.

Would the same apply to the similar loop in the
BTGreaterStrategyNumber case?

The fuzzer highlighted that that's the case with this one too. More
partitions could be pruned that are not pruned today.

I've attached the updated patch and the updated fuzzer tool, which has
some hand-modified adjustments made to record the plans.

I did the following:

truncate pp_fuzz.debug_output;
CALL pp_fuzz.run(iterations := 10000, seed := 12341);
\o somefile.txt
select line from pp_fuzz.debug_output order by id;

Then made the code adjustments and ran the same again into another
output file and compared with my diff tool. The first difference it
highlighted was a greater-than query that didn't prune one partition
that it could have. I included that as a regression test.

David

Attachments:

t253545_5
partprune_fuzz.sqlapplication/octet-stream; name=partprune_fuzz.sqlDownload
v2-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchapplication/octet-stream; name=v2-0001-Fix-incorrect-multi-column-RANGE-partition-prunin.patchDownload+64-4
#6Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: David Rowley (#5)
Re: More partition pruning bugs with multi-column RANGE partitions

Hi,

On Wed, 26 Aug 2026 at 18:11, David Rowley <dgrowleyml@gmail.com> wrote:

On Wed, 26 Aug 2026 at 23:09, David Rowley <dgrowleyml@gmail.com> wrote:

On Wed, 26 Aug 2026 at 02:24, Ayush Tiwari <ayushtiwari.slg01@gmail.com>

wrote:

Should the check use the actual array bounds instead?

if (nextoff < 0 || nextoff >= boundinfo->ndatums)

I modified the fuzzer tool to record the plans of each query tested,
and it seems that not visiting the end elements could result in
partitions *not* being pruned that could be pruned, so making that the
condition seems correct.

Would the same apply to the similar loop in the
BTGreaterStrategyNumber case?

The fuzzer highlighted that that's the case with this one too. More
partitions could be pruned that are not pruned today.

Sorry, you're right. I had used a slightly different partition layout
for that test and mistakenly said it was the same layout.

I've attached the updated patch and the updated fuzzer tool, which has

some hand-modified adjustments made to record the plans.

I did the following:

truncate pp_fuzz.debug_output;
CALL pp_fuzz.run(iterations := 10000, seed := 12341);
\o somefile.txt
select line from pp_fuzz.debug_output order by id;

Then made the code adjustments and ran the same again into another
output file and compared with my diff tool. The first difference it
highlighted was a greater-than query that didn't prune one partition
that it could have. I included that as a regression test.

Thanks for updating both loops. The v2 patch looks good to me.

Regards,
Ayush

#7Tender Wang
tndrwang@gmail.com
In reply to: David Rowley (#5)
Re: More partition pruning bugs with multi-column RANGE partitions

David Rowley <dgrowleyml@gmail.com> 于2026年8月26日周三 20:41写道:

Then made the code adjustments and ran the same again into another
output file and compared with my diff tool. The first difference it
highlighted was a greater-than query that didn't prune one partition
that it could have. I included that as a regression test.

The v2 LGTM.

--
Thanks,
Tender Wang

#8David Rowley
dgrowleyml@gmail.com
In reply to: Ayush Tiwari (#6)
Re: More partition pruning bugs with multi-column RANGE partitions

On Thu, 27 Aug 2026 at 01:15, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:

Thanks for updating both loops. The v2 patch looks good to me.

Thanks for looking. Fix pushed.

David