Skip to content

Commit

Permalink
sql: add metrics for prepared statement memory usage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
michae2 committed Feb 23, 2023
1 parent 2efb369 commit e4872bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 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
14 changes: 13 additions & 1 deletion 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 @@ -92,6 +96,8 @@ 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{
Expand All @@ -108,6 +114,12 @@ func MakeMemMetrics(endpoint string, histogramWindow time.Duration) MemoryMetric
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets}),
SessionCurBytesCount: metric.NewGauge(MetaMemSessionCurBytes),
SessionPreparedMaxBytesHist: metric.NewHistogram(metric.HistogramOptions{
Metadata: MetaMemMaxSessionPreparedBytes,
Duration: histogramWindow,
MaxVal: log10int64times1000,
SigFigs: 3,
Buckets: metric.MemoryUsage64MBBuckets}),
SessionPreparedCurBytesCount: metric.NewGauge(MetaMemSessionPreparedCurBytes),
}

}

0 comments on commit e4872bc

Please sign in to comment.