@@ -53,12 +53,21 @@ 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; "
66+ pgDatabaseQuery = "select pg_database.datname,pg_database.datconnlimit FROM pg_database"
5867 pgDatabaseSizeQuery = "SELECT pg_database_size($1)"
5968)
6069
61- // Update implements Collector and exposes database size.
70+ // Update implements Collector and exposes database size and connection limits .
6271// It is called by the Prometheus registry when collecting metrics.
6372// The list of databases is retrieved from pg_database and filtered
6473// by the excludeDatabase config parameter. The tradeoff here is that
@@ -81,21 +90,32 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
8190
8291 for rows .Next () {
8392 var datname sql.NullString
84- if err := rows .Scan (& datname ); err != nil {
93+ var connLimit sql.NullInt64
94+ if err := rows .Scan (& datname , & connLimit ); err != nil {
8595 return err
8696 }
8797
8898 if ! datname .Valid {
8999 continue
90100 }
101+ database := datname .String
91102 // Ignore excluded databases
92103 // Filtering is done here instead of in the query to avoid
93104 // a complicated NOT IN query with a variable number of parameters
94- if sliceContains (c .excludedDatabases , datname . String ) {
105+ if sliceContains (c .excludedDatabases , database ) {
95106 continue
96107 }
97108
98- databases = append (databases , datname .String )
109+ databases = append (databases , database )
110+
111+ connLimitMetric := 0.0
112+ if connLimit .Valid {
113+ connLimitMetric = float64 (connLimit .Int64 )
114+ }
115+ ch <- prometheus .MustNewConstMetric (
116+ pgDatabaseConnectionLimitsDesc ,
117+ prometheus .GaugeValue , connLimitMetric , database ,
118+ )
99119 }
100120
101121 // Query the size of the databases
@@ -114,6 +134,7 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
114134 pgDatabaseSizeDesc ,
115135 prometheus .GaugeValue , sizeMetric , datname ,
116136 )
137+
117138 }
118139 return rows .Err ()
119140}
0 commit comments