Sub topic 8
Working with Intervals
This chapter assumes basic SQL, including SELECT, and a general understanding of UTC and time-zone offsets. PostgreSQL uses an interval to represent an amount of time rather than one fixed moment.
What an interval represents
An interval can contain months, days, and time units such as hours or seconds. These units do not all have a fixed duration:
| Interval part | Meaning | Fixed duration? |
|---|---|---|
| `month` | Calendar months | No |
| `day` | Calendar days | Not always |
| `hour`, `minute`, `second` | Elapsed time | Yes |
A calendar month can have 28, 29, 30, or 31 days. A calendar day can contain 23 or 25 elapsed hours when daylight saving time changes. PostgreSQL keeps these distinctions instead of converting every interval to seconds.
You can create intervals with a string literal:
SELECT interval '2 months 3 days 4 hours 5 minutes';You can add or subtract an interval from a date, timestamp, or timestamptz:
SELECT date '2024-04-15' + interval '2 weeks' AS due_at,
timestamp '2024-04-15 09:30' - interval '90 minutes' AS earlier;The result type depends on the input. Adding an interval to a date produces a timestamp because the interval can include a time component.
Calendar units versus elapsed time
Month arithmetic follows calendar rules. If the target month does not have the original day, PostgreSQL adjusts the result to the last valid day:
SELECT date '2024-01-31' + interval '1 month' AS month_end_add;
-- 2024-02-29 00:00:00Adding another month to that result is not necessarily the same as adding 60 days. Use months when the requirement is calendar-based, such as “one month after the billing date.” Use hours or seconds when the requirement is elapsed time, such as “run 24 hours after the event.”
Intervals and daylight saving time
For a timestamptz, interval '1 day' advances the local calendar date and attempts to keep the same local clock time. interval '24 hours' advances exactly 24 elapsed hours.
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-03-09 12:00:00' + interval '1 day'
AS plus_one_day,
TIMESTAMPTZ '2024-03-09 12:00:00' + interval '24 hours'
AS plus_24_hours;
-- plus_one_day: 2024-03-10 12:00:00-04
-- plus_24_hours: 2024-03-10 13:00:00-04The clocks move forward in New York on March 10, 2024. Therefore, noon on the next local day is only 23 elapsed hours later, while 24 hours later is displayed as 13:00.
A working model
Think of interval arithmetic as applying calendar fields first and fixed-time fields according to PostgreSQL's timestamp rules. The exact result also depends on the input type and the session TimeZone.
Checking interval values
You can compare results by extracting fields or converting a fixed-duration interval to seconds:
SELECT extract(epoch FROM interval '36 hours') AS seconds;
-- 129600
SELECT date '2024-06-01' + interval '1 week' AS next_week;extract(epoch FROM interval ...) is useful for intervals made only from fixed units. It can be misleading for intervals containing months because a month does not have one universal number of seconds.
Exercise
Which interval should you use for a reminder at the same local time on the following calendar day: interval '1 day' or interval '24 hours'?
Show answer
Use interval '1 day'. It follows the local calendar and preserves the local clock time across daylight saving changes.