@@ -156,20 +156,13 @@ func (g *Gauges) TableBloat() *prometheus.GaugeVec {
156
156
}
157
157
158
158
var tableUsageQuery = `
159
- WITH top_big_tables as (
160
- SELECT relname, pg_total_relation_size(relid)
161
- FROM pg_catalog.pg_statio_user_tables
162
- ORDER BY pg_total_relation_size(relid) desc
163
- LIMIT 20
164
- )
165
159
SELECT s.relname,
166
160
coalesce(s.seq_tup_read, 0) as seq_tup_read,
167
161
coalesce(s.idx_tup_fetch, 0) as idx_tup_fetch,
168
162
coalesce(s.n_tup_ins, 0) as n_tup_ins,
169
163
coalesce(s.n_tup_upd, 0) as n_tup_upd,
170
164
coalesce(s.n_tup_del, 0) as n_tup_del
171
- FROM top_big_tables tbt
172
- JOIN pg_stat_all_tables s on s.relname = tbt.relname
165
+ FROM pg_stat_user_tables s
173
166
ORDER BY 2 desc
174
167
`
175
168
@@ -223,3 +216,47 @@ func (g *Gauges) TableUsage() *prometheus.GaugeVec {
223
216
}()
224
217
return gauge
225
218
}
219
+
220
+ var tableSecScansQuery = `
221
+ select relname,
222
+ coalesce(seq_scan, 0) as seq_scan,
223
+ coalesce(idx_scan, 0) as idx_scan
224
+ from pg_stat_user_tables
225
+ `
226
+
227
+ type tableScans struct {
228
+ Name string `db:"relname"`
229
+ SecScan float64 `db:"seq_scan"`
230
+ IdxScan float64 `db:"idx_scan"`
231
+ }
232
+
233
+ func (g * Gauges ) TableScans () * prometheus.GaugeVec {
234
+ var gauge = prometheus .NewGaugeVec (
235
+ prometheus.GaugeOpts {
236
+ Name : "postgresql_table_scans" ,
237
+ Help : "table scans statistics" ,
238
+ ConstLabels : g .labels ,
239
+ },
240
+ []string {"table" , "scan" },
241
+ )
242
+ go func () {
243
+ for {
244
+ var tables []tableScans
245
+ if err := g .query (tableSecScansQuery , & tables , emptyParams ); err == nil {
246
+ for _ , table := range tables {
247
+ gauge .With (prometheus.Labels {
248
+ "table" : table .Name ,
249
+ "scan" : "seq_scan" ,
250
+ }).Set (table .SecScan )
251
+ gauge .With (prometheus.Labels {
252
+ "table" : table .Name ,
253
+ "scan" : "idx_scan" ,
254
+ }).Set (table .IdxScan )
255
+ }
256
+ }
257
+ time .Sleep (g .interval )
258
+ }
259
+ }()
260
+
261
+ return gauge
262
+ }
0 commit comments