Skip to content

Commit

Permalink
Merge #97590
Browse files Browse the repository at this point in the history
97590: sql: add metrics for prepared statement memory usage r=yuzefovich,DrewKimball a=michae2

Add node-level metrics for memory used by prepared statements across all sessions.

Assists: #72581

Epic: None

Release note (ui change): Add the following new metrics to track memory usage of prepared statements in sessions:
- sql.mem.internal.session.prepared.current
- sql.mem.internal.session.prepared.max-avg
- sql.mem.internal.session.prepared.max-count
- sql.mem.internal.session.prepared.max-max
- sql.mem.internal.session.prepared.max-p50
- sql.mem.internal.session.prepared.max-p75
- sql.mem.internal.session.prepared.max-p90
- sql.mem.internal.session.prepared.max-p99
- sql.mem.internal.session.prepared.max-p99.9
- sql.mem.internal.session.prepared.max-p99.99
- sql.mem.internal.session.prepared.max-p99.999
- sql.mem.sql.session.prepared.current
- sql.mem.sql.session.prepared.max-avg
- sql.mem.sql.session.prepared.max-count
- sql.mem.sql.session.prepared.max-max
- sql.mem.sql.session.prepared.max-p50
- sql.mem.sql.session.prepared.max-p75
- sql.mem.sql.session.prepared.max-p90
- sql.mem.sql.session.prepared.max-p99
- sql.mem.sql.session.prepared.max-p99.9
- sql.mem.sql.session.prepared.max-p99.99
- sql.mem.sql.session.prepared.max-p99.999

Co-authored-by: Michael Erickson <[email protected]>
  • Loading branch information
