Skip to content

Commit

Permalink
Added support new metrics from pg_stat_database_conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
CHERTS committed Nov 26, 2024
1 parent 87cacb2 commit a001d61
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions internal/collector/postgres_conflicts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
)

const (
postgresDatabaseConflictsQuery = "SELECT datname AS database, confl_tablespace, confl_lock, confl_snapshot, confl_bufferpin, confl_deadlock FROM pg_stat_database_conflicts where pg_is_in_recovery() = 't'"
postgresDatabaseConflictsQuery15 = "SELECT datname AS database, confl_tablespace, confl_lock, confl_snapshot, confl_bufferpin, confl_deadlock " +
"FROM pg_stat_database_conflicts WHERE pg_is_in_recovery() = 't'"

postgresDatabaseConflictsQueryLatest = "SELECT datname AS database, confl_tablespace, confl_lock, confl_snapshot, confl_bufferpin, confl_deadlock, confl_active_logicalslot " +
"FROM pg_stat_database_conflicts WHERE pg_is_in_recovery() = 't'"
)

type postgresConflictsCollector struct {
Expand Down Expand Up @@ -39,7 +43,7 @@ func (c *postgresConflictsCollector) Update(config Config, ch chan<- prometheus.
}
defer conn.Close()

res, err := conn.Query(postgresDatabaseConflictsQuery)
res, err := conn.Query(selectDatabaseConflictsQuery(config.serverVersionNum))
if err != nil {
return err
}
Expand All @@ -52,19 +56,21 @@ func (c *postgresConflictsCollector) Update(config Config, ch chan<- prometheus.
ch <- c.conflicts.newConstMetric(stat.snapshot, stat.database, "snapshot")
ch <- c.conflicts.newConstMetric(stat.bufferpin, stat.database, "bufferpin")
ch <- c.conflicts.newConstMetric(stat.deadlock, stat.database, "deadlock")
ch <- c.conflicts.newConstMetric(stat.active_logicalslot, stat.database, "active_logicalslot")
}

return nil
}

// postgresConflictStat represents per-database recovery conflicts stats based on pg_stat_database_conflicts.
type postgresConflictStat struct {
database string
tablespace float64
lock float64
snapshot float64
bufferpin float64
deadlock float64
database string
tablespace float64
lock float64
snapshot float64
bufferpin float64
deadlock float64
active_logicalslot float64
}

// parsePostgresDatabasesStats parses PGResult, extract data and return struct with stats values.
Expand Down Expand Up @@ -124,6 +130,8 @@ func parsePostgresConflictStats(r *model.PGResult, labelNames []string) map[stri
s.bufferpin = v
case "confl_deadlock":
s.deadlock = v
case "confl_active_logicalslot":
s.active_logicalslot = v
default:
continue
}
Expand All @@ -134,3 +142,13 @@ func parsePostgresConflictStats(r *model.PGResult, labelNames []string) map[stri

return stats
}

// selectDatabaseConflictsQuery returns suitable pg_stat_database_conflicts query depending on passed version.
func selectDatabaseConflictsQuery(version int) string {
switch {
case version < PostgresV16:
return postgresDatabaseConflictsQuery15
default:
return postgresDatabaseConflictsQueryLatest
}
}

0 comments on commit a001d61

Please sign in to comment.