change_regress4reindexconcurrently

This commit is contained in:
jiajunchang 2021-11-25 19:25:49 +08:00
parent 831c15f67c
commit 20ea1a2235
9 changed files with 443 additions and 1 deletions

View File

@ -0,0 +1,87 @@
--
-- REINDEX CONCURRENTLY
--
CREATE TABLE concur_reindex_tab (c1 int);
-- REINDEX
REINDEX TABLE concur_reindex_tab; -- notice
NOTICE: table "concur_reindex_tab" has no indexes
REINDEX TABLE CONCURRENTLY concur_reindex_tab; -- notice
NOTICE: table "concur_reindex_tab" has no indexes
ALTER TABLE concur_reindex_tab ADD COLUMN c2 text; -- add toast index
-- Normal index with integer column
CREATE UNIQUE INDEX concur_reindex_ind1 ON concur_reindex_tab(c1);
-- Normal index with text column
CREATE INDEX concur_reindex_ind2 ON concur_reindex_tab(c2);
-- UNION INDEX index with expression
CREATE UNIQUE INDEX concur_reindex_ind3 ON concur_reindex_tab(abs(c1));
-- Duplicates column names error
CREATE INDEX concur_reindex_ind4 ON concur_reindex_tab(c1, c1, c2);
ERROR: duplicate column name
-- Create table for check on foreign key dependence switch with indexes swapped
ALTER TABLE concur_reindex_tab ADD PRIMARY KEY USING INDEX concur_reindex_ind1;
CREATE TABLE concur_reindex_tab2 (c1 int REFERENCES concur_reindex_tab);
INSERT INTO concur_reindex_tab VALUES (1, 'a');
INSERT INTO concur_reindex_tab VALUES (2, 'a');
-- Check materialized views
CREATE MATERIALIZED VIEW concur_reindex_matview AS SELECT * FROM concur_reindex_tab;
REINDEX INDEX CONCURRENTLY concur_reindex_ind1;
REINDEX TABLE CONCURRENTLY concur_reindex_tab;
REINDEX TABLE CONCURRENTLY concur_reindex_matview;
-- Check views
CREATE VIEW concur_reindex_view AS SELECT * FROM concur_reindex_tab;
REINDEX TABLE CONCURRENTLY concur_reindex_view; -- Error
ERROR: "concur_reindex_view" is not a table or materialized view
-- Check that comments are preserved
CREATE TABLE testcomment (i int);
CREATE INDEX testcomment_idx1 ON testcomment(i);
COMMENT ON INDEX testcomment_idx1 IS 'test comment';
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
obj_description
-----------------
test comment
(1 row)
REINDEX TABLE testcomment;
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
obj_description
-----------------
test comment
(1 row)
REINDEX TABLE CONCURRENTLY testcomment;
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
obj_description
-----------------
test comment
(1 row)
DROP TABLE testcomment;
-- Check error
-- Cannot run inside a transaction block
BEGIN;
REINDEX TABLE CONCURRENTLY concur_reindex_tab;
ERROR: REINDEX CONCURRENTLY cannot run inside a transaction block
COMMIT;
REINDEX TABLE CONCURRENTLY pg_database; -- no shared relation
ERROR: concurrent index creation on system catalog tables is not supported
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relations
ERROR: concurrent index creation on system catalog tables is not supported
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: can only reindex the currently open database
-- Check the relation status, there should not be invalid indexe
\d concur_reindex_tab
Table "public.concur_reindex_tab"
Column | Type | Modifiers
--------+---------+-----------
c1 | integer | not null
c2 | text |
Indexes:
"concur_reindex_ind1" PRIMARY KEY, btree (c1) TABLESPACE pg_default
"concur_reindex_ind3" UNIQUE, btree (abs(c1)) TABLESPACE pg_default
"concur_reindex_ind2" btree (c2) TABLESPACE pg_default
Referenced by:
TABLE "concur_reindex_tab2" CONSTRAINT "concur_reindex_tab2_c1_fkey" FOREIGN KEY (c1) REFERENCES concur_reindex_tab(c1)
DROP VIEW concur_reindex_view;
DROP MATERIALIZED VIEW concur_reindex_matview;
DROP TABLE concur_reindex_tab, concur_reindex_tab2;

View File

