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 <[email protected]> Author: Nitin Motiani <[email protected]> Reviewed-by: Dilip Kumar <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] 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(-)
