KingbaseES普通表修改表结构请参考:KingbaseES变更表结构表重写问题

数据类型转换重写与不重写:

  • varchar(x) 转换到 varchar(y) 当 y>=x,不需要重写。
  • numeric(x,z) 转换到 numeric(y,z) 当 y>=x,或者不指定精度类型,不需要重写。
  • numeric(x,c) 转换到 numeric(y,z) 当 y=x c>z,当numeric数据类型标度不一致时,需要重写。
  • varbit(x) 转换到 varbit(y) 当 y>=x,不需要重写。
  • timestamp(x) 转换到 timestamp(y) 当 y>=x,或者转换为timestamp,不需要重写。
  • timestamptz(x) 转换到 timestamptz(y) 当 y>=x,或者转换为timestamptz,不需要重写。
  • interval(x) 转换到 interval(y) 当 y>=x ,或者转换为interval,不需要重写。
  • timestamp 转换到 text、varchar、varchar(n),char(n),需要重写。
  • timestamp(x)转换到 text、varchar、varchar(n)、char(n),n>=x,需要重写。
  • text 转换到 char、char(x)、varchar(n),需要重写。
  • text 转换到 varchar,不需要重写。
  • numeric(x) 转换到 numeric(y),y>=x,不需要重写。
  • numeric(x) 转换到 numeric,不需要重写
  • numeric(x,y) 转换到 numeric,不需要重写

一、普通表的修改:

普通表数据类型长度或者精度由小改大表不会重写,索引也不会重写。

test=# create table t01(id int,name varchar(10));
CREATE TABLE
test=# insert into t01 select generate_series(1,10),substr(md5(random()::text),1,10);
INSERT 0 10
test=# create index on t01 (name);
CREATE INDEX
test=# \d t01
Table "public.t01"
Column | Type | Collation | Nullable | Default
--------+----------------------------+-----------+----------+---------
id | integer | | |
name | character varying(10 char) | | |
Indexes:
"t01_name_idx" btree (name) test=#
test=# select sys_relation_filenode('t01'),sys_relation_filenode('t01_name_idx');
SYS_RELATION_FILENODE | SYS_RELATION_FILENODE
-----------------------+-----------------------
289043 | 289046
(1 row) # 设置客户端消息级别client_min_messages=debug5
test=# set client_min_messages=debug5; # 修改t01表name字段长度varchar(10)为varchar(15)
test=# alter table t01 alter column name type varchar(15);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: rehashing catalog cache id 68 for sys_recyclebin; 33 tups, 16 buckets
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428092/1/7
ALTER TABLE test=# select sys_relation_filenode('t01'),sys_relation_filenode('t01_name_idx');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILENODE | SYS_RELATION_FILENODE
-----------------------+-----------------------
289043 | 289046
(1 row) test=#

分区表修改表结构是否跟普通表一样?

二、分区表的修改:

分区表分区键无法修改数据类型,本测试只针对非分区键进行测试。

create table tb(id bigint,pdate date,info varchar2(10)) partition by range(pdate) INTERVAL ('1 MONTH'::INTERVAL)
(
PARTITION tb_p0 VALUES LESS THAN ('2023-01-01'),
PARTITION tb_p1 VALUES LESS THAN ('2023-02-01'),
PARTITION tb_p2 VALUES LESS THAN ('2023-03-01'),
PARTITION tb_p3 VALUES LESS THAN ('2023-04-01')
);
CREATE TABLE
insert into tb select generate_series(1,100),'2023-01-01'::date,substr(md5(random()::text),1,10);
INSERT 0 100
insert into tb select generate_series(101,200),'2023-02-01'::date,substr(md5(random()::text),1,10);
INSERT 0 100
insert into tb select generate_series(201,300),'2023-03-01'::date,substr(md5(random()::text),1,10);
INSERT 0 100

2.1.分区表非索引列数据类型的修改:

修改非索引列的类型:由小改大,表不会发生重写,索引自然也没有发生重写。

修改非索引列的类型:由大改小,遵循KingbaseES变更表结构表重写规则,表需要重写,索引也需要重写。

