Skip to content
Telemetry
Database reliability SQL recipe

Measure long-running database transactions

Find application transaction classes with long tails, rollbacks, and excessive open duration.

Intermediatedatabase_transaction_eventsReviewed 2026-07-28Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . We checked the SQL syntax, required event fields, sample results, and limits on using the query. Who reviews this page

Question answered

Which application transaction classes remain open the longest?

Long transactions hold resources and can amplify lock contention. Application events can identify the owning workflow and release even when server-side diagnostics remain necessary.

Event schema

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampTransaction terminal-event time.
serviceUtf8Owning application service.
transaction_nameUtf8Stable logical transaction class.
releaseUtf8Application release.
duration_msFloat64Begin-to-terminal transaction duration.
outcomeUtf8committed, rolled_back, or timed_out.
DataFusion SQL

Copy the query

sql
SELECT
  service,
  transaction_name,
  release,
  COUNT(*) AS transactions,
  approx_percentile_cont(duration_ms, 0.95) AS p95_duration_ms,
  MAX(duration_ms) AS max_duration_ms,
  SUM(CASE WHEN outcome <> 'committed' THEN 1 ELSE 0 END) AS unsuccessful
FROM database_transaction_events
WHERE timestamp_utc >= now() - INTERVAL '7 days'
GROUP BY service, transaction_name, release
ORDER BY p95_duration_ms DESC, unsuccessful DESC;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. We review the synthetic sample output separately. Check field types, thresholds, and counting rules against your own data. Read the testing methodology.

Query result

p95 transaction duration

Invoice finalization has the longest tail and the most unsuccessful outcomes.

servicetransaction_namereleasetransactionsp95_duration_msmax_duration_msunsuccessful
billinginvoice-finalizeapi-888206,80024,10028
projectsmember-importapi-882404,21018,2009

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

p95 transaction duration: static chart of synthetic p95_duration_ms values from the Measure long-running database transactions example result
Download this SVG chart of the sample results for an article, runbook, or design review. Please credit Telemetry.

Reproduce the example

Download the sample data

The JSON bundle includes the event schema with field types, illustrative input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Transaction class connects duration to an application owner.
  2. 2The maximum exposes extreme resource holds beside a stable percentile.
  3. 3Count rollbacks and timeouts alongside commits to include every final transaction result.

Edge cases to check

  • Join start and completion events separately to find transactions with no final result.
  • Server wait events and lock graphs require database-native diagnostics.
  • Do not log statements with bound values.

Recommended dashboard

  • Bars: p95_duration_ms by transaction
  • Trend: unsuccessful by release
  • Table: longest safe transaction events

Alert guidance

Alert when a critical transaction times out or its p95 exceeds an owner-approved bound.

Read alert setup

Set up the events this query needs

Related instrumentation and guides

Continue the analysis

Run it on your events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key