Skip to content

stat_user_tables: Add total size metric #904

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

Merged
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
24 changes: 21 additions & 3 deletions collector/pg_stat_user_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ var (
[]string{"datname", "schemaname", "relname"},
prometheus.Labels{},
)
statUserTablesTotalSize = prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTableSubsystem, "size_bytes"),
"Total disk space used by this table, in bytes, including all indexes and TOAST data",
[]string{"datname", "schemaname", "relname"},
prometheus.Labels{},
)

statUserTablesQuery = `SELECT
current_database() datname,
Expand All @@ -173,7 +179,8 @@ var (
vacuum_count,
autovacuum_count,
analyze_count,
autoanalyze_count
autoanalyze_count,
pg_total_relation_size(relid) as total_size
FROM
pg_stat_user_tables`
)
Expand All @@ -191,10 +198,10 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
for rows.Next() {
var datname, schemaname, relname sql.NullString
var seqScan, seqTupRead, idxScan, idxTupFetch, nTupIns, nTupUpd, nTupDel, nTupHotUpd, nLiveTup, nDeadTup,
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount sql.NullInt64
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, totalSize sql.NullInt64
var lastVacuum, lastAutovacuum, lastAnalyze, lastAutoanalyze sql.NullTime

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); err != nil {
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 {
return err
}

Expand Down Expand Up @@ -419,6 +426,17 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
autoanalyzeCountMetric,
datnameLabel, schemanameLabel, relnameLabel,
)

totalSizeMetric := 0.0
if totalSize.Valid {
totalSizeMetric = float64(totalSize.Int64)
}
ch <- prometheus.MustNewConstMetric(
statUserTablesTotalSize,
prometheus.GaugeValue,
totalSizeMetric,
datnameLabel, schemanameLabel, relnameLabel,
)
}

if err := rows.Err(); err != nil {
Expand Down
10 changes: 7 additions & 3 deletions collector/pg_stat_user_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
"vacuum_count",
"autovacuum_count",
"analyze_count",
"autoanalyze_count"}
"autoanalyze_count",
"total_size"}
rows := sqlmock.NewRows(columns).
AddRow("postgres",
"public",
Expand All @@ -94,7 +95,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
11,
12,
13,
14)
14,
15)
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric)
go func() {
Expand Down Expand Up @@ -170,7 +172,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
"vacuum_count",
"autovacuum_count",
"analyze_count",
"autoanalyze_count"}
"autoanalyze_count",
"total_size"}
rows := sqlmock.NewRows(columns).
AddRow("postgres",
nil,
Expand All @@ -193,6 +196,7 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
nil,
nil,
nil,
nil,
nil)
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric)
Expand Down