From ddb932aa53cc4fb6d97d52a4bfbb6cae62cb8f4a Mon Sep 17 00:00:00 2001 From: Jensen Date: Mon, 3 Jun 2024 14:44:21 +0800 Subject: [PATCH 1/2] When truncate table, if the table does not have any auto-increment columns, there is no need to call the Reset interface of increment_service. --- pkg/sql/compile/ddl.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pkg/sql/compile/ddl.go b/pkg/sql/compile/ddl.go index ddf72f37263f..c67b2d5b86de 100644 --- a/pkg/sql/compile/ddl.go +++ b/pkg/sql/compile/ddl.go @@ -1942,14 +1942,26 @@ func (s *Scope) TruncateTable(c *Compile) error { if isTemp { oldId = rel.GetTableID(c.ctx) } - err = incrservice.GetAutoIncrementService(c.ctx).Reset( - c.ctx, - oldId, - newId, - keepAutoIncrement, - c.proc.TxnOperator) - if err != nil { - return err + + // check if contains any auto_increment column(include __mo_fake_pk_col), if so, reset the auto_increment value + tblDef := rel.GetTableDef(c.ctx) + var containAuto bool + for _, col := range tblDef.Cols { + if col.Typ.AutoIncr { + containAuto = true + break + } + } + if containAuto { + err = incrservice.GetAutoIncrementService(c.ctx).Reset( + c.ctx, + oldId, + newId, + keepAutoIncrement, + c.proc.TxnOperator) + if err != nil { + return err + } } // update index information in mo_catalog.mo_indexes From d4cb35caca61ed8a1221833d60a6fec16aae1204 Mon Sep 17 00:00:00 2001 From: Jensen Date: Mon, 3 Jun 2024 17:48:18 +0800 Subject: [PATCH 2/2] add simlar check for drop table --- pkg/sql/compile/ddl.go | 46 ++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/pkg/sql/compile/ddl.go b/pkg/sql/compile/ddl.go index c67b2d5b86de..a343ab96b367 100644 --- a/pkg/sql/compile/ddl.go +++ b/pkg/sql/compile/ddl.go @@ -2153,12 +2153,22 @@ func (s *Scope) DropTable(c *Compile) error { } if dbName != catalog.MO_CATALOG && tblName != catalog.MO_INDEXES { - err := incrservice.GetAutoIncrementService(c.ctx).Delete( - c.ctx, - rel.GetTableID(c.ctx), - c.proc.TxnOperator) - if err != nil { - return err + tblDef := rel.GetTableDef(c.ctx) + var containAuto bool + for _, col := range tblDef.Cols { + if col.Typ.AutoIncr { + containAuto = true + break + } + } + if containAuto { + err := incrservice.GetAutoIncrementService(c.ctx).Delete( + c.ctx, + rel.GetTableID(c.ctx), + c.proc.TxnOperator) + if err != nil { + return err + } } } @@ -2180,13 +2190,23 @@ func (s *Scope) DropTable(c *Compile) error { } if dbName != catalog.MO_CATALOG && tblName != catalog.MO_INDEXES { - // When drop table 'mo_catalog.mo_indexes', there is no need to delete the auto increment data - err := incrservice.GetAutoIncrementService(c.ctx).Delete( - c.ctx, - rel.GetTableID(c.ctx), - c.proc.TxnOperator) - if err != nil { - return err + tblDef := rel.GetTableDef(c.ctx) + var containAuto bool + for _, col := range tblDef.Cols { + if col.Typ.AutoIncr { + containAuto = true + break + } + } + if containAuto { + // When drop table 'mo_catalog.mo_indexes', there is no need to delete the auto increment data + err := incrservice.GetAutoIncrementService(c.ctx).Delete( + c.ctx, + rel.GetTableID(c.ctx), + c.proc.TxnOperator) + if err != nil { + return err + } } } }