-
Notifications
You must be signed in to change notification settings - Fork 1
/
statio_tables.go
70 lines (62 loc) · 2.37 KB
/
statio_tables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package pgstats
import (
"github.com/vynaloze/pgstats/nullable"
)
// PgStatIoAllTablesView represents content of pg_statio_all_tables view
type PgStatIoAllTablesView []PgStatIoTablesRow
// PgStatIoSystemTablesView represents content of pg_statio_system_tables view
type PgStatIoSystemTablesView []PgStatIoTablesRow
// PgStatIoUserTablesView represents content of pg_statio_user_tables view
type PgStatIoUserTablesView []PgStatIoTablesRow
// PgStatIoTablesRow represents schema of pg_statio_*_tables views
type PgStatIoTablesRow struct {
// OID of a table
Relid int64 `json:"relid"`
// Name of the schema that this table is in
Schemaname string `json:"schemaname"`
// Name of this table
Relname string `json:"relname"`
// Number of disk blocks read from this table
HeapBlksRead nullable.Int64 `json:"heap_blks_read"`
// Number of buffer hits in this table
HeapBlksHit nullable.Int64 `json:"heap_blks_hit"`
// Number of disk blocks read from all indexes on this table
IdxBlksRead nullable.Int64 `json:"idx_blks_read"`
// Number of buffer hits in all indexes on this table
IdxBlksHit nullable.Int64 `json:"idx_blks_hit"`
// Number of disk blocks read from this table's TOAST table (if any)
ToastBlksRead nullable.Int64 `json:"toast_blks_read"`
// Number of buffer hits in this table's TOAST table (if any)
ToastBlksHit nullable.Int64 `json:"toast_blks_hit"`
// Number of disk blocks read from this table's TOAST table indexes (if any)
TidxBlksRead nullable.Int64 `json:"tidx_blks_read"`
// Number of buffer hits in this table's TOAST table indexes (if any)
TidxBlksHit nullable.Int64 `json:"tidx_blks_hit"`
}
func (s *PgStats) fetchIoTables(view string) ([]PgStatIoTablesRow, error) {
db := s.conn.db
query := "select relid,schemaname,relname," +
"heap_blks_read,heap_blks_hit," +
"idx_blks_read,idx_blks_hit," +
"toast_blks_read,toast_blks_hit," +
"tidx_blks_read,tidx_blks_hit from " + view
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
data := make([]PgStatIoTablesRow, 0)
for rows.Next() {
row := new(PgStatIoTablesRow)
err := rows.Scan(&row.Relid, &row.Schemaname, &row.Relname,
&row.HeapBlksRead, &row.HeapBlksHit,
&row.IdxBlksRead, &row.IdxBlksHit,
&row.ToastBlksRead, &row.ToastBlksHit,
&row.TidxBlksRead, &row.TidxBlksHit)
if err != nil {
return nil, err
}
data = append(data, *row)
}
return data, rows.Err()
}