This repository was archived by the owner on Nov 28, 2022. It is now read-only.
File tree 3 files changed +50
-9
lines changed
3 files changed +50
-9
lines changed Original file line number Diff line number Diff line change 1
1
package gauges
2
2
3
3
import (
4
+ "fmt"
4
5
"strings"
5
6
"time"
6
7
8
+ "github.com/ContaAzul/postgresql_exporter/postgres"
7
9
"github.com/apex/log"
8
10
"github.com/prometheus/client_golang/prometheus"
9
11
)
10
12
11
- const slowQueriesQuery = `
12
- SELECT total_time, query
13
- FROM pg_stat_statements
14
- WHERE dbid = (SELECT datid FROM pg_stat_database WHERE datname = current_database())
15
- ORDER BY total_time desc limit 10
16
- `
17
-
18
13
type slowQuery struct {
19
14
Query string `db:"query"`
20
15
Time float64 `db:"total_time"`
@@ -42,7 +37,17 @@ func (g *Gauges) SlowestQueries() *prometheus.GaugeVec {
42
37
go func () {
43
38
for {
44
39
var queries []slowQuery
45
- if err := g .query (slowQueriesQuery , & queries , emptyParams ); err == nil {
40
+ if err := g .query (
41
+ fmt .Sprintf (`
42
+ SELECT %[1]s as total_time, query
43
+ FROM pg_stat_statements
44
+ WHERE dbid = (SELECT datid FROM pg_stat_database WHERE datname = current_database())
45
+ ORDER BY %[1]s desc limit 10` ,
46
+ postgres .Version (g .version ()).PgStatStatementsTotalTimeColumn (),
47
+ ),
48
+ & queries ,
49
+ emptyParams ,
50
+ ); err == nil {
46
51
for _ , query := range queries {
47
52
gauge .With (prometheus.Labels {
48
53
"query" : strings .Join (strings .Fields (query .Query ), " " ),
Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ func (v Version) IsEqualOrGreaterThan10() bool {
13
13
return v >= 100000
14
14
}
15
15
16
+ // IsEqualOrGreaterThan13 returns whether this version is greater than 13.0
17
+ func (v Version ) IsEqualOrGreaterThan13 () bool {
18
+ return v >= 130000
19
+ }
20
+
16
21
// IsWalReplayPausedFunctionName returns the name of the function to verify whether the replication
17
22
// log is paused according to the postgres version
18
23
func (v Version ) IsWalReplayPausedFunctionName () string {
@@ -49,11 +54,19 @@ func (v Version) LastWalReplayedLsnFunctionName() string {
49
54
return "pg_last_xlog_replay_location"
50
55
}
51
56
52
- // CurrentWalLsnFunctionName returns the name of the function that gets current
57
+ // CurrentWalLsnFunctionName returns the name of the function that gets current
53
58
// write-ahead log write location according to the postgres version
54
59
func (v Version ) CurrentWalLsnFunctionName () string {
55
60
if v .IsEqualOrGreaterThan10 () {
56
61
return "pg_current_wal_lsn"
57
62
}
58
63
return "pg_current_xlog_location"
59
64
}
65
+
66
+ // PgStatStatementsTotalTimeColumn returns the name of the column that contains the total time spent executing the statement.
67
+ func (v Version ) PgStatStatementsTotalTimeColumn () string {
68
+ if v .IsEqualOrGreaterThan13 () {
69
+ return "total_exec_time"
70
+ }
71
+ return "total_time"
72
+ }
Original file line number Diff line number Diff line change @@ -50,3 +50,26 @@ func TestIsEqualOrGreaterThan10(t *testing.T) {
50
50
})
51
51
}
52
52
}
53
+
54
+ func TestIsEqualOrGreaterThan13 (t * testing.T ) {
55
+ tt := []struct {
56
+ version int
57
+ expected bool
58
+ }{
59
+ {90407 , false },
60
+ {90600 , false },
61
+ {90606 , false },
62
+ {100000 , false },
63
+ {100004 , false },
64
+ {110000 , false },
65
+ {110004 , false },
66
+ {130000 , true },
67
+ }
68
+
69
+ for _ , tc := range tt {
70
+ testName := fmt .Sprintf ("expecting IsEqualOrGreaterThan13(\" %v\" ) to be %v" , tc .version , tc .expected )
71
+ t .Run (testName , func (t * testing.T ) {
72
+ assert .Equal (t , Version (tc .version ).IsEqualOrGreaterThan13 (), tc .expected )
73
+ })
74
+ }
75
+ }
You can’t perform that action at this time.
0 commit comments