int4range Discrete Bounds Endpoints
int4range normalizes discrete bounds so '[1,5]' equals '[1,6)', the upper edge stays exclusive, and lower_inc plus upper_inc expose endpoint inclusion.
Code
WITH probes AS (
SELECT * FROM (VALUES
(int4range(1, 5)),
(int4range(1, 5, '[]')),
(int4range(3, 3))
) AS t(span)
)
SELECT
span,
-- discrete ranges normalize: '[1,5]' equals '[1,6)', the upper edge stays exclusive
int4range(1, 5, '[]') = int4range(1, 6) AS canonical,
-- exclusive upper edge: 4 hits, 5 does not
span @> 4 AS has_four,
span @> 5 AS has_five,
-- lower_inc/upper_inc expose endpoint inclusion
lower_inc(span) AS lower_inc,
upper_inc(span) AS upper_inc,
-- empty range: lower bound equals upper
isempty(span) AS empty
FROM probes;What the demo normalizes
Three probes enter: int4range(1, 5), the inclusive form int4range(1, 5, ‘[]’), and the degenerate int4range(3, 3). Discrete ranges canonicalize on output, so the inclusive [1,5] prints as [1,6), and the equality check confirms it matches int4range(1, 6) exactly.
Containment follows the exclusive upper edge: contains-4 is true for both non-empty spans while contains-5 is false. lower_inc and upper_inc report which endpoints are included, and int4range(3, 3) prints empty with isempty true.
Caveats and details
- Discrete types store ranges in canonical half-open form automatically.
- isempty, not a NULL check, is how emptiness is tested.
Interactive Tools for This Topic
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SQL FormatterReformat SQL by uppercasing reserved words and breaking lines before SELECT, FROM, WHERE, and other clauses.
FAQ
- Why does [1,5] print as [1,6)?
- Integer ranges normalize to half-open bounds: the closed upper endpoint becomes the next excluded integer, so the printed canonical form and the equality probe both show [1,6).
Report a problem with this exampleOpens a pre-filled issue on GitHub.