range types
a start and an end as one value. overlaps with &&, empty ranges, and multiranges.
- address
- everything.types.ranges
- part
- part 6, types and values
- chapter
- chapter 10 of 11
- reading time
- 7 min
a range is a start and an end stored as one value: int4range, numrange, daterange, tstzrange. each bound is inclusive [ or exclusive ), and discrete ranges are stored in one canonical form, [lower, upper). && asks whether two ranges overlap, @> whether one contains a value or a range.
an exclusion constraint with && makes double bookings impossible, and multiranges (postgres 14) hold several ranges with gaps between them.
the picture
you know how every booking table has starts_at and ends_at, and every overlap query is four comparisons that somebody gets wrong at the edges? a range is that pair as one value, with the edges spelled out, and operators that already know the rules.
think of a ruler with a highlighter stroke on it. the stroke has two ends, and each end is either drawn over the mark (inclusive) or stops just before it (exclusive). for whole numbers, "up to and including 5" and "up to but not including 6" cover exactly the same marks, so postgres writes them all the same way.
[2,5] and [2,6) hold the same four, so they're the same value. [4,4) holds nothing and becomes empty.bounds and canonical form
the constructor functions take a bound spec as the third argument, '[)' by default. a literal writes the brackets itself:
SELECT int4range(2, 5) AS default_bounds,
int4range(2, 5, '[]') AS both_inclusive,
'(2,5]'::int4range AS literal,
int4range(2, 5, '[]') = int4range(2, 6) AS same_value,
numrange(2, 5, '[]') AS continuous;| default_boundsint4range | both_inclusiveint4range | literalint4range | same_valuebool | continuousnumrange |
|---|---|---|---|---|
| [2,5) | [2,6) | [3,6) | t | [2,5] |
int4range, int8range and daterange are discrete: there's a next value after 5 (6) and after march 4 (march 5), so postgres can rewrite every range to [lower, upper). numrange and tstzrange are continuous, there's no "next" number after 5.0, so [2,5] stays as typed. either way, [) is the shape to use for time: a stay from the 2nd to the 5th is [2026-03-02,2026-03-05), checking out on the morning of the 5th, and the next guest's [2026-03-05, ...) doesn't overlap it. the same half-open rule as filtering a month.
a NULL bound means unbounded on that side, and an empty range is a real value, not NULL:
SELECT int4range(1, NULL) AS from_one_on, 'empty'::int4range AS empty,
int4range(3, 3) = 'empty' AS equal_to_empty, 'empty'::int4range && int4range(1, 5) AS empty_overlaps;| from_one_onint4range | emptyint4range | equal_to_emptybool | empty_overlapsbool |
|---|---|---|---|
| [1,) | empty | t | f |
an empty range overlaps nothing, and is contained in everything.
the operators
| operator | asks | example |
|---|---|---|
&& | do they overlap? | nights && daterange('2026-03-04', '2026-03-06') |
@> | contains a value, or a whole range | nights @> date '2026-03-03' |
<@ | is contained by | date '2026-03-03' <@ nights |
-|- | adjacent: touching, no gap, no overlap | [2,5) -|- [5,8) |
+ * - | union, intersection, difference | int4range(1,5) * int4range(3,8) is [3,5) |
here are a room's three bookings in march, against a stay from the 4th to the 6th. two of them overlap it:
SELECT daterange('2026-03-02', '2026-03-05') && daterange('2026-03-05', '2026-03-07');no double bookings
a UNIQUE constraint checks that no two rows are equal. an exclusion constraint generalises it: no two rows may satisfy an operator, here &&, overlap. it's backed by a GiST index, which knows how to search ranges:
CREATE TABLE bookings (
guest text NOT NULL,
nights daterange NOT NULL,
EXCLUDE USING gist (nights WITH &&)
);
INSERT INTO bookings VALUES ('ada', '[2026-03-02,2026-03-05)'), ('grace', '[2026-03-05,2026-03-07)');
INSERT INTO bookings VALUES ('edsger', '[2026-03-04,2026-03-06)');the check happens in the database, under concurrency, so two sessions booking the same night at once can't both win (one waits for the other, then fails). no application-side "check, then insert" can promise that.
a real hotel has more than one room: no overlap in the same room. that's room WITH =, nights WITH &&, and plain GiST has no = for integers:
CREATE TABLE stays (room int, nights daterange, EXCLUDE USING gist (room WITH =, nights WITH &&));the btree_gist extension adds those operator classes (ext=btree_gist creates it first):
CREATE TABLE stays (room int, nights daterange, EXCLUDE USING gist (room WITH =, nights WITH &&));
INSERT INTO stays VALUES (101, '[2026-03-02,2026-03-05)'), (102, '[2026-03-02,2026-03-05)');
INSERT INTO stays VALUES (101, '[2026-03-04,2026-03-06)');room 102 the same nights is fine, room 101 again isn't. postgres 18 also has a shorthand for this pattern, PRIMARY KEY (room, nights WITHOUT OVERLAPS), covered with the rest of time ranges and temporal keys.
multiranges
a range has no holes. ask for the union of two ranges with a gap between them and you get an error ("would not be contiguous"). postgres 14 pg 14+ added multiranges: an ordered set of non-overlapping ranges, one per range type (int4multirange, datemultirange, ...). range_agg builds one from rows, and the usual operators work on them, so "which nights are free?" is one subtraction:
SELECT range_agg(r) AS merged
FROM (VALUES (int4range(1, 3)), (int4range(2, 5)), (int4range(7, 9))) AS t (r);| mergedint4multirange |
|---|
| {[1,5),[7,9)} |
| booked | free |
|---|---|
| {[2026-03-02,2026-03-07),[2026-03-09,2026-03-11… | {[2026-03-01,2026-03-02),[2026-03-07,2026-03-09… |
CREATE TABLE bookings (guest text, nights daterange);
INSERT INTO bookings VALUES ('ada', '[2026-03-02,2026-03-05)'), ('grace', '[2026-03-05,2026-03-07)'), ('alan', '[2026-03-09,2026-03-11)');
SELECT free
FROM unnest(datemultirange(daterange('2026-03-01', '2026-03-11')) - (SELECT range_agg(nights) FROM bookings)) AS free
ORDER BY free;| freedaterange |
|---|
| [2026-03-01,2026-03-02) |
| [2026-03-07,2026-03-09) |
the gotcha: indexes and queries
a b-tree on a range column only serves = and sorting. overlap and containment want GiST (or SP-GiST), which is what the exclusion constraint built for you:
SET TimeZone = 'UTC';
CREATE TABLE slots AS
SELECT i AS id, tstzrange(timestamptz '2026-03-01 00:00+00' + i * interval '30 minutes', timestamptz '2026-03-01 00:00+00' + (i + 1) * interval '30 minutes') AS during
FROM generate_series(0, 9999) AS i;
CREATE INDEX ON slots USING gist (during);
ANALYZE slots;
EXPLAIN (COSTS OFF) SELECT id FROM slots WHERE during && tstzrange('2026-03-02 10:00+00', '2026-03-02 11:00+00');index types compares GiST with the others, and GiST and SP-GiST shows how it searches.
keylines
- a range is a start and an end as one value, each bound inclusive
[or exclusive). - discrete ranges (int, date) are stored canonically as
[lower, upper). &&overlaps,@>contains,-|-touches. an empty range overlaps nothing.EXCLUDE USING gist (... WITH &&)makes overlaps impossible, even under concurrency.- multiranges (14+) hold ranges with gaps; range_agg builds them.