#15250·rocksdb

Stats history deltas use an unguarded uint64 subtraction, so Statistics::Reset() records counts near 2^64 (also persisted to disk)

Author: hanke580Created Sep 22, 2026Updated Sep 22, 2026

Summary

DBImpl::PersistStats stores the difference between the current ticker values and the previous snapshot using a plain uint64_t subtraction, in both of its branches. The arithmetic is only valid while tickers are monotone. Statistics::Reset() is a documented public API that sets every ticker back to zero, after which the subtraction wraps and the recorded "delta" is a value around 1.8e19.

Those values go into the in-memory stats history read back by GetStatsHistory, and — when persist_stats_to_disk is set — into the ___rocksdb_stats_history___ column family, where they are durable and survive restarts. Nothing distinguishes them from real counts.

Reproduction

stats_persist_period_sec = 1, ordinary Put/Get/Flush churn, then Statistics::Reset(), then a little more churn. Run on main today:

=== stats_persist_period_sec=1, persist_stats_to_disk=false (in memory) ===
   after normal churn         slices=4   absurd values=0
   -> calling Statistics::Reset()
   Reset() -> OK
   after Reset() + churn      slices=9   absurd values=13
                              worst: rocksdb.number.block_compression_bypassed = 18446744073709551603

=== stats_persist_period_sec=1, persist_stats_to_disk=true (persistent stats CF) ===
   after normal churn         slices=4   absurd values=0
   -> calling Statistics::Reset()
   Reset() -> OK
   after Reset() + churn      slices=10  absurd values=17
                              worst: rocksdb.files.deleted.immediately = 18446744073709551615

18446744073709551603 is 2^64 - 13: the ticker stood at 13 when the previous sample was taken and at 0 afterwards. 18446744073709551615 is 2^64 - 1.

The "after normal churn" rows are the control — with the accumulator only growing, the same run records zero absurd values, so the delta arithmetic is correct until the reset.

Root cause

db/db_impl/db_impl.cc, DBImpl::PersistStats, both branches:

cpp
// line 1396 — persist_stats_to_disk == true
if (stats_slice_.find(stat.first) != stats_slice_.end()) {
  uint64_t delta = stat.second - stats_slice_[stat.first];
  s = batch.Put(persist_stats_cf_handle_, Slice(key, std::min(100, length)),
                std::to_string(delta));
}
cpp
// line 1432 — persist_stats_to_disk == false
if (stats_slice_.find(stat.first) != stats_slice_.end()) {
  stats_delta[stat.first] = stat.second - stats_slice_[stat.first];
}

stats_slice_ holds the previous sample. Both operands are uint64_t; neither subtraction is guarded against the current value being smaller.

Statistics::Reset() is public and documented to do exactly that (include/rocksdb/statistics.h):

cpp
// Resets all ticker and histogram stats
virtual Status Reset() { return Status::NotSupported("Not implemented"); }

StatisticsImpl::Reset() implements it and returns OK, so a caller has every reason to use it.

Suggested fix

Clamp at both sites:

cpp
uint64_t prev = stats_slice_[stat.first];
uint64_t delta = stat.second >= prev ? stat.second - prev : stat.second;

Treating the post-reset value as the delta is the natural reading — the counter restarted, so everything it has counted since is new. Skipping the entry would also be defensible. Recording 2^64 minus a small number as a count is not.

Environment

  • main at 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21)
  • CMake Release, gcc 11.4.0, Ubuntu 22.04 x86-64
  • Also present in 11.8.1