id 在 postgresql 中可以有多大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17755447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:01:38  来源:igfitidea点击:

How large can an id get in postgresql

postgresqlintegerprimary-key

提问by abden003

I am using postgresql, and was wondering how large

我正在使用 postgresql,并想知道有多大

id INTEGER PRIMARY KEY

can get compared to

可以比较

id SERIAL PRIMARY KEY

In java an intis 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?

在 java 中,anint是 4 个字节(32 位),因此它可以达到 2,147,483,647。在 postgresql 中是这种情况吗?如果是这样,那是否意味着我不能超过 2,147,483,647 行?

回答by Hart CO

Here is a handy chart for PostgreSQL:

这是一个方便的 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.

您的评估是正确的,如果您使用的数据类型不足,您的唯一 ID 就会用完。

回答by Clodoaldo Neto

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)

数据类型 smallserial、serial 和 bigserial 不是真正的类型,而仅仅是创建唯一标识符列的符号方便(类似于某些其他数据库支持的 AUTO_INCREMENT 属性)

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

Abigserial是 8 个字节长。如果这还不够,可以使用128 位 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_v1mcfunction is provided by the uuid-ossp module

uuid_generate_v1mc功能由uuid-ossp模块提供

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

uuid 函数的主要优点是它们生成的 id 在不同系统中很可能是唯一的。Aserial将在这些系统之间发生冲突。