Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add metrics for prepared statement memory usage #97590

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2542,6 +2542,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 @@ -2563,6 +2571,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