Skip to content

Commit

Permalink
opt: fix panic due to concurrent map writes when copying metadata
Browse files Browse the repository at this point in the history
This commit fixes a bug that caused a panic due to concurrent map writes
when copying table metadata. The fix is to make a deep copy of the map
before updating it.

Fixes cockroachdb#66717

Release note (bug fix): Fixed a panic that could occur in the optimizer
when executing a prepared plan with placeholders. This could happen when
one of the tables used by the query had computed columns or a partial
index.
  • Loading branch information
rytaft committed Jun 23, 2021
1 parent 6e69e98 commit 3c017fd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/sql/opt/table_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,18 @@ func (tm *TableMeta) copyScalars(copyScalar func(Expr) Expr) {
if tm.Constraints != nil {
tm.Constraints = copyScalar(tm.Constraints).(ScalarExpr)
}

computedCols := make(map[ColumnID]ScalarExpr, len(tm.ComputedCols))
for col, e := range tm.ComputedCols {
tm.ComputedCols[col] = copyScalar(e).(ScalarExpr)
computedCols[col] = copyScalar(e).(ScalarExpr)
}
tm.ComputedCols = computedCols

partialIndexPredicates := make(map[cat.IndexOrdinal]ScalarExpr, len(tm.PartialIndexPredicates))
for idx, e := range tm.PartialIndexPredicates {
tm.PartialIndexPredicates[idx] = copyScalar(e).(ScalarExpr)
partialIndexPredicates[idx] = copyScalar(e).(ScalarExpr)
}
tm.PartialIndexPredicates = partialIndexPredicates
}

// IndexColumns returns the metadata IDs for the set of columns in the given
Expand Down

0 comments on commit 3c017fd

Please sign in to comment.