Skip to content

Commit 5edc331

Browse files
SticksmanSuperQ
andauthored
Record table only size bytes as well in addition to the total size bytes (#1149)
* Record table only size bytes as well in addition to the total size bytes Signed-off-by: Felix Yuan <[email protected]> * Update collector/pg_stat_user_tables.go Co-authored-by: Ben Kochie <[email protected]> Signed-off-by: Felix Yuan <[email protected]> * Update collector/pg_stat_user_tables.go Co-authored-by: Ben Kochie <[email protected]> Signed-off-by: Felix Yuan <[email protected]> * Finish renaming elements to index and table size Signed-off-by: Felix Yuan <[email protected]> --------- Signed-off-by: Felix Yuan <[email protected]> Co-authored-by: Ben Kochie <[email protected]>
1 parent f8b7139 commit 5edc331

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

Diff for: collector/pg_stat_user_tables.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ var (
150150
[]string{"datname", "schemaname", "relname"},
151151
prometheus.Labels{},
152152
)
153-
statUserTablesTotalSize = prometheus.NewDesc(
154-
prometheus.BuildFQName(namespace, userTableSubsystem, "size_bytes"),
155-
"Total disk space used by this table, in bytes, including all indexes and TOAST data",
153+
statUserIndexSize = prometheus.NewDesc(
154+
prometheus.BuildFQName(namespace, userTableSubsystem, "index_size_bytes"),
155+
"Total disk space used by this index, in bytes",
156+
[]string{"datname", "schemaname", "relname"},
157+
prometheus.Labels{},
158+
)
159+
statUserTableSize = prometheus.NewDesc(
160+
prometheus.BuildFQName(namespace, userTableSubsystem, "table_size_bytes"),
161+
"Total disk space used by this table, in bytes",
156162
[]string{"datname", "schemaname", "relname"},
157163
prometheus.Labels{},
158164
)
@@ -180,7 +186,8 @@ var (
180186
autovacuum_count,
181187
analyze_count,
182188
autoanalyze_count,
183-
pg_total_relation_size(relid) as total_size
189+
pg_indexes_size(relid) as indexes_size,
190+
pg_table_size(relid) as table_size
184191
FROM
185192
pg_stat_user_tables`
186193
)
@@ -198,10 +205,10 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
198205
for rows.Next() {
199206
var datname, schemaname, relname sql.NullString
200207
var seqScan, seqTupRead, idxScan, idxTupFetch, nTupIns, nTupUpd, nTupDel, nTupHotUpd, nLiveTup, nDeadTup,
201-
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, totalSize sql.NullInt64
208+
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, indexSize, tableSize sql.NullInt64
202209
var lastVacuum, lastAutovacuum, lastAnalyze, lastAutoanalyze sql.NullTime
203210

204-
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount, &totalSize); err != nil {
211+
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount, &indexSize, &tableSize); err != nil {
205212
return err
206213
}
207214

@@ -427,14 +434,25 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
427434
datnameLabel, schemanameLabel, relnameLabel,
428435
)
429436

430-
totalSizeMetric := 0.0
431-
if totalSize.Valid {
432-
totalSizeMetric = float64(totalSize.Int64)
437+
indexSizeMetric := 0.0
438+
if indexSize.Valid {
439+
indexSizeMetric = float64(indexSize.Int64)
440+
}
441+
ch <- prometheus.MustNewConstMetric(
442+
statUserIndexSize,
443+
prometheus.GaugeValue,
444+
indexSizeMetric,
445+
datnameLabel, schemanameLabel, relnameLabel,
446+
)
447+
448+
tableSizeMetric := 0.0
449+
if tableSize.Valid {
450+
tableSizeMetric = float64(tableSize.Int64)
433451
}
434452
ch <- prometheus.MustNewConstMetric(
435-
statUserTablesTotalSize,
453+
statUserTableSize,
436454
prometheus.GaugeValue,
437-
totalSizeMetric,
455+
tableSizeMetric,
438456
datnameLabel, schemanameLabel, relnameLabel,
439457
)
440458
}

Diff for: collector/pg_stat_user_tables_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
7272
"autovacuum_count",
7373
"analyze_count",
7474
"autoanalyze_count",
75-
"total_size"}
75+
"index_size",
76+
"table_size"}
7677
rows := sqlmock.NewRows(columns).
7778
AddRow("postgres",
7879
"public",
@@ -96,7 +97,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
9697
12,
9798
13,
9899
14,
99-
15)
100+
15,
101+
16)
100102
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
101103
ch := make(chan prometheus.Metric)
102104
go func() {
@@ -128,6 +130,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
128130
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 12},
129131
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 13},
130132
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 14},
133+
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 15},
134+
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 16},
131135
}
132136

133137
convey.Convey("Metrics comparison", t, func() {
@@ -173,7 +177,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
173177
"autovacuum_count",
174178
"analyze_count",
175179
"autoanalyze_count",
176-
"total_size"}
180+
"index_size",
181+
"table_size"}
177182
rows := sqlmock.NewRows(columns).
178183
AddRow("postgres",
179184
nil,
@@ -197,6 +202,7 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
197202
nil,
198203
nil,
199204
nil,
205+
nil,
200206
nil)
201207
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
202208
ch := make(chan prometheus.Metric)
@@ -229,6 +235,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
229235
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
230236
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
231237
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
238+
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0},
239+
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0},
232240
}
233241

234242
convey.Convey("Metrics comparison", t, func() {

0 commit comments

Comments
 (0)