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: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Cc: 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-10-23 13:14:05
Message-ID: CAH5HC96OaXibbEbBaPVGTeb6UZyBCsQSWWd5hND+k_uhobTAnQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Oct 23, 2026 at 12:34 PM Dilip Kumar <dilipbalaut(at)gmail(dot)com> wrote:
>
> So IIUC the issue relates to the btree date handling function and not
> to gist? If so we can use a normal btree index in the new test cases
> we have added instead of gist. I haven't yet reviewed the code
> completely but I will do it soon.
>

Thanks for taking a look, Dilip. The issue is specific to GiST.
contrib/btree_gist/btree_date.c is the GiST operator class for date in
the btree_gist extension, not btree code[1]. The failing functions are
its GiST penalty and distance support functions (gbt_date_penalty and
gdb_date_dist). GiST calls them when routing inserts through internal
pages and for KNN searches. A regular btree index on date never calls
these functions. I tested the standard btree date functions with
infinity, -infinity and they pass without the fix.

That's why the test uses a GiST index with enough rows to have
internal pages. On unpatched code, the INSERT of infinite dates fails
in the penalty function, and the KNN query fails in the distance
function. With the patch, both pass.

One question which I'm unclear on : the bug report also noted that the
SQL operator date <-> date (date_dist in btree_date.c) fails on
+/-infinity. In v1, I left date_dist unchanged because its return type
is int4, which has no infinity representation (similar to int4_dist
raising an error when the distance cannot fit in int4). The
alternative is to make date_dist return PG_INT32_MAX for infinite
inputs, similar to how ts_dist saturates for timestamp. Do you think
we should saturate date_dist to PG_INT32_MAX as well, or keep the
error for the SQL operator?

Regards,
Nitin Motiani
Google

[1] https://www.postgresql.org/docs/current/btree-gist.html

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2026-10-23 13:45:42 Re: START_REPLICATION silently truncates an overlong LSN component
Previous Message Nitin Motiani 2026-10-23 12:08:53 Re: BUG #19765: Wrong result from a chain of LEFT JOINs since v16