test=# set client_min_messages =debug5;
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SET
test=# select oid,relname from sys_class where relname='tb';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+---------
289063 | tb
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p0';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289066 | tb_tb_p0
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p1';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289069 | tb_tb_p1
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p2';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289072 | tb_tb_p2
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p3';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289075 | tb_tb_p3
(1 row) # 修改tb分区表info列为varchar(20)
test=# alter table tb alter column info type varchar(20);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428111/1/10
ALTER TABLE test=# select oid,relname from sys_class where relname='tb';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+---------
289063 | tb
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p0';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289066 | tb_tb_p0
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p1';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289069 | tb_tb_p1
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p2';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289072 | tb_tb_p2
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p3';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289075 | tb_tb_p3
(1 row) test=# alter table tb alter column info type varchar(30);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428112/1/10
ALTER TABLE
test=# select oid,relname from sys_class where relname='tb';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+---------
289063 | tb
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p0';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289066 | tb_tb_p0
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p1';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289069 | tb_tb_p1
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p2';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289072 | tb_tb_p2
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p3';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289075 | tb_tb_p3
(1 row)

分区表修改非索引列字段长度或者是精度由小变大,跟普通表一样,不需要重写。

2.2.分区表全局(GLOBAL)索引列数据类型的修改:

修改全局(GLOBAL)索引列的类型:由小改大,表不会发生重写,索引也没有发生重写。

修改全局(GLOBAL)索引列的类型:由大改小,遵循KingbaseES变更表结构表重写规则,表需要重写,索引也需要重写。

test=# \d+ tb
Partitioned table "public.tb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | bigint | | | | plain | |
pdate | date | | | | plain | |
info | character varying(10 char) | | not null | | extended | |
Partition key: RANGE (pdate)
Range interval: INTERVAL ('0-1'::pg_catalog.interval)
Partitions: tb_tb_p0 FOR VALUES FROM (MINVALUE) TO ('2023-01-01 00:00:00'),
tb_tb_p1 FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2023-02-01 00:00:00'),
tb_tb_p2 FOR VALUES FROM ('2023-02-01 00:00:00') TO ('2023-03-01 00:00:00'),
tb_tb_p3 FOR VALUES FROM ('2023-03-01 00:00:00') TO ('2023-04-01 00:00:00') # 添加主键
test=# alter table tb add constraint tb_pk primary key (info);
ALTER TABLE
test=# \d+ tb
Partitioned table "public.tb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | bigint | | | | plain | |
pdate | date | | | | plain | |
info | character varying(10 char) | | not null | | extended | |
Partition key: RANGE (pdate)
Range interval: INTERVAL ('0-1'::pg_catalog.interval)
Indexes:
"tb_pk" PRIMARY KEY, btree (info) INCLUDE (tableoid) GLOBAL
Partitions: tb_tb_p0 FOR VALUES FROM (MINVALUE) TO ('2023-01-01 00:00:00'),
tb_tb_p1 FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2023-02-01 00:00:00'),
tb_tb_p2 FOR VALUES FROM ('2023-02-01 00:00:00') TO ('2023-03-01 00:00:00'),
tb_tb_p3 FOR VALUES FROM ('2023-03-01 00:00:00') TO ('2023-04-01 00:00:00') test=# select sys_relation_filepath('tb_pk');
SYS_RELATION_FILEPATH
-----------------------
base/12176/289111
(1 row) test=# select oid,relname from sys_class where relname='tb';
OID | RELNAME
--------+---------
289063 | tb
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p0';
OID | RELNAME
--------+----------
289066 | tb_tb_p0
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p1';
OID | RELNAME
--------+----------
289069 | tb_tb_p1
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p2';
OID | RELNAME
--------+----------
289072 | tb_tb_p2
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p3';
OID | RELNAME
--------+----------
289075 | tb_tb_p3
(1 row) test=# alter table tb alter info type varchar(20);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: drop auto-cascades to index tb_pk
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428121/1/19
ALTER TABLE
test=# select sys_relation_filepath('tb_pk');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH
-----------------------
base/12176/289111
(1 row) test=# select oid,relname from sys_class where relname='tb';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+---------
289063 | tb
(1 row) test=# select oid,relname from sys_class where relname='tb_tb_p2';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+----------
289072 | tb_tb_p2
(1 row) # 添加全局索引
test=# create unique index on tb(id,info) global;
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: building index "tb_id_info_idx" on table "tb" serially
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428122/1/4
CREATE INDEX
test=# \d+ tb
Partitioned table "public.tb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | bigint | | | | plain | |
pdate | date | | | | plain | |
info | character varying(20 char) | | not null | | extended | |
Partition key: RANGE (pdate)
Range interval: INTERVAL ('0-1'::pg_catalog.interval)
Indexes:
"tb_pk" PRIMARY KEY, btree (info) INCLUDE (tableoid) GLOBAL
"tb_id_info_idx" UNIQUE, btree (id, info) INCLUDE (tableoid) GLOBAL
Partitions: tb_tb_p0 FOR VALUES FROM (MINVALUE) TO ('2023-01-01 00:00:00'),
tb_tb_p1 FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2023-02-01 00:00:00'),
tb_tb_p2 FOR VALUES FROM ('2023-02-01 00:00:00') TO ('2023-03-01 00:00:00'),
tb_tb_p3 FOR VALUES FROM ('2023-03-01 00:00:00') TO ('2023-04-01 00:00:00') test=# select sys_relation_filepath('tb_pk');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH
-----------------------
base/12176/289111
(1 row) test=# select sys_relation_filepath('tb_id_info_idx');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH
-----------------------
base/12176/289115
(1 row) test=# alter table tb alter info type varchar(30);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: drop auto-cascades to index tb_pk
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428123/1/27
ALTER TABLE
test=# select sys_relation_filepath('tb_pk');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH
-----------------------
base/12176/289111
(1 row) test=# select sys_relation_filepath('tb_id_info_idx');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH
-----------------------
base/12176/289115
(1 row)

