postgresql 修改字段長(zhǎng)度的操作
使用數(shù)據(jù)庫postgresql的時(shí)候,有時(shí)會(huì)遇到字段長(zhǎng)度擴(kuò)展的情況,由于之前已經(jīng)有數(shù)據(jù)了,所以只能修改字段長(zhǎng)度,不能通過刪除再增加的方式。
可以使用如下方式進(jìn)行
ALTER TABLE your_table_name alter COLUMN your_column_name type character varying(3000);
通過上面的一句話就可以把你的表中相應(yīng)的字段的長(zhǎng)度修改為3000.
補(bǔ)充:PostgreSQL字符類型長(zhǎng)度變更的性能
背景
業(yè)務(wù)有時(shí)會(huì)遇到表中的字符型字段的長(zhǎng)度不夠用的問題,需要修改表定義。但是表里的數(shù)據(jù)已經(jīng)很多了,修改字段長(zhǎng)度會(huì)不會(huì)造成應(yīng)用堵塞呢?
測(cè)試驗(yàn)證
做了個(gè)小測(cè)試,如下
建表并插入1000w數(shù)據(jù)
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類型長(zhǎng)度
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
變更c(diǎn)har類型長(zhǎng)度
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
變更c(diǎn)har類型,varchar和text類型互轉(zhuǎn)
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
定義變更后的數(shù)據(jù)
定義變更后,數(shù)據(jù)位置未變,即沒有產(chǎn)生新的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擴(kuò)容以外的定義變更,每個(gè)tuple產(chǎn)生一條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
結(jié)論
varchar擴(kuò)容,varchar轉(zhuǎn)text只需修改元數(shù)據(jù),毫秒完成。
其它轉(zhuǎn)換需要的時(shí)間和數(shù)據(jù)量有關(guān),1000w數(shù)據(jù)10~40秒,但是不改變數(shù)據(jù)文件,只是做檢查。
縮容時(shí)如果定義長(zhǎng)度不夠容納現(xiàn)有數(shù)據(jù)報(bào)錯(cuò)
不建議使用char類型,除了埋坑幾乎沒什么用,這一條不僅適用與PG,所有關(guān)系數(shù)據(jù)庫應(yīng)該都適用。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL 數(shù)據(jù)同步到ES 搭建操作
這篇文章主要介紹了PostgreSQL 數(shù)據(jù)同步到ES 搭建操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享
這篇文章主要介紹了postgreSQL數(shù)據(jù)庫默認(rèn)用戶postgres常用命令分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL自定義函數(shù)并且調(diào)用方式
這篇文章主要介紹了PostgreSQL如何自定義函數(shù)并且調(diào)用,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06PostgreSQL數(shù)據(jù)庫中跨庫訪問解決方案
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫中跨庫訪問解決方案,需要的朋友可以參考下2017-05-05PostgreSQL 查看數(shù)據(jù)庫,索引,表,表空間大小的示例代碼
PostgreSQL 提供了多個(gè)系統(tǒng)管理函數(shù)來查看表,索引,表空間及數(shù)據(jù)庫的大小,下面詳細(xì)介紹一下2013-08-08PostgreSQL數(shù)據(jù)庫中匿名塊的寫法實(shí)例
這篇文章主要介紹了PostgreSQL數(shù)據(jù)庫中匿名塊的寫法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01