Re: Fix overflow in justify_interval related functions - Mailing list pgsql-hackers

From Joseph Koshakow
Subject Re: Fix overflow in justify_interval related functions
Date
Msg-id CAAvxfHceWFDCTGqzjuRgYa46SPAKJViHosXZ1dqYLmtnpzqnLw@mail.gmail.com
Whole thread Raw
In response to Re: Fix overflow in justify_interval related functions  (Nathan Bossart <nathandbossart@gmail.com>)
Responses Re: Fix overflow in justify_interval related functions  (Joseph Koshakow <koshy44@gmail.com>)
Re: Fix overflow in justify_interval related functions  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Mon, Feb 14, 2022 at 11:23 PM Nathan Bossart
<nathandbossart@gmail.com> wrote:
> It's a little weird
> that justify_hours() and justify_days() can overflow in cases where there
> is still a valid interval representation, but as Tom noted, those functions
> have specific charters to follow.

Yes it is a bit weird, but this follows the same behavior as adding
Intervals. The following query overflows:
  postgres=# SELECT interval '2147483647 days' + interval '1 day';
  ERROR:  interval out of range
Even though the following query does not:
  postgres=# SELECT justify_days(interval '2147483647 days') + interval '1 day';
            ?column?
  -----------------------------
   5965232 years 4 mons 8 days
  (1 row)

The reason is, as Tom mentioned, that Interval's months, days, and
time (microseconds) are stored separately. They are only combined
during certain scenarios such as testing for equality, ordering,
justify_* methods, etc. I think the idea behind it is that not every month
has 30 days and not every day has 24 hrs, though I'm not sure
.
> > +             ereport(ERROR,
> > +                             (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
> > +                             errmsg("interval out of range")));
>
> nitpick: I think there is ordinarily an extra space before errmsg() so that
> it lines up with errcode().

I've attached a patch to add the space. Thanks so much for your review
and comments!

- Joe Koshakow

Attachment

pgsql-hackers by date:

Previous
From: Ranier Vilela
Date:
Subject: Re: Postgres 14.2 Windows can't rename temporary statistics file
Next
From: Joseph Koshakow
Date:
Subject: Re: Fix overflow in DecodeInterval