Sub topic 12
Querying Time Ranges with Half-Open Intervals
This chapter assumes that you know SELECT, WHERE, GROUP BY, and basic PostgreSQL date and time types. It also assumes that you understand that timestamptz represents an instant, while a time zone controls how that instant is displayed.
A time range has a start and an end. A half-open range includes its start but excludes its end:
starts_at >= startstarts_at < end
This pattern is safer than including both endpoints. Adjacent ranges can touch without counting the same row twice.
-- Events in June 2024, with UTC boundaries
SELECT id, name, starts_at
FROM event
WHERE starts_at >= TIMESTAMPTZ '2024-06-01 00:00:00+00'
AND starts_at < TIMESTAMPTZ '2024-07-01 00:00:00+00'
ORDER BY starts_at;An event exactly at midnight on July 1 is not part of June. It belongs to the next range. This makes daily, monthly, and reporting windows easy to join:
June: [June 1, July 1)
July: [July 1, August 1)The square bracket means “included.” The round bracket means “excluded.”
Why not use `BETWEEN`?
BETWEEN includes both endpoints. This can create overlap:
-- Both queries include the event at exactly 10:00
WHERE starts_at BETWEEN
TIMESTAMPTZ '2024-06-01 09:00:00+00'
AND TIMESTAMPTZ '2024-06-01 10:00:00+00';
WHERE starts_at BETWEEN
TIMESTAMPTZ '2024-06-01 10:00:00+00'
AND TIMESTAMPTZ '2024-06-01 11:00:00+00';If you run both queries for separate reporting buckets, the event at 10:00 appears twice. Use >= for the lower boundary and < for the upper boundary instead.
Local calendar days
A user’s day depends on the user’s time zone. Convert the instant to that zone before finding the local date or local boundary.
For example, this query counts events by calendar day in New York:
SELECT (starts_at AT TIME ZONE 'America/New_York')::date AS local_day,
count(*) AS events
FROM event
GROUP BY 1
ORDER BY 1;To filter one local day, calculate its boundaries in the named zone. The result of AT TIME ZONE is a timestamptz when it starts with a plain timestamp.
SELECT id, name, starts_at
FROM event
WHERE starts_at >= TIMESTAMP '2024-06-01 00:00:00'
AT TIME ZONE 'America/New_York'
AND starts_at < TIMESTAMP '2024-06-02 00:00:00'
AT TIME ZONE 'America/New_York'
ORDER BY starts_at;These boundaries may be 23, 24, or 25 elapsed hours apart because daylight saving time can change the offset. That is correct: the query describes a local calendar day.
Indexes and parameters
A half-open comparison is also a good shape for an index on starts_at:
CREATE INDEX event_starts_at_idx ON event (starts_at);
SELECT *
FROM event
WHERE starts_at >= $1
AND starts_at < $2;The application should provide $1 and $2 as correctly typed timestamp values. Do not convert the column inside the WHERE clause unless you need local grouping. Applying a function to the column can prevent an ordinary index from being used efficiently.
Exercise
Which events belong to the range from 09:00 through, but not including, 10:00 UTC?
Show answer
Use WHERE starts_at >= TIMESTAMPTZ '2024-06-01 09:00:00+00' AND starts_at < TIMESTAMPTZ '2024-06-01 10:00:00+00'.