@ -0,0 +1,59 @@
--
-- REINDEX CONCURRENTLY PARALLEL
--
CREATE TABLE reind_con_tab(id serial primary key, data text);
NOTICE: CREATE TABLE will create implicit sequence "reind_con_tab_id_seq" for serial column "reind_con_tab.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "reind_con_tab_pkey" for table "reind_con_tab"
INSERT INTO reind_con_tab(data) VALUES ('aa');
INSERT INTO reind_con_tab(data) VALUES ('aaa');
INSERT INTO reind_con_tab(data) VALUES ('aaaa');
INSERT INTO reind_con_tab(data) VALUES ('aaaaa');
\d reind_con_tab;
Table "public.reind_con_tab"
Column | Type | Modifiers
--------+---------+------------------------------------------------------------
id | integer | not null default nextval('reind_con_tab_id_seq'::regclass)
data | text |
Indexes:
"reind_con_tab_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
SELECT data FROM reind_con_tab WHERE id =3;
\parallel off
data
------
aaaa
(1 row)
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
UPDATE reind_con_tab SET data = 'bbbb' WHERE id = 3;
\parallel off
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
INSERT INTO reind_con_tab(data) VALUES('cccc');
\parallel off
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
DELETE FROM reind_con_tab WHERE data = 'aaa';
\parallel off
SELECT * FROM reind_con_tab;
id | data
----+-------
1 | aa
4 | aaaaa
3 | bbbb
5 | cccc
(4 rows)
\d reind_con_tab;
Table "public.reind_con_tab"
Column | Type | Modifiers
--------+---------+------------------------------------------------------------
id | integer | not null default nextval('reind_con_tab_id_seq'::regclass)
data | text |
Indexes:
"reind_con_tab_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
DROP TABLE reind_con_tab;

View File

@ -0,0 +1,55 @@
--
-- REINDEX CONCURRENTLY PARTITION
--
drop table if exists t1;
NOTICE: table "t1" does not exist, skipping
create table t1(
c_id varchar,
c_w_id integer,
c_date date
)
partition by range (c_date,c_w_id)
(
PARTITION t1_1 values less than ('20170331',5),
PARTITION t1_2 values less than ('20170731',450),
PARTITION t1_3 values less than ('20170930',1062),
PARTITION t1_4 values less than ('20171231',1765),
PARTITION t1_5 values less than ('20180331',2024),
PARTITION t1_6 values less than ('20180731',2384),
PARTITION t1_7 values less than ('20180930',2786),
PARTITION t1_8 values less than (maxvalue,maxvalue)
);
insert into t1 values('gauss1',4,'20170301');
insert into t1 values('gauss2',400,'20170625');
insert into t1 values('gauss3',480,'20170920');
insert into t1 values('gauss4',1065,'20170920');
insert into t1 values('gauss5',1800,'20170920');
insert into t1 values('gauss6',2030,'20170920');
insert into t1 values('gauss7',2385,'20170920');
insert into t1 values('gauss8',2789,'20191020');
insert into t1 values('gauss9',2789,'20171020');
create index idx_t1 on t1 using btree(c_id) LOCAL;
create index idx2_t1 on t1 using btree(c_id) LOCAL (
PARTITION t1_1_index,
PARTITION t1_2_index,
PARTITION t1_3_index,
PARTITION t1_4_index,
PARTITION t1_5_index,
PARTITION t1_6_index,
PARTITION t1_7_index,
PARTITION t1_8_index
);
reindex index CONCURRENTLY idx_t1;
reindex index CONCURRENTLY idx2_t1 partition t1_1_index;
reindex table CONCURRENTLY t1;
reindex table CONCURRENTLY t1 partition t1_1;
drop index idx_t1;
drop index idx2_t1;
create index idx_t1 on t1 using btree(c_id);
reindex index idx_t1;
reindex index CONCURRENTLY idx_t1; --ERROR, can't reindex concurrently global index partition
ERROR: cannot reindex concurrently global partition index " public.idx_t1"
reindex table t1;
reindex table CONCURRENTLY t1; --WARNING, can't reindex concurrently global index partition
WARNING: cannot reindex concurrently global partition index " public.idx_t1", skipping
drop table t1;

View File

