专业企业网站开发公司,做网站要几个人,国外搜索引擎入口,汕头网站建设推广费用1. serial 与 bigserial 类型理解
serial 与 bigserial 在 PostgreSQL 中属于伪类型#xff0c;实际只在 create table 时可以引用该类型。serial 和 bigserial 类型#xff0c;实际上在这个字段上做的操作就是#xff1a;
创建了一个 integer (serial) 和 bigint (bigser…1. serial 与 bigserial 类型理解
serial 与 bigserial 在 PostgreSQL 中属于伪类型实际只在 create table 时可以引用该类型。serial 和 bigserial 类型实际上在这个字段上做的操作就是
创建了一个 integer (serial) 和 bigint (bigserial) 类型创建一个对应的序列序列的最大值指定为 integer 和 bigint 的最大值把这个序列和字段关联上
我们可以通过下面表 t1 理解一下上面说的三步
建表语句
postgres# create table t1(id serial, bid bigserial, tid int, btid bigint);
CREATE TABLEpostgres# \d t1Table public.t1Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------------------------------------------------------------------------------------------------------------------id | integer | | not null | nextval(t1_id_seq::regclass) | plain | | |bid | bigint | | not null | nextval(t1_bid_seq::regclass) | plain | | |tid | integer | | | | plain | | |btid | bigint | | | | plain | | |
Access method: heap查看序列的定义
postgres# \d t1_id_seqSequence public.t1_id_seqType | Start | Minimum | Maximum | Increment | Cycles? | Cache
----------------------------------------------------------------integer | 1 | 1 | 2147483647 | 1 | no | 1
Owned by: public.t1.idpostgres# \d t1_tid_seqSequence public.t1_bid_seqType | Start | Minimum | Maximum | Increment | Cycles? | Cache
------------------------------------------------------------------------bigint | 1 | 1 | 9223372036854775807 | 1 | no | 1
Owned by: public.t1.bid复现语句
create table t2 (tid bigint not null primary key);
create sequence t2_tid1_seq;
alter table t2 alter column set default nextval(t2_tid1_seq ::regclass);2. 转换方法
之所以要聊一聊转换方法其实是源于一次与研发同事的讨论。研发的同事说可以直接把定义了 serial 字段类型的字段从 int4 (integer) 改为 int8 (bigint)但是我一直认为这样做是没有意义的因为按照 PostgreSQL 严谨的作风序列绝对是有最大值限制的于是就有了第一部分对于表结构和序列定义的详细调查。
事实证明有如下几个结论
serial 字段的 int4 是可以直接改为 int8 的并没有严格限制但是从 int4 改为 int8 并没有什么实际意义serial 字段仍然不能存储超过 2147483647 条数据serial 的序列有最大值 2147483647并且达到最大值后不允许循环使用如果要将 serial 的存储大小从 int4 变更为更大只能通过复现语句创建新的 int8 序列修改原字段类型为 int8 后重新关联新索引下面有具体步骤
将 serial 改为 bigserial 的具体步骤
-- 先把字段类型调整为 int8, 这一步操作耗时较长在8核心SSD固态盘的主机上2亿条数据耗时超过一刻钟
alter table t1 alter column id type int8;-- 创建sequence, 默认bigint
-- 起始值从当前表中选取
select max(id) into max_id_fromt1 from t1;
create sequence t1_id1_seq start with max_id_fromt1;-- 先将表的自增量主键字段 默认值置为null
alter table t1 alter column id set default null;-- 设置默认值为新的 sequence
alter table t1 alter column id set default nextval(t1_id1_seq ::regclass);-- 原来的sequence 可以删除
drop sequence t1_id_seq;扩展
PostgreSQL 中支持 3 种自增资端最大长度分别如下
smallserial - 32767serial - 2147483647bigserial - 9223372036854775807
End