-
Notifications
You must be signed in to change notification settings - Fork 1
/
stat_functions.go
49 lines (42 loc) · 1.55 KB
/
stat_functions.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
package pgstats
import (
"github.com/vynaloze/pgstats/nullable"
)
// PgStatUserFunctionsView represents content of pg_stat_user_functions view
type PgStatUserFunctionsView []PgStatFunctionsRow
// PgStatXactUserFunctionsView represents content of pg_stat_xact_user_functions view
type PgStatXactUserFunctionsView []PgStatFunctionsRow
// PgStatFunctionsRow represents schema of pg_stat*_user_functions views
type PgStatFunctionsRow struct {
// OID of a function
Funcid int64 `json:"funcid"`
// Name of the schema this function is in
Schemaname string `json:"schemaname"`
// Name of this function
Funcname string `json:"funcname"`
// Number of times this function has been called
Calls nullable.Int64 `json:"calls"`
// Total time spent in this function and all other functions called by it, in milliseconds
TotalTime nullable.Float64 `json:"total_time"`
// Total time spent in this function itself, not including other functions called by it, in milliseconds
SelfTime nullable.Float64 `json:"self_time"`
}
func (s *PgStats) fetchFunctions(view string) ([]PgStatFunctionsRow, error) {
db := s.conn.db
query := "select funcid,schemaname,funcname,calls,total_time,self_time from " + view
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
data := make([]PgStatFunctionsRow, 0)
for rows.Next() {
row := new(PgStatFunctionsRow)
err := rows.Scan(&row.Funcid, &row.Schemaname, &row.Funcname, &row.Calls, &row.TotalTime, &row.SelfTime)
if err != nil {
return nil, err
}
data = append(data, *row)
}
return data, rows.Err()
}