@ -0,0 +1,51 @@
--
-- REINDEX CONCURRENTLY PARTITION PARALLEL
--
drop table if exists t2;
NOTICE: table "t2" does not exist, skipping
CREATE TABLE t2 (id int, data text) partition by range(id)(partition p1 values less than(100), partition p2 values less than(200), partition p3 values less than(MAXVALUE));
insert into t2 select generate_series(1,500),generate_series(1,500);
create index ind_id on t2(id) LOCAL;
select * from t2 where id = 4;
id | data
----+------
4 | 4
(1 row)
\parallel on
REINDEX index CONCURRENTLY ind_id;
select * from t2 where id = 3;
\parallel off
id | data
----+------
3 | 3
(1 row)
\parallel on
REINDEX index CONCURRENTLY ind_id;
delete from t2 where id = 4;
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
insert into t2 values (4,3);
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
update t2 set data = 4 where id = 4;
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
select * from t2 where id = 4;
\parallel off
id | data
----+------
4 | 4
(1 row)
select * from t2 where id = 4;
id | data
----+------
4 | 4
(1 row)
drop table t2;

View File

@ -831,8 +831,15 @@ test: comment_proc
test: hw_package
test: procedure_privilege_test
test: cast_privileges_test
test: reindex_concurrently
test: reindex_concurrently_parallel
test: reindex_concurrently_partition
test: reindex_concurrently_partition_parallel
#test: hw_dbms_sql1
test: hw_cipher_sm4
test: hw_cipher_aes128
test: sequence_cache_test
test: pg_buffercache_pages
test: pg_buffercache_pages

View File

@ -0,0 +1,54 @@
--
-- REINDEX CONCURRENTLY
--
CREATE TABLE concur_reindex_tab (c1 int);
-- REINDEX
REINDEX TABLE concur_reindex_tab; -- notice
REINDEX TABLE CONCURRENTLY concur_reindex_tab; -- notice
ALTER TABLE concur_reindex_tab ADD COLUMN c2 text; -- add toast index
-- Normal index with integer column
CREATE UNIQUE INDEX concur_reindex_ind1 ON concur_reindex_tab(c1);
-- Normal index with text column
CREATE INDEX concur_reindex_ind2 ON concur_reindex_tab(c2);
-- UNION INDEX index with expression
CREATE UNIQUE INDEX concur_reindex_ind3 ON concur_reindex_tab(abs(c1));
-- Duplicates column names error
CREATE INDEX concur_reindex_ind4 ON concur_reindex_tab(c1, c1, c2);
-- Create table for check on foreign key dependence switch with indexes swapped
ALTER TABLE concur_reindex_tab ADD PRIMARY KEY USING INDEX concur_reindex_ind1;
CREATE TABLE concur_reindex_tab2 (c1 int REFERENCES concur_reindex_tab);
INSERT INTO concur_reindex_tab VALUES (1, 'a');
INSERT INTO concur_reindex_tab VALUES (2, 'a');
-- Check materialized views
CREATE MATERIALIZED VIEW concur_reindex_matview AS SELECT * FROM concur_reindex_tab;
REINDEX INDEX CONCURRENTLY concur_reindex_ind1;
REINDEX TABLE CONCURRENTLY concur_reindex_tab;
REINDEX TABLE CONCURRENTLY concur_reindex_matview;
-- Check views
CREATE VIEW concur_reindex_view AS SELECT * FROM concur_reindex_tab;
REINDEX TABLE CONCURRENTLY concur_reindex_view; -- Error
-- Check that comments are preserved
CREATE TABLE testcomment (i int);
CREATE INDEX testcomment_idx1 ON testcomment(i);
COMMENT ON INDEX testcomment_idx1 IS 'test comment';
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
REINDEX TABLE testcomment;
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
REINDEX TABLE CONCURRENTLY testcomment;
SELECT obj_description('testcomment_idx1'::regclass, 'pg_class');
DROP TABLE testcomment;
-- Check error
-- Cannot run inside a transaction block
BEGIN;
REINDEX TABLE CONCURRENTLY concur_reindex_tab;
COMMIT;
REINDEX TABLE CONCURRENTLY pg_database; -- no shared relation
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relations
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
-- Check the relation status, there should not be invalid indexe
\d concur_reindex_tab
DROP VIEW concur_reindex_view;
DROP MATERIALIZED VIEW concur_reindex_matview;
DROP TABLE concur_reindex_tab, concur_reindex_tab2;

