Skip to content

stats.go: statsInfoMap entries set to nil but never deleted — unbounded map growth #24060

Closed

Description

@XuPeng-SH

Summary

In pkg/vm/engine/disttae/stats.go, the GlobalStats.statsInfoMap map accumulates entries that are set to nil but never removed via delete(). Over time, this causes unbounded growth of the map itself (keys + internal hash buckets).

Root Cause

Setting entries to nil (stats.go:790):

broadcastWithoutUpdate := func() {
    gs.mu.Lock()
    defer gs.mu.Unlock()
    gs.mu.statsInfoMap[wrapKey.Key] = nil  // key remains in map
    gs.mu.cond.Broadcast()
}

Setting entries to nil (stats.go:838):

} else if _, ok := gs.mu.statsInfoMap[wrapKey.Key]; !ok {
    gs.mu.statsInfoMap[wrapKey.Key] = nil  // key remains in map
}

No delete operation exists: A search for delete(.*statsInfoMap returns zero results in the entire codebase.

Related evidence: The commented-out code at engine.go:894:

//e.globalStats.RemoveTid(tblId)

suggests cleanup was considered but never implemented.

Impact

  • In systems with high table churn (frequent CREATE/DROP), the map accumulates stale nil entries
  • Go maps do not shrink their internal bucket array even when entries are deleted, so nil entries still consume memory in the hash table structure
  • The StatsInfoKey struct used as map key contains table/database IDs — each entry is at least 16+ bytes overhead
  • Over time, this causes gradual memory growth proportional to the total number of unique tables ever accessed

Suggested Fix

  1. Use delete(gs.mu.statsInfoMap, key) instead of setting to nil
  2. Implement the commented-out RemoveTid() cleanup path when tables are dropped
  3. Or add periodic cleanup that removes nil entries from the map

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions