diff --git a/pkg/client/client.go b/pkg/client/client.go index 5c3e274..cd69820 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -19,6 +19,7 @@ import ( type Client struct { db *sqlx.DB tunnel *Tunnel + serverVersion string History []history.Record `json:"history"` ConnectionString string `json:"connection_string"` } @@ -62,6 +63,7 @@ func New() (*Client, error) { History: history.New(), } + client.setServerVersion() return &client, nil } @@ -113,9 +115,20 @@ func NewFromUrl(url string, sshInfo *shared.SSHInfo) (*Client, error) { History: history.New(), } + client.setServerVersion() return &client, nil } +func (client *Client) setServerVersion() { + res, err := client.query("SELECT version()") + if err != nil || len(res.Rows) < 1 { + return + } + + version := res.Rows[0][0].(string) + client.serverVersion = strings.Split(version, " ")[1] +} + func (client *Client) Test() error { return client.db.Ping() } @@ -211,7 +224,15 @@ func (client *Client) TableConstraints(table string) (*Result, error) { // Returns all active queriers on the server func (client *Client) Activity() (*Result, error) { - return client.query(statements.Activity) + chunks := strings.Split(client.serverVersion, ".") + version := strings.Join(chunks[0:2], ".") + + query := statements.Activity[version] + if query == "" { + query = statements.Activity["default"] + } + + return client.query(query) } func (client *Client) Query(query string) (*Result, error) { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 2c4e935..1448cf2 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -159,6 +159,13 @@ func test_Info(t *testing.T) { assert.NotEqual(t, nil, res) } +func test_Activity(t *testing.T) { + res, err := testClient.Activity() + + assert.Equal(t, nil, err) + assert.NotEqual(t, nil, res) +} + func test_Databases(t *testing.T) { res, err := testClient.Databases() @@ -360,6 +367,7 @@ func TestAll(t *testing.T) { test_NewClientFromUrl(t) test_Test(t) test_Info(t) + test_Activity(t) test_Databases(t) test_Objects(t) test_Table(t) diff --git a/pkg/statements/sql.go b/pkg/statements/sql.go index 82f72d0..67bafcd 100644 --- a/pkg/statements/sql.go +++ b/pkg/statements/sql.go @@ -107,25 +107,6 @@ WHERE // --------------------------------------------------------------------------- - Activity = ` -SELECT - datname, - query, - state, - waiting, - query_start, - state_change, - pid, - datid, - application_name, - client_addr -FROM - pg_stat_activity -WHERE - state IS NOT NULL` - - // --------------------------------------------------------------------------- - Objects = ` SELECT n.nspname as "schema", @@ -151,3 +132,15 @@ WHERE has_schema_privilege(n.nspname, 'USAGE') ORDER BY 1, 2` ) + +var ( + Activity = map[string]string{ + "default": "SELECT * FROM pg_stat_activity", + "9.1": "SELECT datname, current_query, waiting, query_start, procpid as pid, datid, application_name, client_addr FROM pg_stat_activity", + "9.2": "SELECT datname, query, state, waiting, query_start, state_change, pid, datid, application_name, client_addr FROM pg_stat_activity", + "9.3": "SELECT datname, query, state, waiting, query_start, state_change, pid, datid, application_name, client_addr FROM pg_stat_activity", + "9.4": "SELECT datname, query, state, waiting, query_start, state_change, pid, datid, application_name, client_addr FROM pg_stat_activity", + "9.5": "SELECT datname, query, state, waiting, query_start, state_change, pid, datid, application_name, client_addr FROM pg_stat_activity", + "9.6": "SELECT datname, query, state, query_start, state_change, pid, datid, application_name, client_addr FROM pg_stat_activity", + } +)