pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix overflow problems in btree_gist's penalty and distance funct
Date: Mon, 23 Nov 2026 22:54:17 +0000
Message-ID: <E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org> (raw)

Fix overflow problems in btree_gist's penalty and distance functions.

The date opclass's penalty and distance functions used date_mi(),
which rejects infinite dates.  Once a GiST index on a date column had
internal pages, inserting 'infinity' or '-infinity' failed, as did any
later insert or KNN search on an index already containing one.  The
date <-> date operator failed the same way, so ORDER BY d <-> x could
succeed with a KNN index scan but fail with a sort.

Compute the penalty with penalty_num(), as the int4 and timestamp
opclasses do.  In the KNN distance function and the <-> operator,
treat same-sign infinities as equal, as the float8 opclass does, and
any other infinite input as infinitely far away: return infinity from
the former and INT_MAX from the latter.  No difference of two finite
dates reaches INT_MAX, so both functions order rows the same way.

gbt_float8_dist() raised an error when the difference of two finite
values overflowed, and gbt_ts_dist() did the same via timestamp_mi()
for valid timestamps too far apart for their difference to fit in an
interval.  A KNN search computes these distances for every index entry
it visits, not just the ones it returns, so a single extreme value in
the index could make such searches fail.

In gbt_float8_dist(), we need only return the infinite distance that
the IEEE subtraction already produces.  In gbt_ts_dist(), compute the
distance in double when the int64 difference overflows.  The <->
operators themselves can still report overflow, as they do for the
integer and float4 opclasses.

gbt_ts_dist(), ts_dist() and tstz_dist() treated two equal infinities
as infinitely far apart.  Treat them as equal instead, as the float8
and date opclasses do, so that indexes containing multiple such
entries won't bloat.  This changes the result of 'infinity'::timestamp
<-> 'infinity'::timestamp (and likewise for -infinity and for
timestamptz) from infinity to zero.  It seems unlikely that anyone
is actually storing such values in btree_gist indexes, but if they
are, they'll likely prefer this behavior anyway, especially since
it now matches the behavior of other btree_gist opclasses.

Bug: #19761
Reported-by: Alperen Keles <alpkeles99@gmail.com>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19761-b72cfc453e487610@postgresql.org
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/210e6c983e87961a7a3bdbae8ebb5055f260a862

Modified Files
--------------
contrib/btree_gist/btree_date.c             | 73 ++++++++++++------------
contrib/btree_gist/btree_float8.c           |  8 ++-
contrib/btree_gist/btree_ts.c               | 53 ++++++++++++++---
contrib/btree_gist/expected/date.out        | 88 +++++++++++++++++++++++++++++
contrib/btree_gist/expected/float8.out      | 19 +++++++
contrib/btree_gist/expected/timestamp.out   | 36 ++++++++++++
contrib/btree_gist/expected/timestamptz.out | 17 ++++++
contrib/btree_gist/sql/date.sql             | 27 +++++++++
contrib/btree_gist/sql/float8.sql           |  8 +++
contrib/btree_gist/sql/timestamp.sql        | 11 ++++
contrib/btree_gist/sql/timestamptz.sql      |  4 ++
11 files changed, 298 insertions(+), 46 deletions(-)



view thread (6+ messages)  latest in thread

Message-ID: <E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org>
Permalink:  ../E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix overflow problems in btree_gist's penalty and distance funct
  In-Reply-To: <E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org>
  References: <E1xVcvQ-000000004bI-42X5@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox