【问题标题】:How large can an id get in postgresql一个 id 在 postgresql 中可以有多大
【发布时间】:2013-07-19 06:59:48
【问题描述】:

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

id INTEGER PRIMARY KEY

可以比较

id SERIAL PRIMARY KEY

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

【问题讨论】:

    标签: postgresql integer primary-key


    【解决方案1】:

    这是一个方便的 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
    smallserial 2 bytes         small autoincrementing integer    1 to 32767
    serial      4 bytes         autoincrementing integer          1 to 2147483647
    bigserial   8 bytes         large autoincrementing integer    1 to 9223372036854775807
    

    Source

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

    【讨论】:

    【解决方案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)

    bigserial 的长度为 8 个字节。如果这还不够,可以使用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
    

    uuid_generate_v1mc 函数由uuid-ossp module 提供

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

    【讨论】:

    • 在这里我认为我处理的万亿记录表很大,无法想象需要超过九个 quintillion ID。
    • 嘿...人们害怕用尽bigintjust haven't done the mathsuuid 密钥对于分布式系统非常方便,但对于密钥耗尽问题是不必要的..
    • @CraigRinger 由于生日悖论 bigint 可能在“用尽”之前很久就在键之间发生冲突。 UUID 有 128 位来避免这个问题。
    • @PaulAJungwirth 如果您随机生成密钥,是的。对于 BIGSERIAL 序列,没有。
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多