Skip to content

Commit f9eac5f

Browse files
authored
Merge a11aef2 into blathers/backport-release-25.4-154711
2 parents e987a7f + a11aef2 commit f9eac5f

File tree

5 files changed

+56
-20
lines changed

5 files changed

+56
-20
lines changed

pkg/sql/catalog/schematelemetry/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
deps = [
1313
"//pkg/jobs",
1414
"//pkg/jobs/jobspb",
15+
"//pkg/keys",
1516
"//pkg/scheduledjobs",
1617
"//pkg/security/username",
1718
"//pkg/server/telemetry",

pkg/sql/catalog/schematelemetry/schema_telemetry_job.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ package schematelemetry
77

88
import (
99
"context"
10+
"math"
1011

1112
"github.com/cockroachdb/cockroach/pkg/jobs"
1213
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
14+
"github.com/cockroachdb/cockroach/pkg/keys"
1315
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
1416
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
1517
"github.com/cockroachdb/cockroach/pkg/sql"
@@ -63,6 +65,31 @@ func (t schemaTelemetryResumer) Resume(ctx context.Context, execCtx interface{})
6365
if k := p.ExecCfg().SchemaTelemetryTestingKnobs; k != nil {
6466
knobs = *k
6567
}
68+
// Notify the stats refresher to update the system.descriptors table stats,
69+
// and update the object count in schema changer metrics.
70+
err := p.ExecCfg().InternalDB.DescsTxn(ctx, func(ctx context.Context, txn descs.Txn) error {
71+
desc, err := txn.Descriptors().ByIDWithLeased(txn.KV()).Get().Table(ctx, keys.DescriptorTableID)
72+
if err != nil {
73+
return err
74+
}
75+
p.ExecCfg().StatsRefresher.NotifyMutation(desc, math.MaxInt64 /* rowCount */)
76+
77+
// Note: This won't be perfectly up-to-date, but it will make sure the
78+
// metric gets updated periodically. It also gets updated after every
79+
// schema change.
80+
tableStats, err := p.ExecCfg().TableStatsCache.GetTableStats(ctx, desc, nil /* typeResolver */)
81+
if err != nil {
82+
return err
83+
}
84+
if len(tableStats) > 0 {
85+
// Use the row count from the most recent statistic.
86+
p.ExecCfg().SchemaChangerMetrics.ObjectCount.Update(int64(tableStats[0].RowCount))
87+
}
88+
return nil
89+
})
90+
if err != nil {
91+
return errors.Wrap(err, "failed to notify stats refresher to update system.descriptors table stats")
92+
}
6693

6794
// Outside of tests, scan the catalog tables AS OF SYSTEM TIME slightly in the
6895
// past. Schema telemetry is not latency-sensitive to the point where a few

pkg/sql/conn_executor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,11 +4166,10 @@ func (ex *connExecutor) txnStateTransitionsApplyWrapper(
41664166
if err := ex.waitForInitialVersionForNewDescriptors(cachedRegions); err != nil {
41674167
return advanceInfo{}, err
41684168
}
4169-
}
4170-
if ex.extraTxnState.descCollection.CountUncommittedNewOrDroppedDescriptors() > 0 {
4169+
41714170
execCfg := ex.planner.ExecCfg()
41724171
if err := UpdateDescriptorCount(ex.Ctx(), execCfg, execCfg.SchemaChangerMetrics); err != nil {
4173-
log.Dev.Warningf(ex.Ctx(), "failed to scan descriptor table: %v", err)
4172+
log.Dev.Warningf(ex.Ctx(), "failed to update descriptor count metric: %v", err)
41744173
}
41754174
}
41764175
fallthrough

pkg/sql/schema_changer.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3574,20 +3574,3 @@ func (p *planner) CanCreateCrossDBSequenceRef() error {
35743574
}
35753575
return nil
35763576
}
3577-
3578-
// UpdateDescriptorCount updates our sql.schema_changer.object_count gauge with
3579-
// a fresh count of objects in the system.descriptor table.
3580-
func UpdateDescriptorCount(
3581-
ctx context.Context, execCfg *ExecutorConfig, metric *SchemaChangerMetrics,
3582-
) error {
3583-
return DescsTxn(ctx, execCfg, func(ctx context.Context, txn isql.Txn, col *descs.Collection) error {
3584-
row, err := txn.QueryRow(ctx, "sql-schema-changer-object-count", txn.KV(),
3585-
`SELECT count(*) FROM system.descriptor`)
3586-
if err != nil {
3587-
return err
3588-
}
3589-
count := *row[0].(*tree.DInt)
3590-
metric.ObjectCount.Update(int64(count))
3591-
return nil
3592-
})
3593-
}

pkg/sql/schema_changer_metrics.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
package sql
77

88
import (
9+
"context"
10+
11+
"github.com/cockroachdb/cockroach/pkg/keys"
912
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
13+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
1014
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
1115
"github.com/cockroachdb/cockroach/pkg/util/metric"
1216
)
@@ -42,3 +46,25 @@ func NewSchemaChangerMetrics() *SchemaChangerMetrics {
4246
ObjectCount: metric.NewGauge(metaObjects),
4347
}
4448
}
49+
50+
// UpdateDescriptorCount updates our sql.schema_changer.object_count gauge with
51+
// a fresh count of objects in the system.descriptor table.
52+
func UpdateDescriptorCount(
53+
ctx context.Context, execCfg *ExecutorConfig, metric *SchemaChangerMetrics,
54+
) error {
55+
return execCfg.InternalDB.DescsTxn(ctx, func(ctx context.Context, txn descs.Txn) error {
56+
desc, err := txn.Descriptors().ByIDWithLeased(txn.KV()).Get().Table(ctx, keys.DescriptorTableID)
57+
if err != nil {
58+
return err
59+
}
60+
tableStats, err := execCfg.TableStatsCache.GetTableStats(ctx, desc, nil /* typeResolver */)
61+
if err != nil {
62+
return err
63+
}
64+
if len(tableStats) > 0 {
65+
// Use the row count from the most recent statistic.
66+
metric.ObjectCount.Update(int64(tableStats[0].RowCount))
67+
}
68+
return nil
69+
})
70+
}

0 commit comments

Comments
 (0)