From a001d616869c508944a7a1a100424cb2c59094a0 Mon Sep 17 00:00:00 2001 From: Mikhail Grigorev Date: Tue, 26 Nov 2024 12:56:22 +0500 Subject: [PATCH] Added support new metrics from pg_stat_database_conflicts --- internal/collector/postgres_conflicts.go | 34 ++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/internal/collector/postgres_conflicts.go b/internal/collector/postgres_conflicts.go index d74d785..a0c70d6 100644 --- a/internal/collector/postgres_conflicts.go +++ b/internal/collector/postgres_conflicts.go @@ -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 { @@ -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 } @@ -52,6 +56,7 @@ 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 @@ -59,12 +64,13 @@ func (c *postgresConflictsCollector) Update(config Config, ch chan<- prometheus. // 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. @@ -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 } @@ -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 + } +}