View File

@ -0,0 +1,33 @@
--
-- REINDEX CONCURRENTLY PARALLEL
--
CREATE TABLE reind_con_tab(id serial primary key, data text);
INSERT INTO reind_con_tab(data) VALUES ('aa');
INSERT INTO reind_con_tab(data) VALUES ('aaa');
INSERT INTO reind_con_tab(data) VALUES ('aaaa');
INSERT INTO reind_con_tab(data) VALUES ('aaaaa');
\d reind_con_tab;
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
SELECT data FROM reind_con_tab WHERE id =3;
\parallel off
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
UPDATE reind_con_tab SET data = 'bbbb' WHERE id = 3;
\parallel off
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
INSERT INTO reind_con_tab(data) VALUES('cccc');
\parallel off
\parallel on
REINDEX TABLE CONCURRENTLY reind_con_tab;
DELETE FROM reind_con_tab WHERE data = 'aaa';
\parallel off
SELECT * FROM reind_con_tab;
\d reind_con_tab;
DROP TABLE reind_con_tab;

View File

@ -0,0 +1,58 @@
--
-- REINDEX CONCURRENTLY PARTITION
--
drop table if exists t1;
create table t1(
c_id varchar,
c_w_id integer,
c_date date
)
partition by range (c_date,c_w_id)
(
PARTITION t1_1 values less than ('20170331',5),
PARTITION t1_2 values less than ('20170731',450),
PARTITION t1_3 values less than ('20170930',1062),
PARTITION t1_4 values less than ('20171231',1765),
PARTITION t1_5 values less than ('20180331',2024),
PARTITION t1_6 values less than ('20180731',2384),
PARTITION t1_7 values less than ('20180930',2786),
PARTITION t1_8 values less than (maxvalue,maxvalue)
);
insert into t1 values('gauss1',4,'20170301');
insert into t1 values('gauss2',400,'20170625');
insert into t1 values('gauss3',480,'20170920');
insert into t1 values('gauss4',1065,'20170920');
insert into t1 values('gauss5',1800,'20170920');
insert into t1 values('gauss6',2030,'20170920');
insert into t1 values('gauss7',2385,'20170920');
insert into t1 values('gauss8',2789,'20191020');
insert into t1 values('gauss9',2789,'20171020');
create index idx_t1 on t1 using btree(c_id) LOCAL;
create index idx2_t1 on t1 using btree(c_id) LOCAL (
PARTITION t1_1_index,
PARTITION t1_2_index,
PARTITION t1_3_index,
PARTITION t1_4_index,
PARTITION t1_5_index,
PARTITION t1_6_index,
PARTITION t1_7_index,
PARTITION t1_8_index
);
reindex index CONCURRENTLY idx_t1;
reindex index CONCURRENTLY idx2_t1 partition t1_1_index;
reindex table CONCURRENTLY t1;
reindex table CONCURRENTLY t1 partition t1_1;
drop index idx_t1;
drop index idx2_t1;
create index idx_t1 on t1 using btree(c_id);
reindex index idx_t1;
reindex index CONCURRENTLY idx_t1; --ERROR, can't reindex concurrently global index partition
reindex table t1;
reindex table CONCURRENTLY t1; --WARNING, can't reindex concurrently global index partition
drop table t1;

View File

@ -0,0 +1,38 @@
--
-- REINDEX CONCURRENTLY PARTITION PARALLEL
--
drop table if exists t2;
CREATE TABLE t2 (id int, data text) partition by range(id)(partition p1 values less than(100), partition p2 values less than(200), partition p3 values less than(MAXVALUE));
insert into t2 select generate_series(1,500),generate_series(1,500);
create index ind_id on t2(id) LOCAL;
select * from t2 where id = 4;
\parallel on
REINDEX index CONCURRENTLY ind_id;
select * from t2 where id = 3;
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
delete from t2 where id = 4;
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
insert into t2 values (4,3);
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
update t2 set data = 4 where id = 4;
\parallel off
\parallel on
REINDEX index CONCURRENTLY ind_id;
select * from t2 where id = 4;
\parallel off
select * from t2 where id = 4;
drop table t2;