@@ -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,178 @@ 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 {
120128 db := instance .getDB ()
121- row := db .QueryRowContext (ctx ,
122- statBGWriterQuery )
123129
124- var cpt , cpr , bcp , bc , mwc , bb , bbf , ba sql.NullInt64
125- var cpwt , cpst sql.NullFloat64
126- var sr sql.NullTime
130+ if instance .version .GE (semver .MustParse ("17.0.0" )) {
131+ row := db .QueryRowContext (ctx , statBGWriterQueryAfter17 )
127132
128- err := row .Scan (& cpt , & cpr , & cpwt , & cpst , & bcp , & bc , & mwc , & bb , & bbf , & ba , & sr )
129- if err != nil {
130- return err
131- }
133+ var bc , mwc , ba sql.NullInt64
134+ var sr sql.NullTime
132135
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 ())
136+ err := row .Scan (& bc , & mwc , & ba , & sr )
137+ if err != nil {
138+ return err
139+ }
140+
141+ bcMetric := 0.0
142+ if bc .Valid {
143+ bcMetric = float64 (bc .Int64 )
144+ }
145+ ch <- prometheus .MustNewConstMetric (
146+ statBGWriterBuffersCleanDesc ,
147+ prometheus .CounterValue ,
148+ bcMetric ,
149+ )
150+ mwcMetric := 0.0
151+ if mwc .Valid {
152+ mwcMetric = float64 (mwc .Int64 )
153+ }
154+ ch <- prometheus .MustNewConstMetric (
155+ statBGWriterMaxwrittenCleanDesc ,
156+ prometheus .CounterValue ,
157+ mwcMetric ,
158+ )
159+ baMetric := 0.0
160+ if ba .Valid {
161+ baMetric = float64 (ba .Int64 )
162+ }
163+ ch <- prometheus .MustNewConstMetric (
164+ statBGWriterBuffersAllocDesc ,
165+ prometheus .CounterValue ,
166+ baMetric ,
167+ )
168+ srMetric := 0.0
169+ if sr .Valid {
170+ srMetric = float64 (sr .Time .Unix ())
171+ }
172+ ch <- prometheus .MustNewConstMetric (
173+ statBGWriterStatsResetDesc ,
174+ prometheus .CounterValue ,
175+ srMetric ,
176+ )
177+ } else {
178+ row := db .QueryRowContext (ctx ,
179+ statBGWriterQueryBefore17 )
180+
181+ var cpt , cpr , bcp , bc , mwc , bb , bbf , ba sql.NullInt64
182+ var cpwt , cpst sql.NullFloat64
183+ var sr sql.NullTime
184+
185+ err := row .Scan (& cpt , & cpr , & cpwt , & cpst , & bcp , & bc , & mwc , & bb , & bbf , & ba , & sr )
186+ if err != nil {
187+ return err
188+ }
189+
190+ cptMetric := 0.0
191+ if cpt .Valid {
192+ cptMetric = float64 (cpt .Int64 )
193+ }
194+ ch <- prometheus .MustNewConstMetric (
195+ statBGWriterCheckpointsTimedDesc ,
196+ prometheus .CounterValue ,
197+ cptMetric ,
198+ )
199+ cprMetric := 0.0
200+ if cpr .Valid {
201+ cprMetric = float64 (cpr .Int64 )
202+ }
203+ ch <- prometheus .MustNewConstMetric (
204+ statBGWriterCheckpointsReqDesc ,
205+ prometheus .CounterValue ,
206+ cprMetric ,
207+ )
208+ cpwtMetric := 0.0
209+ if cpwt .Valid {
210+ cpwtMetric = float64 (cpwt .Float64 )
211+ }
212+ ch <- prometheus .MustNewConstMetric (
213+ statBGWriterCheckpointsReqTimeDesc ,
214+ prometheus .CounterValue ,
215+ cpwtMetric ,
216+ )
217+ cpstMetric := 0.0
218+ if cpst .Valid {
219+ cpstMetric = float64 (cpst .Float64 )
220+ }
221+ ch <- prometheus .MustNewConstMetric (
222+ statBGWriterCheckpointsSyncTimeDesc ,
223+ prometheus .CounterValue ,
224+ cpstMetric ,
225+ )
226+ bcpMetric := 0.0
227+ if bcp .Valid {
228+ bcpMetric = float64 (bcp .Int64 )
229+ }
230+ ch <- prometheus .MustNewConstMetric (
231+ statBGWriterBuffersCheckpointDesc ,
232+ prometheus .CounterValue ,
233+ bcpMetric ,
234+ )
235+ bcMetric := 0.0
236+ if bc .Valid {
237+ bcMetric = float64 (bc .Int64 )
238+ }
239+ ch <- prometheus .MustNewConstMetric (
240+ statBGWriterBuffersCleanDesc ,
241+ prometheus .CounterValue ,
242+ bcMetric ,
243+ )
244+ mwcMetric := 0.0
245+ if mwc .Valid {
246+ mwcMetric = float64 (mwc .Int64 )
247+ }
248+ ch <- prometheus .MustNewConstMetric (
249+ statBGWriterMaxwrittenCleanDesc ,
250+ prometheus .CounterValue ,
251+ mwcMetric ,
252+ )
253+ bbMetric := 0.0
254+ if bb .Valid {
255+ bbMetric = float64 (bb .Int64 )
256+ }
257+ ch <- prometheus .MustNewConstMetric (
258+ statBGWriterBuffersBackendDesc ,
259+ prometheus .CounterValue ,
260+ bbMetric ,
261+ )
262+ bbfMetric := 0.0
263+ if bbf .Valid {
264+ bbfMetric = float64 (bbf .Int64 )
265+ }
266+ ch <- prometheus .MustNewConstMetric (
267+ statBGWriterBuffersBackendFsyncDesc ,
268+ prometheus .CounterValue ,
269+ bbfMetric ,
270+ )
271+ baMetric := 0.0
272+ if ba .Valid {
273+ baMetric = float64 (ba .Int64 )
274+ }
275+ ch <- prometheus .MustNewConstMetric (
276+ statBGWriterBuffersAllocDesc ,
277+ prometheus .CounterValue ,
278+ baMetric ,
279+ )
280+ srMetric := 0.0
281+ if sr .Valid {
282+ srMetric = float64 (sr .Time .Unix ())
283+ }
284+ ch <- prometheus .MustNewConstMetric (
285+ statBGWriterStatsResetDesc ,
286+ prometheus .CounterValue ,
287+ srMetric ,
288+ )
226289 }
227- ch <- prometheus .MustNewConstMetric (
228- statBGWriterStatsResetDesc ,
229- prometheus .CounterValue ,
230- srMetric ,
231- )
232290
233291 return nil
234292}
0 commit comments