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 . SQL compatibility, event contract, synthetic output, and operational caveats. Editorial ownership

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 contract

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. The deterministic sample output is synthetic and reviewed separately; validate field types, thresholds, and business definitions 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
Indexable SVG of the deterministic example output. Download it for an article, runbook, or design review with attribution.

Reproduce the example

Download the public fixture

The JSON bundle includes the typed event contract, 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. 3Non-commit outcomes include rollback and timeout for a full terminal view.

Edge cases to decide

  • Missing terminal events need a separate start-to-terminal join.
  • 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

Put the recipe to work

Related instrumentation and guides

Continue the analysis

Run it on real 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