Skip to content

Commit bf7e0da

Browse files
committed
support PG 9.4
1 parent d08b0e9 commit bf7e0da

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ env:
1717
- PG_VERSION=10
1818
- PG_VERSION=9.6
1919
- PG_VERSION=9.5
20+
- PG_VERSION=9.4
2021

2122
before_install:
2223
- sudo /etc/init.d/postgresql stop

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ conn, err := pgstats.Connect("foo", "username", "password", pgstats.SslMode("dis
8989
- 10
9090
- 9.6
9191
- 9.5
92+
- 9.4
9293

9394
[Full pg_stat view / version table](https://github.com/vynaloze/pgstats/wiki/Supported-stats-vs-PG-version)
9495

@@ -101,7 +102,7 @@ Y | `v0.2.0` | pg_stat_statements | ~June '19
101102
Y | `v0.3.0` | support PG 11 | ~June '19
102103
Y | `v0.4.0` | support PG 9.6 | ~June '19
103104
Y | `v0.5.0` | support PG 9.5 | ~June '19
104-
N | `v0.6.0` | support PG 9.4 | ~June '19
105+
Y | `v0.6.0` | support PG 9.4 | ~June '19
105106
N | `v1.0.0` | global wrapper for the ease of use | ~July '19
106107
N | `v1.1.0` | add missing integration tests | ~July '19
107108

pgstats.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (s *PgStats) PgStatSubscription() (PgStatSubscriptionView, error) {
7070
// PgStatSsl returns a slice, containing statistics about SSL usage
7171
// on the connection for each backend or WAL sender process.
7272
//
73+
// Supported since PostgreSQL 9.5.
74+
//
7375
// For more details, see:
7476
// https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-SSL
7577
func (s *PgStats) PgStatSsl() (PgStatSslView, error) {

pgstats_integration_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func TestPgSsl(t *testing.T) {
6363
t.Error(err)
6464
}
6565
a, err := s.PgStatSsl()
66-
validate(t, len(a), err)
66+
if err != nil {
67+
if strings.Contains(err.Error(), "Unsupported PostgreSQL version: 9.") {
68+
return
69+
}
70+
t.Error(err)
71+
}
72+
if len(a) == 0 {
73+
t.Error("No data returned by query")
74+
}
6775
}
6876

6977
func TestPgStatProgressVacuum(t *testing.T) {

stat_ssl.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pgstats
22

3-
import "database/sql"
3+
import (
4+
"database/sql"
5+
"github.com/pkg/errors"
6+
)
47

58
// PgStatSslView represents content of pg_stat_ssl view
69
type PgStatSslView []PgStatSslRow
@@ -26,6 +29,14 @@ type PgStatSslRow struct {
2629
}
2730

2831
func (s *PgStats) fetchSsl() (PgStatSslView, error) {
32+
version, err := s.getPgVersion()
33+
if err != nil {
34+
return nil, err
35+
}
36+
if version < 9.5 {
37+
return nil, errors.Errorf("Unsupported PostgreSQL version: %f", version)
38+
}
39+
2940
db := s.conn.db
3041
query := "select pid,ssl,version,cipher,bits," +
3142
"compression,clientdn from pg_stat_ssl"

stat_statements.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ type PgStatStatementsRow struct {
1616
// Number of times executed
1717
Calls int64 `json:"calls"`
1818
// Total time spent in the statement, in milliseconds
19+
// Supported since PostgreSQL 9.5
1920
TotalTime float64 `json:"total_time"`
2021
// Minimum time spent in the statement, in milliseconds
22+
// Supported since PostgreSQL 9.5
2123
MinTime float64 `json:"min_time"`
2224
// Maximum time spent in the statement, in milliseconds
25+
// Supported since PostgreSQL 9.5
2326
MaxTime float64 `json:"max_time"`
2427
// Mean time spent in the statement, in milliseconds
28+
// Supported since PostgreSQL 9.5
2529
MeanTime float64 `json:"mean_time"`
2630
// Population standard deviation of time spent in the statement, in milliseconds
31+
// Supported since PostgreSQL 9.5
2732
StddevTime float64 `json:"stddev_time"`
2833
// Total number of rows retrieved or affected by the statement
2934
Rows int64 `json:"rows"`
@@ -54,6 +59,17 @@ type PgStatStatementsRow struct {
5459
}
5560

5661
func (s *PgStats) fetchStatements() (PgStatStatementsView, error) {
62+
version, err := s.getPgVersion()
63+
if err != nil {
64+
return nil, err
65+
}
66+
if version > 9.4 {
67+
return s.fetchStatements95()
68+
}
69+
return s.fetchStatements94()
70+
}
71+
72+
func (s *PgStats) fetchStatements95() (PgStatStatementsView, error) {
5773
db := s.conn.db
5874
query := "select userid,dbid,queryid,query,calls," +
5975
"total_time,min_time,max_time,mean_time,stddev_time," +
@@ -82,3 +98,31 @@ func (s *PgStats) fetchStatements() (PgStatStatementsView, error) {
8298
}
8399
return data, rows.Err()
84100
}
101+
102+
func (s *PgStats) fetchStatements94() (PgStatStatementsView, error) {
103+
db := s.conn.db
104+
query := "select userid,dbid,queryid,query,calls," +
105+
"rows,shared_blks_hit,shared_blks_read,shared_blks_dirtied,shared_blks_written," +
106+
"local_blks_hit,local_blks_read,local_blks_dirtied,local_blks_written,temp_blks_read," +
107+
"temp_blks_written,blk_read_time,blk_write_time from pg_stat_statements"
108+
109+
rows, err := db.Query(query)
110+
if err != nil {
111+
return nil, err
112+
}
113+
defer rows.Close()
114+
115+
data := make([]PgStatStatementsRow, 0)
116+
for rows.Next() {
117+
row := new(PgStatStatementsRow)
118+
err := rows.Scan(&row.Userid, &row.Dbid, &row.Queryid, &row.Query, &row.Calls,
119+
&row.Rows, &row.SharedBlksHit, &row.SharedBlksRead, &row.SharedBlksDirtied, &row.SharedBlksWritten,
120+
&row.LocalBlksHit, &row.LocalBlksRead, &row.LocalBlksDirtied, &row.LocalBlksWritten, &row.TempBlksRead,
121+
&row.TempBlksWritten, &row.BlkReadTime, &row.BlkWriteTime)
122+
if err != nil {
123+
return nil, err
124+
}
125+
data = append(data, *row)
126+
}
127+
return data, rows.Err()
128+
}

0 commit comments

Comments
 (0)