@@ -17,6 +17,7 @@ import (
17
17
"context"
18
18
"database/sql"
19
19
20
+ "github.com/blang/semver/v4"
20
21
"github.com/prometheus/client_golang/prometheus"
21
22
)
22
23
@@ -101,7 +102,7 @@ var (
101
102
prometheus.Labels {},
102
103
)
103
104
104
- statBGWriterQuery = `SELECT
105
+ statBGWriterQueryBefore17 = `SELECT
105
106
checkpoints_timed
106
107
,checkpoints_req
107
108
,checkpoint_write_time
@@ -114,121 +115,178 @@ var (
114
115
,buffers_alloc
115
116
,stats_reset
116
117
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;`
117
125
)
118
126
119
127
func (PGStatBGWriterCollector ) Update (ctx context.Context , instance * instance , ch chan <- prometheus.Metric ) error {
120
128
db := instance .getDB ()
121
- row := db .QueryRowContext (ctx ,
122
- statBGWriterQuery )
123
129
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 )
127
132
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
132
135
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
+ )
226
289
}
227
- ch <- prometheus .MustNewConstMetric (
228
- statBGWriterStatsResetDesc ,
229
- prometheus .CounterValue ,
230
- srMetric ,
231
- )
232
290
233
291
return nil
234
292
}
0 commit comments