Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: data types - How to store uint64 in postresql

Tags:

postgresql

Bigint in postgresql is 8 byte integer. which is has half the range as uint64 (as one bit is used to sign the integer)

I need to do a lot of aggregation on the column and I am under the impression that aggregation on NUMERIC type is slow in comparison to integer types.

How should I optimize my storage in this case?

like image 915
samol Avatar asked Feb 20 '14 13:02

samol


People also ask

How can you store the binary data in PostgreSQL?

PostgreSQL provides two distinct ways to store binary data. Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

What is integer [] in PostgreSQL?

Integer ( INT ) is a 4-byte integer that has a range from -2,147,483,648 to 2,147,483,647. Serial is the same as integer except that PostgreSQL will automatically generate and populate values into the SERIAL column. This is similar to AUTO_INCREMENT column in MySQL or AUTOINCREMENT column in SQLite.

What type of data is stored in PostgreSQL?

PostgreSQL has three character data types namely, CHAR(n), VARCHAR(n), and TEXT. CHAR(n) is used for data(string) with a fixed-length of characters with padded spaces.


3 Answers

Unless you have a concrete reason, just use NUMERIC. It is slower, quite a lot slower, but that might not matter as much as you think.

You don't really have any alternative, as PostgreSQL doesn't support unsigned 64-bit integers at the SQL level. You could add a new datatype as an extension module, but it'd be a lot of work.

You could shove the unsigned 64-bit int bitwise into a 64-bit signed int, so values above maxuint64/2 are negative. But that'll be totally broken for aggregation, and would generally be horribly ugly.

like image 145
Craig Ringer Avatar answered Oct 19 '22 06:10

Craig Ringer


There is also an extension to provide an additional uint64 datatype in postgresql. See Github

It is by Peter Eisentraut

like image 35
schoetbi Avatar answered Oct 19 '22 07:10

schoetbi


sum() will return numeric if the input is bigint so it will not overflow

select sum(a)
from (values (9223372036854775807::bigint), (9223372036854775807)) s(a)
;
         sum          
----------------------
 18446744073709551614

http://www.postgresql.org/docs/current/static/functions-aggregate.html

like image 2
Clodoaldo Neto Avatar answered Oct 19 '22 08:10

Clodoaldo Neto