9

How do I store an unsigned int (uint32) in postgres? I noticed that numeric(10,0) would fit the number of digits, but is this the best way?

On further research another similar problem is storing a uint64. I've found numeric(20,0) check (BETWEEN 0 AND '18446744073709551615'::numeric(20,0)). There aren't any native types for this I believe.

1

1 Answer 1

8

Arithmetic on numeric values is very slow compared to the integer types.

Use bigint. It stores an 8 byte integer up to 2^63 - 1 = 9223372036854775807

[You probably don't need the entire unsigned range]

4
  • 1
    I want to store the exact unsigned value.. So bigint is faster than numeric(10,0) check (value >= 0 and value < '4294967296'::numeric(10,0))?
    – Fire
    Sep 9, 2014 at 0:24
  • 3
    @Fire Much, much faster. You still need the check constraint. Sep 9, 2014 at 0:25
  • So bigint check (value >= 0 and value < '4294967296'::bigint)
    – Fire
    Sep 9, 2014 at 0:31
  • 9
    What if you're trying to store an unsigned Int64... and actually need the whole unsigned range? Feb 4, 2022 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.