Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

PMM-9630 Use status, status_code only for some versions. #327

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions agents/postgres/pgstatmonitor/pgstatmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const (
commandTypeUpdate = "UPDATE"
commandTypeInsert = "INSERT"
commandTypeDelete = "DELETE"
commandTypeUtiity = "UTILITY"
commandTypeUtility = "UTILITY"
)

var commandTypeToText = []string{
Expand All @@ -101,7 +101,7 @@ var commandTypeToText = []string{
commandTypeUpdate,
commandTypeInsert,
commandTypeDelete,
commandTypeUtiity,
commandTypeUtility,
commandTextNotAvailable,
}

Expand Down
17 changes: 3 additions & 14 deletions agents/postgres/pgstatmonitor/pgstatmonitor_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
v10 = version.Must(version.NewVersion("1.0.0-beta-2"))
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 @@ -83,13 +83,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 @@ -180,9 +173,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 @@ -258,7 +249,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
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 @@ -308,8 +299,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
2 changes: 1 addition & 1 deletion agents/postgres/pgstatmonitor/pgstatmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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
7 changes: 4 additions & 3 deletions agents/postgres/pgstatmonitor/stat_monitor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (ssc *statMonitorCache) getStatMonitorExtended(ctx context.Context, q *refo
databases := queryDatabases(q)
usernames := queryUsernames(q)

pgMonitorVersion, _, err := getPGMonitorVersion(q)
pgMonitorVersion, prerelease, err := getPGMonitorVersion(q)
if err != nil {
err = errors.Wrap(err, "failed to get row and view for pg_stat_monitor version")
return
Expand All @@ -92,10 +92,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 <= pgStatMonitorVersion10PG14 && prerelease != "" {
// only pg_stat_monitor 0.9.0, 1.0.0-beta-2, 1.0.0-rc.1, 1.0.0-rc.2 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
5 changes: 3 additions & 2 deletions docker-compose-pg-load.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ version: '3.7'

services:
postgres-pgmonitor:
image: ${POSTGRES_IMAGE:-perconalab/percona-distribution-postgresql:13.3}
image: ${POSTGRES_IMAGE:-perconalab/percona-distribution-postgresql:14.1}
container_name: pmm-agent-postgres-pgmonitor
command: >
-c shared_preload_libraries=pg_stat_monitor
-c track_activity_query_size=2048
-c pg_stat_monitor.pgsm_query_max_len=10000
-c pg_stat_monitor.pgsm_normalized_query=0
-c pg_stat_monitor.pgsm_enable_query_plan=1
-c track_io_timing=on
ports:
- 127.0.0.1:5432:5432
Expand All @@ -21,7 +22,7 @@ services:
- test_db_postgres:/docker-entrypoint-initdb.d/

postgres-load:
image: ${POSTGRES_IMAGE:-perconalab/percona-distribution-postgresql:13.3}
image: ${POSTGRES_IMAGE:-perconalab/percona-distribution-postgresql:14.1}
container_name: pmm-agent-postgres-load
depends_on:
- postgres-pgmonitor
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ services:
-c shared_preload_libraries='${PG_PRELOADED_LIBS:-pg_stat_statements}'
-c track_activity_query_size=2048
-c pg_stat_statements.max=10000
-c pg_stat_monitor.pgsm_query_max_len=10000
-c pg_stat_statements.track=all
-c pg_stat_statements.save=off
-c track_io_timing=on
Expand Down