cockroachdb / cockroachdb/cockroach
sql: instrument remaining per-query goroutines for SQL CPU accounting
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Summary**
Follow-up to #169997, which added always-on per-query SQL CPU measurement. A few per-query goroutines and KV call sites are still uninstrumented and contribute to under/over-reporting of the `SQLCPUTime` stat.
**`LocalKVCPUTime` gaps (cause SQL CPU over-reporting)**
- **Sequences** — `pkg/sql/sequence.go`. Five KV call sites in `incrementSequenceUsingCache`, `SetSequenceValueByID`, and `getSequenceValueFromDesc` (`p.txn.Inc`, `kv.IncrementValRetryable`, `p.txn.Put`, `DB.Put`, `txn.Get`). Runs on the gateway execution goroutine. Originally flagged by @ZhouXing19 on #169997.
**`RawSQLCPUTime` gaps (cause SQL CPU under-reporting)**
- **Virtual table population workers** — `pkg/sql/virtual_table.go:107`, `stopper.RunAsyncTaskEx` in `setupGenerator`. Worker runs the vtable's population function on a dedicated goroutine.
- **Planhook async workers** — `pkg/sql/planhook.go:186`. BACKUP, RESTORE, IMPORT, ALTER LOCALITY, etc. execute their body on this goroutine.
- **Row-engine inbox reader** — `pkg/sql/flowinfra/inbound.go:133`. Decodes producer messages and pushes to a `RowChannel`. Lower value (decoding only, bounded by bytes received), but closes a parity gap with the vectorized inbox, which runs decoding inline on the consumer's processor goroutine.
**Pattern**
For each goroutine, follow the pattern used in #169997 for the HashRouter and `ParallelUnorderedSynchronizer` worker: per-goroutine `timeutil.CPUStopWatch`, emit `Metrics.RawSQLCPUTime` as `ProducerMetadata` on the path that ships metadata back to the consumer (`DistSQLReceiver.pushMeta` aggregates into `topLevelQueryStats.rawSQLCPUTime`).
For sequences, add a planner-side accumulator drained at the gateway into `topLevelQueryStats.localKVCPUTime`.
Jira issue: CRDB-64087
Contributor guide
Research direction
Start by reading #169997 and the named entry points in pkg/sql/sequence.go, pkg/sql/virtual_table.go, pkg/sql/planhook.go, and pkg/sql/flowinfra/inbound.go. Trace how worker metadata reaches DistSQLReceiver.pushMeta and how sequence KV work reaches the gateway; done means the listed goroutines and sequence call sites are included in SQLCPUTime without breaking existing accounting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sql
- Domain
- databases, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100