craig[bot] and michae2 committed Feb 24, 2023
2 parents 769eac6 + aa31d92 commit e7c2286
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
14 changes: 14 additions & 0 deletions pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,13 @@ func (s *Server) newConnExecutor(
memMetrics.SessionMaxBytesHist,
-1 /* increment */, noteworthyMemoryUsageBytes, s.cfg.Settings,
)
sessionPreparedMon := mon.NewMonitor(
"session prepared statements",
mon.MemoryResource,
memMetrics.SessionPreparedCurBytesCount,
memMetrics.SessionPreparedMaxBytesHist,
-1 /* increment */, noteworthyMemoryUsageBytes, s.cfg.Settings,
)
// The txn monitor is started in txnState.resetForNewSQLTxn().
txnMon := mon.NewMonitor(
"txn",
Expand All @@ -975,6 +982,7 @@ func (s *Server) newConnExecutor(
clientComm: clientComm,
mon: sessionRootMon,
sessionMon: sessionMon,
sessionPreparedMon: sessionPreparedMon,
sessionDataStack: sdMutIterator.sds,
dataMutatorIterator: sdMutIterator,
state: txnState{
Expand Down Expand Up @@ -1205,10 +1213,12 @@ func (ex *connExecutor) close(ctx context.Context, closeType closeType) {

if closeType != panicClose {
ex.state.mon.Stop(ctx)
ex.sessionPreparedMon.Stop(ctx)
ex.sessionMon.Stop(ctx)
ex.mon.Stop(ctx)
} else {
ex.state.mon.EmergencyStop(ctx)
ex.sessionPreparedMon.EmergencyStop(ctx)
ex.sessionMon.EmergencyStop(ctx)
ex.mon.EmergencyStop(ctx)
}
Expand Down Expand Up @@ -1254,6 +1264,9 @@ type connExecutor struct {
// statistics for result sets (which escape transactions).
mon *mon.BytesMonitor
sessionMon *mon.BytesMonitor

// sessionPreparedMon tracks memory usage by prepared statements.
sessionPreparedMon *mon.BytesMonitor
// memMetrics contains the metrics that statements executed on this connection
// will contribute to.
memMetrics MemoryMetrics
Expand Down Expand Up @@ -1812,6 +1825,7 @@ func (ex *connExecutor) activate(
// single threaded, and the point of buffering is just to avoid contention.
ex.mon.Start(ctx, parentMon, reserved)
ex.sessionMon.StartNoReserved(ctx, ex.mon)
ex.sessionPreparedMon.StartNoReserved(ctx, ex.sessionMon)

// Enable the trace if configured.
if traceSessionEventLogEnabled.Get(&ex.server.cfg.Settings.SV) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/conn_executor_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (ex *connExecutor) prepare(
) (_ *PreparedStatement, retErr error) {

prepared := &PreparedStatement{
memAcc: ex.sessionMon.MakeBoundAccount(),
memAcc: ex.sessionPreparedMon.MakeBoundAccount(),
refCount: 1,

createdAt: timeutil.Now(),
Expand Down
49 changes: 26 additions & 23 deletions pkg/sql/mem_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type MemoryMetrics struct {
TxnCurBytesCount *metric.Gauge
SessionMaxBytesHist metric.IHistogram
SessionCurBytesCount *metric.Gauge

// For prepared statements.
SessionPreparedMaxBytesHist metric.IHistogram
SessionPreparedCurBytesCount *metric.Gauge
}

// MetricStruct implements the metrics.Struct interface.
Expand Down Expand Up @@ -66,20 +70,26 @@ func makeMemMetricMetadata(name, help string) metric.Metadata {
}
}

func makeMemMetricHistogram(
metadata metric.Metadata, histogramWindow time.Duration,
) metric.IHistogram {
return metric.NewHistogram(metric.HistogramOptions{
Metadata: metadata,
Duration: histogramWindow,
MaxVal: log10int64times1000,
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets,
})
}

// MakeBaseMemMetrics instantiates the metric objects for an SQL endpoint, but
// only includes the root metrics: .max and .current, without txn and session.
func MakeBaseMemMetrics(endpoint string, histogramWindow time.Duration) BaseMemoryMetrics {
prefix := "sql.mem." + endpoint
MetaMemMaxBytes := makeMemMetricMetadata(prefix+".max", "Memory usage per sql statement for "+endpoint)
MetaMemCurBytes := makeMemMetricMetadata(prefix+".current", "Current sql statement memory usage for "+endpoint)
return BaseMemoryMetrics{
MaxBytesHist: metric.NewHistogram(metric.HistogramOptions{
Metadata: MetaMemMaxBytes,
Duration: histogramWindow,
MaxVal: log10int64times1000,
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets,
}),
MaxBytesHist: makeMemMetricHistogram(MetaMemMaxBytes, histogramWindow),
CurBytesCount: metric.NewGauge(MetaMemCurBytes),
}
}
Expand All @@ -92,22 +102,15 @@ func MakeMemMetrics(endpoint string, histogramWindow time.Duration) MemoryMetric
MetaMemTxnCurBytes := makeMemMetricMetadata(prefix+".txn.current", "Current sql transaction memory usage for "+endpoint)
MetaMemMaxSessionBytes := makeMemMetricMetadata(prefix+".session.max", "Memory usage per sql session for "+endpoint)
MetaMemSessionCurBytes := makeMemMetricMetadata(prefix+".session.current", "Current sql session memory usage for "+endpoint)
MetaMemMaxSessionPreparedBytes := makeMemMetricMetadata(prefix+".session.prepared.max", "Memory usage by prepared statements per sql session for "+endpoint)
MetaMemSessionPreparedCurBytes := makeMemMetricMetadata(prefix+".session.prepared.current", "Current sql session memory usage by prepared statements for "+endpoint)
return MemoryMetrics{
BaseMemoryMetrics: base,
TxnMaxBytesHist: metric.NewHistogram(metric.HistogramOptions{
Metadata: MetaMemMaxTxnBytes,
Duration: histogramWindow,
MaxVal: log10int64times1000,
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets}),
TxnCurBytesCount: metric.NewGauge(MetaMemTxnCurBytes),
SessionMaxBytesHist: metric.NewHistogram(metric.HistogramOptions{
Metadata: MetaMemMaxSessionBytes,
Duration: histogramWindow,
MaxVal: log10int64times1000,
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets}),
SessionCurBytesCount: metric.NewGauge(MetaMemSessionCurBytes),
BaseMemoryMetrics: base,
TxnMaxBytesHist: makeMemMetricHistogram(MetaMemMaxTxnBytes, histogramWindow),
TxnCurBytesCount: metric.NewGauge(MetaMemTxnCurBytes),
SessionMaxBytesHist: makeMemMetricHistogram(MetaMemMaxSessionBytes, histogramWindow),
SessionCurBytesCount: metric.NewGauge(MetaMemSessionCurBytes),
SessionPreparedMaxBytesHist: makeMemMetricHistogram(MetaMemMaxSessionPreparedBytes, histogramWindow),
SessionPreparedCurBytesCount: metric.NewGauge(MetaMemSessionPreparedCurBytes),
}

}
16 changes: 16 additions & 0 deletions pkg/ts/catalog/chart_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,14 @@ var charts = []sectionDescription{
Title: "Session Current",
Metrics: []string{"sql.mem.internal.session.current"},
},
{
Title: "Prepared Statements All",
Metrics: []string{"sql.mem.internal.session.prepared.max"},
},
{
Title: "Prepared Statements Current",
Metrics: []string{"sql.mem.internal.session.prepared.current"},
},
{
Title: "Txn All",
Metrics: []string{"sql.mem.internal.txn.max"},
Expand All @@ -2570,6 +2578,14 @@ var charts = []sectionDescription{
Title: "Max",
Metrics: []string{"sql.mem.sql.session.max"},
},
{
Title: "Prepared Statements Current",
Metrics: []string{"sql.mem.sql.session.prepared.current"},
},
{
Title: "Prepared Statements Max",
Metrics: []string{"sql.mem.sql.session.prepared.max"},
},
},
},
{
Expand Down

0 comments on commit e7c2286

Please sign in to comment.