Thread: Confusion about the range types
Hey!
This is first time I'm writing, so I hope I've done this correctly :)
I'm confused about how range types are returned when queried
When I insert range like this:
INSERT INTO public.tests
VALUES (int4range(7,8,'[]'))
or this
INSERT INTO public.tests
VALUES ('[7,8]')
----------------
After when querying table my return value for this column is not [7,8] but it is [7,9).
I found this behaviour confusing, because I want to insert 7-8 ranges (including upper value) and then present that range to a user.
Maybe I'm missing something :)
My PG version is: PostgreSQL 14.0 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 12.0.5 (clang-1205.0.22.9), 64-bit
This is first time I'm writing, so I hope I've done this correctly :)
I'm confused about how range types are returned when queried
When I insert range like this:
INSERT INTO public.tests
VALUES (int4range(7,8,'[]'))
or this
INSERT INTO public.tests
VALUES ('[7,8]')
----------------
After when querying table my return value for this column is not [7,8] but it is [7,9).
I found this behaviour confusing, because I want to insert 7-8 ranges (including upper value) and then present that range to a user.
Maybe I'm missing something :)
My PG version is: PostgreSQL 14.0 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 12.0.5 (clang-1205.0.22.9), 64-bit
--
Sent with HEY — Email as it should be
Dino Maric <dinom@hey.com> writes: > When I insert range like this: > INSERT INTO public.tests > VALUES (int4range(7,8,'[]')) > After when querying table my return value for this column is not [7,8] > but it is [7,9). > I found this behaviour confusing, because I want to insert 7-8 ranges > (including upper value) and then present that range to a user. This is the effect of canonicalization, as explained here: https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-DISCRETE If you don't like it you can make a range type with a different canonicalization function, or no such function, but that might have odd effects on the behavior of range comparison operators. regards, tom lane
Thanks Tom, I understand.
But still it does feel a bit strange that value I'm storing is different when presenting.
For example I want to use range in healthcare app and value user stores it must be the same when presenting.
So in that case it is [7-8] and [7-9) are not the same thing :)
But still it does feel a bit strange that value I'm storing is different when presenting.
For example I want to use range in healthcare app and value user stores it must be the same when presenting.
So in that case it is [7-8] and [7-9) are not the same thing :)
On January 7, 2023, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Dino Maric <dinom@hey.com> writes:
> When I insert range like this:
> INSERT INTO public.tests
> VALUES (int4range(7,8,'[]'))
> After when querying table my return value for this column is not [7,8]
> but it is [7,9).
> I found this behaviour confusing, because I want to insert 7-8 ranges
> (including upper value) and then present that range to a user.
This is the effect of canonicalization, as explained here:
https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-DISCRETE
If you don't like it you can make a range type with a different
canonicalization function, or no such function, but that might
have odd effects on the behavior of range comparison operators.
regards, tom lane
--
Sent with HEY — Email as it should be