From c8e2cf74ecfbb83fa2842506adc19a2d0a828e65 Mon Sep 17 00:00:00 2001 From: maryliag Date: Fri, 17 Feb 2023 14:13:27 -0500 Subject: [PATCH] sqlstats: fix data race on get percentile values The function `GetPercentileValues` was using a read lock, since it was only reading the values from `latencySummary`, but the `Stream.flush` used by the `latencySummary` modifies the value, so a read and write lock is necessary. Fixes #97273 Release note: None --- pkg/sql/sqlstats/insights/detector.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/sql/sqlstats/insights/detector.go b/pkg/sql/sqlstats/insights/detector.go index 6b927c68edb0..56c9cc78b664 100644 --- a/pkg/sql/sqlstats/insights/detector.go +++ b/pkg/sql/sqlstats/insights/detector.go @@ -91,8 +91,9 @@ func (d *anomalyDetector) isSlow(stmt *Statement) (decision bool) { } func (d *anomalyDetector) GetPercentileValues(id appstatspb.StmtFingerprintID) PercentileValues { - d.mu.RLock() - defer d.mu.RUnlock() + // latencySummary.Query might modify its own state (Stream.flush), so a read-write lock is necessary. + d.mu.Lock() + defer d.mu.Unlock() latencies := PercentileValues{} if entry, ok := d.mu.index[id]; ok { latencySummary := entry.Value.(latencySummaryEntry).value