Skip to content

Add new metrics for long running transactions#17

Open
joaofoltran wants to merge 2 commits intomainfrom
add_long_running_tx_metrics
Open

Add new metrics for long running transactions#17
joaofoltran wants to merge 2 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.

metrics (one for each threshold, 1min, 5min, 10min, 30min) and another
one returns the duration of the longest running transaction.
@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)

"pg_long_running_transactions",
"Current number of long running transactions",
[]string{},
prometheus.BuildFQName(namespace, longRunningTransactionsSubsystem, "count"),
Copy link

Choose a reason for hiding this comment

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

note: breaking change as this goes from pg_long_running_transactions to pg_long_running_transactions_count.

Copy link
Author

Choose a reason for hiding this comment

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

We are not using these metrics anywhere, so it shouldn't break any dashboards. And I dont intend to push this to the upstream. The metric inherently changed, we have a count for each threshold, so its a different "metric" warranting a different name, wdyt?

Copy link

Choose a reason for hiding this comment

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

Was mostly just a call out that if there were consumers, it was a breaking change. No other action needed.

FROM pg_catalog.pg_stat_activity
WHERE state IS DISTINCT FROM 'idle'
AND query NOT LIKE 'autovacuum:%'
AND pg_stat_activity.xact_start IS NOT NULL;
Copy link

Choose a reason for hiding this comment

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

opt: Very minor optimisation here is to extract this into a CTE to avoid calculating the whole EXTRACT(EPOCH FROM clock_timestamp() - pg_stat_activity.xact_start 5 times.

Copy link
Author

Choose a reason for hiding this comment

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

yep! for sure much better

longRunningTransactionsAgeInSeconds,
prometheus.GaugeValue,
ageValue,
)
Copy link

Choose a reason for hiding this comment

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

We don't actually have tests running in CI 😬 but these changes will break the relevant test in https://github.com/planetscale/postgres_exporter/blob/main/collector/pg_long_running_transactions_test.go.

Copy link
Author

Choose a reason for hiding this comment

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

The change in the metric itself will break the tests, so I'll update them too.

prometheus.GaugeValue,
count1800s,
"1800",
)
Copy link

Choose a reason for hiding this comment

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

Very mild thing, but a loop here would be a little cleaner:

  thresholds := []string{"60", "300", "600", "1800"}
  counts := []float64{count60s, count300s, count600s, count1800s}

  for i, threshold := range thresholds {
      ch <- prometheus.MustNewConstMetric(
          longRunningTransactionsCount,
          prometheus.GaugeValue,
          counts[i],
          threshold,
      )
  }

@mble
Copy link

mble commented Feb 2, 2026

@joaofoltran I've re-added CI so if you could rebase, that would be great.

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.

2 participants