← All notes

PostgreSQL range types

Answer: Range types store an interval of values, like int4range or tstzrange, with inclusive [ or exclusive ) bounds. Operators test overlap (&&), containment (@>) and adjacency, and you can define your own range types. Multiranges hold sets of non-overlapping ranges.

CREATE TABLE booking (
  room int,
  during tstzrange
);

SELECT * FROM booking
WHERE during && tstzrange('2026-11-04 10:00', '2026-11-04 11:00');

SELECT int4range(1, 10) @> 5;    -- true
SELECT int4range(1, 5) + int4range(5, 9);  -- [1,9)

Bounds

The constructor defaults to [): lower inclusive, upper exclusive. Use lower_inc, upper_inc, lower_inf and upper_inf to inspect bounds.

Notes

Pair a GiST index with an exclusion constraint to prevent overlapping bookings.

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 8.17. Range Types, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 9.20. Range/Multirange Functions and Operators, PostgreSQL License.

Cite this

Permanent ID: iil:note:q93gqkw0

iinventedlight. 2026. "PostgreSQL range types." iinventedlight Library, iil:note:q93gqkw0, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-range-types/. License: CC-BY-4.0.

@misc{iil_note_q93gqkw0,
  author = {{iinventedlight}},
  title = {{PostgreSQL range types}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/postgres-range-types/},
  note = {Permanent ID iil:note:q93gqkw0. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-range-types.md.