-
Notifications
You must be signed in to change notification settings - Fork 1
/
stat_progress_vacuum.go
75 lines (68 loc) · 3.05 KB
/
stat_progress_vacuum.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
71
72
73
74
75
package pgstats
import (
"github.com/pkg/errors"
"github.com/vynaloze/pgstats/nullable"
)
// PgStatProgressVacuumView represents content of pg_stat_progress_vacuum view
type PgStatProgressVacuumView []PgStatProgressVacuumRow
// PgStatProgressVacuumRow represents schema of pg_stat_progress_vacuum view
type PgStatProgressVacuumRow struct {
// Process ID of backend.
Pid int64 `json:"pid"`
// OID of the database to which this backend is connected.
Datid int64 `json:"datid"`
// Name of the database to which this backend is connected.
Datname string `json:"datname"`
// OID of the table being vacuumed.
Relid int64 `json:"relid"`
// Current processing phase of vacuum. See:
// https://www.postgresql.org/docs/current/progress-reporting.html#VACUUM-PHASES
Phase string `json:"phase"`
// Total number of heap blocks in the table. This number is reported as of the beginning of the scan;
// blocks added later will not be (and need not be) visited by this VACUUM.
HeapBlksTotal nullable.Int64 `json:"heap_blks_total"`
// Number of heap blocks scanned.
// Because the visibility map is used to optimize scans, some blocks will be skipped without inspection;
// skipped blocks are included in this total, so that this number will eventually become equal to heap_blks_total
// when the vacuum is complete. This counter only advances when the phase is scanning heap.
HeapBlksScanned nullable.Int64 `json:"heap_blks_scanned"`
// Number of heap blocks vacuumed. Unless the table has no indexes, this counter only advances when the phase is vacuuming heap.
// Blocks that contain no dead tuples are skipped, so the counter may sometimes skip forward in large increments.
HeapBlksVacuumed nullable.Int64 `json:"heap_blks_vacuumed"`
// Number of completed index vacuum cycles.
IndexVacuumCount nullable.Int64 `json:"index_vacuum_count"`
// Number of dead tuples that we can store before needing to perform an index vacuum cycle, based on maintenance_work_mem.
MaxDeadTuples nullable.Int64 `json:"max_dead_tuples"`
// Number of dead tuples collected since the last index vacuum cycle.
NumDeadTuples nullable.Int64 `json:"num_dead_tuples"`
}
func (s *PgStats) fetchProgressVacuum() (PgStatProgressVacuumView, error) {
version, err := s.getPgVersion()
if err != nil {
return nil, err
}
if version < 9.6 {
return nil, errors.Errorf("Unsupported PostgreSQL version: %f", version)
}
db := s.conn.db
query := "select pid,datid,datname,relid,phase," +
"heap_blks_total,heap_blks_scanned,heap_blks_vacuumed,index_vacuum_count,max_dead_tuples," +
"num_dead_tuples from pg_stat_progress_vacuum"
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
data := make(PgStatProgressVacuumView, 0)
for rows.Next() {
row := new(PgStatProgressVacuumRow)
err := rows.Scan(&row.Pid, &row.Datid, &row.Datname, &row.Relid, &row.Phase,
&row.HeapBlksTotal, &row.HeapBlksScanned, &row.HeapBlksVacuumed, &row.IndexVacuumCount, &row.MaxDeadTuples,
&row.NumDeadTuples)
if err != nil {
return nil, err
}
data = append(data, *row)
}
return data, rows.Err()
}