From 732767f95a1c177c08f4a22f75dbbc89fb6ac4be Mon Sep 17 00:00:00 2001 From: Nitin Motiani Date: Sat, 21 Nov 2026 13:56:53 +0000 Subject: [PATCH v3 3/3] btree_gist: Treat same-sign infinite timestamps as zero distance apart. 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. This changes the result of 'infinity'::timestamp <-> 'infinity'::timestamp (and likewise for -infinity and timestamptz) from infinity to zero. Discussion: https://postgr.es/m/19761-b72cfc453e487610@postgresql.org --- contrib/btree_gist/btree_ts.c | 34 ++++++++++++++------- contrib/btree_gist/expected/timestamp.out | 17 +++++++++++ contrib/btree_gist/expected/timestamptz.out | 17 +++++++++++ contrib/btree_gist/sql/timestamp.sql | 4 +++ contrib/btree_gist/sql/timestamptz.sql | 4 +++ 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/contrib/btree_gist/btree_ts.c b/contrib/btree_gist/btree_ts.c index ad285ae4fc4..21490961862 100644 --- a/contrib/btree_gist/btree_ts.c +++ b/contrib/btree_gist/btree_ts.c @@ -114,8 +114,12 @@ gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo) Interval *i; int64 diff; + /* + * Treat same-sign infinities as equal, as the float8 opclass does, and + * any other infinite input as infinitely far away. + */ if (TIMESTAMP_NOT_FINITE(*aa) || TIMESTAMP_NOT_FINITE(*bb)) - return get_float8_infinity(); + return (*aa == *bb) ? 0.0 : get_float8_infinity(); /* * Some valid timestamps are too far apart for their difference to fit in @@ -157,11 +161,15 @@ ts_dist(PG_FUNCTION_ARGS) if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b)) { - Interval *p = palloc_object(Interval); - - p->day = INT_MAX; - p->month = INT_MAX; - p->time = PG_INT64_MAX; + Interval *p = palloc0_object(Interval); + + /* same-sign infinities are equal, as in gbt_ts_dist */ + if (a != b) + { + p->day = INT_MAX; + p->month = INT_MAX; + p->time = PG_INT64_MAX; + } PG_RETURN_INTERVAL_P(p); } else @@ -181,11 +189,15 @@ tstz_dist(PG_FUNCTION_ARGS) if (TIMESTAMP_NOT_FINITE(a) || TIMESTAMP_NOT_FINITE(b)) { - Interval *p = palloc_object(Interval); - - p->day = INT_MAX; - p->month = INT_MAX; - p->time = PG_INT64_MAX; + Interval *p = palloc0_object(Interval); + + /* same-sign infinities are equal, as in gbt_ts_dist */ + if (a != b) + { + p->day = INT_MAX; + p->month = INT_MAX; + p->time = PG_INT64_MAX; + } PG_RETURN_INTERVAL_P(p); } diff --git a/contrib/btree_gist/expected/timestamp.out b/contrib/btree_gist/expected/timestamp.out index d24062ea9d0..833e4e54bee 100644 --- a/contrib/btree_gist/expected/timestamp.out +++ b/contrib/btree_gist/expected/timestamp.out @@ -108,3 +108,20 @@ SELECT a FROM timestampfar ORDER BY a <-> '294276-12-31 23:59:59' LIMIT 1; Sun Dec 31 23:59:59 294276 (1 row) +-- same-sign infinities are zero distance apart +SELECT a, a <-> 'infinity' FROM timestamptmp ORDER BY a <-> 'infinity' LIMIT 3; + a | ?column? +----------+---------- + infinity | @ 0 + infinity | @ 0 + infinity | @ 0 +(3 rows) + +SELECT a, a <-> '-infinity' FROM timestamptmp ORDER BY a <-> '-infinity' LIMIT 3; + a | ?column? +-----------+---------- + -infinity | @ 0 + -infinity | @ 0 + -infinity | @ 0 +(3 rows) + diff --git a/contrib/btree_gist/expected/timestamptz.out b/contrib/btree_gist/expected/timestamptz.out index 75a15a42568..5ade8c70fbf 100644 --- a/contrib/btree_gist/expected/timestamptz.out +++ b/contrib/btree_gist/expected/timestamptz.out @@ -209,3 +209,20 @@ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> ' Thu Jan 24 12:28:12 2019 PST | @ 37 days 7 hours 28 mins 18 secs (3 rows) +-- same-sign infinities are zero distance apart +SELECT a, a <-> 'infinity' FROM timestamptztmp ORDER BY a <-> 'infinity' LIMIT 3; + a | ?column? +----------+---------- + infinity | @ 0 + infinity | @ 0 + infinity | @ 0 +(3 rows) + +SELECT a, a <-> '-infinity' FROM timestamptztmp ORDER BY a <-> '-infinity' LIMIT 3; + a | ?column? +-----------+---------- + -infinity | @ 0 + -infinity | @ 0 + -infinity | @ 0 +(3 rows) + diff --git a/contrib/btree_gist/sql/timestamp.sql b/contrib/btree_gist/sql/timestamp.sql index f0ffd2531fc..09c60a066ed 100644 --- a/contrib/btree_gist/sql/timestamp.sql +++ b/contrib/btree_gist/sql/timestamp.sql @@ -43,3 +43,7 @@ INSERT INTO timestampfar VALUES ('4714-11-24 00:00:00 BC'), ('294276-12-31 23:59 EXPLAIN (COSTS OFF) SELECT a FROM timestampfar ORDER BY a <-> '294276-12-31 23:59:59' LIMIT 1; SELECT a FROM timestampfar ORDER BY a <-> '294276-12-31 23:59:59' LIMIT 1; + +-- same-sign infinities are zero distance apart +SELECT a, a <-> 'infinity' FROM timestamptmp ORDER BY a <-> 'infinity' LIMIT 3; +SELECT a, a <-> '-infinity' FROM timestamptmp ORDER BY a <-> '-infinity' LIMIT 3; diff --git a/contrib/btree_gist/sql/timestamptz.sql b/contrib/btree_gist/sql/timestamptz.sql index f70caa4a649..9af0cd23261 100644 --- a/contrib/btree_gist/sql/timestamptz.sql +++ b/contrib/btree_gist/sql/timestamptz.sql @@ -78,3 +78,7 @@ SELECT count(*) FROM timestamptztmp WHERE a > '2018-12-18 10:59:54 GMT+4'::time EXPLAIN (COSTS OFF) SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3; SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3; + +-- same-sign infinities are zero distance apart +SELECT a, a <-> 'infinity' FROM timestamptztmp ORDER BY a <-> 'infinity' LIMIT 3; +SELECT a, a <-> '-infinity' FROM timestamptztmp ORDER BY a <-> '-infinity' LIMIT 3; --