Skip to content

Commit

Permalink
Merge pull request #146 from blogh/fix9.1
Browse files Browse the repository at this point in the history
Fix the display of the state of transaction for PostgreSQL < 9.1
  • Loading branch information
blogh authored Aug 10, 2020
2 parents 1bbc7f1 + 254c2b2 commit c723dd4
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions pgactivity/Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,22 @@ def pg_get_active_connections(self,):
"""
Get total of active connections.
"""
query = """
SELECT
COUNT(*) as active_connections
FROM pg_stat_activity
WHERE state = 'active'
"""

if self.pg_num_version < 90200:
# prior to PostgreSQL 9.1, there was no state column
query = """
SELECT
COUNT(*) as active_connections
FROM pg_stat_activity
WHERE current_query NOT LIKE '<IDLE>%%'
"""
else:
query = """
SELECT
COUNT(*) as active_connections
FROM pg_stat_activity
WHERE state = 'active'
"""

cur = self.pg_conn.cursor()
cur.execute(query,)
Expand Down Expand Up @@ -488,7 +498,18 @@ def pg_get_activities(self, duration_mode=1):
EXTRACT(epoch FROM (NOW() - pg_stat_activity.{duration_column})) AS duration,
pg_stat_activity.waiting AS wait,
pg_stat_activity.usename AS user,
pg_stat_activity.current_query AS query,
CASE
WHEN pg_stat_activity.current_query = '<IDLE> in transaction (aborted)' THEN 'idle in transaction (aborted)'
WHEN pg_stat_activity.current_query = '<IDLE> in transaction' THEN 'idle in transaction'
WHEN pg_stat_activity.current_query = '<IDLE>' THEN 'idle'
ELSE 'active'
END
AS state,
CASE
WHEN pg_stat_activity.current_query LIKE '<IDLE>%%' THEN 'None'
ELSE pg_stat_activity.current_query
END
AS query,
false AS is_parallel_worker
FROM
pg_stat_activity
Expand Down Expand Up @@ -560,8 +581,18 @@ def pg_get_waiting(self, duration_mode=1):
pg_locks.locktype AS type,
pg_locks.relation::regclass AS relation,
EXTRACT(epoch FROM (NOW() - pg_stat_activity.{duration_column})) AS duration,
NULL AS state,
pg_stat_activity.current_query AS query
CASE
WHEN pg_stat_activity.current_query = '<IDLE> in transaction (aborted)' THEN 'idle in transaction (aborted)'
WHEN pg_stat_activity.current_query = '<IDLE> in transaction' THEN 'idle in transaction'
WHEN pg_stat_activity.current_query = '<IDLE>' THEN 'idle'
ELSE 'active'
END
AS state,
CASE
WHEN pg_stat_activity.current_query LIKE '<IDLE>%%' THEN 'None'
ELSE pg_stat_activity.current_query
END
AS query
FROM
pg_catalog.pg_locks
JOIN pg_catalog.pg_stat_activity ON(pg_catalog.pg_locks.pid = pg_catalog.pg_stat_activity.procpid)
Expand Down Expand Up @@ -682,6 +713,7 @@ def pg_get_blocking(self, duration_mode=1):
query = """
SELECT
pid,
appname,
CASE
WHEN LENGTH(datname) > 16
THEN SUBSTRING(datname FROM 0 FOR 6)||'...'||SUBSTRING(datname FROM '........$')
Expand All @@ -693,8 +725,18 @@ def pg_get_blocking(self, duration_mode=1):
mode,
locktype AS type,
duration,
state,
query
CASE
WHEN sq.query = '<IDLE> in transaction (aborted)' THEN 'idle in transaction (aborted)'
WHEN sq.query = '<IDLE> in transaction' THEN 'idle in transaction'
WHEN sq.query = '<IDLE>' THEN 'idle'
ELSE 'active'
END
AS state,
CASE
WHEN sq.query LIKE '<IDLE>%%' THEN 'None'
ELSE sq.query
END
AS query
FROM
(
SELECT
Expand Down

0 comments on commit c723dd4

Please sign in to comment.