Fix activity query

* Modify the activity query to ensure each state is always reported.

* Fix the activity preprocessor to unwrap the value from the array
  containing it.
This commit is contained in:
2025-10-18 12:49:09 -04:00
parent 175fd85f9f
commit 8182ad75b9
2 changed files with 37 additions and 21 deletions

View File

@@ -196,12 +196,28 @@ metrics:
type: set
query:
0: >
SELECT state,
count(*) AS backend_count,
COALESCE(EXTRACT(EPOCH FROM max(now() - state_change)), 0) AS max_state_time
FROM pg_stat_activity
WHERE datname = %(dbname)s
GROUP BY state
SELECT
states.state,
COALESCE(a.backend_count, 0) AS backend_count,
COALESCE(a.max_state_time, 0) AS max_state_time
FROM (
SELECT state,
count(*) AS backend_count,
COALESCE(EXTRACT(EPOCH FROM now() - min(state_change)), 0) AS max_state_time
FROM pg_stat_activity
WHERE datname = %(dbname)s
GROUP BY state
) AS a
RIGHT JOIN
unnest(ARRAY[
'active',
'idle',
'idle in transaction',
'idle in transaction (aborted)',
'fastpath function call',
'disabled'
]) AS states(state)
USING (state);
test_args:
dbname: postgres
@@ -235,7 +251,7 @@ metrics:
locks:
type: row
query:
0:
0: >
SELECT COUNT(*) AS total,
SUM(CASE WHEN granted THEN 1 ELSE 0 END) AS granted
FROM pg_locks