分区表修改全局(GLOBAL)索引列字段长度或者是精度由小变大,跟普通表一样,不需要重写。

2.3.分区表本地(LOCAL)索引列数据类型的修改:

修改本地(LOCAL)索引列的类型:由小改大,表不会发生重写,索引发生重写。

修改本地(LOCAL)索引列的类型:由大改小,遵循KingbaseES变更表结构表重写规则,表需要重写,索引也需要重写。

test=# \d+ tb
Partitioned table "public.tb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | bigint | | | | plain | |
pdate | date | | | | plain | |
info | character varying(30 char) | | not null | | extended | |
Partition key: RANGE (pdate)
Range interval: INTERVAL ('0-1'::pg_catalog.interval)
Indexes:
"tb_pk" PRIMARY KEY, btree (info) INCLUDE (tableoid) GLOBAL
"tb_id_info_idx" UNIQUE, btree (id, info) INCLUDE (tableoid) GLOBAL
Partitions: tb_tb_p0 FOR VALUES FROM (MINVALUE) TO ('2023-01-01 00:00:00'),
tb_tb_p1 FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2023-02-01 00:00:00'),
tb_tb_p2 FOR VALUES FROM ('2023-02-01 00:00:00') TO ('2023-03-01 00:00:00'),
tb_tb_p3 FOR VALUES FROM ('2023-03-01 00:00:00') TO ('2023-04-01 00:00:00') # 创建本地索引
test=# create index on tb(pdate,info);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: building index "tb_tb_p0_pdate_info_idx" on table "tb_tb_p0" serially
DEBUG: building index "tb_tb_p1_pdate_info_idx" on table "tb_tb_p1" serially
DEBUG: building index "tb_tb_p2_pdate_info_idx" on table "tb_tb_p2" serially
DEBUG: building index "tb_tb_p3_pdate_info_idx" on table "tb_tb_p3" serially
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428125/1/12
CREATE INDEX
test=# \d+ tb
Partitioned table "public.tb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | bigint | | | | plain | |
pdate | date | | | | plain | |
info | character varying(30 char) | | not null | | extended | |
Partition key: RANGE (pdate)
Range interval: INTERVAL ('0-1'::pg_catalog.interval)
Indexes:
"tb_pk" PRIMARY KEY, btree (info) INCLUDE (tableoid) GLOBAL
"tb_id_info_idx" UNIQUE, btree (id, info) INCLUDE (tableoid) GLOBAL
"tb_pdate_info_idx" btree (pdate, info)
Partitions: tb_tb_p0 FOR VALUES FROM (MINVALUE) TO ('2023-01-01 00:00:00'),
tb_tb_p1 FOR VALUES FROM ('2023-01-01 00:00:00') TO ('2023-02-01 00:00:00'),
tb_tb_p2 FOR VALUES FROM ('2023-02-01 00:00:00') TO ('2023-03-01 00:00:00'),
tb_tb_p3 FOR VALUES FROM ('2023-03-01 00:00:00') TO ('2023-04-01 00:00:00') test=# select sys_relation_filepath('tb_pk'),sys_relation_filepath('tb_id_info_idx');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH | SYS_RELATION_FILEPATH
-----------------------+-----------------------
base/12176/289111 | base/12176/289115
(1 row) test=# select oid,relname from sys_class where relname='tb_pdate_info_idx';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+-------------------
289119 | tb_pdate_info_idx
(1 row) # 修改本地索引依赖info列
test=# alter table tb alter info type varchar(40);
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: EventTriggerInvoke 267860
DEBUG: drop auto-cascades to index tb_tb_p0_pdate_info_idx
DEBUG: drop auto-cascades to index tb_tb_p1_pdate_info_idx
DEBUG: drop auto-cascades to index tb_tb_p2_pdate_info_idx
DEBUG: rehashing catalog cache id 68 for sys_recyclebin; 33 tups, 16 buckets
DEBUG: drop auto-cascades to index tb_tb_p3_pdate_info_idx
DEBUG: drop auto-cascades to index tb_pk
DEBUG: building index "tb_tb_p0_pdate_info_idx" on table "tb_tb_p0" serially
DEBUG: building index "tb_tb_p1_pdate_info_idx" on table "tb_tb_p1" serially
DEBUG: building index "tb_tb_p2_pdate_info_idx" on table "tb_tb_p2" serially
DEBUG: building index "tb_tb_p3_pdate_info_idx" on table "tb_tb_p3" serially
DEBUG: EventTriggerInvoke 13761
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 428127/1/51
ALTER TABLE
test=# select sys_relation_filepath('tb_pk'),sys_relation_filepath('tb_id_info_idx');
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
SYS_RELATION_FILEPATH | SYS_RELATION_FILEPATH
-----------------------+-----------------------
base/12176/289111 | base/12176/289115
(1 row) test=# select oid,relname from sys_class where relname='tb_pdate_info_idx';
DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
OID | RELNAME
--------+-------------------
289127 | tb_pdate_info_idx
(1 row) test=#

