@@ -17,6 +17,7 @@ import (
1717 "context"
1818 "database/sql"
1919
20+ "github.com/blang/semver/v4"
2021 "github.com/prometheus/client_golang/prometheus"
2122)
2223
@@ -101,7 +102,7 @@ var (
101102 prometheus.Labels {},
102103 )
103104
104- statBGWriterQuery = `SELECT
105+ statBGWriterQueryBefore17 = `SELECT
105106 checkpoints_timed
106107 ,checkpoints_req
107108 ,checkpoint_write_time
@@ -114,121 +115,177 @@ var (
114115 ,buffers_alloc
115116 ,stats_reset
116117 FROM pg_stat_bgwriter;`
118+
119+ statBGWriterQueryAfter17 = `SELECT
120+ buffers_clean
121+ ,maxwritten_clean
122+ ,buffers_alloc
123+ ,stats_reset
124+ FROM pg_stat_bgwriter;`
117125)
118126
119127func (PGStatBGWriterCollector ) Update (ctx context.Context , instance * instance , ch chan <- prometheus.Metric ) error {
120- db := instance .getDB ()
121- row := db . QueryRowContext ( ctx ,
122- statBGWriterQuery )
128+ if instance .version . GE ( semver . MustParse ( "17.0.0" )) {
129+ db := instance . getDB ()
130+ row := db . QueryRowContext ( ctx , statBGWriterQueryAfter17 )
123131
124- var cpt , cpr , bcp , bc , mwc , bb , bbf , ba sql.NullInt64
125- var cpwt , cpst sql.NullFloat64
126- var sr sql.NullTime
132+ var bc , mwc , ba sql.NullInt64
133+ var sr sql.NullTime
127134
128- err := row .Scan (& cpt , & cpr , & cpwt , & cpst , & bcp , & bc , & mwc , & bb , & bbf , & ba , & sr )
129- if err != nil {
130- return err
131- }
135+ err := row .Scan (& bc , & mwc , & ba , & sr )
136+ if err != nil {
137+ return err
138+ }
132139
133- cptMetric := 0.0
134- if cpt .Valid {
135- cptMetric = float64 (cpt .Int64 )
136- }
137- ch <- prometheus .MustNewConstMetric (
138- statBGWriterCheckpointsTimedDesc ,
139- prometheus .CounterValue ,
140- cptMetric ,
141- )
142- cprMetric := 0.0
143- if cpr .Valid {
144- cprMetric = float64 (cpr .Int64 )
145- }
146- ch <- prometheus .MustNewConstMetric (
147- statBGWriterCheckpointsReqDesc ,
148- prometheus .CounterValue ,
149- cprMetric ,
150- )
151- cpwtMetric := 0.0
152- if cpwt .Valid {
153- cpwtMetric = float64 (cpwt .Float64 )
154- }
155- ch <- prometheus .MustNewConstMetric (
156- statBGWriterCheckpointsReqTimeDesc ,
157- prometheus .CounterValue ,
158- cpwtMetric ,
159- )
160- cpstMetric := 0.0
161- if cpst .Valid {
162- cpstMetric = float64 (cpst .Float64 )
163- }
164- ch <- prometheus .MustNewConstMetric (
165- statBGWriterCheckpointsSyncTimeDesc ,
166- prometheus .CounterValue ,
167- cpstMetric ,
168- )
169- bcpMetric := 0.0
170- if bcp .Valid {
171- bcpMetric = float64 (bcp .Int64 )
172- }
173- ch <- prometheus .MustNewConstMetric (
174- statBGWriterBuffersCheckpointDesc ,
175- prometheus .CounterValue ,
176- bcpMetric ,
177- )
178- bcMetric := 0.0
179- if bc .Valid {
180- bcMetric = float64 (bc .Int64 )
181- }
182- ch <- prometheus .MustNewConstMetric (
183- statBGWriterBuffersCleanDesc ,
184- prometheus .CounterValue ,
185- bcMetric ,
186- )
187- mwcMetric := 0.0
188- if mwc .Valid {
189- mwcMetric = float64 (mwc .Int64 )
190- }
191- ch <- prometheus .MustNewConstMetric (
192- statBGWriterMaxwrittenCleanDesc ,
193- prometheus .CounterValue ,
194- mwcMetric ,
195- )
196- bbMetric := 0.0
197- if bb .Valid {
198- bbMetric = float64 (bb .Int64 )
199- }
200- ch <- prometheus .MustNewConstMetric (
201- statBGWriterBuffersBackendDesc ,
202- prometheus .CounterValue ,
203- bbMetric ,
204- )
205- bbfMetric := 0.0
206- if bbf .Valid {
207- bbfMetric = float64 (bbf .Int64 )
208- }
209- ch <- prometheus .MustNewConstMetric (
210- statBGWriterBuffersBackendFsyncDesc ,
211- prometheus .CounterValue ,
212- bbfMetric ,
213- )
214- baMetric := 0.0
215- if ba .Valid {
216- baMetric = float64 (ba .Int64 )
217- }
218- ch <- prometheus .MustNewConstMetric (
219- statBGWriterBuffersAllocDesc ,
220- prometheus .CounterValue ,
221- baMetric ,
222- )
223- srMetric := 0.0
224- if sr .Valid {
225- srMetric = float64 (sr .Time .Unix ())
140+ bcMetric := 0.0
141+ if bc .Valid {
142+ bcMetric = float64 (bc .Int64 )
143+ }
144+ ch <- prometheus .MustNewConstMetric (
145+ statBGWriterBuffersCleanDesc ,
146+ prometheus .CounterValue ,
147+ bcMetric ,
148+ )
149+ mwcMetric := 0.0
150+ if mwc .Valid {
151+ mwcMetric = float64 (mwc .Int64 )
152+ }
153+ ch <- prometheus .MustNewConstMetric (
154+ statBGWriterMaxwrittenCleanDesc ,
155+ prometheus .CounterValue ,
156+ mwcMetric ,
157+ )
158+ baMetric := 0.0
159+ if ba .Valid {
160+ baMetric = float64 (ba .Int64 )
161+ }
162+ ch <- prometheus .MustNewConstMetric (
163+ statBGWriterBuffersAllocDesc ,
164+ prometheus .CounterValue ,
165+ baMetric ,
166+ )
167+ srMetric := 0.0
168+ if sr .Valid {
169+ srMetric = float64 (sr .Time .Unix ())
170+ }
171+ ch <- prometheus .MustNewConstMetric (
172+ statBGWriterStatsResetDesc ,
173+ prometheus .CounterValue ,
174+ srMetric ,
175+ )
176+ } else {
177+ db := instance .getDB ()
178+ row := db .QueryRowContext (ctx , statBGWriterQueryBefore17 )
179+
180+ var cpt , cpr , bcp , bc , mwc , bb , bbf , ba sql.NullInt64
181+ var cpwt , cpst sql.NullFloat64
182+ var sr sql.NullTime
183+
184+ err := row .Scan (& cpt , & cpr , & cpwt , & cpst , & bcp , & bc , & mwc , & bb , & bbf , & ba , & sr )
185+ if err != nil {
186+ return err
187+ }
188+
189+ cptMetric := 0.0
190+ if cpt .Valid {
191+ cptMetric = float64 (cpt .Int64 )
192+ }
193+ ch <- prometheus .MustNewConstMetric (
194+ statBGWriterCheckpointsTimedDesc ,
195+ prometheus .CounterValue ,
196+ cptMetric ,
197+ )
198+ cprMetric := 0.0
199+ if cpr .Valid {
200+ cprMetric = float64 (cpr .Int64 )
201+ }
202+ ch <- prometheus .MustNewConstMetric (
203+ statBGWriterCheckpointsReqDesc ,
204+ prometheus .CounterValue ,
205+ cprMetric ,
206+ )
207+ cpwtMetric := 0.0
208+ if cpwt .Valid {
209+ cpwtMetric = float64 (cpwt .Float64 )
210+ }
211+ ch <- prometheus .MustNewConstMetric (
212+ statBGWriterCheckpointsReqTimeDesc ,
213+ prometheus .CounterValue ,
214+ cpwtMetric ,
215+ )
216+ cpstMetric := 0.0
217+ if cpst .Valid {
218+ cpstMetric = float64 (cpst .Float64 )
219+ }
220+ ch <- prometheus .MustNewConstMetric (
221+ statBGWriterCheckpointsSyncTimeDesc ,
222+ prometheus .CounterValue ,
223+ cpstMetric ,
224+ )
225+ bcpMetric := 0.0
226+ if bcp .Valid {
227+ bcpMetric = float64 (bcp .Int64 )
228+ }
229+ ch <- prometheus .MustNewConstMetric (
230+ statBGWriterBuffersCheckpointDesc ,
231+ prometheus .CounterValue ,
232+ bcpMetric ,
233+ )
234+ bcMetric := 0.0
235+ if bc .Valid {
236+ bcMetric = float64 (bc .Int64 )
237+ }
238+ ch <- prometheus .MustNewConstMetric (
239+ statBGWriterBuffersCleanDesc ,
240+ prometheus .CounterValue ,
241+ bcMetric ,
242+ )
243+ mwcMetric := 0.0
244+ if mwc .Valid {
245+ mwcMetric = float64 (mwc .Int64 )
246+ }
247+ ch <- prometheus .MustNewConstMetric (
248+ statBGWriterMaxwrittenCleanDesc ,
249+ prometheus .CounterValue ,
250+ mwcMetric ,
251+ )
252+ bbMetric := 0.0
253+ if bb .Valid {
254+ bbMetric = float64 (bb .Int64 )
255+ }
256+ ch <- prometheus .MustNewConstMetric (
257+ statBGWriterBuffersBackendDesc ,
258+ prometheus .CounterValue ,
259+ bbMetric ,
260+ )
261+ bbfMetric := 0.0
262+ if bbf .Valid {
263+ bbfMetric = float64 (bbf .Int64 )
264+ }
265+ ch <- prometheus .MustNewConstMetric (
266+ statBGWriterBuffersBackendFsyncDesc ,
267+ prometheus .CounterValue ,
268+ bbfMetric ,
269+ )
270+ baMetric := 0.0
271+ if ba .Valid {
272+ baMetric = float64 (ba .Int64 )
273+ }
274+ ch <- prometheus .MustNewConstMetric (
275+ statBGWriterBuffersAllocDesc ,
276+ prometheus .CounterValue ,
277+ baMetric ,
278+ )
279+ srMetric := 0.0
280+ if sr .Valid {
281+ srMetric = float64 (sr .Time .Unix ())
282+ }
283+ ch <- prometheus .MustNewConstMetric (
284+ statBGWriterStatsResetDesc ,
285+ prometheus .CounterValue ,
286+ srMetric ,
287+ )
226288 }
227- ch <- prometheus .MustNewConstMetric (
228- statBGWriterStatsResetDesc ,
229- prometheus .CounterValue ,
230- srMetric ,
231- )
232289
233290 return nil
234291}
0 commit comments