cockroachdb / cockroachdb/cockroach
sql: support resource groups in SQL Stats (statement_statistics, transaction_statistics)
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Is your feature request related to a problem? Please describe.**
Resource groups were introduced as a SQL-level construct in v26.3 (`CREATE/ALTER/DROP/SHOW RESOURCE GROUP`, backed by `system.resource_groups`, gated behind `sql.experimental_resource_groups.enabled` + admin role). A resource group is bound to a session via `SET resource_group = ''` and stamped onto every transaction for admission control.
Today SQL Stats has no way to attribute statement/transaction statistics to the resource group an execution ran under. Operators can't answer "how much service latency / CPU / contention is attributable to resource group X" or compare the same fingerprint's behavior across groups.
**Describe the solution you'd like**
Add `resource_group` as a new dimension on `system.statement_statistics` and `system.transaction_statistics` (and the corresponding `statement_activity`/`transaction_activity` rollups), surfaced through the `crdb_internal.*_statistics` views.
The resource group is already known at execution time — `ex.sessionResourceGroupID()` reads `sessionData().ResourceGroupID`, and `ResourceGroupName` is also available — so plumbing it into `sqlstats.RecordedStmtStats` / `RecordedTxnStats` is straightforward.
Key design point: it should be a **key/PK dimension** (analogous to `app_name` / `node_id`), *not* part of the query `fingerprint_id`. The fingerprint stays query-shape-only; a given fingerprint fans out into one row per resource group. This is unlike `plan_gist`, which is stored in the value rather than the key (the keyed plan dimension is `plan_hash`).
**Code References:**
- [resource_group.go](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/resource_group.go) — DDL surface
- [resourcegroupcache/cache.go:50](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/resourcegroupcache/cache.go#L50) — name→id resolution
- [conn_executor.go:3976](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/conn_executor.go#L3976) — `sessionResourceGroupID()`
- [appstatspb/app_stats.pb.go:362](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/appstatspb/app_stats.pb.go#L362) — `StatementStatisticsKey`
- [systemschema/system.go:728](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/catalog/systemschema/system.go#L728) — `statement_statistics` PK
- [sqlstats/ssprovider.go:62](https://github.com/cockroachdb/cockroach/blob/master/pkg/sql/sqlstats/ssprovider.go#L62) — `RecordedStmtStats`
**Considerations / open questions**
- Adding a PK column to `statement_statistics`/`transaction_statistics` is a heavy change: migration, new hash-shard expr, golden-file/schema-hash test updates, and version-gated mixed-version handling (see the `system-table-change` checklist).
- Store the resource group **id** in the key (stable, compact, matches admission control); resolve to name at display time. Need a defined meaning for `0` (no group set).
- The name→id cache (`resourcegroupcache`) currently only resolves the two built-in groups; user-defined groups can be created/stored but not yet `SET`. May be worth sequencing this work after the rangefeed-backed cache lands, since until then nearly all rows would be `0` or a built-in id.
**Additional context**
Filed via agent.
Jira issue: CRDB-64731
Epic CRDB-65731
Contributor guide
Assessment
This issue has not been assessed yet.