Postgres varchar(100)[] -> text[] 列转换

Fel*_*ipe 3 postgresql datatypes alter-table ddl online-operations

我需要将列类型从 更改 varchar(100)[]text[]。该表有 > 100M 行和大量写入负载,因此我想避免获取访问排他锁并重写索引。我看到了这个问题,非阵列varchartext改变管理这一点,所以我想知道:做同样与阵列列应用?

文档+邮件列表表示,如果满足以下条件,此更改将很快:

旧类型要么是二进制强制转换为新类型,要么是新类型上的无约束域)

varchar(100)[]to是这种情况text[]吗?

Fel*_*ipe 5

调整邮件列表中的测试代码显示转换需要全表重写:

test=> set client_min_messages = debug1;
SET

test=> CREATE TABLE t1 (id serial PRIMARY KEY, name character varying(30)[]);
DEBUG:  CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
DEBUG:  building index "pg_toast_203729867_index" on table "pg_toast_203729867"
DEBUG:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
DEBUG:  building index "t1_pkey" on table "t1"
CREATE TABLE

test=> INSERT INTO t1 (id) SELECT generate_series(1,1000000) i;
INSERT 0 1000000

test=> CREATE INDEX ON t1 (name);
DEBUG:  building index "t1_name_idx" on table "t1"
CREATE INDEX

test=> ALTER TABLE t1 ALTER COLUMN name TYPE text[];
DEBUG:  building index "pg_toast_203730108_index" on table "pg_toast_203730108"
DEBUG:  rewriting table "t1"
DEBUG:  building index "t1_pkey" on table "t1"
DEBUG:  building index "t1_name_idx" on table "t1"
Run Code Online (Sandbox Code Playgroud)

但是,如果转换为varchar[],则不需要重写。不受约束varchartext 功能相同(并且都是非标准 PG 扩展)

test=> set client_min_messages = debug1;
SET
test=> CREATE TABLE t1 (id serial PRIMARY KEY, name character varying(30)[]);
DEBUG:  CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
DEBUG:  building index "pg_toast_49914_index" on table "pg_toast_49914" serially
DEBUG:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
DEBUG:  building index "t1_pkey" on table "t1" serially
DEBUG:  index "t1_pkey" can safely use deduplication
CREATE TABLE
test=> INSERT INTO t1 (id) SELECT generate_series(1,1000000) i;
INSERT 0 1000000
test=> CREATE INDEX ON t1 (name);
DEBUG:  building index "t1_name_idx" on table "t1" with request for 1 parallel worker
DEBUG:  index "t1_name_idx" cannot use deduplication
CREATE INDEX
test=> ALTER TABLE t1 ALTER COLUMN name TYPE varchar[];
ALTER TABLE
Run Code Online (Sandbox Code Playgroud)