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

Commit 7094286

Browse files
bern4rdellidiogonicoleti
authored andcommitted
Sum index reads by schema
1 parent 10d56c6 commit 7094286

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

gauges/indexes.go

+68-15
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,89 @@ func (g *Gauges) UnusedIndexes() prometheus.Gauge {
6161
)
6262
}
6363

64+
type schemaIndexBlocksRead struct {
65+
Name string `db:"schemaname"`
66+
IndexBlocksRead float64 `db:"idx_blks_read"`
67+
}
68+
6469
// IndexBlocksRead returns the sum of the number of disk blocks read from all public indexes
6570
func (g *Gauges) IndexBlocksRead() prometheus.Gauge {
66-
return g.new(
71+
var gauge = prometheus.NewGaugeVec(
6772
prometheus.GaugeOpts{
68-
Name: "postgresql_index_blks_read_sum",
73+
Name: "postgresql_index_blocks_read_sum",
6974
Help: "Sum of the number of disk blocks read from all user indexes",
7075
ConstLabels: g.labels,
7176
},
72-
`
73-
SELECT coalesce(sum(idx_blks_read), 0)
74-
FROM pg_statio_user_indexes
75-
WHERE schemaname NOT IN ('pg_catalog','information_schema','monitoring')
76-
`,
77+
[]string{"schema"},
7778
)
79+
80+
const schemaIndexBlocksReadQuery = `
81+
SELECT
82+
schemaname,
83+
coalesce(sum(idx_blks_read), 0) AS idx_blks_read
84+
FROM pg_statio_user_indexes
85+
WHERE schemaname NOT IN ('pg_catalog','information_schema','monitoring')
86+
GROUP BY schemaname;
87+
`
88+
89+
go func() {
90+
for {
91+
var schemas []schemaIndexBlocksRead
92+
if err := g.query(schemaIndexBlocksReadQuery, &schemas, emptyParams); err == nil {
93+
for _, schema := range schemas {
94+
gauge.With(prometheus.Labels{
95+
"table": schema.Name,
96+
}).Set(table.IndexBlocksRead)
97+
}
98+
}
99+
time.Sleep(g.interval)
100+
}
101+
}()
102+
103+
return gauge
104+
}
105+
106+
107+
type schemaIndexBlocksHit struct {
108+
Name string `db:"schemaname"`
109+
IndexBlocksRead float64 `db:"idx_blks_hit"`
78110
}
79111

80112
// IndexBlocksHit returns the sum of the number of buffer hits on all user indexes
81-
func (g *Gauges) IndexBlocksHit() prometheus.Gauge {
82-
return g.new(
113+
func (g *Gauges) IndexBlocksRead() prometheus.Gauge {
114+
var gauge = prometheus.NewGaugeVec(
83115
prometheus.GaugeOpts{
84-
Name: "postgresql_index_blks_hit_sum",
116+
Name: "postgresql_index_blocks_hit_sum",
85117
Help: "Sum of the number of buffer hits on all user indexes",
86118
ConstLabels: g.labels,
87119
},
88-
`
89-
SELECT coalesce(sum(idx_blks_hit), 0)
90-
FROM pg_statio_user_indexes
91-
WHERE schemaname NOT IN ('pg_catalog','information_schema','monitoring')
92-
`,
120+
[]string{"schema"},
93121
)
122+
123+
const schemaIndexBlocksHitQuery = `
124+
SELECT
125+
schemaname,
126+
coalesce(sum(idx_blks_hit), 0) AS idx_blks_hit
127+
FROM pg_statio_user_indexes
128+
WHERE schemaname NOT IN ('pg_catalog','information_schema','monitoring')
129+
GROUP BY schemaname;
130+
`
131+
132+
go func() {
133+
for {
134+
var schemas []schemaIndexBlocksHit
135+
if err := g.query(schemaIndexBlocksHitQuery, &schemas, emptyParams); err == nil {
136+
for _, schema := range schemas {
137+
gauge.With(prometheus.Labels{
138+
"table": schema.Name,
139+
}).Set(table.IndexBlocksRead)
140+
}
141+
}
142+
time.Sleep(g.interval)
143+
}
144+
}()
145+
146+
return gauge
94147
}
95148

96149
const indexBloatQuery = `

0 commit comments

Comments
 (0)