怎么在postgresql中对字段长度进行修改

这期内容当中小编将会给大家带来有关怎么在postgresql中对字段长度进行修改,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

使用数据库postgresql的时候,有时会遇到字段长度扩展的情况,由于之前已经有数据了,所以只能修改字段长度,不能通过删除再增加的方式。

可以使用如下方式进行

ALTER TABLE your_table_name alter COLUMN your_column_name type character varying(3000);

通过上面的一句话就可以把你的表中相应的字段的长度修改为3000.

补充:PostgreSQL字符类型长度变更的性能

背景

业务有时会遇到表中的字符型字段的长度不够用的问题,需要修改表定义。但是表里的数据已经很多了,修改字段长度会不会造成应用堵塞呢?

测试验证

做了个小测试,如下

建表并插入1000w数据

postgres=# create table tbx1(id int,c1 char(10),c2 varchar(10));
CREATE TABLE
postgres=# insert into tbx1 select id ,'aaaaa','aaaaa' from generate_series(1,10000000) id;
INSERT 0 10000000

变更varchar类型长度

postgres=# alter table tbx1 alter COLUMN c2 type varchar(100);
ALTER TABLE
Time: 1.873 ms
postgres=# alter table tbx1 alter COLUMN c2 type varchar(99);
ALTER TABLE
Time: 12815.678 ms
postgres=# alter table tbx1 alter COLUMN c2 type varchar(4);
ERROR: value too long for type character varying(4)
Time: 5.328 ms

变更char类型长度

postgres=# alter table tbx1 alter COLUMN c1 type char(100);
ALTER TABLE
Time: 35429.282 ms
postgres=# alter table tbx1 alter COLUMN c1 type char(6);
ALTER TABLE
Time: 20004.198 ms
postgres=# alter table tbx1 alter COLUMN c1 type char(4);
ERROR: value too long for type character(4)
Time: 4.671 ms

变更char类型,varchar和text类型互转

alter table tbx1 alter COLUMN c1 type varchar(6);
ALTER TABLE
Time: 18880.369 ms
postgres=# alter table tbx1 alter COLUMN c1 type text;
ALTER TABLE
Time: 12.691 ms
postgres=# alter table tbx1 alter COLUMN c1 type varchar(20);
ALTER TABLE
Time: 32846.016 ms
postgres=# alter table tbx1 alter COLUMN c1 type char(20);
ALTER TABLE
Time: 39796.784 ms
postgres=# alter table tbx1 alter COLUMN c1 type text;
ALTER TABLE
Time: 32091.025 ms
postgres=# alter table tbx1 alter COLUMN c1 type char(20);
ALTER TABLE
Time: 26031.344 ms

定义变更后的数据

定义变更后,数据位置未变,即没有产生新的tuple

postgres=# select ctid,id from tbx1 limit 5;
 ctid | id 
-------+----
 (0,1) | 1
 (0,2) | 2
 (0,3) | 3
 (0,4) | 4
 (0,5) | 5
(5 rows)

除varchar扩容以外的定义变更,每个tuple产生一条WAL记录

$ pg_xlogdump -f -s 3/BE002088 -n 5
rmgr: Heap    len (rec/tot):   3/  181, tx:    1733, lsn: 3/BE002088, prev 3/BE001FB8, desc: INSERT off 38, blkref #0: rel 1663/13269/16823 blk 58358
rmgr: Heap    len (rec/tot):   3/  181, tx:    1733, lsn: 3/BE002140, prev 3/BE002088, desc: INSERT off 39, blkref #0: rel 1663/13269/16823 blk 58358
rmgr: Heap    len (rec/tot):   3/  181, tx:    1733, lsn: 3/BE0021F8, prev 3/BE002140, desc: INSERT off 40, blkref #0: rel 1663/13269/16823 blk 58358
rmgr: Heap    len (rec/tot):   3/  181, tx:    1733, lsn: 3/BE0022B0, prev 3/BE0021F8, desc: INSERT off 41, blkref #0: rel 1663/13269/16823 blk 58358
rmgr: Heap    len (rec/tot):   3/  181, tx:    1733, lsn: 3/BE002368, prev 3/BE0022B0, desc: INSERT off 42, blkref #0: rel 1663/13269/16823 blk 58358

结论

varchar扩容,varchar转text只需修改元数据,毫秒完成。

其它转换需要的时间和数据量有关,1000w数据10~40秒,但是不改变数据文件,只是做检查。

缩容时如果定义长度不够容纳现有数据报错

不建议使用char类型,除了埋坑几乎没什么用,这一条不仅适用与PG,所有关系数据库应该都适用。

上述就是小编为大家分享的怎么在postgresql中对字段长度进行修改了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。