Re: btree_gist: <-> fails on same-sign infinite intervals

From: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
To: shihao zhong <zhong950419(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Kirill Reshke <reshkekirill(at)gmail(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: btree_gist: <-> fails on same-sign infinite intervals
Date: 2026-12-05 18:11:38
Message-ID: CAPHduZBhTY2Tfe7yqJ0Xh0QaXmy5O=sqrqPgDNL2VkqSFCoQWQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sat, 5 Dec 2026, shihao zhong wrote:
> v2 still has one ordering problem. From '0', '-2147483648 mons' gets
> infinity but the farther '-2147483647 mons -2147483647 days' gets a
> finite distance. So the index and a sort disagree on which is nearest.

Thanks, I see the same on master (91d71a71cb) + Tom's 0002 + v2: the two
distances are infinity and '178956970 years 7 mons 2147483647 days', and
on a small table a GiST scan and a seqscan sort return them in a
different order. Kirill's (months => -2147483648, days => 30) case is
the same problem. v2 spread the span only after a field overflowed, but
the plain field-wise subtraction can already give a larger result
(months near INT32_MAX plus days), so the spread has to go that far
too. My mistake.

> Attached is v3 of Raja's patch with a fix for that.

With v3 both cases are finite and in order, and index and sort agree.

I also compared every <-> result with the exact span (int128,
month = 30 days) over all pairs of 256 intervals built from months
{0, +-1, +-1e9, +-2147483647, -2147483648}, days {0, +-1, +-30,
+-2147483647, -2147483648}, time {0, +-INT64_MAX, INT64_MIN}, plus three
random sets of ~500 values biased to the field limits:

v2: 1738 pairs ordered wrongly; for 173 of 256 probes a GiST scan
returns infinity before a finite distance.
v3: none; every finite result equals the exact span, infinity only
for spans at or above the largest finite interval, and the GiST
order matches <-> for every probe.

btree_gist check passes with v3, its new "monotonic" test fails with
the v2 code, and pgindent --check is clean.

v3 uses the same int128.h functions as v2 (int128_sub_int64,
int128_sub_int64_mul_int64, int128_div_mod_int32, none of which are in
REL_17/18), so the back-patch question is unchanged.

Not tested: back branches, a build without native int128.

Small thing: the commit message is still my v2 text; it could mention
the spill into the day and time fields and credit Shihao.

Regards,
Sai Pranav

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Pavan 2026-12-05 18:32:11 Re: BUG #19790: "cannot start a subtransaction ... inval messages" after "ALTER DOMAIN ... DROP CONSTRAINT"
Previous Message shihao zhong 2026-12-05 17:29:42 Re: BUG #19954: pg_upgrade fails to handle foreign data wrapper and event trigger when their owner demoted