← Back to PostgreSQL, page 3

postgresql-0042 · Medium mid

Use range types such as daterange to store periods and query overlaps and containment.

Range types (int4range, numrange, daterange, tstzrange) store a period as one value with inclusive or exclusive bounds. Operators include @> for contains, && for overlaps and * for intersection, and upper and lower extract bounds. A GiST index speeds up overlap queries, and multiranges (PostgreSQL 14+) store sets of ranges.

Full answer

Tables and rows used here:

sql
CREATE TABLE rentals (id int, item text, period daterange);
INSERT INTO rentals VALUES
  (1, 'camera', '[2026-12-01,2026-12-05)'),
  (2, 'camera', '[2026-12-05,2026-12-08)'),
  (3, 'tripod', '[2026-12-03,2026-12-10)');
CREATE INDEX rentals_period ON rentals USING gist (period);
sql
SELECT id, item, period @> date '2026-12-05' AS on_dec5, period && daterange('2026-12-04', '2026-12-06') AS overlaps_window, upper(period) - lower(period) AS days FROM rentals ORDER BY id;

PostgreSQL returns:

text
 id |  item  | on_dec5 | overlaps_window | days
----+--------+---------+-----------------+------
 1  | camera | f       | t               | 4
 2  | camera | t       | t               | 3
 3  | tripod | t       | t               | 7
(3 rows)
sql
SELECT a.id AS first, b.id AS second, a.period * b.period AS shared FROM rentals a JOIN rentals b ON a.id < b.id AND a.period && b.period ORDER BY 1, 2;

Result on PostgreSQL 17:

text
 first | second |          shared
-------+--------+-------------------------
 1     | 3      | [2026-12-03,2026-12-05)
 2     | 3      | [2026-12-05,2026-12-08)
(2 rows)
sql
SELECT range_agg(period)::text AS busy_days FROM rentals WHERE item = 'camera';

Result:

text
         busy_days
---------------------------
 {[2026-12-01,2026-12-08)}
(1 row)

The half-open bound [start, end) means rental 1 ends before 5 December, so it does not contain that day while rental 2 does; back-to-back rentals do not overlap. Rental lengths come from subtracting bounds. The self-join finds overlapping pairs and their intersections: rental 3 overlaps both camera rentals. range_agg merges the camera's two adjacent periods into one multirange, {[2026-12-01,2026-12-08)}. An exclusion constraint can forbid overlapping rentals of the same item entirely.

sql
CREATE TABLE rentals (id int, item text, period daterange);
INSERT INTO rentals VALUES
  (1, 'camera', '[2026-12-01,2026-12-05)'),
  (2, 'camera', '[2026-12-05,2026-12-08)'),
  (3, 'tripod', '[2026-12-03,2026-12-10)');
CREATE INDEX rentals_period ON rentals USING gist (period);

SELECT id, item, period @> date '2026-12-05' AS on_dec5, period && daterange('2026-12-04', '2026-12-06') AS overlaps_window, upper(period) - lower(period) AS days FROM rentals ORDER BY id;

SELECT a.id AS first, b.id AS second, a.period * b.period AS shared FROM rentals a JOIN rentals b ON a.id < b.id AND a.period && b.period ORDER BY 1, 2;

SELECT range_agg(period)::text AS busy_days FROM rentals WHERE item = 'camera';

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Ranges store periods as one value
  • Half-open bounds avoid off-by-one overlaps
  • @>, && and * operators
  • GiST indexes speed up overlap tests
  • range_agg builds multiranges

Common mistakes

  • Using inclusive end dates and double-booking boundaries
  • Two separate columns with hand-written overlap logic

Follow-ups

  • How do exclusion constraints prevent overlaps?
  • How are empty ranges handled?

Tips

  • Explain half-open bounds