Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add 'postgres_table_tuples_total' based on pg_class.reltuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Aug 31, 2021
1 parent 9f81b6b commit 7c0bc47
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
28 changes: 20 additions & 8 deletions internal/collector/postgres_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (

const (
userTablesQuery = "SELECT current_database() AS database, s1.schemaname AS schema, s1.relname AS table, " +
"seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_live_tup, n_dead_tup, " +
"n_mod_since_analyze," +
"extract('epoch' from age(now(), greatest(last_vacuum, last_autovacuum))) as last_vacuum_seconds, " +
"extract('epoch' from age(now(), greatest(last_analyze, last_autoanalyze))) as last_analyze_seconds, " +
"extract('epoch' from greatest(last_vacuum, last_autovacuum)) as last_vacuum_time," +
"extract('epoch' from greatest(last_analyze, last_autoanalyze)) as last_analyze_time," +
"seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, " +
"n_live_tup, n_dead_tup, n_mod_since_analyze, " +
"extract('epoch' from age(now(), greatest(last_vacuum, last_autovacuum))) AS last_vacuum_seconds, " +
"extract('epoch' from age(now(), greatest(last_analyze, last_autoanalyze))) AS last_analyze_seconds, " +
"extract('epoch' from greatest(last_vacuum, last_autovacuum)) AS last_vacuum_time," +
"extract('epoch' from greatest(last_analyze, last_autoanalyze)) AS last_analyze_time," +
"vacuum_count, autovacuum_count, analyze_count, autoanalyze_count, heap_blks_read, heap_blks_hit, idx_blks_read, " +
"idx_blks_hit, toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit, pg_table_size(s1.relid) AS size_bytes " +
"FROM pg_stat_user_tables s1 JOIN pg_statio_user_tables s2 USING (schemaname, relname) " +
"idx_blks_hit, toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit, " +
"pg_table_size(s1.relid) AS size_bytes, reltuples " +
"FROM pg_stat_user_tables s1 JOIN pg_statio_user_tables s2 USING (schemaname, relname) JOIN pg_class c ON s1.relid = c.oid " +
"WHERE NOT EXISTS (SELECT 1 FROM pg_locks WHERE relation = s1.relid AND mode = 'AccessExclusiveLock' AND granted)"
)

Expand All @@ -44,6 +45,7 @@ type postgresTablesCollector struct {
maintenance typedDesc
io typedDesc
sizes typedDesc
reltuples typedDesc
labelNames []string
}

Expand Down Expand Up @@ -164,6 +166,12 @@ func NewPostgresTablesCollector(constLabels labels, settings model.CollectorSett
labels, constLabels,
settings.Filters,
),
reltuples: newBuiltinTypedDesc(
descOpts{"postgres", "table", "tuples_total", "Number of rows in the table based on pg_class.reltuples value.", 0},
prometheus.GaugeValue,
labels, constLabels,
settings.Filters,
),
}, nil
}

Expand Down Expand Up @@ -278,6 +286,7 @@ func (c *postgresTablesCollector) Update(config Config, ch chan<- prometheus.Met
}

ch <- c.sizes.newConstMetric(stat.sizebytes, stat.database, stat.schema, stat.table)
ch <- c.reltuples.newConstMetric(stat.reltuples, stat.database, stat.schema, stat.table)
}
}

Expand Down Expand Up @@ -317,6 +326,7 @@ type postgresTableStat struct {
tidxread float64
tidxhit float64
sizebytes float64
reltuples float64
}

// parsePostgresTableStats parses PGResult and returns structs with stats values.
Expand Down Expand Up @@ -422,6 +432,8 @@ func parsePostgresTableStats(r *model.PGResult, labelNames []string) map[string]
s.tidxhit = v
case "size_bytes":
s.sizebytes = v
case "reltuples":
s.reltuples = v
default:
continue
}
Expand Down
6 changes: 5 additions & 1 deletion internal/collector/postgres_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestPostgresTablesCollector_Update(t *testing.T) {
"postgres_table_last_analyze_time",
"postgres_table_maintenance_total",
"postgres_table_size_bytes",
"postgres_table_tuples_total",
},
optional: []string{
"postgres_table_io_blocks_total",
Expand All @@ -49,7 +50,7 @@ func Test_parsePostgresTableStats(t *testing.T) {
name: "normal output",
res: &model.PGResult{
Nrows: 1,
Ncols: 30,
Ncols: 32,
Colnames: []pgproto3.FieldDescription{
{Name: []byte("database")}, {Name: []byte("schema")}, {Name: []byte("table")},
{Name: []byte("seq_scan")}, {Name: []byte("seq_tup_read")}, {Name: []byte("idx_scan")}, {Name: []byte("idx_tup_fetch")},
Expand All @@ -59,6 +60,7 @@ func Test_parsePostgresTableStats(t *testing.T) {
{Name: []byte("vacuum_count")}, {Name: []byte("autovacuum_count")}, {Name: []byte("analyze_count")}, {Name: []byte("autoanalyze_count")},
{Name: []byte("heap_blks_read")}, {Name: []byte("heap_blks_hit")}, {Name: []byte("idx_blks_read")}, {Name: []byte("idx_blks_hit")},
{Name: []byte("toast_blks_read")}, {Name: []byte("toast_blks_hit")}, {Name: []byte("tidx_blks_read")}, {Name: []byte("tidx_blks_hit")},
{Name: []byte("size_bytes")}, {Name: []byte("reltuples")},
},
Rows: [][]sql.NullString{
{
Expand All @@ -70,6 +72,7 @@ func Test_parsePostgresTableStats(t *testing.T) {
{String: "910", Valid: true}, {String: "920", Valid: true}, {String: "930", Valid: true}, {String: "940", Valid: true},
{String: "4528", Valid: true}, {String: "5845", Valid: true}, {String: "458", Valid: true}, {String: "698", Valid: true},
{String: "125", Valid: true}, {String: "825", Valid: true}, {String: "699", Valid: true}, {String: "375", Valid: true},
{String: "458523", Valid: true}, {String: "50000", Valid: true},
},
},
},
Expand All @@ -80,6 +83,7 @@ func Test_parsePostgresTableStats(t *testing.T) {
inserted: 300, updated: 400, deleted: 500, hotUpdated: 150, live: 600, dead: 100, modified: 500,
lastvacuumAge: 700, lastanalyzeAge: 800, lastvacuumTime: 12345678, lastanalyzeTime: 87654321, vacuum: 910, autovacuum: 920, analyze: 930, autoanalyze: 940,
heapread: 4528, heaphit: 5845, idxread: 458, idxhit: 698, toastread: 125, toasthit: 825, tidxread: 699, tidxhit: 375,
sizebytes: 458523, reltuples: 50000,
},
},
},
Expand Down

0 comments on commit 7c0bc47

Please sign in to comment.