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

Commit dd52fcd

Browse files
Diogo Nicoletidiogonicoleti
Diogo Nicoleti
authored andcommitted
Improve PostgreSQL version checker functions
1 parent de193cd commit dd52fcd

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

Diff for: gauges/backends.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type backendsByWaitEventType struct {
123123
}
124124

125125
func (g *Gauges) backendsByWaitEventTypeQuery() string {
126-
if postgres.Version(g.version()).Is96Or10() {
126+
if postgres.Version(g.version()).IsEqualOrGreaterThan96() {
127127
return `
128128
SELECT
129129
COUNT(*) AS total,

Diff for: gauges/gauge.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ func (g *Gauges) queryWithTimeout(
149149
return err
150150
}
151151

152-
func (g *Gauges) version() string {
153-
var version string
154-
if err := g.db.QueryRow("show server_version").Scan(&version); err != nil {
152+
func (g *Gauges) version() int {
153+
var version int
154+
if err := g.db.QueryRow("show server_version_num").Scan(&version); err != nil {
155155
log.WithField("db", g.name).WithError(err).Error("failed to get postgresql version, assuming 9.6.0")
156-
return "9.6.0"
156+
return 90600
157157
}
158158
return version
159159
}

Diff for: gauges/vacuum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (g *Gauges) VacuumRunningTotal() prometheus.Gauge {
112112
SELECT COUNT(*) FROM pg_stat_progress_vacuum WHERE datname = current_database()
113113
`
114114

115-
if !postgres.Version(g.version()).Is96Or10() {
115+
if !postgres.Version(g.version()).IsEqualOrGreaterThan96() {
116116
log.WithField("db", g.name).
117117
Warn("postgresql_vacuum_running_total disabled because it's only supported for PostgreSQL 9.6 or newer versions")
118118
return prometheus.NewGauge(gaugeOpts)

Diff for: postgres/version.go

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package postgres
22

3-
import "strings"
4-
53
// Version for a postgres server
6-
type Version string
4+
type Version int
5+
6+
// IsEqualOrGreaterThan96 returns whether this is version is greater than 9.6.x
7+
func (v Version) IsEqualOrGreaterThan96() bool {
8+
return v >= 90600
9+
}
10+
11+
// IsEqualOrGreaterThan10 returns whether this is version is greater than 10.x
12+
func (v Version) IsEqualOrGreaterThan10() bool {
13+
return v >= 100000
14+
}
715

816
// IsWalReplayPausedFunctionName returns the name of the function to verify whether the replication
917
// log is paused according to the postgres version
1018
func (v Version) IsWalReplayPausedFunctionName() string {
11-
if v.is10() {
19+
if v.IsEqualOrGreaterThan10() {
1220
return "pg_is_wal_replay_paused"
1321
}
1422
return "pg_is_xlog_replay_paused"
@@ -17,7 +25,7 @@ func (v Version) IsWalReplayPausedFunctionName() string {
1725
// LastWalReceivedLsnFunctionName returns the name of the function that returns the last write-ahead
1826
// log location received and synced to disk by replication according to the postgres version
1927
func (v Version) LastWalReceivedLsnFunctionName() string {
20-
if v.is10() {
28+
if v.IsEqualOrGreaterThan10() {
2129
return "pg_last_wal_receive_lsn"
2230
}
2331
return "pg_last_xlog_receive_location"
@@ -26,7 +34,7 @@ func (v Version) LastWalReceivedLsnFunctionName() string {
2634
// WalLsnDiffFunctionName returns the name of the function that returns the difference between two write-ahead
2735
// log locations
2836
func (v Version) WalLsnDiffFunctionName() string {
29-
if v.is10() {
37+
if v.IsEqualOrGreaterThan10() {
3038
return "pg_wal_lsn_diff"
3139
}
3240
return "pg_xlog_location_diff"
@@ -35,22 +43,9 @@ func (v Version) WalLsnDiffFunctionName() string {
3543
// LastWalReplayedLsnFunctionName returns the name of the function that returns the last write-ahead
3644
// log location replayed during recovery according to the postgres version
3745
func (v Version) LastWalReplayedLsnFunctionName() string {
38-
if v.is10() {
46+
if v.IsEqualOrGreaterThan10() {
3947
return "pg_last_wal_replay_lsn"
4048
}
4149
return "pg_last_xlog_replay_location"
4250

4351
}
44-
45-
func (v Version) is96() bool {
46-
return strings.HasPrefix(string(v), "9.6.")
47-
}
48-
49-
func (v Version) is10() bool {
50-
return strings.HasPrefix(string(v), "10.")
51-
}
52-
53-
// Is96Or10 returns whether this is version 9.6.x, 10.x or not
54-
func (v Version) Is96Or10() bool {
55-
return v.is96() || v.is10()
56-
}

Diff for: postgres/version_test.go

+32-16
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,46 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
func TestFunctionVersions(t *testing.T) {
11-
is96 := Version.is96
12-
is10 := Version.is10
13-
is96Or10 := Version.Is96Or10
10+
func TestIsEqualOrGreaterThan96(t *testing.T) {
11+
tt := []struct {
12+
version int
13+
expected bool
14+
}{
15+
{90407, false},
16+
{90600, true},
17+
{90606, true},
18+
{100000, true},
19+
{100004, true},
20+
{110000, true},
21+
{110004, true},
22+
}
23+
24+
for _, tc := range tt {
25+
testName := fmt.Sprintf("expecting IsEqualOrGreaterThan96(\"%v\") to be %v", tc.version, tc.expected)
26+
t.Run(testName, func(t *testing.T) {
27+
assert.Equal(t, Version(tc.version).IsEqualOrGreaterThan96(), tc.expected)
28+
})
29+
}
30+
}
1431

32+
func TestIsEqualOrGreaterThan10(t *testing.T) {
1533
tt := []struct {
16-
str string
17-
fn func(Version) bool
34+
version int
1835
expected bool
1936
}{
20-
{"9.6.6", is96, true},
21-
{"9.5.4", is96, false},
22-
{"10.3", is10, true},
23-
{"9.6.6", is10, false},
24-
{"9.6.6", is96Or10, true},
25-
{"9.5.4", is96Or10, false},
26-
{"10.4", is96Or10, true},
27-
{"11.0", is96Or10, false},
37+
{90407, false},
38+
{90600, false},
39+
{90606, false},
40+
{100000, true},
41+
{100004, true},
42+
{110000, true},
43+
{110004, true},
2844
}
2945

3046
for _, tc := range tt {
31-
testName := fmt.Sprintf("expecting %p(\"%v\") to be %v", tc.fn, tc.str, tc.expected)
47+
testName := fmt.Sprintf("expecting IsEqualOrGreaterThan10(\"%v\") to be %v", tc.version, tc.expected)
3248
t.Run(testName, func(t *testing.T) {
33-
assert.Equal(t, tc.fn(Version(tc.str)), tc.expected)
49+
assert.Equal(t, Version(tc.version).IsEqualOrGreaterThan10(), tc.expected)
3450
})
3551
}
3652
}

0 commit comments

Comments
 (0)