Re: BUG #19761: btree_gist: "cannot subtract infinite dates" when inserting 'infinity' into a GiST index on date

From: Nitin Motiani <nitinmotiani(at)google(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Dilip Kumar <dilipbalaut(at)gmail(dot)com>, alpkeles99(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19761: btree_gist: "cannot subtract infinite dates" when inserting 'infinity' into a GiST index on date
Date: 2026-11-22 17:31:32
Message-ID: CAH5HC94oNQqoQ3i682oLUM7pdBmfhhRNH9c5kpmM92Kxs91zuA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Tom,

Apologies for the delay on this. I am attaching the new patches. I'm
attaching multiple patch files which can be squashed together if
needed.

On Mon, Oct 26, 2026 at 9:40 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> I think this is not there yet. An important behavior of the
> corresponding code for float8 (float_penalty_num_impl()) is that
> it makes the distance between two same-sign infinity values be zero,
> not infinity. This is mathematically wrong but we must do it for
> indexing purposes, else inserting multiple infinities will end up
> splitting each infinity onto its own index page when we want all
> identical infinities to pack onto as few index pages as possible.
>

Made the changes for the same signed infinity distances to be 0. The
penalty function was already fine so I just updated the distance
functions we were changing to handle the infinity. These are in the
0001 patch file.

> BTW, while checking this I happened to notice that
> gbt_float8_dist has a variant of the same problem:
>
> r = arg1 - arg2;
> if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
> float_overflow_error();
>
> I don't think we want an error thrown there either;
> returning the infinite distance (or DBL_MAX?) seems better.
>

Created the patch 0002 to handle this in gbt_float8_dist. I went with
returning infinity instead of DBL_MAX as that might collide with a
case where the actual distance is DBL_MAX. I considered changing
float8_dist also but its behaviour is the same as int and float4 so I
didn't change that.

I also noticed that the timestamp type has the same issue. So I added
the following to handle the overflow case.

return fabs(((float8) *aa - (float8) *bb) / USECS_PER_SEC);

I am not sure if we should just change the existing logic to use the
above in all the cases. Let me know what you think of that.

Like the date type, the distance functions for the timestamp also
return infinity for two same-signed infinities. I changed that also
but since it alters existing results, I'm unsure if it is safe to
merge. Therefore I'm sending it as a separate patch 0003. Please take
a look.

Thanks & Regards,
Nitin Motiani
Google

Attachment Content-Type Size
v3-0001-btree_gist-Fix-cannot-subtract-infinite-dates-err.patch application/octet-stream 9.2 KB
v3-0003-btree_gist-Treat-same-sign-infinite-timestamps-as.patch application/octet-stream 5.6 KB
v3-0002-btree_gist-Don-t-throw-overflow-errors-from-KNN-d.patch application/octet-stream 7.3 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Alexander Lakhin 2026-11-22 18:00:00 Re: BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state
Previous Message Srinath Reddy Sadipiralla 2026-11-22 17:27:58 Re: BUG #19897: Recovery fails after "LOG: consistent recovery state reached"