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

Commit 93bf114

Browse files
authored
Merge pull request #71 from luizm/feat/add-slow-queries-suport-pg-13
feat: add support pg13
2 parents 763a3a9 + a86a353 commit 93bf114

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

gauges/slow_queries.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package gauges
22

33
import (
4+
"fmt"
45
"strings"
56
"time"
67

8+
"github.com/ContaAzul/postgresql_exporter/postgres"
79
"github.com/apex/log"
810
"github.com/prometheus/client_golang/prometheus"
911
)
1012

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-
1813
type slowQuery struct {
1914
Query string `db:"query"`
2015
Time float64 `db:"total_time"`
@@ -42,7 +37,17 @@ func (g *Gauges) SlowestQueries() *prometheus.GaugeVec {
4237
go func() {
4338
for {
4439
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 {
4651
for _, query := range queries {
4752
gauge.With(prometheus.Labels{
4853
"query": strings.Join(strings.Fields(query.Query), " "),

postgres/version.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func (v Version) IsEqualOrGreaterThan10() bool {
1313
return v >= 100000
1414
}
1515

16+
// IsEqualOrGreaterThan13 returns whether this version is greater than 13.0
17+
func (v Version) IsEqualOrGreaterThan13() bool {
18+
return v >= 130000
19+
}
20+
1621
// IsWalReplayPausedFunctionName returns the name of the function to verify whether the replication
1722
// log is paused according to the postgres version
1823
func (v Version) IsWalReplayPausedFunctionName() string {
@@ -49,11 +54,19 @@ func (v Version) LastWalReplayedLsnFunctionName() string {
4954
return "pg_last_xlog_replay_location"
5055
}
5156

52-
// CurrentWalLsnFunctionName returns the name of the function that gets current
57+
// CurrentWalLsnFunctionName returns the name of the function that gets current
5358
// write-ahead log write location according to the postgres version
5459
func (v Version) CurrentWalLsnFunctionName() string {
5560
if v.IsEqualOrGreaterThan10() {
5661
return "pg_current_wal_lsn"
5762
}
5863
return "pg_current_xlog_location"
5964
}
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+
}

postgres/version_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,26 @@ func TestIsEqualOrGreaterThan10(t *testing.T) {
5050
})
5151
}
5252
}
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+
}

0 commit comments

Comments
 (0)