Skip to content

Commit aed85d0

Browse files
authored
Merge pull request #22 from greigwise/master
Fix connections metric to work with postgres 9.6+
2 parents 913f6ee + fd98556 commit aed85d0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
55

66
## [Unreleased]
7+
### Fixed
8+
- metric-postgres-connections: Handle postgres version 9.6 and above.
9+
710
### Changed
811
- Updated all the scripts to add an optional timeout setting.
912

bin/metric-postgres-connections.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,17 @@ def run
8181
password: config[:password],
8282
connect_timeout: config[:timeout])
8383
request = [
84-
'select count(*), waiting from pg_stat_activity',
85-
"where datname = '#{config[:database]}' group by waiting"
84+
"select case when count(*) = 1 then 'waiting' else",
85+
"'case when wait_event_type is null then false else true end' end as wait_col",
86+
'from information_schema.columns',
87+
"where table_name = 'pg_stat_activity' and table_schema = 'pg_catalog'",
88+
"and column_name = 'waiting'"
89+
]
90+
wait_col = con.exec(request.join(' ')).first['wait_col']
91+
92+
request = [
93+
"select count(*), #{wait_col} as waiting from pg_stat_activity",
94+
"where datname = '#{config[:database]}' group by #{wait_col}"
8695
]
8796

8897
metrics = {

0 commit comments

Comments
 (0)