diff --git a/collector/pg_stat_user_tables.go b/collector/pg_stat_user_tables.go index 949a0ea2..af3822ca 100644 --- a/collector/pg_stat_user_tables.go +++ b/collector/pg_stat_user_tables.go @@ -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, @@ -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` ) @@ -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 } @@ -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 { diff --git a/collector/pg_stat_user_tables_test.go b/collector/pg_stat_user_tables_test.go index e592fa5e..5e82335c 100644 --- a/collector/pg_stat_user_tables_test.go +++ b/collector/pg_stat_user_tables_test.go @@ -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", @@ -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() { @@ -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, @@ -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)