Okay, let's break this down.
You want to alert when there are many queries running for longer than 500 seconds.
-
Understanding the Metric pg_stat_activity_max_tx_duration:
- This metric is a gauge. It reports the duration of the single longest running transaction among all active sessions at the time of scrape.
- A high value (e.g., > 500) on this metric tells you that at least one query is running longer than 500 seconds.
- It does not tell you how many queries are running longer than 500 seconds.
-
Increase vs. Rate:
increase() and rate() are functions used for counters. Counters are metrics that only ever increase (or reset to zero upon restart). They measure cumulative totals (like bytes transferred, requests served, errors counted).
pg_stat_activity_max_tx_duration is a gauge. It can go up or down.
- Therefore, neither
increase nor rate is appropriate for pg_stat_activity_max_tx_duration.
-
Finding the Right Metric for Counting:
- To count many long-running queries, you need a metric that gives you information about each active query or at least a count based on query state and duration.
- A more suitable metric provided by the
postgres_exporter (depending on version and configuration) is typically one that exposes the state and duration per active connection. Look for something like:
pg_stat_activity_duration_seconds: This metric usually represents the duration of the current state for each entry in pg_stat_activity, often labeled by datname, state, usename, etc. This is the metric you need for counting.
- (Less ideal, but sometimes used):
pg_stat_activity_state_change_time combined with time(), but this is harder to count effectively above a threshold.
Assuming pg_stat_activity_duration_seconds is available:
This metric allows you to filter for queries above your threshold (500 seconds) and then count them.
PromQL Query to Count Long Queries:
You want to count the number of entries in pg_stat_activity where pg_stat_activity_duration_seconds is greater than 500. It's usually best to group this by database (datname) to see which database is experiencing the issue.
count(pg_stat_activity_duration_seconds > 500) by (datname)
pg_stat_activity_duration_seconds > 500: This expression filters the time series, keeping only those where the duration is greater than 500 seconds.
count(...) by (datname): This counts the number of series that passed the filter, grouping the count by the datname label. This gives you the number of long queries per database.
Alert Condition:
You need to define what "many" means. Let's say you consider 5 or more long-running queries in a single database to be "many".
# Example Prometheus Alert Rule (in a rules file)
groups:
- name: postgres_activity_alerts
rules:
- alert: ManyLongRunningQueries
expr: |
count(pg_stat_activity_duration_seconds > 500) by (datname) > 5
for: 5m # Wait for the condition to be true for 5 minutes
labels:
severity: warning # Or critical, depending on impact
annotations:
summary: "Many long-running queries detected on database {{ $labels.datname }} on instance {{ $labels.instance }}"
description: "Database {{ $labels.datname }} on {{ $labels.instance }} has {{ $value }} queries running for longer than 500 seconds. Investigate `pg_stat_activity`."
Explanation of the Alert:
alert: ManyLongRunningQueries: The name of the alert.
expr: ...: The PromQL query that defines the condition. It triggers when the count of queries running > 500s in any database (datname) is greater than 5.
for: 5m: The alert will only fire if the condition (> 5) has been true for a continuous period of 5 minutes. This prevents flapping alerts due to temporary spikes.
labels: Standard labels like severity.
annotations: Provide useful information in the alert notification (summary, description). {{ $labels.datname }} and {{ $value }} are templating variables that insert the relevant values from the time series.
In summary:
- Do not use
increase or rate with pg_stat_activity_max_tx_duration.
- Use a metric that reports duration per query (like
pg_stat_activity_duration_seconds).
- Filter this metric by duration (
> 500).
- Use
count() to count the results of the filter, grouped by datname.
- Set your alert condition based on the threshold for this count (
> 5 in the example).