Skip to content

Commit

Permalink
sql: Fix race condition in the query cache
Browse files Browse the repository at this point in the history
The PreparedMetadata was being put into the cache, but then the
InferredTypes field was being set after that by execPrepare. The fix
is to make a copy of PreparedMetadata before entering it into the cache.

Release note: None
  • Loading branch information
andy-kimball committed Feb 5, 2019
1 parent 6ed3a1e commit 317a77d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ func (p *planner) prepareUsingOptimizer(
if opc.allowMemoReuse {
stmt.Prepared.Memo = memo
if opc.useCache {
// execPrepare sets the PrepareMetadata.InferredTypes field after this
// point. However, once the PrepareMetadata goes into the cache, it
// can't be modified without causing race conditions. So make a copy of
// it now.
// TODO(radu): Determine if the extra object allocation is really
// necessary.
pm := stmt.Prepared.PrepareMetadata
cachedData := querycache.CachedData{
SQL: stmt.SQL,
Memo: memo,
// We rely on stmt.Prepared.PrepareMetadata not being subsequently modified.
// TODO(radu): this also holds on to the memory referenced by other
// PreparedStatement fields.
PrepareMetadata: &stmt.Prepared.PrepareMetadata,
SQL: stmt.SQL,
Memo: memo,
PrepareMetadata: &pm,
}
p.execCfg.QueryCache.Add(&p.queryCacheSession, &cachedData)
}
Expand Down

0 comments on commit 317a77d

Please sign in to comment.