Skip to content

Commit

Permalink
validate: use immutable descriptors only
Browse files Browse the repository at this point in the history
The descriptor validation logic will accept any implementation of
catalog.Descriptor be it mutable or immutable, it doesn't care. However,
using mutable implementations can have a significant performance impact
especially in the case of tables, where every column or index or
constraint lookup will lead to the cache being regenerated for the whole
descriptor.

This commit fixes this by having validate.Validate replace any mutable
descriptor instances it encounters with immutable copies. This doesn't
change anything except performance.

Fixes #95827.

Release note: None
  • Loading branch information
Marius Posta committed Jan 25, 2023
1 parent b84f0eb commit b3713c7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/sql/catalog/internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func Validate(
targetLevel catalog.ValidationLevel,
descriptors ...catalog.Descriptor,
) catalog.ValidationErrors {
for i, d := range descriptors {
// Replace mutable descriptors with immutable copies. Validation is
// read-only in any case, and using immutables can have a significant
// impact on performance when validating tables due to columns, indexes,
// and so forth being cached.
if mut, ok := d.(catalog.MutableDescriptor); ok {
descriptors[i] = mut.ImmutableCopy()
}
}
vea := validationErrorAccumulator{
ValidationTelemetry: telemetry,
targetLevel: targetLevel,
Expand Down Expand Up @@ -409,9 +418,17 @@ func (cs *collectorState) getMissingDescs(
return nil, err
}
for _, desc := range resps {
if desc != nil {
cs.vdg.descriptors[desc.GetID()] = desc
if desc == nil {
continue
}
if mut, ok := desc.(catalog.MutableDescriptor); ok {
// Replace mutable descriptors with immutable copies. Validation is
// read-only in any case, and using immutables can have a significant
// impact on performance when validating tables due to columns, indexes,
// and so forth being cached.
desc = mut.ImmutableCopy()
}
cs.vdg.descriptors[desc.GetID()] = desc
}
return resps, nil
}
Expand Down

0 comments on commit b3713c7

Please sign in to comment.