diff --git a/pkg/sql/logictest/testdata/logic_test/rename_index b/pkg/sql/logictest/testdata/logic_test/rename_index index d1fd58d4f483..a575dc090f48 100644 --- a/pkg/sql/logictest/testdata/logic_test/rename_index +++ b/pkg/sql/logictest/testdata/logic_test/rename_index @@ -91,3 +91,7 @@ ALTER INDEX users@ufo RENAME TO foo statement ok ALTER INDEX users@rar RENAME TO bar + +# Regression test for #24774 +statement ok +ALTER INDEX users@"primary" RENAME TO pk diff --git a/pkg/sql/rename_index.go b/pkg/sql/rename_index.go index aef7ab3d0fc4..f7d297175ed0 100644 --- a/pkg/sql/rename_index.go +++ b/pkg/sql/rename_index.go @@ -77,7 +77,9 @@ func (p *planner) RenameIndex(ctx context.Context, n *tree.RenameIndex) (planNod return nil, fmt.Errorf("index name %q already exists", string(n.NewName)) } - tableDesc.RenameIndexDescriptor(idx, string(n.NewName)) + if err := tableDesc.RenameIndexDescriptor(idx, string(n.NewName)); err != nil { + return nil, err + } if err := tableDesc.SetUpVersion(); err != nil { return nil, err diff --git a/pkg/sql/sqlbase/structured.go b/pkg/sql/sqlbase/structured.go index a4e60a660f1f..551220eabe28 100644 --- a/pkg/sql/sqlbase/structured.go +++ b/pkg/sql/sqlbase/structured.go @@ -1896,21 +1896,25 @@ func (desc *TableDescriptor) FindIndexByName(name string) (IndexDescriptor, bool } // RenameIndexDescriptor renames an index descriptor. -func (desc *TableDescriptor) RenameIndexDescriptor(index IndexDescriptor, name string) { +func (desc *TableDescriptor) RenameIndexDescriptor(index IndexDescriptor, name string) error { id := index.ID + if id == desc.PrimaryIndex.ID { + desc.PrimaryIndex.Name = name + return nil + } for i := range desc.Indexes { if desc.Indexes[i].ID == id { desc.Indexes[i].Name = name - return + return nil } } for _, m := range desc.Mutations { if idx := m.GetIndex(); idx != nil && idx.ID == id { idx.Name = name - return + return nil } } - panic(fmt.Sprintf("index with id = %d does not exist", id)) + return fmt.Errorf("index with id = %d does not exist", id) } // FindIndexByID finds an index (active or inactive) with the specified ID.