Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions agent/agents/postgres/pgstatmonitor/pgstatmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ const (
pgStatMonitorVersion10PG12
pgStatMonitorVersion10PG13
pgStatMonitorVersion10PG14
pgStatMonitorVersion11PG12

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
pgStatMonitorVersion11PG12 is unused (deadcode)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

pgStatMonitorVersion11PG13

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
pgStatMonitorVersion11PG13 is unused (deadcode)

pgStatMonitorVersion11PG14
pgStatMonitorVersion20PG12
pgStatMonitorVersion20PG13
pgStatMonitorVersion20PG14
)

const (
Expand Down Expand Up @@ -165,6 +171,26 @@ func getPGMonitorVersion(q *reform.Querier) (pgStatMonitorVersion, pgStatMonitor

version := pgStatMonitorVersion06
switch {
case pgsmVersion.Core().GreaterThanOrEqual(v20):
if pgVersion >= 14 {
version = pgStatMonitorVersion20PG14
break
}
if pgVersion >= 13 {
version = pgStatMonitorVersion20PG13
break
}
version = pgStatMonitorVersion20PG12
case pgsmVersion.Core().GreaterThanOrEqual(v11):
if pgVersion >= 14 {
version = pgStatMonitorVersion11PG14
break
}
if pgVersion >= 13 {
version = pgStatMonitorVersion11PG13
break
}
version = pgStatMonitorVersion11PG12
case pgsmVersion.Core().GreaterThanOrEqual(v10):
if pgVersion >= 14 {
version = pgStatMonitorVersion10PG14
Expand Down
19 changes: 5 additions & 14 deletions agent/agents/postgres/pgstatmonitor/pgstatmonitor_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import (
)

var (
v10 = version.Must(version.NewVersion("1.0.0-beta-2"))
v20 = version.Must(version.NewVersion("2.0.0"))
v11 = version.Must(version.NewVersion("1.1.0"))
v10 = version.Must(version.NewVersion("1.0.0"))
v09 = version.Must(version.NewVersion("0.9"))
v08 = version.Must(version.NewVersion("0.8"))
)
Expand Down Expand Up @@ -82,13 +84,6 @@ type pgStatMonitor struct {
WalRecords int64
WalFpi int64
WalBytes int64
// state_code = 0 state 'PARSING'
// state_code = 1 state 'PLANNING'
// state_code = 2 state 'ACTIVE'
// state_code = 3 state 'FINISHED'
// state_code = 4 state 'FINISHED WITH ERROR'
StateCode int64
State string

// < pg0.6

Expand Down Expand Up @@ -179,9 +174,7 @@ func NewPgStatMonitorStructs(v pgStatMonitorVersion) (*pgStatMonitor, reform.Vie
field{info: parse.FieldInfo{Name: "Message", Type: "*string", Column: "message"}, pointer: &s.Message},
field{info: parse.FieldInfo{Name: "WalRecords", Type: "int64", Column: "wal_records"}, pointer: &s.WalRecords},
field{info: parse.FieldInfo{Name: "WalFpi", Type: "int64", Column: "wal_fpi"}, pointer: &s.WalFpi},
field{info: parse.FieldInfo{Name: "WalBytes", Type: "int64", Column: "wal_bytes"}, pointer: &s.WalBytes},
field{info: parse.FieldInfo{Name: "StateCode", Type: "int64", Column: "state_code"}, pointer: &s.StateCode},
field{info: parse.FieldInfo{Name: "State", Type: "string", Column: "state"}, pointer: &s.State})
field{info: parse.FieldInfo{Name: "WalBytes", Type: "int64", Column: "wal_bytes"}, pointer: &s.WalBytes})
}

if v <= pgStatMonitorVersion10PG12 {
Expand Down Expand Up @@ -257,7 +250,7 @@ func (v *pgStatMonitorAllViewType) NewStruct() reform.Struct {

// String returns a string representation of this struct or record.
func (s pgStatMonitor) String() string {
res := make([]string, 51)
res := make([]string, 49)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci-lint] reported by reviewdog 🐶
mnd: Magic number: 49, in detected (gomnd)

res[0] = "Bucket: " + reform.Inspect(s.Bucket, true)
res[1] = "BucketStartTime: " + reform.Inspect(s.BucketStartTime, true)
res[2] = "UserID: " + reform.Inspect(s.UserID, true)
Expand Down Expand Up @@ -307,8 +300,6 @@ func (s pgStatMonitor) String() string {
res[46] = "WalRecords: " + reform.Inspect(s.WalRecords, true)
res[47] = "WalFpi: " + reform.Inspect(s.WalFpi, true)
res[48] = "WalBytes: " + reform.Inspect(s.WalBytes, true)
res[49] = "StateCode: " + reform.Inspect(s.StateCode, true)
res[50] = "State: " + reform.Inspect(s.State, true)
return strings.Join(res, ", ")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func filter(mb []*agentpb.MetricsBucket) []*agentpb.MetricsBucket {
func TestVersion(t *testing.T) {
pgsmVersion, err := ver.NewVersion("1.0.0-beta-2")
require.NoError(t, err)
require.True(t, pgsmVersion.GreaterThanOrEqual(v10))
require.True(t, pgsmVersion.LessThan(v10))
}

func TestPGStatMonitorSchema(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ func (ssc *statMonitorCache) getStatMonitorExtended(ctx context.Context, q *refo

row, view := NewPgStatMonitorStructs(pgMonitorVersion)
conditions := "WHERE queryid IS NOT NULL AND query IS NOT NULL"
if pgMonitorVersion >= pgStatMonitorVersion09 {
// only pg_stat_monitor 0.9.0 and above supports state_code. It tells what is the query's current state.
if pgMonitorVersion >= pgStatMonitorVersion09 && pgMonitorVersion < pgStatMonitorVersion20PG12 {
// only pg_stat_monitor from 0.9.0 until 2.0.0 supports state_code. It tells what is the query's current state.
// To have correct data in QAN, we have to get only queries that are either 'FINISHED' or 'FINISHED WITH ERROR'.
conditions += " AND (state_code = 3 OR state_code = 4)"
ssc.l.Debug("PGSM version with state and state_code")
}
rows, e := q.SelectRows(view, conditions)
if e != nil {
Expand Down