为什么创建带有序列列的 table 会创建一个最大值为 bigint 的序列?

Why does creating a table with a serial column create a sequence with a max val of a bigint?

我有点困惑,是 PostgreSql 的新手,我认为 serial = integer 和 bigserial = bigint,所以我很困惑为什么当我创建一个 table 与串行 PK。

the PostgreSQL 9.4 docs for Data Type SERIAL 中所述,序列是使用 CREATE SEQUENCE tablename_colname_seq 隐式创建的,没有任何其他参数。

documentation for CREATE SEQUENCE 说:

The optional clause MAXVALUE maxvalue determines the maximum value for the sequence. If this clause is not supplied or NO MAXVALUE is specified, then default values will be used. The defaults are 2^63-1 and -1 for ascending and descending sequences, respectively.

因此记录的行为是使用您正在寻找的最大值创建序列。

序列和列是两个不同的东西。一个序列可以 return 一个大于 int4 列可以存储的整数。当您指定 SERIALBIGSERIAL 时,PostgreSQL 粗略地将其转换为:

  1. 创建一个 int4 或 int8 列
  2. 创建序列
  3. 使列拥有的序列
  4. 将列默认值设置为序列中的下一个值

SERIAL 是为了方便,但没有实际的列类型 SERIAL