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

From: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Nitin Motiani <nitinmotiani(at)google(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: [PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9)
Date: 2026-11-24 08:54:33
Message-ID: CAON2xHN4hdndi-b3H=X-wRyRVbLzcC2ABMxFoQLnuBa72zqZRg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v1-0001-btree_gist-avoid-numeric-overflow-in-gbt_numeric_penalty.patch application/octet-stream 6.1 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2026-11-24 08:56:00 Re: Use local buffer for packed numerics
Previous Message shveta malik 2026-11-24 08:52:24 Re: [PATCH] Report changes discarded for relations not in the subscription