diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index 8f67df5b3..5833f028f 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -4651,9 +4651,22 @@ alter_table_cmd: $$ = (Node *)n; } | + INVISIBLE + { + AlterTableCmd *n = makeNode(AlterTableCmd); + n->subtype = AT_InvisibleIndexDirect; + $$ = (Node *)n; + } + | + VISIBLE + { + AlterTableCmd *n = makeNode(AlterTableCmd); + n->subtype = AT_VisibleIndexDirect; + $$ = (Node *)n; + } + | ALTER INDEX index_name INVISIBLE { - BCompatibilityOptionSupportCheck($4); AlterTableCmd *n = makeNode(AlterTableCmd); n->subtype = AT_InvisibleIndex; n->name = $3; @@ -4662,7 +4675,6 @@ alter_table_cmd: | ALTER INDEX index_name VISIBLE { - BCompatibilityOptionSupportCheck($4); AlterTableCmd *n = makeNode(AlterTableCmd); n->subtype = AT_VisibleIndex; n->name = $3; @@ -5440,13 +5452,11 @@ table_index_option: } | INVISIBLE { - BCompatibilityOptionSupportCheck($1); Value *n = makeString("invisible"); $$ = (Node*)n; } | VISIBLE { - BCompatibilityOptionSupportCheck($1); Value *n = makeString("visible"); $$ = (Node*)n; } diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index a97c0257d..35dcac754 100755 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -681,6 +681,7 @@ static void ATExecUnusableIndex(Relation rel); static void ATUnusableGlobalIndex(Relation rel); static void ATExecUnusableAllIndexOnPartition(Relation rel, const char* partition_name); static void ATExecVisibleIndex(Relation rel, char* index_name, bool visible); +static void ATExecVisibleIndexDirect(Relation rel, bool visible); static void ATExecModifyRowMovement(Relation rel, bool rowMovement); static void ATExecTruncatePartition(Relation rel, AlterTableCmd* cmd); static void ATExecTruncateSubPartition(Relation rel, AlterTableCmd* cmd); @@ -829,6 +830,8 @@ inline static bool CStoreSupportATCmd(AlterTableType cmdtype) #endif case AT_VisibleIndex: case AT_InvisibleIndex: + case AT_InvisibleIndexDirect: + case AT_VisibleIndexDirect: ret = true; break; default: @@ -8432,6 +8435,8 @@ static void ATPrepCmd(List** wqueue, Relation rel, AlterTableCmd* cmd, bool recu case AT_ReplaceRelOptions: /* reset them all, then set just these */ case AT_InvisibleIndex: case AT_VisibleIndex: + case AT_InvisibleIndexDirect: + case AT_VisibleIndexDirect: ATSimplePermissions(rel, ATT_TABLE | ATT_INDEX | ATT_VIEW); /* This command never recurses */ /* No command-specific prep needed */ @@ -9060,6 +9065,12 @@ static void ATExecCmd(List** wqueue, AlteredTableInfo* tab, Relation rel, AlterT case AT_VisibleIndex: ATExecVisibleIndex(rel, cmd->name, true); break; + case AT_InvisibleIndexDirect: + ATExecVisibleIndexDirect(rel, false); + break; + case AT_VisibleIndexDirect: + ATExecVisibleIndexDirect(rel, true); + break; case AT_AddIndex: /* ADD INDEX */ address = ATExecAddIndex(tab, rel, (IndexStmt*)cmd->def, false, lockmode); break; @@ -25425,6 +25436,18 @@ static void ATExecVisibleIndex(Relation rel, char* index_name, bool visible) } } +static void ATExecVisibleIndexDirect(Relation rel, bool visible) +{ + if (RelationIsIndex(rel) && OidIsValid(rel->rd_id)) { + ATExecSetIndexVisibleState(rel->rd_id, visible); + } else { + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("can not set visible for relation %s, as it is not a index", + RelationGetRelationName(rel)))); + } +} + /* * @@GaussDB@@ * Target : data partition diff --git a/src/include/nodes/parsenodes_common.h b/src/include/nodes/parsenodes_common.h index e7e4133c2..3690ab5de 100644 --- a/src/include/nodes/parsenodes_common.h +++ b/src/include/nodes/parsenodes_common.h @@ -915,6 +915,8 @@ typedef enum AlterTableType { AT_COMMENTS, AT_InvisibleIndex, AT_VisibleIndex, + AT_InvisibleIndexDirect, + AT_VisibleIndexDirect, AT_ModifyColumn, AT_SetCharsetCollate, AT_ConvertCharset, diff --git a/src/test/regress/expected/invisible_index.out b/src/test/regress/expected/invisible_index.out index 373bcda40..781cb2176 100644 --- a/src/test/regress/expected/invisible_index.out +++ b/src/test/regress/expected/invisible_index.out @@ -1,17 +1,53 @@ /* unsupported */ create schema invisible_index; set search_path to 'invisible_index'; -create table t1 (a int, b int, constraint key_a primary key(a) visible); --error -ERROR: visible is supported only in B compatible database. -create table t1 (a int, b int, constraint key_a primary key(a)); -NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "key_a" for table "t1" -alter table t1 alter index key_a invisible; --error -ERROR: invisible is supported only in B compatible database. -alter table t1 add constraint key_b unique (b) visible; --error -ERROR: visible is supported only in B compatible database. +create table t1 (a int, b int, constraint key1 primary key(a) visible); +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "key1" for table "t1" +alter table t1 alter index key1 invisible; +alter table t1 add constraint key2 unique (b) visible; +NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "key2" for table "t1" +create table t2 (a int, b int, constraint key3 primary key(a) invisible); +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "key3" for table "t2" +alter table t2 alter index key3 visible; +alter table t2 add constraint key4 unique (b) invisible; +NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "key4" for table "t2" +create table t3 (a int, b int); +create index key5 on t3(a) visible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | t +(1 row) + +alter index key5 invisible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | f +(1 row) + +create table t4 (a int, b int); +create index key6 on t4(a) invisible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | f +(1 row) + +alter index key6 visible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | t +(1 row) + reset search_path; drop schema invisible_index cascade; -NOTICE: drop cascades to table invisible_index.t1 +NOTICE: drop cascades to 4 other objects +DETAIL: drop cascades to table invisible_index.t1 +drop cascades to table invisible_index.t2 +drop cascades to table invisible_index.t3 +drop cascades to table invisible_index.t4 create database invisible_index_db dbcompatibility 'B'; \c invisible_index_db -- create table with index @@ -26,6 +62,36 @@ select indkey, indisvisible from pg_index where indrelid = 't1'::regclass order 2 | f (2 rows) +create table t3 (a int, b int); +create index key5 on t3(a) visible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | t +(1 row) + +alter index key5 invisible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | f +(1 row) + +create table t4 (a int, b int); +create index key6 on t4(a) invisible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | f +(1 row) + +alter index key6 visible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; + indkey | indisvisible +--------+-------------- + 1 | t +(1 row) + insert into t1 values (generate_series(1, 100), generate_series(1, 100)); analyze t1; set enable_seqscan = off; diff --git a/src/test/regress/sql/invisible_index.sql b/src/test/regress/sql/invisible_index.sql index bd283270a..2ff1d8291 100644 --- a/src/test/regress/sql/invisible_index.sql +++ b/src/test/regress/sql/invisible_index.sql @@ -2,11 +2,25 @@ create schema invisible_index; set search_path to 'invisible_index'; -create table t1 (a int, b int, constraint key_a primary key(a) visible); --error +create table t1 (a int, b int, constraint key1 primary key(a) visible); +alter table t1 alter index key1 invisible; +alter table t1 add constraint key2 unique (b) visible; -create table t1 (a int, b int, constraint key_a primary key(a)); -alter table t1 alter index key_a invisible; --error -alter table t1 add constraint key_b unique (b) visible; --error +create table t2 (a int, b int, constraint key3 primary key(a) invisible); +alter table t2 alter index key3 visible; +alter table t2 add constraint key4 unique (b) invisible; + +create table t3 (a int, b int); +create index key5 on t3(a) visible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; +alter index key5 invisible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + +create table t4 (a int, b int); +create index key6 on t4(a) invisible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; +alter index key6 visible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; reset search_path; drop schema invisible_index cascade; @@ -21,6 +35,18 @@ alter table t1 alter index key_b visible, alter index key_b invisible; select indkey, indisvisible from pg_index where indrelid = 't1'::regclass order by indkey; +create table t3 (a int, b int); +create index key5 on t3(a) visible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; +alter index key5 invisible; +select indkey, indisvisible from pg_index where indrelid = 't3'::regclass order by indkey; + +create table t4 (a int, b int); +create index key6 on t4(a) invisible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; +alter index key6 visible; +select indkey, indisvisible from pg_index where indrelid = 't4'::regclass order by indkey; + insert into t1 values (generate_series(1, 100), generate_series(1, 100)); analyze t1;