Aurora PostgreSQL,版本 10.4。
我有一个包含几百万行的表。其中一列定义为 character varying(255)
。曾几何时,255 已经足够了,但现在不行了,所以我必须做更多。
我找到了这个 in the PG 9.1 release notes :
- Allow ALTER TABLE ... SET DATA TYPE to avoid table rewrites in appropriate cases (Noah Misch, Robert Haas)
For example, converting a varchar column to text no longer requires a rewrite of the table. However, increasing the length constraint on a varchar column still requires a table rewrite.
这表明更改为更长的 varchar
是不切实际的(因为重写该大小的表会锁定它很长时间),但更改为 text
会工作。这是正确的吗?
进行此类更改时,我还应该了解其他任何事情吗?很明显,我想避免数据丢失,而且我不能让这个表在短时间内无法访问。
最佳答案
您应该已经阅读了所有发行说明。
因为只是 one version later
Increasing the length limit for a varchar or varbit column, or removing the limit altogether, no longer requires a table rewrite.
您可以自己轻松测试:
postgres=# select version(); version ------------------------------------------------------------ PostgreSQL 10.5, compiled by Visual C++ build 1800, 64-bit (1 row) postgres=# \timing on Timing is on. postgres=# create table alter_test (id serial, some_col varchar(255)); CREATE TABLE Time: 22.331 ms postgres=# insert into alter_test (some_col) select md5(random()::text) from generate_series(1,10e6); INSERT 0 10000000 Time: 40894.275 ms (00:40.894) postgres=# alter table alter_test alter column some_col type varchar(500); ALTER TABLE Time: 5.297 ms postgres=#
关于PostgreSQL:在非常大的表中增加列的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290815/