目录
概念描述
哪些操作会导致分区表的全局索引失效(比如 move partition,drop partition ,truncate partition ,split partition , merge partitions )
测试验证
1、环境准备
CREATE TABLE t_ran
(
user_number NUMBER(11),
start_time timestamp(0) without time zone,
business_name character(2000),
ng_staff_id character(50)
)
PARTITION BY RANGE (start_time)
(PARTITION PART_20180228 VALUES LESS THAN (TO_DATE('2018-03-01','YYYY-MM-DD')),
PARTITION PART_20180301 VALUES LESS THAN (TO_DATE('2018-03-02','YYYY-MM-DD')),
PARTITION PART_20180302 VALUES LESS THAN (TO_DATE('2018-03-03','YYYY-MM-DD')),
PARTITION PART_20180303 VALUES LESS THAN (TO_DATE('2018-03-04','YYYY-MM-DD')),
PARTITION PART_20180304 VALUES LESS THAN (TO_DATE('2018-03-05','YYYY-MM-DD')),
PARTITION PART_20180305 VALUES LESS THAN (TO_DATE('2018-03-06','YYYY-MM-DD')),
PARTITION PART_20180306 VALUES LESS THAN (TO_DATE('2018-03-07','YYYY-MM-DD')),
PARTITION PART_20180307 VALUES LESS THAN (TO_DATE('2018-03-08','YYYY-MM-DD')),
PARTITION PART_20180308 VALUES LESS THAN (TO_DATE('2018-03-09','YYYY-MM-DD')),
PARTITION PART_20180309 VALUES LESS THAN (TO_DATE('2018-03-10','YYYY-MM-DD')),
PARTITION PART_20180310 VALUES LESS THAN (TO_DATE('2018-03-11','YYYY-MM-DD')),
PARTITION PART_20180311 VALUES LESS THAN (TO_DATE('2018-03-12','YYYY-MM-DD')),
PARTITION PART_20180312 VALUES LESS THAN (TO_DATE('2018-03-13','YYYY-MM-DD')),
PARTITION PART_20180313 VALUES LESS THAN (TO_DATE('2018-03-14','YYYY-MM-DD')),
PARTITION PART_20180314 VALUES LESS THAN (TO_DATE('2018-03-15','YYYY-MM-DD')),
PARTITION PART_20180315 VALUES LESS THAN (TO_DATE('2018-03-16','YYYY-MM-DD')),
PARTITION PART_20180316 VALUES LESS THAN (TO_DATE('2018-03-17','YYYY-MM-DD')),
PARTITION PART_20180317 VALUES LESS THAN (TO_DATE('2018-03-18','YYYY-MM-DD')),
PARTITION PART_20180318 VALUES LESS THAN (TO_DATE('2018-03-19','YYYY-MM-DD')),
PARTITION PART_20180319 VALUES LESS THAN (TO_DATE('2018-03-20','YYYY-MM-DD')),
PARTITION PART_20180320 VALUES LESS THAN (TO_DATE('2018-03-21','YYYY-MM-DD')),
PARTITION PART_20180321 VALUES LESS THAN (TO_DATE('2018-03-22','YYYY-MM-DD')),
PARTITION PART_20180322 VALUES LESS THAN (TO_DATE('2018-03-23','YYYY-MM-DD')),
PARTITION PART_20180323 VALUES LESS THAN (TO_DATE('2018-03-24','YYYY-MM-DD')),
PARTITION PART_20180324 VALUES LESS THAN (TO_DATE('2018-03-25','YYYY-MM-DD')),
PARTITION PART_20180325 VALUES LESS THAN (TO_DATE('2018-03-26','YYYY-MM-DD')),
PARTITION PART_20180326 VALUES LESS THAN (TO_DATE('2018-03-27','YYYY-MM-DD')),
PARTITION PART_20180327 VALUES LESS THAN (TO_DATE('2018-03-28','YYYY-MM-DD')),
PARTITION PART_20180328 VALUES LESS THAN (TO_DATE('2018-03-29','YYYY-MM-DD')),
PARTITION PART_20180329 VALUES LESS THAN (TO_DATE('2018-03-30','YYYY-MM-DD')),
PARTITION PART_20180330 VALUES LESS THAN (TO_DATE('2018-03-31','YYYY-MM-DD')),
PARTITION PART_20180331 VALUES LESS THAN (TO_DATE('2018-04-01','YYYY-MM-DD')),
PARTITION PART_max VALUES LESS THAN (MAXVALUE))
;
openGauss=#
openGauss=# \d pg_index
Table "pg_catalog.pg_index"
Column | Type | Modifiers
----------------+--------------+-----------
indexrelid | oid | not null
indrelid | oid | not null
indnatts | smallint | not null
indisunique | boolean | not null
indisprimary | boolean | not null
indisexclusion | boolean | not null
indimmediate | boolean | not null
indisclustered | boolean | not null
indisusable | boolean | not null
indisvalid | boolean | not null
indcheckxmin | boolean | not null
indisready | boolean | not null
indkey | int2vector | not null
indcollation | oidvector | not null
indclass | oidvector | not null
indoption | int2vector | not null
indexprs | pg_node_tree |
indpred | pg_node_tree |
indisreplident | boolean |
indnkeyatts | smallint |
Indexes:
"pg_index_indexrelid_index" UNIQUE, btree (indexrelid) TABLESPACE pg_default
"pg_index_indrelid_index" btree (indrelid) TABLESPACE pg_default
Replica Identity: NOTHING
openGauss=# insert into t_ran select * from customer_t_copy;
INSERT 0 1282751
openGauss=# CREATE INDEX ind_t_ran ON t_ran(start_time) LOCAL;
CREATE INDEX
openGauss=#
openGauss=# CREATE INDEX ind2_t_ran ON t_ran(start_time,user_number) ;
CREATE INDEX
2、测试 truncate partition
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# alter table t_ran truncate partition part_20180319;
ALTER TABLE
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | f | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# alter index ind2_t_ran rebuild;
REINDEX
openGauss=#
openGauss=#
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
结论:经过以上实验验证,发现 indisusable 字段发生变化,truncate partition 这个操作会导致全局索引失效,分区索引没有影响。
3、测试 split partition
openGauss=# select relname,parentid,interval,boundaries from pg_partition where parentid=(select parentid from pg_partition where relname='t_ran' );
relname | parentid | interval | boundaries
---------------+----------+----------+-------------------------
t_ran | 40968 | |
part_20180301 | 40968 | | {"2018-03-02 00:00:00"}
part_20180302 | 40968 | | {"2018-03-03 00:00:00"}
part_20180303 | 40968 | | {"2018-03-04 00:00:00"}
part_20180304 | 40968 | | {"2018-03-05 00:00:00"}
part_20180305 | 40968 | | {"2018-03-06 00:00:00"}
part_20180306 | 40968 | | {"2018-03-07 00:00:00"}
part_20180307 | 40968 | | {"2018-03-08 00:00:00"}
part_20180308 | 40968 | | {"2018-03-09 00:00:00"}
part_20180309 | 40968 | | {"2018-03-10 00:00:00"}
part_20180310 | 40968 | | {"2018-03-11 00:00:00"}
part_20180311 | 40968 | | {"2018-03-12 00:00:00"}
part_20180312 | 40968 | | {"2018-03-13 00:00:00"}
part_20180313 | 40968 | | {"2018-03-14 00:00:00"}
part_20180316 | 40968 | | {"2018-03-17 00:00:00"}
part_20180317 | 40968 | | {"2018-03-18 00:00:00"}
part_20180318 | 40968 | | {"2018-03-19 00:00:00"}
part_20180319 | 40968 | | {"2018-03-20 00:00:00"}
part_20180320 | 40968 | | {"2018-03-21 00:00:00"}
part_20180322 | 40968 | | {"2018-03-23 00:00:00"}
part_20180323 | 40968 | | {"2018-03-24 00:00:00"}
part_20180324 | 40968 | | {"2018-03-25 00:00:00"}
part_20180326 | 40968 | | {"2018-03-27 00:00:00"}
part_20180327 | 40968 | | {"2018-03-28 00:00:00"}
part_20180328 | 40968 | | {"2018-03-29 00:00:00"}
part_20180330 | 40968 | | {"2018-03-31 00:00:00"}
part_20180331 | 40968 | | {"2018-04-01 00:00:00"}
part_max | 40968 | | {NULL}
(28 rows)
这个是opengauss数据库句法问题,和Oracle数据库语法不一样。
penGauss=# alter table t_ran split partition part_max at (to_date('2018-04-02','YYYY-MM-DD')) into (partition part_20180401,partition part_max);
ERROR: resulting partition "part_max" name conflicts with that of an existing partition
^
--------------------
GAUSS-00923: "resulting partition '%s' name conflicts with that of an existing partition"
SQLSTATE: 42710
错误原因: SPLIT PARTITION操作得到的分区名称与已有分区名冲突,该分割分区操作不能执行。
解决办法: 建议修改结果分区名称。
---------------------
openGauss=# select relname,parentid,interval,boundaries from pg_partition where parentid=(select parentid from pg_partition where relname='t_ran' );
relname | parentid | interval | boundaries
---------------+----------+----------+-------------------------
t_ran | 40968 | |
part_20180301 | 40968 | | {"2018-03-02 00:00:00"}
part_20180302 | 40968 | | {"2018-03-03 00:00:00"}
part_20180303 | 40968 | | {"2018-03-04 00:00:00"}
part_20180304 | 40968 | | {"2018-03-05 00:00:00"}
part_20180305 | 40968 | | {"2018-03-06 00:00:00"}
part_20180306 | 40968 | | {"2018-03-07 00:00:00"}
part_20180307 | 40968 | | {"2018-03-08 00:00:00"}
part_20180308 | 40968 | | {"2018-03-09 00:00:00"}
part_20180309 | 40968 | | {"2018-03-10 00:00:00"}
part_20180310 | 40968 | | {"2018-03-11 00:00:00"}
part_20180311 | 40968 | | {"2018-03-12 00:00:00"}
part_20180312 | 40968 | | {"2018-03-13 00:00:00"}
part_20180313 | 40968 | | {"2018-03-14 00:00:00"}
part_20180316 | 40968 | | {"2018-03-17 00:00:00"}
part_20180317 | 40968 | | {"2018-03-18 00:00:00"}
part_20180318 | 40968 | | {"2018-03-19 00:00:00"}
part_20180320 | 40968 | | {"2018-03-21 00:00:00"}
part_20180322 | 40968 | | {"2018-03-23 00:00:00"}
part_20180323 | 40968 | | {"2018-03-24 00:00:00"}
part_20180324 | 40968 | | {"2018-03-25 00:00:00"}
part_20180326 | 40968 | | {"2018-03-27 00:00:00"}
part_20180327 | 40968 | | {"2018-03-28 00:00:00"}
part_20180328 | 40968 | | {"2018-03-29 00:00:00"}
part_20180330 | 40968 | | {"2018-03-31 00:00:00"}
part_20180331 | 40968 | | {"2018-04-01 00:00:00"}
part_20180319 | 40968 | | {"2018-03-20 00:00:00"}
part_20180401 | 40968 | | {"2018-04-02 00:00:00"}
part_max1 | 40968 | | {NULL}
(29 rows)
openGauss=# alter table t_ran split partition part_max at (to_date('2018-04-02','YYYY-MM-DD')) into (partition part_20180401,partition part_max1);
ALTER TABLE
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | f | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# alter index ind2_t_ran rebuild;
REINDEX
openGauss=#
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=#
结论:经过以上实验验证,发现 indisusable 字段发生变化,split partition 这个操作会导致全局索引失效,分区索引没有影响。
4、测试merge partitions
openGauss=# alter table t_ran merge partitions part_20180320,part_20180322 into partition part_20180322;
ALTER TABLE
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | f | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# select 'alter index ' || indexrelid::regclass ||' rebuild; ' from pg_index where indisusable='f';
?column?
----------------------------------
alter index ind2_t_ran rebuild;
(1 row)
openGauss=# alter index ind2_t_ran rebuild;
REINDEX
openGauss=#
openGauss=#
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
结论:经过以上实验验证,发现 indisusable 字段发生变化,merge partition 这个操作会导致全局索引失效,分区索引没有影响。
5、drop partition
openGauss=# alter table t_ran drop partition part_20180322;
ALTER TABLE
openGauss=# select relname,parentid,interval,boundaries from pg_partition where parentid=(select parentid from pg_partition where relname='t_ran' );
relname | parentid | interval | boundaries
---------------+----------+----------+-------------------------
t_ran | 40968 | |
part_20180301 | 40968 | | {"2018-03-02 00:00:00"}
part_20180302 | 40968 | | {"2018-03-03 00:00:00"}
part_20180303 | 40968 | | {"2018-03-04 00:00:00"}
part_20180304 | 40968 | | {"2018-03-05 00:00:00"}
part_20180305 | 40968 | | {"2018-03-06 00:00:00"}
part_20180306 | 40968 | | {"2018-03-07 00:00:00"}
part_20180307 | 40968 | | {"2018-03-08 00:00:00"}
part_20180308 | 40968 | | {"2018-03-09 00:00:00"}
part_20180309 | 40968 | | {"2018-03-10 00:00:00"}
part_20180310 | 40968 | | {"2018-03-11 00:00:00"}
part_20180311 | 40968 | | {"2018-03-12 00:00:00"}
part_20180312 | 40968 | | {"2018-03-13 00:00:00"}
part_20180313 | 40968 | | {"2018-03-14 00:00:00"}
part_20180316 | 40968 | | {"2018-03-17 00:00:00"}
part_20180317 | 40968 | | {"2018-03-18 00:00:00"}
part_20180318 | 40968 | | {"2018-03-19 00:00:00"}
part_20180323 | 40968 | | {"2018-03-24 00:00:00"}
part_20180324 | 40968 | | {"2018-03-25 00:00:00"}
part_20180326 | 40968 | | {"2018-03-27 00:00:00"}
part_20180327 | 40968 | | {"2018-03-28 00:00:00"}
part_20180328 | 40968 | | {"2018-03-29 00:00:00"}
part_20180330 | 40968 | | {"2018-03-31 00:00:00"}
part_20180331 | 40968 | | {"2018-04-01 00:00:00"}
part_20180319 | 40968 | | {"2018-03-20 00:00:00"}
part_20180401 | 40968 | | {"2018-04-02 00:00:00"}
part_max1 | 40968 | | {NULL}
(27 rows)
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | f | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# alter index ind2_t_ran rebuild;
REINDEX
openGauss=#
openGauss=#
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
结论:经过以上实验验证,发现 indisusable 字段发生变化,drop partition 这个操作会导致全局索引失效,分区索引没有影响。
6、move partition
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=# alter table t_ran move partition part_20180401 TABLESPACE pg_default;
ALTER TABLE
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
miao=> CREATE TABLESPACE ds_tbs RELATIVE LOCATION 'db_tbs/';
CREATE TABLESPACE
miao=> \q
[omm@db1 db1]$
openGauss=# alter table t_ran move partition part_20180401 TABLESPACE ds_tbs;
ALTER TABLE
openGauss=# select indexrelid,indisusable,indexrelid::regclass,indrelid::regclass,pg_get_indexdef(indexrelid) from pg_index where indrelid='t_ran'::regclass;
indexrelid | indisusable | indexrelid | indrelid | pg_get_indexdef
------------+-------------+------------+----------+--------------------------------------------------------------------------------------------------------------------------
41138 | t | ind2_t_ran | t_ran | CREATE INDEX ind2_t_ran ON t_ran USING ubtree (start_time, user_number) WITH (storage_type=USTORE) TABLESPACE pg_default
41104 | t | ind_t_ran | t_ran | CREATE INDEX ind_t_ran ON t_ran USING ubtree (start_time) LOCAL WITH (storage_type=USTORE) TABLESPACE pg_default
(2 rows)
openGauss=#
结论:经过以上实验验证,发现 indisusable 字段没有变化,move partition 这个操作对全局索引和分区索引没有影响。
知识总结
1、truncate partition
全局索引:失效
分区索引:正常、没影响
2、split partition
全局索引:失效
分区索引:正常、没影响
3、merge partitions
全局索引:失效
分区索引:正常、没影响
4、drop partition
全局索引:失效
分区索引:正常、没影响
5、move partition
全局索引:正常、没影响
分区索引:正常、没影响