Sub topic 13
Range types
This chapter assumes you know SELECT, INSERT, CREATE TABLE, and constraints. It also assumes you can run queries in PostgreSQL. A range type stores a continuous span of values, such as dates, numbers, or timestamps, in one column.
What a range contains
A range has a lower bound and an upper bound. Each bound can be inclusive or exclusive:
[2026-01-01, 2026-02-01)includes January 1 but excludes February 1.(10, 20]excludes 10 but includes 20.- An omitted bound means the range is unbounded.
emptyrepresents a range with no values.
PostgreSQL provides built-in range types for common value types.
| Range type | Values inside the range |
|---|---|
| `int4range` | 32-bit integers |
| `int8range` | 64-bit integers |
| `numrange` | Exact numeric values |
| `daterange` | Dates |
| `tsrange` | Timestamps without time zones |
| `tstzrange` | Timestamps with time zones |
The two bounds are written as constructor arguments. PostgreSQL usually displays ranges with a half-open form: inclusive lower bound and exclusive upper bound.
SELECT
daterange('2026-01-01', '2026-02-01', '[)') AS january,
daterange('2026-01-01', '2026-02-01', '[)') @> DATE '2026-01-15' AS contains_date,
daterange('2026-01-01', '2026-02-01', '[)') && daterange('2026-02-01', '2026-03-01', '[)') AS overlaps;The @> operator tests whether the left range contains the value or range on the right. The && operator tests whether two ranges overlap. Adjacent ranges do not overlap when one ends exactly where the other begins.
Defining and querying ranges
A range column often replaces separate start_date and end_date columns when the application treats the interval as one unit.
CREATE TABLE room_bookings (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
room_id bigint NOT NULL,
occupied daterange NOT NULL,
CHECK (NOT isempty(occupied))
);
INSERT INTO room_bookings (room_id, occupied)
VALUES
(7, daterange('2026-01-10', '2026-01-15', '[)')),
(7, daterange('2026-02-01', '2026-02-05', '[)'));
SELECT id, lower(occupied) AS starts_on, upper(occupied) AS ends_on
FROM room_bookings
WHERE occupied @> DATE '2026-01-12';lower() and upper() return the bounds. Functions such as lower_inc() and upper_inc() tell you whether a bound is inclusive. lower_inf() and upper_inf() identify an unbounded side.
For scheduling, PostgreSQL can enforce that ranges for the same resource do not overlap. This requires a GiST index and the btree_gist extension for equality on bigint.
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE room_bookings
ADD CONSTRAINT room_bookings_no_overlap
EXCLUDE USING gist (
room_id WITH =,
occupied WITH &&
);The exclusion constraint rejects a new booking when its room_id matches an existing row and its occupied range overlaps that row.
Range types and multiranges
A range describes one continuous span. A multirange describes several non-overlapping spans of the same subtype. For example, a datemultirange can represent a person's available dates while excluding holidays. Use a multirange when separate intervals belong together and querying them as one value is useful. Otherwise, a table with one row per interval is often easier to update and index.
Exercise
Which operator checks whether daterange('2026-04-01', '2026-05-01', '[)') contains April 30?
Show answer
Use @>: daterange('2026-04-01', '2026-05-01', '[)') @> DATE '2026-04-30'.