Re: INT4RANGE Upper bound always includes a higher number

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: themyth(dot)cs(at)gmail(dot)com
Cc: pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: INT4RANGE Upper bound always includes a higher number
Date: 2023-10-03 20:57:32
Message-ID: 2451988.1696366652@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

PG Doc comments form <noreply(at)postgresql(dot)org> writes:
> According to your example (copied from your docs):

> -- includes 3, does not include 7, and does include all points in between
> SELECT '[3,7)'::int4range;

> But this is not true, it shows 3 and 7

What's not true about it?

postgres=# SELECT 3 <@ '[3,7)'::int4range;
?column?
----------
t
(1 row)

postgres=# SELECT 6 <@ '[3,7)'::int4range;
?column?
----------
t
(1 row)

postgres=# SELECT 7 <@ '[3,7)'::int4range;
?column?
----------
f
(1 row)

7 is not a member of that range, only an endpoint.

> And if i do:
> SELECT '(3,7]'::INT4RANGE;
> It shows:

> [4,8)
> (1 row)

This is a consequence of canonicalization. There are four different
ways to write the same integer range:

[3,6]
[3,7)
(2,6]
(2,7)

All of these include 3,4,5,6 and no other integer.

INT4RANGE has a canonicalize function that converts ranges into the
"[m,n)" form so that ranges that are functionally identical look
identical. If you don't like that, you can make a user-defined
range type with a different canonicalize function, or none at all.
See

https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-DISCRETE

regards, tom lane

In response to

Browse pgsql-docs by date

  From Date Subject
Next Message David G. Johnston 2023-10-03 21:56:13 Re: Corresponding documentation page does not mention about `spread` mode
Previous Message PG Doc comments form 2023-10-03 19:25:19 Corresponding documentation page does not mention about `spread` mode