Skip to content
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
40 changes: 29 additions & 11 deletions collector/pg_stat_user_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ 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",
statUserIndexSize = prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTableSubsystem, "index_size_bytes"),
"Total disk space used by this index, in bytes",
[]string{"datname", "schemaname", "relname"},
prometheus.Labels{},
)
statUserTableSize = prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTableSubsystem, "table_size_bytes"),
"Total disk space used by this table, in bytes",
[]string{"datname", "schemaname", "relname"},
prometheus.Labels{},
)
Expand Down Expand Up @@ -180,7 +186,8 @@ var (
autovacuum_count,
analyze_count,
autoanalyze_count,
pg_total_relation_size(relid) as total_size
pg_indexes_size(relid) as indexes_size,
pg_table_size(relid) as table_size
FROM
pg_stat_user_tables`
)
Expand All @@ -198,10 +205,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, totalSize sql.NullInt64
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, indexSize, tableSize 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, &totalSize); 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, &indexSize, &tableSize); err != nil {
return err
}

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

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

tableSizeMetric := 0.0
if tableSize.Valid {
tableSizeMetric = float64(tableSize.Int64)
}
ch <- prometheus.MustNewConstMetric(
statUserTablesTotalSize,
statUserTableSize,
prometheus.GaugeValue,
totalSizeMetric,
tableSizeMetric,
datnameLabel, schemanameLabel, relnameLabel,
)
}
Expand Down
14 changes: 11 additions & 3 deletions collector/pg_stat_user_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
"autovacuum_count",
"analyze_count",
"autoanalyze_count",
"total_size"}
"index_size",
"table_size"}
rows := sqlmock.NewRows(columns).
AddRow("postgres",
"public",
Expand All @@ -96,7 +97,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
12,
13,
14,
15)
15,
16)
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric)
go func() {
Expand Down Expand Up @@ -128,6 +130,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 12},
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 13},
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_COUNTER, value: 14},
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 15},
{labels: labelMap{"datname": "postgres", "schemaname": "public", "relname": "a_table"}, metricType: dto.MetricType_GAUGE, value: 16},
}

convey.Convey("Metrics comparison", t, func() {
Expand Down Expand Up @@ -173,7 +177,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
"autovacuum_count",
"analyze_count",
"autoanalyze_count",
"total_size"}
"index_size",
"table_size"}
rows := sqlmock.NewRows(columns).
AddRow("postgres",
nil,
Expand All @@ -197,6 +202,7 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
nil,
nil,
nil,
nil,
nil)
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric)
Expand Down Expand Up @@ -229,6 +235,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0},
{labels: labelMap{"datname": "postgres", "schemaname": "unknown", "relname": "unknown"}, metricType: dto.MetricType_GAUGE, value: 0},
}

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