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

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Raja Sai pranav <rajasaipranav0(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: btree_gist: <-> fails on same-sign infinite intervals
Date: 2026-12-03 18:16:23
Message-ID: CALdSSPha=Bt8K+LfZ=YyjHWH9mye+SAPx8E1+uUthn+BNN4aog@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, 3 Dec 2026 at 09:59, Raja Sai pranav <rajasaipranav0(at)gmail(dot)com> wrote:
>
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
> > I think your fix for interval_dist() is good,
> > but I believe that Kirill has identified an independent problem:
> > intr2num() and its callers haven't been modernized to handle
> > infinite intervals. I don't like the details of what he did though.
> > I prefer something more like the attached.
>
> Agreed, both are needed. I tested on master (f68727292a), cassert build,
> comparing index-scan vs seqscan ORDER BY v <-> probe LIMIT 20 on a 10k-row
> table with +-infinity and near-overflow values:
>
> master: 4 of 10 probes disagree
> my v1 only: 2 (the +-178000000 years probes; -infinity rows
> ranked near them, which your intr2num() change fixes)
> your 0002 only: 2 (the +-2147483647 hours probes; Kirill's case still
> gives infinity / wrong row)
> both: 0
>
> The two patches apply in either order with offsets only. btree_gist
> check passes for each, main regress passes with both. No NaN seen with
> infinite keys.
>
> One gap left in my v1: '-2147483647 hours' <-> '2147483647 hours' is
> about 497102 years, but v1 returns infinity because the span doesn't fit
> in the time field alone, so a KNN scan and a sort still disagree on a
> table holding '178000000 years'. The attached v2, on top of your 0002,
> spreads such a span over the month and day fields and returns infinity
> only when that doesn't fit. It also adds KNN tests for this case and for
> -infinity (your 0002 has none); each fails if the matching code is
> reverted.
>
> Regards,
> Sai Pranav

Looks like both patches are needed. However with v2 applied there is this case:

SELECT make_interval(months => 0)
<-> make_interval(months => -2147483648, days => 30);

return inf while answer in 2147483647 months

We need to allow PG_INT32_MAX months as attached

--
Best regards,
Kirill Reshke

Attachment Content-Type Size
0001-Allow-the-largest-finite-interval-distance.diff application/octet-stream 2.0 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-12-03 18:24:43 Re: BUG #19965: btree_gist index on timetz returns too few rows for <, > and <>
Previous Message Andrey Rachitskiy 2026-12-03 18:13:05 Re: BUG #19956: json #>, #>> and json_extract_path resolve a negative array index against an earlier sibling array