From cd5b964c3b870269ffd596dcc01175b2ca1196f2 Mon Sep 17 00:00:00 2001
From: Raja Sai pranav <rajasaipranav0@gmail.com>
Date: Tue, 1 Dec 2026 08:04:28 +0530
Subject: [PATCH v1] btree_gist: Don't report a finite interval distance as
 infinity

Commit c1dd701fb5 made interval_dist() return +Infinity whenever the
field-wise subtraction overflows.  But the fields of an interval can
have opposite signs, so two intervals can be close together even when
subtracting one of their fields overflows.  For example,
'178956971 days -2147483647 hours' and '2147483647 hours' are 10 hours
apart, yet <-> reported infinity.  The GiST distance function, which
works on the intervals' lengths in seconds, put that row first, so an
index scan and a sort disagreed on which row is nearest.

When a field overflows, compute the difference in the linear form that
interval comparison uses, and return it as a pure time interval if it
fits; report +Infinity only when it does not.

Reported-by: Kirill Reshke <reshkekirill@gmail.com>
Backpatch-through: 17
---
 contrib/btree_gist/btree_interval.c      | 27 +++++++++++++++++++++++-
 contrib/btree_gist/expected/interval.out |  6 ++++++
 contrib/btree_gist/sql/interval.sql      |  1 +
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/contrib/btree_gist/btree_interval.c b/contrib/btree_gist/btree_interval.c
index b51b294636..8580cb113a 100644
--- a/contrib/btree_gist/btree_interval.c
+++ b/contrib/btree_gist/btree_interval.c
@@ -6,6 +6,7 @@
 #include "btree_gist.h"
 #include "btree_utils_num.h"
 #include "common/int.h"
+#include "common/int128.h"
 #include "utils/fmgrprotos.h"
 #include "utils/rel.h"
 #include "utils/sortsupport.h"
@@ -174,7 +175,31 @@ interval_dist(PG_FUNCTION_ARGS)
 		pg_sub_s32_overflow(a->day, b->day, &result->day) ||
 		pg_sub_s64_overflow(a->time, b->time, &result->time) ||
 		INTERVAL_NOT_FINITE(result))
-		INTERVAL_NOEND(result);
+	{
+		/*
+		 * A field overflowed, but the inputs can still be close together,
+		 * since the fields may have opposite signs.  Compute the difference
+		 * in the linear form that interval comparison uses (a month is 30
+		 * days, a day is 24 hours), and return it as a pure time interval if
+		 * it fits; only otherwise is the distance +Infinity.
+		 */
+		INT128		span = int64_to_int128(a->time);
+
+		int128_add_int64_mul_int64(&span,
+								   a->month * INT64CONST(30) + a->day,
+								   USECS_PER_DAY);
+		int128_sub_int64(&span, b->time);
+		int128_sub_int64_mul_int64(&span,
+								   b->month * INT64CONST(30) + b->day,
+								   USECS_PER_DAY);
+
+		result->month = 0;
+		result->day = 0;
+		if (int128_compare(span, int64_to_int128(PG_INT64_MAX)) < 0)
+			result->time = int128_to_int64(span);
+		else
+			INTERVAL_NOEND(result);
+	}
 
 	PG_RETURN_INTERVAL_P(result);
 }
diff --git a/contrib/btree_gist/expected/interval.out b/contrib/btree_gist/expected/interval.out
index bb105abf23..5b82a22582 100644
--- a/contrib/btree_gist/expected/interval.out
+++ b/contrib/btree_gist/expected/interval.out
@@ -129,3 +129,9 @@ SELECT '-178956970 years -7 months'::interval <-> '1 mon'::interval AS negate_ov
  infinity
 (1 row)
 
+SELECT make_interval(days => 178956971, hours => -2147483647) <-> make_interval(hours => 2147483647) AS fields_cancel;
+ fields_cancel 
+---------------
+ @ 10 hours
+(1 row)
+
diff --git a/contrib/btree_gist/sql/interval.sql b/contrib/btree_gist/sql/interval.sql
index 5ceedd2ee1..6c987c63b7 100644
--- a/contrib/btree_gist/sql/interval.sql
+++ b/contrib/btree_gist/sql/interval.sql
@@ -49,3 +49,4 @@ SELECT 'infinity'::interval <-> 'infinity'::interval AS same_pos,
        '1 day'::interval <-> 'infinity'::interval AS finite_inf;
 SELECT '178000000 years'::interval <-> '-178000000 years'::interval AS sub_overflow;
 SELECT '-178956970 years -7 months'::interval <-> '1 mon'::interval AS negate_overflow;
+SELECT make_interval(days => 178956971, hours => -2147483647) <-> make_interval(hours => 2147483647) AS fields_cancel;
-- 
2.50.1 (Apple Git-155)

