Skip to content
Advertisement

Lower bound for Postgres integer type out of range?

Per Postgres documentation, an integer type is defined between -2147483648 and +2147483647.
I thought that these boundaries were inclusive, but if I try:

an Integer out of range error is raised.

The upper bound seems to be casted properly:

And if I increase the lower bound by one it works fine as well:

The same result is applied to smallint. Is there something obvious that I’m missing here, or are lower bounds excluded for Postgres numeric types?

Advertisement

Answer

TLDR: operator precedence.

This is tricky at first sight. The same cast of the lower bound seemingly fails for smallint and bigint, too:

But looks are deceiving. This is what really happens:

- is taken to be “unary minus” operator, which only kicks in after :: (the “PostgreSQL-style typecast”). And since the range of integer (int4) is -2147483648 to +2147483647 as you quoted accurately, the expression fails at:

db<>fiddle here

Use one of these instead:

Also ever so slightly more efficient, since that’s just a cast, not a cast + negation operation.

You could even:

Just to overrule operator precedence. But the last one looks awkward. And it’s slightly less efficient. 🙂

Related:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement