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.

SqlData Integrity

Code

snippet
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

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).