skip to content

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.

five int4ranges as typed, and the form postgres stores. a solid end is inclusive, a hollow one exclusive. the dots are the integers each range contains: [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:

sqlempty db
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;
output, from postgres 18.6
SELECT 1
default_boundsint4rangeboth_inclusiveint4rangeliteralint4rangesame_valueboolcontinuousnumrange
[2,5)[2,6)[3,6)t[2,5]
(1 row)

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:

sqlempty db
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;
output, from postgres 18.6
SELECT 1
from_one_onint4rangeemptyint4rangeequal_to_emptyboolempty_overlapsbool
[1,)emptytf
(1 row)

an empty range overlaps nothing, and is contained in everything.

the operators

operatorasksexample
&&do they overlap?nights && daterange('2026-03-04', '2026-03-06')
@>contains a value, or a whole rangenights @> date '2026-03-03'
<@is contained bydate '2026-03-03' <@ nights
-|-adjacent: touching, no gap, no overlap[2,5) -|- [5,8)
+ * -union, intersection, differenceint4range(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:

march bookings for one room, a label every three days. ada and grace overlap the stay edsger wanted (dashed), so the exclusion constraint refused it. grace starts the day ada leaves: adjacent, not overlapping.
predict the output
sql
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:

sqlempty db
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)');
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 2
#3 · ERROR 23P01
ERROR 23P01: conflicting key value violates exclusion constraint "bookings_nights_excl"
DETAIL: Key (nights)=([2026-03-04,2026-03-06)) conflicts with existing key (nights)=([2026-03-02,2026-03-05)).

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:

sqlempty db
CREATE TABLE stays (room int, nights daterange, EXCLUDE USING gist (room WITH =, nights WITH &&));
output, from postgres 18.6
ERROR 42704: data type integer has no default operator class for access method "gist"
HINT: You must specify an operator class for the index or define a default operator class for the data type.

the btree_gist extension adds those operator classes (ext=btree_gist creates it first):

sqlempty db · ext=btree_gist
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)');
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 2
#3 · ERROR 23P01
ERROR 23P01: conflicting key value violates exclusion constraint "stays_room_nights_excl"
DETAIL: Key (room, nights)=(101, [2026-03-04,2026-03-06)) conflicts with existing key (room, nights)=(101, [2026-03-02,2026-03-05)).

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:

sqlempty db
SELECT range_agg(r) AS merged
FROM (VALUES (int4range(1, 3)), (int4range(2, 5)), (int4range(7, 9))) AS t (r);
output, from postgres 18.6
SELECT 1
mergedint4multirange
{[1,5),[7,9)}
(1 row)
bookedfree
{[2026-03-02,2026-03-07),[2026-03-09,2026-03-11…{[2026-03-01,2026-03-02),[2026-03-07,2026-03-09…
(1 row)
real output, postgres 18.6 (PGlite 0.5.12) · ranges
march 1 to 11 as a datemultirange, minus every booked night: what's left are the free nights. postgres merged ada's and grace's touching stays into one booked range.
sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 3
#3 · SELECT 2
freedaterange
[2026-03-01,2026-03-02)
[2026-03-07,2026-03-09)
(2 rows)

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:

sqlempty db
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');
output, from postgres 18.6
#1 · SET
#2 · SELECT 10000
#3 · CREATE INDEX
#4 · ANALYZE
#5 · EXPLAIN
Bitmap Heap Scan on slots Recheck Cond: (during && '["2026-03-02 10:00:00+00","2026-03-02 11:00:00+00")'::tstzrange) -> Bitmap Index Scan on slots_during_idx Index Cond: (during && '["2026-03-02 10:00:00+00","2026-03-02 11:00:00+00")'::tstzrange)

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.