Skip to content

Commit 3fc80a7

Browse files
committed
Exclude the metrics fetching session's data from pg_stat_activity
To reduce the observer effect, filter out pg_stat_activity rows of the postgres_exporter session from all SA queries, based on pid / procpid. A bit annoying to see idling DBs showing pg_stat_activity_count "active" count of 1 Signed-off-by: Kaarel Moppel <[email protected]>
1 parent cb0bac6 commit 3fc80a7

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

cmd/postgres_exporter/queries.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ var queryOverrides = map[string][]OverrideQuery{
142142
count(*) AS count,
143143
MAX(EXTRACT(EPOCH FROM now() - xact_start))::float AS max_tx_duration
144144
FROM pg_stat_activity
145+
WHERE pid <> pg_backend_pid()
145146
GROUP BY datname,state,usename,application_name,backend_type,wait_event_type,wait_event) AS tmp2
146147
ON tmp.state = tmp2.state AND pg_database.datname = tmp2.datname
147148
`,
@@ -156,7 +157,9 @@ var queryOverrides = map[string][]OverrideQuery{
156157
application_name,
157158
COALESCE(count(*),0) AS count,
158159
COALESCE(MAX(EXTRACT(EPOCH FROM now() - xact_start))::float,0) AS max_tx_duration
159-
FROM pg_stat_activity GROUP BY datname,usename,application_name
160+
FROM pg_stat_activity
161+
WHERE procpid <> pg_backend_pid()
162+
GROUP BY datname,usename,application_name
160163
`,
161164
},
162165
},

collector/pg_long_running_transactions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ var (
5656
FROM pg_catalog.pg_stat_activity
5757
WHERE state IS DISTINCT FROM 'idle'
5858
AND query NOT LIKE 'autovacuum:%'
59-
AND pg_stat_activity.xact_start IS NOT NULL;
59+
AND pg_stat_activity.xact_start IS NOT NULL
60+
AND pid <> pg_backend_pid();
6061
`
6162
)
6263

collector/pg_process_idle.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
5656
COUNT(*) AS process_idle_seconds_count
5757
FROM pg_stat_activity
5858
WHERE state ~ '^idle'
59+
AND pid <> pg_backend_pid();
5960
GROUP BY state, application_name
6061
),
6162
buckets AS (
@@ -72,6 +73,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
7273
FROM
7374
pg_stat_activity,
7475
UNNEST(ARRAY[1, 2, 5, 15, 30, 60, 90, 120, 300]) AS le
76+
WHERE pid <> pg_backend_pid()
7577
GROUP BY state, application_name, le
7678
ORDER BY state, application_name, le
7779
)

0 commit comments

Comments
 (0)