Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: drop index concurrently (#15594)

Danny Xu8a12e51f9f71
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 5689674a71..bf6d918f62 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -155,6 +155,7 @@ const (
DuplicateIndexInTable Code = 815
IndexTypeNotAllowed Code = 816
RedundantIndex Code = 817
+ DropIndexUnconcurrently Code = 818
// 1001 ~ 1099 charset error code.
DisabledCharset Code = 1001
@@ -586,8 +587,8 @@ const (
// PostgreSQLIndexNoDuplicateColumn is an advisor type for Postgresql no duplicate columns in index.
PostgreSQLIndexNoDuplicateColumn Type = "bb.plugin.advisor.postgresql.index.no-duplicate-column"
- // PostgreSQLCreateIndexConcurrently is an advisor type for PostgreSQL to create index concurrently.
- PostgreSQLCreateIndexConcurrently Type = "bb.plugin.advisor.postgresql.index.create-concurrently"
+ // PostgreSQLIndexConcurrently is an advisor type for PostgreSQL to create or drop index concurrently.
+ PostgreSQLIndexConcurrently Type = "bb.plugin.advisor.postgresql.index.create-concurrently"
// PostgreSQLColumnTypeDisallowList is an advisor type for Postgresql column type disallow list.
PostgreSQLColumnTypeDisallowList Type = "bb.plugin.advisor.postgresql.column.type-disallow-list"
diff --git a/backend/plugin/advisor/pg/advisor_index_create_concurrently.go b/backend/plugin/advisor/pg/advisor_index_create_concurrently.go
index 89c89923b9..7ffe11f6b7 100644
--- a/backend/plugin/advisor/pg/advisor_index_create_concurrently.go
+++ b/backend/plugin/advisor/pg/advisor_index_create_concurrently.go
@@ -13,20 +13,20 @@ import (
)
var (
- _ advisor.Advisor = (*IndexCreateConcurrentlyAdvisor)(nil)
+ _ advisor.Advisor = (*IndexConcurrentlyAdvisor)(nil)
_ ast.Visitor = (*indexCreateConcurrentlyChecker)(nil)
)
func init() {
- advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLCreateIndexConcurrently, &IndexCreateConcurrentlyAdvisor{})
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLIndexConcurrently, &IndexConcurrentlyAdvisor{})
}
-// IndexCreateConcurrentlyAdvisor is the advisor checking for to create index concurrently.
-type IndexCreateConcurrentlyAdvisor struct {
+// IndexConcurrentlyAdvisor is the advisor checking for to create index concurrently.
+type IndexConcurrentlyAdvisor struct {
}
// Check checks for to create index concurrently.
-func (*IndexCreateConcurrentlyAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+func (*IndexConcurrentlyAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
stmtList, ok := checkCtx.AST.([]ast.Node)
if !ok {
return nil, errors.Errorf("failed to convert to Node")
@@ -56,7 +56,8 @@ type indexCreateConcurrentlyChecker struct {
// Visit implements ast.Visitor interface.
func (checker *indexCreateConcurrentlyChecker) Visit(in ast.Node) ast.Visitor {
- if node, ok := in.(*ast.CreateIndexStmt); ok {
+ switch node := in.(type) {
+ case *ast.CreateIndexStmt:
if !node.Concurrently {
checker.adviceList = append(checker.adviceList, &storepb.Advice{
Status: checker.level,
@@ -68,7 +69,18 @@ func (checker *indexCreateConcurrentlyChecker) Visit(in ast.Node) ast.Visitor {
},
})
}
+ case *ast.DropIndexStmt:
+ if !node.Concurrently {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.DropIndexUnconcurrently.Int32(),
+ Title: checker.title,
+ Content: "Droping indexes will block writes on the table, unless use CONCURRENTLY",
+ StartPosition: &storepb.Position{
+ Line: int32(in.LastLine()),
+ },
+ })
+ }
}
-
return checker
}
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 5c69b61133..8733f6b4b6 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1343,7 +1343,7 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
}
case SchemaRuleCreateIndexConcurrently:
if engine == storepb.Engine_POSTGRES {
- return PostgreSQLCreateIndexConcurrently, nil
+ return PostgreSQLIndexConcurrently, nil
}
case SchemaRuleIndexTypeAllowList:
if engine == storepb.Engine_MYSQL {
diff --git a/backend/plugin/parser/sql/ast/drop_index_stmt.go b/backend/plugin/parser/sql/ast/drop_index_stmt.go
index 1e4d492bcc..5c3bf2a503 100644
--- a/backend/plugin/parser/sql/ast/drop_index_stmt.go
+++ b/backend/plugin/parser/sql/ast/drop_index_stmt.go
@@ -6,7 +6,8 @@ type DropIndexStmt struct {
// Here use IndexDef because the drop index statement needs the schema name for PostgreSQL.
// If the drop index statement doesn't contain schema name, the Table of this index is nil.
- IndexList []*IndexDef
- IfExists bool
- Behavior DropBehavior
+ IndexList []*IndexDef
+ IfExists bool
+ Concurrently bool
+ Behavior DropBehavior
}
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index 6c118056bf..3330cdbfba 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -289,8 +289,9 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
switch in.DropStmt.RemoveType {
case pgquery.ObjectType_OBJECT_INDEX:
dropIndex := &ast.DropIndexStmt{
- IfExists: in.DropStmt.MissingOk,
- Behavior: convertDropBehavior(in.DropStmt.Behavior),
+ IfExists: in.DropStmt.MissingOk,
+ Concurrently: in.DropStmt.Concurrent,
+ Behavior: convertDropBehavior(in.DropStmt.Behavior),
}
for _, object := range in.DropStmt.Objects {
list, ok := object.Node.(*pgquery.Node_List)