Add new metrics for long running transactions#17
Open
joaofoltran wants to merge 3 commits intomainfrom
Open
Conversation
mble
reviewed
Jan 28, 2026
| } | ||
| defer rows.Close() | ||
| // Query for each threshold | ||
| for _, threshold := range longRunningTransactionThresholds { |
There was a problem hiding this comment.
med: I don't love this. We can re-express all this in a single query:
SELECT
count(*) FILTER (
WHERE
extract('epoch', clock_timestamp() - xact_start) >= 60
)
AS count_60,
count(*) FILTER (
WHERE
extract('epoch', clock_timestamp() - xact_start)
>= 300
)
AS count_300,
count(*) FILTER (
WHERE
extract('epoch', clock_timestamp() - xact_start)
>= 600
)
AS count_600,
count(*) FILTER (
WHERE
extract('epoch', clock_timestamp() - xact_start)
>= 1800
)
AS count_1800,
COALESCE(
max(extract('epoch', clock_timestamp() - xact_start)),
0
)
AS oldest_timestamp_seconds
FROM
pg_catalog.pg_stat_activity
WHERE
state IS DISTINCT FROM 'idle'
AND query NOT LIKE 'autovacuum:%'
AND xact_start IS NOT NULL;Can then break it up based out of the results.
Author
There was a problem hiding this comment.
oh, yes, much better, will update :)
| defer rows.Close() | ||
| // Query for each threshold | ||
| for _, threshold := range longRunningTransactionThresholds { | ||
| rows, err := db.QueryContext(ctx, longRunningTransactionsQuery, threshold) |
There was a problem hiding this comment.
med: With the updated query, we can use QueryRowContext instead to avoid manual row closure handling.
Author
|
Updated applying your changes @mble , check it out see if it improved :) |
mble
reviewed
Jan 29, 2026
|
@joaofoltran I've re-added CI so if you could rebase, that would be great. |
metrics (one for each threshold, 1min, 5min, 10min, 30min) and another one returns the duration of the longest running transaction.
_count suffix is reserved for histograms/summaries. Use pg_long_running_transactions (no suffix) since this is a gauge.
84ee559 to
4af8fca
Compare
Author
|
@mble rebased and some changes :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Updated both metrics for long running transactions. One returns 4 metrics (one for each threshold, 1min, 5min, 10min, 30min) and another one returns the duration of the longest running transaction.
With this we can check if there was a long running transaction during the time of any issues that could cause xmin/lsn retention.
The original code was just counting how many transactions and then bringing the longest running one. This new code we know if there are multiple long running ones.
Next I'll be adding these to our grafana metrics so we can check them when diagnosing issues.