分区表本地(LOCAL)索引列修改字段长度或者是精度由小变大,表、全局索引不需要重写,但是本地索引需要重写。

2.4.分区表修改表结构总结:

  • 分区表修改非索引列字段长度或者是精度由小变大,跟普通表一样,不需要重写。

  • 分区表修改全局(GLOBAL)索引列字段长度或者是精度由小变大,跟普通表一样,不需要重写。

  • 分区表本地(LOCAL)索引列修改字段长度或者是精度由小变大,表、全局索引不需要重写,但是本地索引需要重写。

三、分区表变更表结构的思考:

通过以上测试可知:分区表修改本地索引依赖字段长度或者标度会导致索引重写。

如果分区表数据量大、子分区多、字段列依赖的索引多,修改分区表(本地索引)列字段长度会发生如下问题:

修改字段长度由小到大:分区表所有的子表不会发生重写,但是索引会发生重写,索引越多阻塞时间越长。

目前想到的解决方法:删除被修改列依赖的所有本地索引,避免长时间的AccessExclusiveLock,修改完成后子表使用concurrently的方式创建。

修改字段长度由大到小:分区表所有的子表都会发生重写,只要表发生重写所有的索引都需要重写,此过程会导致业务不可用(影响太大)。

对分区表使用detach?分区表要求所有子分区的表结构必须跟父表一致,所有的子分区全部修改完成后再attach回去,跟直接改区别不大,可以避免业务中断。需要根据业务提前准备好脚本。

