How large can an id get in postgresql

て烟熏妆下的殇ゞ 提交于 2020-01-29 04:28:07

问题


I am using postgresql, and was wondering how large

id INTEGER PRIMARY KEY

can get compared to

id SERIAL PRIMARY KEY

In java an int is 4 bytes (32 bits) so it can get up to 2,147,483,647. Is this the case in postgresql? If so does that mean I cannot go past 2,147,483,647 rows?


回答1:


Here is a handy chart for PostgreSQL:

Name        Storage Size    Description                       Range
smallint    2 bytes         small-range integer               -32768 to +32767
integer     4 bytes         usual choice for integer          -2147483648 to +2147483647
bigint      8 bytes         large-range integer               -9223372036854775808 to 9223372036854775807
serial      4 bytes         autoincrementing integer          1 to 2147483647
bigserial   8 bytes         large autoincrementing integer    1 to 9223372036854775807

Source

Your assessment is right, you'd run out of unique ID's if you used a data type that was insufficient.




回答2:


The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases)

A bigserial is 8 bytes long. If that is not enough it is possible to use the 128 bits uuid:

create table t (
    id uuid primary key
);
insert into t (id)
select uuid_generate_v1mc();
select * from t;
                  id                  
--------------------------------------
 916bf7e6-f0c2-11e2-8d14-d372d5ab075f

The uuid_generate_v1mc function is provided by the uuid-ossp module

The main advantage of the uuid functions is that they generate an id that is very likely to be unique among different systems. A serial will have collisions between those systems.



来源:https://stackoverflow.com/questions/17755447/how-large-can-an-id-get-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!