[GENERAL] Negative numbers to DOMAIN casting

Поиск
Список
Период
Сортировка
От Matija Lesar
Тема [GENERAL] Negative numbers to DOMAIN casting
Дата
Msg-id CAPx3hmM3_RjqrEA3yLSXTytN+U9ThnPEhjF9rQAxh=sxi1mNyA@mail.gmail.com
обсуждение исходный текст
Ответы Re: [GENERAL] Negative numbers to DOMAIN casting
Список pgsql-general
Hi,

I have uint4 domain created like this:
CREATE DOMAIN uint4 AS int8
   CHECK(VALUE BETWEEN 0 AND 4294967295);


If I try to cast negative number to this domain check constraint is not validated:
SELECT -1::uint4, pg_typeof(-1::uint4), 1::uint4, pg_typeof(1::uint4);
 ?column? | pg_typeof | uint4 | pg_typeof
----------+-----------+-------+-----------
       -1 | bigint    |     1 | uint4

Also the pg_typeof returns bigint, but if i do int8 to int2 conversion pg_typeof returns right type:
SELECT pg_typeof(-1::int4::int2);
 pg_typeof
-----------
 smallint

If I put number inside brackets I get check error:
select (-1)::uint4;
ERROR:  value for domain uint4 violates check constraint "uint4_check"


The same error is thrown if I use CAST:
SELECT CAST(-1 AS uint4);
ERROR:  value for domain uint4 violates check constraint "uint4_check"

And also if domain is used in table then check is also working as expected:
CREATE TABLE test(i uint4);
INSERT INTO test VALUES(-1);
ERROR:  value for domain uint4 violates check constraint "uint4_check"

I tested this on PG 9.4 and 9.5.
Is this expected behavior?

Regards,
Matija Lesar

В списке pgsql-general по дате отправления:

Предыдущее
От: Thomas Kellerer
Дата:
Сообщение: Re: [GENERAL] pg_upgrade 9.0 to 9.6
Следующее
От: Mikhail
Дата:
Сообщение: [GENERAL] Re: pg_upgrade 9.0 to 9.6