@@ -53,12 +53,22 @@ var (
5353 "Disk space used by the database" ,
5454 []string {"datname" }, nil ,
5555 )
56+ pgDatabaseConnectionLimitsDesc = prometheus .NewDesc (
57+ prometheus .BuildFQName (
58+ namespace ,
59+ databaseSubsystem ,
60+ "connection_limit" ,
61+ ),
62+ "Connection limit set for the database" ,
63+ []string {"datname" }, nil ,
64+ )
5665
57- pgDatabaseQuery = "SELECT pg_database.datname FROM pg_database;"
58- pgDatabaseSizeQuery = "SELECT pg_database_size($1)"
66+ pgDatabaseQuery = "SELECT pg_database.datname FROM pg_database;"
67+ pgDatabaseSizeQuery = "SELECT pg_database_size($1)"
68+ pgDatabaseConnectionLimitsQuery = "select pg_database.datconnlimit FROM pg_database WHERE pg_database.datname = $1"
5969)
6070
61- // Update implements Collector and exposes database size.
71+ // Update implements Collector and exposes database size and connection limits .
6272// It is called by the Prometheus registry when collecting metrics.
6373// The list of databases is retrieved from pg_database and filtered
6474// by the excludeDatabase config parameter. The tradeoff here is that
@@ -114,6 +124,20 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
114124 pgDatabaseSizeDesc ,
115125 prometheus .GaugeValue , sizeMetric , datname ,
116126 )
127+
128+ var connLimit sql.NullInt64
129+ err = db .QueryRowContext (ctx , pgDatabaseConnectionLimitsQuery , datname ).Scan (& connLimit )
130+ if err != nil {
131+ return err
132+ }
133+ connLimitMetric := 0.0
134+ if connLimit .Valid {
135+ connLimitMetric = float64 (connLimit .Int64 )
136+ }
137+ ch <- prometheus .MustNewConstMetric (
138+ pgDatabaseConnectionLimitsDesc ,
139+ prometheus .GaugeValue , connLimitMetric , datname ,
140+ )
117141 }
118142 return rows .Err ()
119143}
0 commit comments