[PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9)

Started by Ewan Young5 days ago4 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Ewan Young
kdbase.hack@gmail.com

Hi,

15b3dc140d9 fixed overflow in btree_gist's date/float8/timestamp penalty
and distance functions, but the numeric opclass has the same problem.
gbt_numeric_penalty() subtracts the key bounds with numeric_sub(), which
overflows for valid-but-extreme values once the index has internal pages:

create table t (a numeric);
insert into t select g from generate_series(1, 1000) g;
create index on t using gist (a);
insert into t values (('9' || repeat('9', 131071))::numeric);
insert into t values (('-9' || repeat('9', 131071))::numeric);
ERROR: value overflows numeric format

The values are fine; only the width of their union isn't.

Attached uses numeric_sub_safe() and, when a width overflows, treats it as
unmeasurable and takes the branch gbt_numeric_penalty() already uses for a
NaN width -- a penalty function shouldn't fail a valid insert, and a NaN
width (a storable value) already lands there without error. The union
contains the original key, so os can only overflow when us does; the
ds = us - os subtraction is then on finite values.

--
Regards,
Ewan Young

Attachments:

t259715_1
v1-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patchapplication/octet-stream; name=v1-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patchDownload+60-15
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ewan Young (#1)
Re: [PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9)

Ewan Young <kdbase.hack@gmail.com> writes:

15b3dc140d9 fixed overflow in btree_gist's date/float8/timestamp penalty
and distance functions, but the numeric opclass has the same problem.

Yeah, we should fix this, but your v1 doesn't feel quite right to me.

1. Wouldn't it make more sense to treat an overflowed subtraction
result as Infinity rather than NaN? That's what happens in
gbt_float8_dist() since 15b3dc140d9.

2. I'm unconvinced that the ds calculation is entirely safe yet.
In particular, it seems we could be computing Inf - Inf there,
which will yield NaN when we'd rather get zero.

regards, tom lane

#3Ewan Young
kdbase.hack@gmail.com
In reply to: Tom Lane (#2)
Re: [PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9)

On Fri, Nov 27, 2026 at 5:43 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Ewan Young <kdbase.hack@gmail.com> writes:

15b3dc140d9 fixed overflow in btree_gist's date/float8/timestamp penalty
and distance functions, but the numeric opclass has the same problem.

Yeah, we should fix this, but your v1 doesn't feel quite right to me.

1. Wouldn't it make more sense to treat an overflowed subtraction
result as Infinity rather than NaN? That's what happens in
gbt_float8_dist() since 15b3dc140d9.

Agreed, done -- an overflowed width is now treated as infinite.

2. I'm unconvinced that the ds calculation is entirely safe yet.
In particular, it seems we could be computing Inf - Inf there,
which will yield NaN when we'd rather get zero.

Right. v2 attached, which handles a NaN or infinite width (whether
from an overflow or
an actual infinite bound) up front, so ds = us - os only runs when us is
finite.

regards, tom lane

--
Regards,
Ewan Young

Attachments:

t259715_3
v2-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patchapplication/octet-stream; name=v2-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patchDownload+72-15
#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ewan Young (#3)
Re: [PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9)

Ewan Young <kdbase.hack@gmail.com> writes:

Right. v2 attached, which handles a NaN or infinite width (whether
from an overflow or
an actual infinite bound) up front, so ds = us - os only runs when us is
finite.

Pushed with some cosmetic adjustments, mostly revising the comments.

regards, tom lane