From ea992fac5819e1f039ea6721c2d94764096105a1 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Fri, 8 Apr 2022 21:06:12 +0000 Subject: [PATCH] sql: fix flake in schemachange.TestWorkload As soon as #79580 merged, it tickled flakes. These flakes were caused by operations to alter the database which would build new mutable tables from immutable tables and thus overwrite existing entries. The test is very flakey without this patch. Release note: None --- pkg/sql/database.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/sql/database.go b/pkg/sql/database.go index a8cc6b569aaa..84fdf737ed41 100644 --- a/pkg/sql/database.go +++ b/pkg/sql/database.go @@ -108,19 +108,28 @@ func (p *planner) forEachMutableTableInDatabase( return err } + // TODO(ajwerner): Rewrite this to not use the internalLookupCtx. lCtx := newInternalLookupCtx(all.OrderedDescriptors(), dbDesc) + var droppedRemoved []descpb.ID for _, tbID := range lCtx.tbIDs { desc := lCtx.tbDescs[tbID] if desc.Dropped() { continue } - mutable := tabledesc.NewBuilder(desc.TableDesc()).BuildExistingMutableTable() - schemaName, found, err := lCtx.GetSchemaName(ctx, desc.GetParentSchemaID(), desc.GetParentID(), p.ExecCfg().Settings.Version) + droppedRemoved = append(droppedRemoved, tbID) + } + descs, err := p.Descriptors().GetMutableDescriptorsByID(ctx, p.Txn(), droppedRemoved...) + if err != nil { + return err + } + for _, d := range descs { + mutable := d.(*tabledesc.Mutable) + schemaName, found, err := lCtx.GetSchemaName(ctx, d.GetParentSchemaID(), d.GetParentID(), p.ExecCfg().Settings.Version) if err != nil { return err } if !found { - return errors.AssertionFailedf("schema id %d not found", desc.GetParentSchemaID()) + return errors.AssertionFailedf("schema id %d not found", d.GetParentSchemaID()) } if err := fn(ctx, schemaName, mutable); err != nil { return err