postgres.git / summary / log / commit / refs
commit 210e6c983e87961a7a3bdbae8ebb5055f260a862
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon Nov 23 22:53:56 2026 +0000
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
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(-)
diff --git a/contrib/btree_gist/btree_date.c b/contrib/btree_gist/btree_date.c
index 68a4107dbf0..397bdfc1d0c 100644
--- a/contrib/btree_gist/btree_date.c
+++ b/contrib/btree_gist/btree_date.c
@@ -90,12 +90,18 @@ gbt_datekey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
static float8
gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
{
- /* we assume the difference can't overflow */
- Datum diff = DirectFunctionCall2(date_mi,
- DateADTGetDatum(*((const DateADT *) a)),
- DateADTGetDatum(*((const DateADT *) b)));
-
- return (float8) abs(DatumGetInt32(diff));
+ DateADT da = *((const DateADT *) a);
+ DateADT db = *((const DateADT *) b);
+
+ /*
+ * Treat same-sign infinities as equal, as the float8 opclass does; but
+ * any other case involving infinite inputs yields infinite distance.
+ */
+ if (DATE_NOT_FINITE(da) || DATE_NOT_FINITE(db))
+ return (da == db) ? 0.0 : get_float8_infinity();
+
+ /* the difference of two finite dates cannot overflow int32 */
+ return (float8) abs(da - db);
}
@@ -118,12 +124,20 @@ PG_FUNCTION_INFO_V1(date_dist);
Datum
date_dist(PG_FUNCTION_ARGS)
{
- /* we assume the difference can't overflow */
- Datum diff = DirectFunctionCall2(date_mi,
- PG_GETARG_DATUM(0),
- PG_GETARG_DATUM(1));
-
- PG_RETURN_INT32(abs(DatumGetInt32(diff)));
+ DateADT a = PG_GETARG_DATEADT(0);
+ DateADT b = PG_GETARG_DATEADT(1);
+
+ /*
+ * As in gdb_date_dist, same-sign infinities are equal. For other cases
+ * involving infinite inputs, int4 has no infinity, so use PG_INT32_MAX.
+ * This is consistent with the ordering produced by gdb_date_dist, since
+ * no difference of two finite dates reaches PG_INT32_MAX.
+ */
+ if (DATE_NOT_FINITE(a) || DATE_NOT_FINITE(b))
+ PG_RETURN_INT32((a == b) ? 0 : PG_INT32_MAX);
+
+ /* the difference of two finite dates cannot overflow int32 */
+ PG_RETURN_INT32(abs(a - b));
}
@@ -208,32 +222,17 @@ gbt_date_penalty(PG_FUNCTION_ARGS)
dateKEY *origentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
dateKEY *newentry = (dateKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
float *result = (float *) PG_GETARG_POINTER(2);
- int32 diff,
- res;
-
- diff = DatumGetInt32(DirectFunctionCall2(date_mi,
- DateADTGetDatum(newentry->upper),
- DateADTGetDatum(origentry->upper)));
-
- res = Max(diff, 0);
-
- diff = DatumGetInt32(DirectFunctionCall2(date_mi,
- DateADTGetDatum(origentry->lower),
- DateADTGetDatum(newentry->lower)));
-
- res += Max(diff, 0);
-
- *result = 0.0;
- if (res > 0)
- {
- diff = DatumGetInt32(DirectFunctionCall2(date_mi,
- DateADTGetDatum(origentry->upper),
- DateADTGetDatum(origentry->lower)));
- *result += FLT_MIN;
- *result += (float) (res / ((double) (res + diff)));
- *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
- }
+ /*
+ * Don't use date_mi here: it rejects infinite dates. Plain int32
+ * subtraction won't do either: it could overflow for cases involving
+ * infinite dates. But we can use penalty_num: it computes in double,
+ * where every int32 and every difference of two int32s is exact. The
+ * output for cases with unequal infinite dates will be finite but large,
+ * which seems fine for penalty calculations.
+ */
+ penalty_num(result, origentry->lower, origentry->upper,
+ newentry->lower, newentry->upper);
PG_RETURN_POINTER(result);
}
diff --git a/contrib/btree_gist/btree_float8.c b/contrib/btree_gist/btree_float8.c
index f740d8ecd85..ee58052e997 100644
--- a/contrib/btree_gist/btree_float8.c
+++ b/contrib/btree_gist/btree_float8.c
@@ -78,9 +78,13 @@ gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
float8 arg2 = *(const float8 *) b;
float8 r;
+ /*
+ * If the difference of two finite values overflows, this subtraction
+ * yields an infinite distance. Returning that is fine, while throwing an
+ * error is not. However, Inf - Inf will yield NaN, while we need to
+ * report zero distance for that case.
+ */
r = arg1 - arg2;
- if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
- float_overflow_error();
if (unlikely(isnan(r)))
{
if (isnan(arg1) && isnan(arg2))
diff --git a/contrib/btree_gist/btree_ts.c b/contrib/btree_gist/btree_ts.c
index 3f5ba91891d..bcdf37e1d29 100644
--- a/contrib/btree_gist/btree_ts.c
+++ b/contrib/btree_gist/btree_ts.c
@@ -7,6 +7,7 @@
#include "btree_gist.h"
#include "btree_utils_num.h"
+#include "common/int.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/float.h"
@@ -116,10 +117,26 @@ gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
const Timestamp *aa = (const Timestamp *) a;
const Timestamp *bb = (const Timestamp *) b;
Interval *i;
+ int64 diff;
+ /*
+ * Treat same-sign infinities as equal, as the float8 opclass does; but
+ * any other case involving infinite inputs yields infinite distance.
+ */
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
+ * an interval. Avoid throwing an error in that case, just do the
+ * computation ourselves. (This is not exactly comparable to what
+ * timestamp_mi does, since we don't apply interval_justify_hours, but it
+ * should be close enough.)
+ */
+ if (pg_sub_s64_overflow(*aa, *bb, &diff))
+ return fabs(((float8) *aa - (float8) *bb) / USECS_PER_SEC);
+ /* Otherwise we use timestamp_mi and convert its output to seconds */
i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
TimestampGetDatumFast(*aa),
TimestampGetDatumFast(*bb)));
@@ -154,9 +171,20 @@ ts_dist(PG_FUNCTION_ARGS)
{
Interval *p = palloc(sizeof(Interval));
- p->day = INT_MAX;
- p->month = INT_MAX;
- p->time = PG_INT64_MAX;
+ if (a == b)
+ {
+ /* same-sign infinities are equal, as in gbt_ts_dist */
+ p->day = 0;
+ p->month = 0;
+ p->time = 0;
+ }
+ else
+ {
+ /* for all other cases, report interval +Infinity */
+ p->day = INT_MAX;
+ p->month = INT_MAX;
+ p->time = PG_INT64_MAX;
+ }
PG_RETURN_INTERVAL_P(p);
}
else
@@ -178,9 +206,20 @@ tstz_dist(PG_FUNCTION_ARGS)
{
Interval *p = palloc(sizeof(Interval));
- p->day = INT_MAX;
- p->month = INT_MAX;
- p->time = PG_INT64_MAX;
+ if (a == b)
+ {
+ /* same-sign infinities are equal, as in gbt_ts_dist */
+ p->day = 0;
+ p->month = 0;
+ p->time = 0;
+ }
+ else
+ {
+ /* for all other cases, report interval +Infinity */
+ 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/date.out b/contrib/btree_gist/expected/date.out
index 5db864bb82c..f1fed43e178 100644
--- a/contrib/btree_gist/expected/date.out
+++ b/contrib/btree_gist/expected/date.out
@@ -89,3 +89,91 @@ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
03-24-2001 | 39
(3 rows)
+-- infinite dates in an index with internal pages (penalty and KNN distance)
+CREATE TABLE dateinf (a date);
+CREATE INDEX dateinfidx ON dateinf USING gist (a);
+INSERT INTO dateinf SELECT '2000-01-01'::date + i FROM generate_series(1, 1000) i;
+INSERT INTO dateinf SELECT 'infinity' FROM generate_series(1, 3);
+INSERT INTO dateinf SELECT '-infinity' FROM generate_series(1, 3);
+EXPLAIN (COSTS OFF)
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+ QUERY PLAN
+---------------------------------------------------
+ Limit
+ -> Index Only Scan using dateinfidx on dateinf
+ Order By: (a <-> '01-01-2000'::date)
+(3 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+ a
+------------
+ 01-02-2000
+ 01-03-2000
+ 01-04-2000
+(3 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> 'infinity'::date LIMIT 3;
+ a
+----------
+ infinity
+ infinity
+ infinity
+(3 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> '-infinity'::date LIMIT 3;
+ a
+-----------
+ -infinity
+ -infinity
+ -infinity
+(3 rows)
+
+-- same orderings via sort, which evaluates the <-> operator on every row
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_indexonlyscan = off;
+EXPLAIN (COSTS OFF)
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+ QUERY PLAN
+------------------------------------------------
+ Limit
+ -> Sort
+ Sort Key: ((a <-> '01-01-2000'::date))
+ -> Seq Scan on dateinf
+(4 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+ a
+------------
+ 01-02-2000
+ 01-03-2000
+ 01-04-2000
+(3 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> 'infinity'::date LIMIT 3;
+ a
+----------
+ infinity
+ infinity
+ infinity
+(3 rows)
+
+SELECT a FROM dateinf ORDER BY a <-> '-infinity'::date LIMIT 3;
+ a
+-----------
+ -infinity
+ -infinity
+ -infinity
+(3 rows)
+
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_indexonlyscan;
+-- and check these distance results directly
+SELECT 'infinity'::date <-> 'infinity'::date, '-infinity'::date <-> '-infinity'::date,
+ 'infinity'::date <-> '-infinity'::date, 'infinity'::date <-> '2000-01-01'::date;
+ ?column? | ?column? | ?column? | ?column?
+----------+----------+------------+------------
+ 0 | 0 | 2147483647 | 2147483647
+(1 row)
+
diff --git a/contrib/btree_gist/expected/float8.out b/contrib/btree_gist/expected/float8.out
index 194bd210ac6..45f832a8877 100644
--- a/contrib/btree_gist/expected/float8.out
+++ b/contrib/btree_gist/expected/float8.out
@@ -124,3 +124,22 @@ SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
RESET enable_seqscan;
RESET enable_indexscan;
RESET enable_bitmapscan;
+-- finite values whose difference overflows float8 (KNN distance must not error)
+CREATE TABLE float8far (a float8);
+CREATE INDEX float8faridx ON float8far USING gist (a);
+INSERT INTO float8far VALUES (1e308), (-1e308);
+EXPLAIN (COSTS OFF)
+SELECT a FROM float8far ORDER BY a <-> 1e308::float8 LIMIT 1;
+ QUERY PLAN
+-------------------------------------------------------
+ Limit
+ -> Index Only Scan using float8faridx on float8far
+ Order By: (a <-> '1e+308'::double precision)
+(3 rows)
+
+SELECT a FROM float8far ORDER BY a <-> 1e308::float8 LIMIT 1;
+ a
+--------
+ 1e+308
+(1 row)
+
diff --git a/contrib/btree_gist/expected/timestamp.out b/contrib/btree_gist/expected/timestamp.out
index 0d94f2f245c..968280a254f 100644
--- a/contrib/btree_gist/expected/timestamp.out
+++ b/contrib/btree_gist/expected/timestamp.out
@@ -89,3 +89,39 @@ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-
Mon Nov 29 20:12:43 2004 | @ 34 days 11 hours 17 mins 35 secs
(3 rows)
+-- 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)
+
+-- finite timestamps too far apart to subtract (KNN distance must not error)
+CREATE TABLE timestampfar (a timestamp);
+CREATE INDEX timestampfaridx ON timestampfar USING gist (a);
+INSERT INTO timestampfar VALUES ('4714-11-24 00:00:00 BC'), ('294276-12-31 23:59:59');
+EXPLAIN (COSTS OFF)
+SELECT a FROM timestampfar ORDER BY a <-> '294276-12-31 23:59:59' LIMIT 1;
+ QUERY PLAN
+-------------------------------------------------------------------------------------
+ Limit
+ -> Index Only Scan using timestampfaridx on timestampfar
+ Order By: (a <-> 'Sun Dec 31 23:59:59 294276'::timestamp without time zone)
+(3 rows)
+
+SELECT a FROM timestampfar ORDER BY a <-> '294276-12-31 23:59:59' LIMIT 1;
+ a
+----------------------------
+ Sun Dec 31 23:59:59 294276
+(1 row)
+
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/date.sql b/contrib/btree_gist/sql/date.sql
index f969ef0a08c..33bd5f83fc6 100644
--- a/contrib/btree_gist/sql/date.sql
+++ b/contrib/btree_gist/sql/date.sql
@@ -35,3 +35,30 @@ SELECT count(*) FROM datetmp WHERE a > '2001-02-13'::date;
EXPLAIN (COSTS OFF)
SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
+
+-- infinite dates in an index with internal pages (penalty and KNN distance)
+CREATE TABLE dateinf (a date);
+CREATE INDEX dateinfidx ON dateinf USING gist (a);
+INSERT INTO dateinf SELECT '2000-01-01'::date + i FROM generate_series(1, 1000) i;
+INSERT INTO dateinf SELECT 'infinity' FROM generate_series(1, 3);
+INSERT INTO dateinf SELECT '-infinity' FROM generate_series(1, 3);
+EXPLAIN (COSTS OFF)
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> 'infinity'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> '-infinity'::date LIMIT 3;
+-- same orderings via sort, which evaluates the <-> operator on every row
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_indexonlyscan = off;
+EXPLAIN (COSTS OFF)
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> '2000-01-01'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> 'infinity'::date LIMIT 3;
+SELECT a FROM dateinf ORDER BY a <-> '-infinity'::date LIMIT 3;
+RESET enable_seqscan;
+RESET enable_indexscan;
+RESET enable_indexonlyscan;
+-- and check these distance results directly
+SELECT 'infinity'::date <-> 'infinity'::date, '-infinity'::date <-> '-infinity'::date,
+ 'infinity'::date <-> '-infinity'::date, 'infinity'::date <-> '2000-01-01'::date;
diff --git a/contrib/btree_gist/sql/float8.sql b/contrib/btree_gist/sql/float8.sql
index a0fc84f94bb..b5491f186f3 100644
--- a/contrib/btree_gist/sql/float8.sql
+++ b/contrib/btree_gist/sql/float8.sql
@@ -52,3 +52,11 @@ SELECT count(*) FROM float8tmp WHERE abs(a) = 1890.0::float8;
RESET enable_seqscan;
RESET enable_indexscan;
RESET enable_bitmapscan;
+
+-- finite values whose difference overflows float8 (KNN distance must not error)
+CREATE TABLE float8far (a float8);
+CREATE INDEX float8faridx ON float8far USING gist (a);
+INSERT INTO float8far VALUES (1e308), (-1e308);
+EXPLAIN (COSTS OFF)
+SELECT a FROM float8far ORDER BY a <-> 1e308::float8 LIMIT 1;
+SELECT a FROM float8far ORDER BY a <-> 1e308::float8 LIMIT 1;
diff --git a/contrib/btree_gist/sql/timestamp.sql b/contrib/btree_gist/sql/timestamp.sql
index 95effebfc47..ed58802612c 100644
--- a/contrib/btree_gist/sql/timestamp.sql
+++ b/contrib/btree_gist/sql/timestamp.sql
@@ -35,3 +35,14 @@ SELECT count(*) FROM timestamptmp WHERE a > '2004-10-26 08:55:08'::timestamp;
EXPLAIN (COSTS OFF)
SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
+
+-- 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;
+-- finite timestamps too far apart to subtract (KNN distance must not error)
+CREATE TABLE timestampfar (a timestamp);
+CREATE INDEX timestampfaridx ON timestampfar USING gist (a);
+INSERT INTO timestampfar VALUES ('4714-11-24 00:00:00 BC'), ('294276-12-31 23:59: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;
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;
[parent: a1725d9fcede]