KingbaseES 分区表修改字段类型的更多相关文章

  1. PostgreSQL 修改字段类型从int到bigint

    由于现在pg的版本,修改int到bigint仍然需要rewrite表,会导致表阻塞,无法使用.但可以考虑其他方式来做.此问题是排查现网pg使用序列的情况时遇到的. 由于int的最大值只有21亿左右,而 ...

  2. sqlServer 2008修改字段类型和重命名字段名称的sql语句

    sqlServer 2008修改字段类型和重命名字段名称的sql语句 //修改字段的类型 alter table fdi_news alter column c_author nvarchar(50) ...

  3. Oracle/SQL 修改字段类型和长度

    标准SQL修改字段类型和长度语句: ALTER TABLE tableName modify column columnName 类型;例如Mysql的修改字段类型语句:alter table tes ...

  4. Mysql字段操作—增加字段、删除字段、修改字段名、修改字段类型(约束条件)

    1.增加字段:    alter table   tablename    add   new_field_id   type   not null default '0';     例:     a ...

  5. oracle如何修改字段类型(oracle总体知识2)

    在一次做开发的时候,遇到需要将数据表的字段类型由number改成varchar,可是该字段又有值, 用  alter table t-name modify cname newType;会报错. 话说 ...

  6. Oracle修改字段类型和长度

    Oracle修改字段名 alter table 表名 rename column 旧字段名 to 新字段名 Oracle修改字段类型和长度 alter table 表名 modify 字段名 数据类型 ...

  7. sql语句修改字段类型和增加字段

    /*修改字段类型*/ ) go /*增加字段和说明*/ ) EXECUTE sp_addextendedproperty N'MS_Description','说明文字',N'user',N'dbo' ...

  8. Oracle创建表、修改字段类型

    1.创建表 1.创建表 create table SCM_PER( --SCM_PER表名 ID ) primary key,--主键ID USERID ),--用户ID --Permission v ...

  9. Oracle基本操作,Oracle修改列名,Oracle修改字段类型

    oracle基本操作,Oracle修改列名,Oracle修改字段类型 >>>>>>>>>>>>>>>>& ...

  10. 曲演杂坛--使用ALTER TABLE修改字段类型的吐血教训

    --===================================================================== 事件起因:开发发现有表插入数据失败,查看后发现INT类型 ...

随机推荐

  1. kmp、z算法、exkmp

    一.kmp算法 1.基本概念 模式串:P 匹配串:T kmp算法精髓:找打一个最大的x,使得T[s+1,...,s+k]的后x个字符,和P的前x个字符相同. 2.next数组 next数组:记录模式串 ...

  2. 【libGDX】ApplicationAdapter生命周期

    1 前言 ​ libGDX 中,用户自定义的渲染窗口需要继承 ApplicationAdapter 类,ApplicationAdapter 实现了 ApplicationListener 接口,但实 ...

  3. SpringBoot下Akka的简单使用

    SpringBoot下Akka的简单使用 Akka框架实现一个异步消息传输,通过定义演员来处理业务逻辑. 首先引入依赖 <!-- akka --> <dependency> & ...

  4. 【Android逆向】制作Fart脱壳机,完成对NCSearch的脱壳操作

    1. 我的手机是Pixel 1 ,下载fart对应的镜像 镜像位置具体参考大佬博客 https://www.anquanke.com/post/id/201896 2 执行 adb reboot bo ...

  5. 如何渲染最原始的yuv视频数据?

    一.整体思路 我们在用纹理增加细节那篇文章中提到过,要将图片渲染在屏幕上,首先要拿到图片的像素数组数据,然后将像素数组数据通过纹理单元传递到片段着色器中,最后通过纹理采样函数将纹理中对应坐标的颜色值采 ...

  6. zip压缩模块,tarfile压缩模块,包和模块,format格式化的复习--day17

    1.zipfile模块 import zipfile #导入模块 1.压缩文件 (1)创建压缩包 参数1压缩包名字,参数2以w模式创建,参数3压缩固定写法 zf = zipfile.ZipFile(& ...

  7. vscode添加gitbash终端方法

    1.打开vscode 2.点击文件,ctrl+, 3.搜索shell windows { ... // 添加如下代码 "terminal.integrated.profiles.window ...

  8. Spring使用注解方式进行事务管理

    目录 使用步骤: 步骤一.在spring配置文件中引入tx:命名空间 步骤二.具有@Transactional 注解的bean自动配置为声明式事务支持 步骤三.在接口或类的声明处 ,写一个@Trans ...

  9. 固态硬盘使用f2fs作为根分区安装linux

    目录 前言 碰到的问题 对策 我的实际操作步骤 0.警告 1. 准备 2. 分区 3. 使用网络安装debian10 4. 备份根分区 5. 修改固态硬盘linux根分区为f2fs 6.恢复备份 7. ...

  10. C#多线程(5):资源池限制

    目录 Semaphore.SemaphoreSlim 类 Semaphore 类 示例 示例说明 信号量 SemaphoreSlim类 示例 区别 Semaphore.SemaphoreSlim 类 ...