Skip to content

Commit 82f971c

Browse files
committed
Add metric for idle postgres processes
1 parent 7681d83 commit 82f971c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

cookbooks/postgresql/templates/default/postgres_queries.yml.erb

+51
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,57 @@ pg_statio_user_tables:
123123
description: "Number of buffer hits in this table's TOAST table indexes (if any)"
124124
<% end -%>
125125

126+
pg_process_idle:
127+
query: |
128+
WITH
129+
metrics AS (
130+
SELECT
131+
state,
132+
application_name,
133+
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
134+
COUNT(*) AS process_idle_seconds_count
135+
FROM pg_stat_activity
136+
WHERE state ~ '^idle'
137+
GROUP BY state, application_name
138+
),
139+
buckets AS (
140+
SELECT
141+
state,
142+
application_name,
143+
le,
144+
SUM(
145+
CASE WHEN EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change)) <= le
146+
THEN 1
147+
ELSE 0
148+
END
149+
)::bigint AS bucket
150+
FROM
151+
pg_stat_activity,
152+
UNNEST(ARRAY[1, 2, 5, 15, 30, 60, 90, 120, 300]) AS le
153+
GROUP BY state, application_name, le
154+
ORDER BY state, application_name, le
155+
)
156+
SELECT
157+
state,
158+
application_name,
159+
process_idle_seconds_sum as seconds_sum,
160+
process_idle_seconds_count as seconds_count,
161+
ARRAY_AGG(le) AS seconds,
162+
ARRAY_AGG(bucket) AS seconds_bucket
163+
FROM metrics JOIN buckets USING (state, application_name)
164+
GROUP BY 1, 2, 3, 4
165+
master: true
166+
metrics:
167+
- state:
168+
usage: "LABEL"
169+
description: "State"
170+
- application_name:
171+
usage: "LABEL"
172+
description: "Application Name"
173+
- seconds:
174+
usage: "HISTOGRAM"
175+
description: "Idle time of server processes"
176+
126177
pg_unfrozen_ids:
127178
query: "SELECT current_database() AS datname, max(age(relfrozenxid)) AS xid_age, max(mxid_age(relminmxid)) AS mxid_age FROM pg_class WHERE relkind IN ('r', 'm')"
128179
metrics:

0 commit comments

Comments
 (0)