pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix overflow problems in btree_gist's penalty and distance funct
6+ messages / 1 participants
[nested] [flat]

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/15b3dc140d9109e37082454bc5041a9e6bddbd8e

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/492bc90572bbbff21ff5770213e3b3e795c32618

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6b90b651a7bc208db2e2793fe15be68b3b4b30cc

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/f572f1a11a4902d9e12140e95107d60c0ec17cc5

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* pgsql: Fix overflow problems in btree_gist's penalty and distance funct
@ 2026-11-23 22:54  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2026-11-23 22:54 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

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_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/d4d8e1159a7f0067e06ef030ea15bd070ee4a00d

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(-)



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-11-23 22:54 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>
2026-11-23 22:54 pgsql: Fix overflow problems in btree_gist's penalty and distance funct Tom Lane <tgl@sss.pgh.pa.us>

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