Skip to content

Commit

Permalink
sql: add full table or index scan count to metrics
Browse files Browse the repository at this point in the history
This new metric allows for users to see a time series of their full table
or index scans in the advanced debug console. This metric is part of
EngineMetrics, so there's a corresponding internal metric that counts
internal full table or index scans from internal engine queries.

Release note (ui change): User can see time series of full table or index
scans in advanced debug console.
  • Loading branch information
barryhe2000 committed Jan 27, 2021
1 parent bf439cd commit c12c4d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
Expand Down Expand Up @@ -301,7 +302,6 @@ func makeMetrics(internal bool) Metrics {
SQLOptFallbackCount: metric.NewCounter(getMetricMeta(MetaSQLOptFallback, internal)),
SQLOptPlanCacheHits: metric.NewCounter(getMetricMeta(MetaSQLOptPlanCacheHits, internal)),
SQLOptPlanCacheMisses: metric.NewCounter(getMetricMeta(MetaSQLOptPlanCacheMisses, internal)),

// TODO(mrtracy): See HistogramWindowInterval in server/config.go for the 6x factor.
DistSQLExecLatency: metric.NewLatency(getMetricMeta(MetaDistSQLExecLatency, internal),
6*metricsSampleInterval),
Expand All @@ -315,8 +315,9 @@ func makeMetrics(internal bool) Metrics {
6*metricsSampleInterval),
SQLTxnsOpen: metric.NewGauge(getMetricMeta(MetaSQLTxnsOpen, internal)),

TxnAbortCount: metric.NewCounter(getMetricMeta(MetaTxnAbort, internal)),
FailureCount: metric.NewCounter(getMetricMeta(MetaFailure, internal)),
TxnAbortCount: metric.NewCounter(getMetricMeta(MetaTxnAbort, internal)),
FailureCount: metric.NewCounter(getMetricMeta(MetaFailure, internal)),
FullTableOrIndexScanCount: metric.NewCounter(getMetricMeta(MetaFullTableOrIndexScan, internal)),
},
StartedStatementCounters: makeStartedStatementCounters(internal),
ExecutedStatementCounters: makeExecutedStatementCounters(internal),
Expand Down Expand Up @@ -566,7 +567,6 @@ func (s *Server) newConnExecutor(
nodeIDOrZero, _ := s.cfg.NodeID.OptionalNodeID()
sdMutator := new(sessionDataMutator)
*sdMutator = s.makeSessionDataMutator(sd, sdDefaults)

ex := &connExecutor{
server: s,
metrics: srvMetrics,
Expand Down
24 changes: 13 additions & 11 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,17 +914,19 @@ func (ex *connExecutor) makeExecPlan(ctx context.Context, planner *planner) erro

flags := planner.curPlan.flags

// We don't execute the statement if:
// - plan contains a full table or full index scan.
// - the session setting disallows full table/index scans.
// - the query is not an internal query.
if (flags.IsSet(planFlagContainsFullIndexScan) || flags.IsSet(planFlagContainsFullTableScan)) &&
planner.EvalContext().SessionData.DisallowFullTableScans && ex.executorType == executorTypeExec {
return errors.WithHint(
pgerror.Newf(pgcode.TooManyRows,
"query `%s` contains a full table/index scan which is explicitly disallowed",
planner.stmt.SQL),
"try overriding the `disallow_full_table_scans` cluster/session setting")
if flags.IsSet(planFlagContainsFullIndexScan) || flags.IsSet(planFlagContainsFullTableScan) {
if ex.executorType == executorTypeExec && planner.EvalContext().SessionData.DisallowFullTableScans {
// We don't execute the statement if:
// - plan contains a full table or full index scan.
// - the session setting disallows full table/index scans.
// - the query is not an internal query.
return errors.WithHint(
pgerror.Newf(pgcode.TooManyRows,
"query `%s` contains a full table/index scan which is explicitly disallowed",
planner.stmt.SQL),
"try overriding the `disallow_full_table_scans` cluster/session setting")
}
ex.metrics.EngineMetrics.FullTableOrIndexScanCount.Inc(1)
}

// TODO(knz): Remove this accounting if/when savepoint rollbacks
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,14 @@ var (
Measurement: "Open SQL Transactions",
Unit: metric.Unit_COUNT,
}
MetaFullTableOrIndexScan = metric.Metadata{
Name: "sql.full.scan.count",
Help: "Number of full table or index scans",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}

// Below are the metadata for the statement started counters.

MetaQueryStarted = metric.Metadata{
Name: "sql.query.started.count",
Help: "Number of SQL queries started",
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/executor_statement_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ type EngineMetrics struct {

// FailureCount counts non-retriable errors in open transactions.
FailureCount *metric.Counter

// FullTableOrIndexScanCount counts the number of full table or index scans.
FullTableOrIndexScanCount *metric.Counter
}

// EngineMetrics implements the metric.Struct interface
Expand Down

0 comments on commit c12c4d3

Please sign in to comment.