Skip to content

Add new metrics for long running transactions#17

Open
joaofoltran wants to merge 3 commits intomainfrom
add_long_running_tx_metrics
Open

Add new metrics for long running transactions#17
joaofoltran wants to merge 3 commits intomainfrom
add_long_running_tx_metrics

Conversation

@joaofoltran
Copy link

@joaofoltran joaofoltran commented Jan 27, 2026

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.

@joaofoltran joaofoltran self-assigned this Jan 27, 2026
@joaofoltran joaofoltran added the enhancement New feature or request label Jan 27, 2026
}
defer rows.Close()
// Query for each threshold
for _, threshold := range longRunningTransactionThresholds {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yes, much better, will update :)

defer rows.Close()
// Query for each threshold
for _, threshold := range longRunningTransactionThresholds {
rows, err := db.QueryContext(ctx, longRunningTransactionsQuery, threshold)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

med: With the updated query, we can use QueryRowContext instead to avoid manual row closure handling.

@joaofoltran
Copy link
Author

Updated applying your changes @mble , check it out see if it improved :)
(has been quite some time since I've touched golang)

@mble
Copy link

mble commented Feb 2, 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.
@joaofoltran joaofoltran force-pushed the add_long_running_tx_metrics branch from 84ee559 to 4af8fca Compare February 25, 2026 13:40
@joaofoltran joaofoltran requested a review from a team as a code owner February 25, 2026 13:40
@joaofoltran
Copy link
Author

@mble rebased and some changes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants