Skip to content

Commit

Permalink
vacuum finished
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmarsh committed Feb 8, 2023
1 parent 24dab2a commit 76b3704
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 13 deletions.
73 changes: 72 additions & 1 deletion exporter/collectors/pg_stat_user_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type PgStatUserTableCollector struct {
nLiveTup *prometheus.Desc
nDeadTup *prometheus.Desc
nModSinceAnalyze *prometheus.Desc
lastVacuum *prometheus.Desc
lastAutoVacuum *prometheus.Desc
lastAnalyze *prometheus.Desc
lastAutoAnalyze *prometheus.Desc
vacuumCount *prometheus.Desc
autoVacuumCount *prometheus.Desc
analyzeCount *prometheus.Desc
autoAnalyzeCount *prometheus.Desc
}

// NewPgStatUserTableCollector instantiates and returns a new PgStatUserTableCollector.
Expand Down Expand Up @@ -98,6 +106,54 @@ func NewPgStatUserTableCollector(db *db.Client) *PgStatUserTableCollector {
variableLabels,
nil,
),
lastVacuum: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "last_vacuum"),
"Last time at which this table was manually vacuumed (not counting VACUUM FULL)",
variableLabels,
nil,
),
lastAutoVacuum: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "last_autovacuum"),
"Last time at which this table was vacuumed by the autovacuum daemon",
variableLabels,
nil,
),
lastAnalyze: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "last_analyze"),
"Last time at which this table was manually analyzed",
variableLabels,
nil,
),
lastAutoAnalyze: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "last_autoanalyze"),
"Last time at which this table was analyzed by the autovacuum daemon",
variableLabels,
nil,
),
vacuumCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "vacuum_count"),
"Number of times this table has been manually vacuumed (not counting VACUUM FULL)",
variableLabels,
nil,
),
autoVacuumCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "autovacuum_count"),
"Number of times this table has been vacuumed by the autovacuum daemon",
variableLabels,
nil,
),
analyzeCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "analyze_count"),
"Number of times this table has been manually analyzed",
variableLabels,
nil,
),
autoAnalyzeCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, userTablesSubSystem, "autoanalyze_count"),
"Number of times this table has been analyzed by the autovacuum daemon",
variableLabels,
nil,
),
}
}

Expand All @@ -114,6 +170,14 @@ func (c *PgStatUserTableCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.nLiveTup
ch <- c.nDeadTup
ch <- c.nModSinceAnalyze
ch <- c.lastVacuum
ch <- c.lastAutoVacuum
ch <- c.lastAnalyze
ch <- c.lastAutoAnalyze
ch <- c.vacuumCount
ch <- c.autoVacuumCount
ch <- c.analyzeCount
ch <- c.autoAnalyzeCount
}

// Collect implements the promtheus.Collector.
Expand Down Expand Up @@ -145,7 +209,14 @@ func (c *PgStatUserTableCollector) Scrape(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(c.nLiveTup, prometheus.GaugeValue, float64(stat.NLiveTup), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.nDeadTup, prometheus.GaugeValue, float64(stat.NDeadTup), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.nModSinceAnalyze, prometheus.GaugeValue, float64(stat.NModSinceAnalyze), stat.DatName, stat.SchemaName, stat.RelName)
// TODO autovacuum. https://github.com/prometheus-community/postgres_exporter/blob/master/queries.yaml
ch <- prometheus.MustNewConstMetric(c.lastVacuum, prometheus.GaugeValue, float64(stat.LastVacuum.Time.UnixMicro()), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.lastAutoVacuum, prometheus.GaugeValue, float64(stat.LastAutoVacuum.Time.UnixMicro()), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.lastAnalyze, prometheus.GaugeValue, float64(stat.LastAnalyze.Time.UnixMicro()), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.lastAutoAnalyze, prometheus.GaugeValue, float64(stat.LastAutoAnalyze.Time.UnixMicro()), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.vacuumCount, prometheus.CounterValue, float64(stat.VacuumCount), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.autoVacuumCount, prometheus.CounterValue, float64(stat.AutoVacuumCount), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.analyzeCount, prometheus.CounterValue, float64(stat.AnalyzeCount), stat.DatName, stat.SchemaName, stat.RelName)
ch <- prometheus.MustNewConstMetric(c.autoAnalyzeCount, prometheus.CounterValue, float64(stat.AutoAnalyzeCount), stat.DatName, stat.SchemaName, stat.RelName)
}
return nil
}
4 changes: 2 additions & 2 deletions exporter/db/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ go_library(
srcs = [
"db.go",
"dsn.go",
"model.go",
"opts.go",
"pg_lock.go",
"pg_stat_activity.go",
"pg_stat_statements.go",
"pg_stat_user_table.go",
],
visibility = ["PUBLIC"],
deps = [
"//third_party/go:pgtype",
"//exporter/db/model",
"//third_party/go:pgx.v4",
"//third_party/go:scany",
],
Expand Down
10 changes: 10 additions & 0 deletions exporter/db/model/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
go_library(
name = "model",
srcs = [
"model.go",
],
visibility = ["PUBLIC"],
deps = [
"//third_party/go:pgtype",
],
)
28 changes: 27 additions & 1 deletion exporter/db/model.go → exporter/db/model/model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package model

import (
"github.com/jackc/pgtype"
Expand Down Expand Up @@ -44,3 +44,29 @@ type PgStatUserTable struct {
AnalyzeCount int `db:"analyze_count"`
AutoAnalyzeCount int `db:"autoanalyze_count"`
}

// PgStatStatement contiains information on user tables.
type PgStatStatement struct {
DatName string `db:"datname"`
SchemaName string `db:"schemaname"`
RelName string `db:"relname"`
SeqScan int `db:"seq_scan"`
SeqTupRead int `db:"seq_tup_read"`
IndexScan int `db:"idx_scan"`
IndexTupFetch int `db:"idx_tup_fetch"`
NTupInsert int `db:"n_tup_ins"`
NTupUpdate int `db:"n_tup_upd"`
NTupDelete int `db:"n_tup_del"`
NTupHotUpdate int `db:"n_tup_hot_upd"`
NLiveTup int `db:"n_live_tup"`
NDeadTup int `db:"n_dead_tup"`
NModSinceAnalyze int `db:"n_mod_since_analyze"`
LastVacuum pgtype.Timestamptz `db:"last_vacuum"`
LastAutoVacuum pgtype.Timestamptz `db:"last_autovacuum"`
LastAnalyze pgtype.Timestamptz `db:"last_analyze"`
LastAutoAnalyze pgtype.Timestamptz `db:"last_autoanalyze"`
VacuumCount int `db:"vacuum_count"`
AutoVacuumCount int `db:"autovacuum_count"`
AnalyzeCount int `db:"analyze_count"`
AutoAnalyzeCount int `db:"autoanalyze_count"`
}
6 changes: 4 additions & 2 deletions exporter/db/pg_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package db

import (
"context"

"github.com/odonate/postgres-exporter/exporter/db/model"
)

const sqlSelectPgLock = `
Expand All @@ -26,8 +28,8 @@ LEFT JOIN
ON tmp.mode=tmp2.mode and pg_database.oid = tmp2.database ORDER BY 1`

// SelectPgLocks selects stats on locks held.
func (db *Client) SelectPgLocks(ctx context.Context) ([]*PgLock, error) {
pgLocks := []*PgLock{}
func (db *Client) SelectPgLocks(ctx context.Context) ([]*model.PgLock, error) {
pgLocks := []*model.PgLock{}
if err := db.Select(ctx, &pgLocks, sqlSelectPgLock); err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions exporter/db/pg_stat_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package db

import (
"context"

"github.com/odonate/postgres-exporter/exporter/db/model"
)

const sqlSelectPgStatActivity = `
Expand Down Expand Up @@ -30,8 +32,8 @@ FROM pg_stat_activity GROUP BY datname,state) AS tmp2
ON tmp.state = tmp2.state AND pg_database.datname = tmp2.datname`

// SelectPgStatActivity selects stats on user tables.
func (db *Client) SelectPgStatActivity(ctx context.Context) ([]*PgStatActivity, error) {
pgStatActivities := []*PgStatActivity{}
func (db *Client) SelectPgStatActivity(ctx context.Context) ([]*model.PgStatActivity, error) {
pgStatActivities := []*model.PgStatActivity{}
if err := db.Select(ctx, &pgStatActivities, sqlSelectPgStatActivity); err != nil {
return nil, err
}
Expand Down
45 changes: 45 additions & 0 deletions exporter/db/pg_stat_statements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package db

import (
"context"

"github.com/odonate/postgres-exporter/exporter/db/model"
)

const sqlSelectPgStatStatements = `
SELECT
t2.rolname,
t3.datname,
queryid,
calls,
total_time / 1000 as total_time_seconds,
min_time / 1000 as min_time_seconds,
max_time / 1000 as max_time_seconds,
mean_time / 1000 as mean_time_seconds,
stddev_time / 1000 as stddev_time_seconds,
rows,
shared_blks_hit,
shared_blks_read,
shared_blks_dirtied,
shared_blks_written,
local_blks_hit,
local_blks_read,
local_blks_dirtied,
local_blks_written,
temp_blks_read,
temp_blks_written,
blk_read_time / 1000 as blk_read_time_seconds,
blk_write_time / 1000 as blk_write_time_seconds
FROM pg_stat_statements t1
JOIN pg_roles t2 ON (t1.userid=t2.oid)
JOIN pg_database t3 ON (t1.dbid=t3.oid)
WHERE t2.rolname != 'rdsadmin'`

// SelectPgStatStatements selects stats on user tables.
func (db *Client) SelectPgStatStatements(ctx context.Context) ([]*model.PgStatStatement, error) {
pgStatStatements := []*model.PgStatStatement{}
if err := db.Select(ctx, &pgStatStatements, sqlSelectPgStatStatements); err != nil {
return nil, err
}
return pgStatStatements, nil
}
10 changes: 5 additions & 5 deletions exporter/db/pg_stat_user_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package db

import (
"context"

"github.com/odonate/postgres-exporter/exporter/db/model"
)

const sqlSelectPgStatUserTables = `
Expand All @@ -28,13 +30,11 @@ SELECT
autovacuum_count,
analyze_count,
autoanalyze_count
FROM
pg_stat_user_tables
`
FROM pg_stat_user_tables`

// SelectPgStatUserTables selects stats on user tables.
func (db *Client) SelectPgStatUserTables(ctx context.Context) ([]*PgStatUserTable, error) {
pgStatUserTables := []*PgStatUserTable{}
func (db *Client) SelectPgStatUserTables(ctx context.Context) ([]*model.PgStatUserTable, error) {
pgStatUserTables := []*model.PgStatUserTable{}
if err := db.Select(ctx, &pgStatUserTables, sqlSelectPgStatUserTables); err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions scripts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


gofmt -w ~/postgres-exporter

0 comments on commit 76b3704

Please sign in to comment.