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
v1-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patch
Description: Binary data
