Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit b81b0dc

Browse files
authored
Merge pull request #30 from ContaAzul/adding_table_scans
Improving table metrics
2 parents c99e529 + cf8a11d commit b81b0dc

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

gauges/tables.go

+45-8
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,13 @@ func (g *Gauges) TableBloat() *prometheus.GaugeVec {
156156
}
157157

158158
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-
)
165159
SELECT s.relname,
166160
coalesce(s.seq_tup_read, 0) as seq_tup_read,
167161
coalesce(s.idx_tup_fetch, 0) as idx_tup_fetch,
168162
coalesce(s.n_tup_ins, 0) as n_tup_ins,
169163
coalesce(s.n_tup_upd, 0) as n_tup_upd,
170164
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
173166
ORDER BY 2 desc
174167
`
175168

@@ -223,3 +216,47 @@ func (g *Gauges) TableUsage() *prometheus.GaugeVec {
223216
}()
224217
return gauge
225218
}
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+
}

gauges/tables_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ func TestTableUsage(t *testing.T) {
2424
assert.True(len(metrics) > 0)
2525
assertNoErrs(t, gauges)
2626
}
27+
28+
func TestTableScans(t *testing.T) {
29+
var assert = assert.New(t)
30+
_, gauges, close := prepare(t)
31+
defer close()
32+
var metrics = evaluate(t, gauges.TableScans())
33+
assert.True(len(metrics) > 0)
34+
assertNoErrs(t, gauges)
35+
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ func watch(db *sql.DB, reg prometheus.Registerer, name string) {
103103
reg.MustRegister(gauges.TransactionsSum())
104104
reg.MustRegister(gauges.UnusedIndexes())
105105
reg.MustRegister(gauges.Up())
106+
reg.MustRegister(gauges.TableScans())
106107
}

0 commit comments

Comments
 (0)