94
94
[]string {"user" , "datname" , "queryid" },
95
95
prometheus.Labels {},
96
96
)
97
+ statStatementsMaxSeconds = prometheus .NewDesc (
98
+ prometheus .BuildFQName (namespace , statStatementsSubsystem , "max_seconds" ),
99
+ "Maximum time spent in a single execution of the statement, in seconds" ,
100
+ []string {"user" , "datname" , "queryid" },
101
+ prometheus.Labels {},
102
+ )
103
+ statStatementsMeanSeconds = prometheus .NewDesc (
104
+ prometheus .BuildFQName (namespace , statStatementsSubsystem , "mean_seconds" ),
105
+ "Mean time spent per execution of the statement, in seconds" ,
106
+ []string {"user" , "datname" , "queryid" },
107
+ prometheus.Labels {},
108
+ )
109
+ statStatementsStddevSeconds = prometheus .NewDesc (
110
+ prometheus .BuildFQName (namespace , statStatementsSubsystem , "stddev_seconds" ),
111
+ "Standard deviation of time spent in the statement, in seconds" ,
112
+ []string {"user" , "datname" , "queryid" },
113
+ prometheus.Labels {},
114
+ )
97
115
98
116
statStatementsQuery = prometheus .NewDesc (
99
117
prometheus .BuildFQName (namespace , statStatementsSubsystem , "query_id" ),
@@ -115,7 +133,10 @@ const (
115
133
pg_stat_statements.total_time / 1000.0 as seconds_total,
116
134
pg_stat_statements.rows as rows_total,
117
135
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
118
- pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
136
+ pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total,
137
+ pg_stat_statements.max_time / 1000.0 as max_seconds,
138
+ pg_stat_statements.mean_time / 1000.0 as mean_seconds,
139
+ pg_stat_statements.stddev_time / 1000.0 as stddev_seconds
119
140
FROM pg_stat_statements
120
141
JOIN pg_database
121
142
ON pg_database.oid = pg_stat_statements.dbid
@@ -137,7 +158,10 @@ const (
137
158
pg_stat_statements.total_exec_time / 1000.0 as seconds_total,
138
159
pg_stat_statements.rows as rows_total,
139
160
pg_stat_statements.blk_read_time / 1000.0 as block_read_seconds_total,
140
- pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total
161
+ pg_stat_statements.blk_write_time / 1000.0 as block_write_seconds_total,
162
+ pg_stat_statements.max_exec_time / 1000.0 as max_seconds,
163
+ pg_stat_statements.mean_exec_time / 1000.0 as mean_seconds,
164
+ pg_stat_statements.stddev_exec_time / 1000.0 as stddev_seconds
141
165
FROM pg_stat_statements
142
166
JOIN pg_database
143
167
ON pg_database.oid = pg_stat_statements.dbid
@@ -159,7 +183,10 @@ const (
159
183
pg_stat_statements.total_exec_time / 1000.0 as seconds_total,
160
184
pg_stat_statements.rows as rows_total,
161
185
pg_stat_statements.shared_blk_read_time / 1000.0 as block_read_seconds_total,
162
- pg_stat_statements.shared_blk_write_time / 1000.0 as block_write_seconds_total
186
+ pg_stat_statements.shared_blk_write_time / 1000.0 as block_write_seconds_total,
187
+ pg_stat_statements.max_exec_time / 1000.0 as max_seconds,
188
+ pg_stat_statements.mean_exec_time / 1000.0 as mean_seconds,
189
+ pg_stat_statements.stddev_exec_time / 1000.0 as stddev_seconds
163
190
FROM pg_stat_statements
164
191
JOIN pg_database
165
192
ON pg_database.oid = pg_stat_statements.dbid
@@ -201,12 +228,12 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc
201
228
for rows .Next () {
202
229
var user , datname , queryid , statement sql.NullString
203
230
var callsTotal , rowsTotal sql.NullInt64
204
- var secondsTotal , blockReadSecondsTotal , blockWriteSecondsTotal sql.NullFloat64
231
+ var secondsTotal , blockReadSecondsTotal , blockWriteSecondsTotal , maxSeconds , meanSeconds , stddevSeconds sql.NullFloat64
205
232
var columns []any
206
233
if c .includeQueryStatement {
207
- columns = []any {& user , & datname , & queryid , & statement , & callsTotal , & secondsTotal , & rowsTotal , & blockReadSecondsTotal , & blockWriteSecondsTotal }
234
+ columns = []any {& user , & datname , & queryid , & statement , & callsTotal , & secondsTotal , & rowsTotal , & blockReadSecondsTotal , & blockWriteSecondsTotal , & maxSeconds , & meanSeconds , & stddevSeconds }
208
235
} else {
209
- columns = []any {& user , & datname , & queryid , & callsTotal , & secondsTotal , & rowsTotal , & blockReadSecondsTotal , & blockWriteSecondsTotal }
236
+ columns = []any {& user , & datname , & queryid , & callsTotal , & secondsTotal , & rowsTotal , & blockReadSecondsTotal , & blockWriteSecondsTotal , & maxSeconds , & meanSeconds , & stddevSeconds }
210
237
}
211
238
if err := rows .Scan (columns ... ); err != nil {
212
239
return err
@@ -280,6 +307,39 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc
280
307
userLabel , datnameLabel , queryidLabel ,
281
308
)
282
309
310
+ maxSecondsMetric := 0.0
311
+ if maxSeconds .Valid {
312
+ maxSecondsMetric = maxSeconds .Float64
313
+ }
314
+ ch <- prometheus .MustNewConstMetric (
315
+ statStatementsMaxSeconds ,
316
+ prometheus .GaugeValue ,
317
+ maxSecondsMetric ,
318
+ userLabel , datnameLabel , queryidLabel ,
319
+ )
320
+
321
+ meanSecondsMetric := 0.0
322
+ if meanSeconds .Valid {
323
+ meanSecondsMetric = meanSeconds .Float64
324
+ }
325
+ ch <- prometheus .MustNewConstMetric (
326
+ statStatementsMeanSeconds ,
327
+ prometheus .GaugeValue ,
328
+ meanSecondsMetric ,
329
+ userLabel , datnameLabel , queryidLabel ,
330
+ )
331
+
332
+ stddevSecondsMetric := 0.0
333
+ if stddevSeconds .Valid {
334
+ stddevSecondsMetric = stddevSeconds .Float64
335
+ }
336
+ ch <- prometheus .MustNewConstMetric (
337
+ statStatementsStddevSeconds ,
338
+ prometheus .GaugeValue ,
339
+ stddevSecondsMetric ,
340
+ userLabel , datnameLabel , queryidLabel ,
341
+ )
342
+
283
343
if c .includeQueryStatement {
284
344
_ , ok := presentQueryIds [queryidLabel ]
285
345
if ! ok {
0 commit comments