unsigned int64 в postgresql

Как в postgresql сделать колонку с типом unsigned int64? По дефолту такого типа нет. Какие есть решения?


Ответы (1 шт):

Автор решения: Мелкий

В postgresql нет unsigned типов данных. Если вам нужно ограничение на минимальное значение - то добавьте check constraint, например во время create table:

create table foo (
  i bigint check (i >= 0)
);

Если вам не хватает диапазона значений bigint - то используйте numeric.


Почему их нет - сошлюсь на core разработчика postgresql в пояснении к релевантному расширению:

Support for unsigned integer types and smaller integer types has been one of the more common outstanding feature request for PostgreSQL. Inclusion of additional integer types into the core is typically rejected with the argument that it would make the type system too complicated and fragile. The experience from writing this module suggests: That is not wrong. Another argument, either explicit or implicit, is that it is a lot of work. Again: true.

The combination of the requirements of the SQL standard and the type system of PostgreSQL effectively create a situation where you need to provide a comprehensive set of operators and functions for each combination of numeric types. So for the three standard integer types, that's 9 "+" operators, 9 "<" operators, and so on. And with 3 + 5 = 8 types, well, you do the math. This module solves that problem by generating most of the code automatically.

Реализация unsigned типов потребует реализацию очень большого числа операторов для взаимодействия с другими типами данных - это негативная сторона впечатляющих возможностей postgresql для расширения.

→ Ссылка