Patch
Fix spurious overflow error in justify_interval()
- Branch
- master
- Risk
- 12
- Base commit
- 32afcc29df98b245d6c2055fd2a21e6cd2d0fd06
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a233b6-f1b3-7d76-ab57-1191e5dc2b35 | jq -r .body | git am
From e1970751cb5517f99875cc9dcf651006e5dc7ab5 Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 14:24:12 -0800
Subject: [PATCH] Fix spurious overflow error in justify_interval()
justify_interval() carries whole months out of the day field and adds
them to the month field with an overflow check, then applies a
sign-normalizing adjustment that may move one month back into the day
field. When the month sum exceeds the int32 range by exactly one and
that adjustment applies, the function reported "interval out of
range" even though the justified result is representable. For
example:
SELECT justify_interval(
interval '2147483647 months 30 days -00:00:00.000001');
failed, although the input equals the already-justified value
'2147483647 months 29 days 23:59:59.999999'. The same happens at the
negative limit, and when the day carry comes partly from the time
field, as in '2147483646 months 61 days -24:00:00.000001'.
Compute the month sum in int64 and check the range only after the sign
adjustment. The earlier "pre-justify" carry keeps its overflow check:
it runs only when day and time have the same sign, and then the later
steps can only move an overflowed month count further out of range, so
an overflow there is genuine.
Oversight in commit 54bd1e43ca5, which added the overflow checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
src/backend/utils/adt/timestamp.c | 26 ++++++++++++++++++--------
src/test/regress/expected/interval.out | 24 ++++++++++++++++++++++++
src/test/regress/sql/interval.sql | 4 ++++
3 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 81f93ae2c26..94f5a9d5255 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -2945,6 +2945,7 @@ interval_justify_interval(PG_FUNCTION_ARGS)
Interval *result;
TimeOffset wholeday;
int32 wholemonth;
+ int64 month;
result = palloc_object(Interval);
result->month = span->month;
@@ -2978,24 +2979,33 @@ interval_justify_interval(PG_FUNCTION_ARGS)
wholemonth = result->day / DAYS_PER_MONTH;
result->day -= wholemonth * DAYS_PER_MONTH;
- if (pg_add_s32_overflow(result->month, wholemonth, &result->month))
- ereport(ERROR,
- (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
- errmsg("interval out of range")));
- if (result->month > 0 &&
+ /*
+ * Compute the new month count in int64. It can exceed the int32 range by
+ * one here and yet be brought back into range by the sign adjustment
+ * below, so check for overflow only after that.
+ */
+ month = (int64) result->month + wholemonth;
+
+ if (month > 0 &&
(result->day < 0 || (result->day == 0 && result->time < 0)))
{
result->day += DAYS_PER_MONTH;
- result->month--;
+ month--;
}
- else if (result->month < 0 &&
+ else if (month < 0 &&
(result->day > 0 || (result->day == 0 && result->time > 0)))
{
result->day -= DAYS_PER_MONTH;
- result->month++;
+ month++;
}
+ if (month < PG_INT32_MIN || month > PG_INT32_MAX)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
+ errmsg("interval out of range")));
+ result->month = (int32) month;
+
if (result->day > 0 && result->time < 0)
{
result->time += USECS_PER_DAY;
diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out
index a16e3ccdb2e..fbd579a43ff 100644
--- a/src/test/regress/expected/interval.out
+++ b/src/test/regress/expected/interval.out
@@ -583,6 +583,30 @@ SELECT justify_interval(interval '2147483647 months -30 days 1440 hrs');
ERROR: interval out of range
SELECT justify_interval(interval '-2147483648 months 30 days -1440 hrs');
ERROR: interval out of range
+SELECT justify_interval(interval '2147483647 months 30 days -00:00:00.000001');
+ justify_interval
+------------------------------------------------------------------
+ @ 178956970 years 7 mons 29 days 23 hours 59 mins 59.999999 secs
+(1 row)
+
+SELECT justify_interval(interval '-2147483648 months -30 days 00:00:00.000001');
+ justify_interval
+----------------------------------------------------------------------
+ @ 178956970 years 8 mons 29 days 23 hours 59 mins 59.999999 secs ago
+(1 row)
+
+SELECT justify_interval(interval '2147483646 months 61 days -24:00:00.000001');
+ justify_interval
+------------------------------------------------------------------
+ @ 178956970 years 7 mons 29 days 23 hours 59 mins 59.999999 secs
+(1 row)
+
+SELECT justify_interval(interval '-2147483647 months -61 days 24:00:00.000001');
+ justify_interval
+----------------------------------------------------------------------
+ @ 178956970 years 8 mons 29 days 23 hours 59 mins 59.999999 secs ago
+(1 row)
+
-- test fractional second input, and detection of duplicate units
SET DATESTYLE = 'ISO';
SET IntervalStyle TO postgres;
diff --git a/src/test/regress/sql/interval.sql b/src/test/regress/sql/interval.sql
index 43bc793925e..def0493da77 100644
--- a/src/test/regress/sql/interval.sql
+++ b/src/test/regress/sql/interval.sql
@@ -197,6 +197,10 @@ SELECT justify_interval(interval '2147483647 months 30 days -24 hrs');
SELECT justify_interval(interval '-2147483648 months -30 days 24 hrs');
SELECT justify_interval(interval '2147483647 months -30 days 1440 hrs');
SELECT justify_interval(interval '-2147483648 months 30 days -1440 hrs');
+SELECT justify_interval(interval '2147483647 months 30 days -00:00:00.000001');
+SELECT justify_interval(interval '-2147483648 months -30 days 00:00:00.000001');
+SELECT justify_interval(interval '2147483646 months 61 days -24:00:00.000001');
+SELECT justify_interval(interval '-2147483647 months -61 days 24:00:00.000001');
-- test fractional second input, and detection of duplicate units
SET DATESTYLE = 'ISO';
base-commit: 32afcc29df98b245d6c2055fd2a21e6cd2d0fd06
--
2.53.0