[PATCH] Fix interval input at minimum signed field values (BUG #19746)
[PATCH] Fix interval input at minimum signed field values (BUG #19746)
Hi hackers,
Attached is a proposed fix for BUG #19746.
The bug causes an interval containing INT64_MIN microseconds to be
printed successfully but rejected when read back.
The patch fixes the interval parsing logic and adds regression tests for
text round trips, boundary values, overflow, and AGO output.
This is my first PostgreSQL patch submission, so feedback would be greatly
appreciated.
Regards,
TinhRe: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
От:
Ishjaap Singh <ishjaap.singh07@gmail.com>
Дата:
Hi Tinh,
> This is my first PostgreSQL patch submission, so feedback would be greatly
> appreciated.
Thanks for working on this. I tested v2 on master (77705d1e5a6).
The cases from the report are fixed: '-2562047788:00:54.775808' and
'@ 2147483648 days ago' (postgres_verbose) read back to the same value,
and the pg_dump example now restores both rows instead of none. The new
tests fail without the datetime.c change and pass with it, make check
passes, and pgindent is clean.
One problem: these are corner cases, but the commit message says
cancellation between fields near the limits should be preserved, and
with v2 that only holds for one field order. These return true on
unpatched master and fail with "interval field value out of range"
with v2:
SELECT interval_send('1 millisecond -9223372036854775808 us ago'::interval)
= interval_send('9223372036854774808 us'::interval);
SELECT interval_send('1 hour -9223372036854775808 us ago'::interval)
= interval_send('9223372033254775808 us'::interval);
SELECT interval_send('1 week -2147483648 days ago'::interval)
= interval_send('2147483641 days'::interval);
SELECT interval_send('-2147483648 months 0.5 years ago'::interval)
= interval_send('2147483642 months'::interval);
DecodeInterval reads fields right to left, so here the minimum field is
negated first and overflows before the other field can bring the total
back into range. The patch's cancellation tests put the minimum first in
the string, so they don't catch this. It would be good to test both
orders.
Smaller notes:
- Some inputs that are invalid either way now get a different error,
e.g. '2147483647 days 1 days ago' says "invalid input syntax" instead
of "field value out of range". That seems fine, since the real problem
is the repeated unit.
- v2 applies to master and REL_17-19, but not REL_15/16. REL_14 has
older input code and didn't show the problem in my tests.
- It isn't in a commitfest yet; registering it would get cfbot testing
it.
Regards,
Ishjaap Singh
Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Tinh, Ishjaap,
Thank you for the patch. I tried v2 (CF entry 7620) and would like to
share what I saw. I can send the scripts and the output if they are
useful. I am new to reviewing here, so please correct me if I misread
something.
Setup
- Master at debfb3c8a8c9 (12 Nov 2026) plus the v2 attachment from the
first message of the thread (3 files: datetime.c, interval.sql,
interval.out). `git apply --check` was clean on that master. (log
00-setup.log)
- configure with --enable-cassert --enable-debug --enable-tap-tests
--without-icu, on Linux x86_64 (WSL Ubuntu, gcc). I built the
unpatched tree and the patched tree separately. Both builds had 0
compiler warnings. (logs 01-*, 02-*)
Tests that passed
- `make check` with the patch: all 243 tests passed, including
interval. (log 10-make-check-patched.log)
- With datetime.c left unpatched and the new test files (interval.sql
and interval.out) kept, `make check` fails in interval only (1 of 243
tests failed). The diff shows the new round trip tests failing, for
example `ERROR: invalid input syntax for type interval:
"-2562047788:00:54.775808"` and `ERROR: interval field value out of
range: "@ 2562047788 hours 54.775808 secs ago"`, and pg_input_is_valid
returning f for '-2562047788:00:54.775808'. So the new tests do
exercise the fix. (logs 11-make-check-tests-only-nofix.log,
11-nofix-regression.diffs)
Manual tests (temporary clusters, one run each on the unpatched and
the patched build; script repro.sql, outputs 22-repro-base.out and
22-repro-patched.out, differences in 23-repro-diff.txt)
- Cases from the bug report. Unpatched:
`'-2562047788:00:54.775808'::interval` gives "invalid input syntax"
and `'@ 2147483648 days ago'` (postgres_verbose) gives "interval field
value out of range". Patched: both are accepted and read back to the
same value, and `'@ 2147483648 months ago'` also works.
- Round trip `interval_send(i) = interval_send(i::text::interval)`
over six values at the limits (INT64_MIN and INT64_MAX microseconds,
INT_MIN days, INT_MIN months, and two combined values), for
intervalstyle postgres, postgres_verbose, sql_standard and iso_8601.
Unpatched: the first three styles raise an error, iso_8601 is fine.
Patched: all four styles return true for all six values.
- Inputs that should stay invalid still give "interval field value out
of range" on the patched build: '-2147483649 days', '2147483649 days
ago', '2147483649 months ago', '-2562047788:00:54.775809' (invalid
input syntax), '2562047788:00:54.775808', '-9223372036854775809 us'.
- '2147483648 days ago', '2147483648 months ago' and
'2562047788:00:54.775808 ago' are now accepted. These are -INT_MIN
days, -INT_MIN months and the minimum time value, so I think that is
right.
- Valid values near the limits ('2147483647 days', '-2147483647
months', '-1 2:03:04', '1 day 02:03:04 ago', '1 -01:02:03', and the
sql_standard examples) gave the same output on both builds.
- The cancellation cases from Ishjaap's message. On unpatched master
they all return true. On the patched build, when the minimum value
comes first in the string they return true (`'-9223372036854775808 us
1 millisecond ago'`, `'-2147483648 days 1 week ago'`), but when it
comes last they fail with "interval field value out of range":
- `'1 millisecond -9223372036854775808 us ago'`
- `'1 hour -9223372036854775808 us ago'`
- `'1 week -2147483648 days ago'`
- `'-2147483648 months 0.5 years ago'`
So I can confirm the problem Ishjaap reported, and I did not see it
fixed in v2. The new tests only cover the minimum-first order.
- `'2147483647 days 1 days ago'` now says "invalid input syntax"
instead of "field value out of range". Same as Ishjaap noted; I agree
this seems fine.
- The server did not crash and there was no PANIC or TRAP line in
either server log (assert-enabled build). (logs 21-server-base.log,
21-server-patched.log)
Questions
- Could the "ago" handling for the minimum value be done so that the
other field can still cancel it? Since the fields are read right to
left, the minimum field is negated before the other field has been
added. One idea (only an idea, I have not tried it) is to accumulate
in a wider type, or to remember the "ago" and apply it once at the end
with a check that does not overflow on the minimum. Please ignore this
if you already have a better plan.
- Could the tests cover both field orders, as Ishjaap suggested? The
four cases above would fit in the existing cancellation block.
- Is the different error for repeated units ("invalid input syntax")
intended? I think it is fine, but it is a visible change in the
message.
Not tested
- Back-patching, and PG 14 to 19 branches (I only used master).
- The pg_dump and restore example from the bug report (I tested the
text round trip only).
- Binary input and output (interval_recv), the ISO 8601 "P" input
parser beyond the round trip above, and other callers of
DecodeTimeForInterval and AdjustDays.
- Fractional values with time fields at the limits beyond the cases
above, and the non-US datestyle or locale settings.
- make check-world, the isolation tests and the other TAP suites (I
ran only make check).
- Meson build, non-assert builds, Windows, 32-bit, other compilers, ICU builds.
- pgindent and a full code review.
Thanks again, and please let me know if you want me to rerun anything
with different settings.
Best regards,
João Rito
Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
Subject: Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
Hi Ishjaap, João,
Thanks for the reviews. Attached is v3, using wider temporary
accumulators to preserve cancellation and check storage limits
after applying AGO.
It includes tests for the original boundary round trips and
your counterexamples in both field orders.
Feedback on this approach would be welcome.
Regards,
Tinh
Thanks for the reviews. Attached is v3, using wider temporary
accumulators to preserve cancellation and check storage limits
after applying AGO.
It includes tests for the original boundary round trips and
your counterexamples in both field orders.
Feedback on this approach would be welcome.
Regards,
Tinh
Vào Thứ 4, 4 thg 11, 2026 vào lúc 18:15 Ishjaap Singh <ishjaap.singh07@gmail.com> đã viết:
Hi Tinh,
> This is my first PostgreSQL patch submission, so feedback would be greatly
> appreciated.
Thanks for working on this. I tested v2 on master (77705d1e5a6).
The cases from the report are fixed: '-2562047788:00:54.775808' and
'@ 2147483648 days ago' (postgres_verbose) read back to the same value,
and the pg_dump example now restores both rows instead of none. The new
tests fail without the datetime.c change and pass with it, make check
passes, and pgindent is clean.
One problem: these are corner cases, but the commit message says
cancellation between fields near the limits should be preserved, and
with v2 that only holds for one field order. These return true on
unpatched master and fail with "interval field value out of range"
with v2:
SELECT interval_send('1 millisecond -9223372036854775808 us ago'::interval)
= interval_send('9223372036854774808 us'::interval);
SELECT interval_send('1 hour -9223372036854775808 us ago'::interval)
= interval_send('9223372033254775808 us'::interval);
SELECT interval_send('1 week -2147483648 days ago'::interval)
= interval_send('2147483641 days'::interval);
SELECT interval_send('-2147483648 months 0.5 years ago'::interval)
= interval_send('2147483642 months'::interval);
DecodeInterval reads fields right to left, so here the minimum field is
negated first and overflows before the other field can bring the total
back into range. The patch's cancellation tests put the minimum first in
the string, so they don't catch this. It would be good to test both
orders.
Smaller notes:
- Some inputs that are invalid either way now get a different error,
e.g. '2147483647 days 1 days ago' says "invalid input syntax" instead
of "field value out of range". That seems fine, since the real problem
is the repeated unit.
- v2 applies to master and REL_17-19, but not REL_15/16. REL_14 has
older input code and didn't show the problem in my tests.
- It isn't in a commitfest yet; registering it would get cfbot testing
it.
Regards,
Ishjaap Singh
Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello
Thanks for working on this!
+ itm_in->tm_usec = int64_to_int128(itm.tm_usec);
+ int128_add_int64_mul_int64(&itm_in->tm_usec, itm.tm_hour, USECS_PER_HOUR);
+ int128_add_int64_mul_int64(&itm_in->tm_usec, itm.tm_min, USECS_PER_MINUTE);
There's also a related preexisting bug: time field overwrites the
fractional part of a day/week unit
SELECT '01:00:00 1.5 days'::interval AS a, '1.5 days 01:00:00'::interval AS b;
a | b
----------------+----------------
1 day 01:00:00 | 1 day 13:00:00
SELECT '01:00 0.5 weeks'::interval, '0.5 weeks 01:00'::interval;
3 days 01:00:00 | 3 days 13:00:00
int128_add_int64(&itm_in->tm_usec, itm.tm_usec) would also fix this
There are also some stale comments now:
* dtype and itm_in are output parameters.
...
* itm_in remains undefined for infinite interval values for which dtype alone
* suffices.
+/* Private wide arithmetic for the general interval parser. */
+static INT128
+NegateIntervalValue(INT128 value)
+{
+ INT128 result = make_int128(~PG_INT128_HI_INT64(value),
+ ~PG_INT128_LO_UINT64(value));
+
+ int128_add_int64(&result, 1);
+ return result;
+}
+
The bug exists on 15+, and this function uses PG19 functions - that is
fine, but this part will need a different solution on older branches.
+/*
+ * Keep general interval input wider than its stored representation until AGO
+ * has been applied. There are at most MAXDATEFIELDS int64 input values, and
+ * even the largest unit scale fits comfortably in this accumulator.
+ */
I am not sure how good this comment is outside this patch, as it
mostly seems to explain implementation details, not the type.
Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746)
Hi Zsolt Parragi,
Thanks for the review! Attached is v4. it nice catch with the old bug i have the
Thanks for the review! Attached is v4. it nice catch with the old bug i have the
change draft base on your suggestions
The bellowing attachment is current my update approach for your review
I changed the clock-time assignment to addition, added tests for fractional
days/weeks in both orders, and fixed the comments. Negation now avoids the
newer int128 helpers to ease backpatching.
Once we agree on the approach, I think i can prepare and test backpatches for the
affected maintained branches, back to PG15 upward ?
Could you take another look would you also prefer the fractional day/week fix
as a separate patch?
Regards,
I changed the clock-time assignment to addition, added tests for fractional
days/weeks in both orders, and fixed the comments. Negation now avoids the
newer int128 helpers to ease backpatching.
Once we agree on the approach, I think i can prepare and test backpatches for the
affected maintained branches, back to PG15 upward ?
Could you take another look would you also prefer the fractional day/week fix
as a separate patch?
Regards,
Vào Thứ 3, 17 thg 11, 2026 vào lúc 05:23 Zsolt Parragi <zsolt.parragi@percona.com> đã viết:
Hello
Thanks for working on this!
+ itm_in->tm_usec = int64_to_int128(itm.tm_usec);
+ int128_add_int64_mul_int64(&itm_in->tm_usec, itm.tm_hour, USECS_PER_HOUR);
+ int128_add_int64_mul_int64(&itm_in->tm_usec, itm.tm_min, USECS_PER_MINUTE);
There's also a related preexisting bug: time field overwrites the
fractional part of a day/week unit
SELECT '01:00:00 1.5 days'::interval AS a, '1.5 days 01:00:00'::interval AS b;
a | b
----------------+----------------
1 day 01:00:00 | 1 day 13:00:00
SELECT '01:00 0.5 weeks'::interval, '0.5 weeks 01:00'::interval;
3 days 01:00:00 | 3 days 13:00:00
int128_add_int64(&itm_in->tm_usec, itm.tm_usec) would also fix this
There are also some stale comments now:
* dtype and itm_in are output parameters.
...
* itm_in remains undefined for infinite interval values for which dtype alone
* suffices.
+/* Private wide arithmetic for the general interval parser. */
+static INT128
+NegateIntervalValue(INT128 value)
+{
+ INT128 result = make_int128(~PG_INT128_HI_INT64(value),
+ ~PG_INT128_LO_UINT64(value));
+
+ int128_add_int64(&result, 1);
+ return result;
+}
+
The bug exists on 15+, and this function uses PG19 functions - that is
fine, but this part will need a different solution on older branches.
+/*
+ * Keep general interval input wider than its stored representation until AGO
+ * has been applied. There are at most MAXDATEFIELDS int64 input values, and
+ * even the largest unit scale fits comfortably in this accumulator.
+ */
I am not sure how good this comment is outside this patch, as it
mostly seems to explain implementation details, not the type.