Telemetry
Database reliability SQL recipe

Find Database Lock Waits and Deadlocks

Rank blocked database operation fingerprints by incident count, wait duration, unresolved locks, and detected deadlocks.

Intermediatedatabase_lock_wait_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 database operations create the most serious lock contention?

Lock contention becomes actionable when blocked application operations are connected to a stable blocker fingerprint, wait duration, and resolution outcome.

Event contract

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampWhen the lock wait was observed.
database_nameUtf8Approved logical database name.
blocked_query_fingerprintUtf8Normalized label for the blocked application operation.
blocking_query_fingerprintUtf8Normalized label for the operation holding the lock.
lock_typeUtf8Controlled lock category.
wait_msFloat64Observed lock wait in milliseconds.
resolvedBooleanWhether the wait ended without remaining blocked.
deadlock_detectedBooleanWhether the database reported a deadlock outcome.
transaction_idUtf8Application-safe transaction identifier.
environmentUtf8Deployment environment.
DataFusion SQL

Copy the query

sql
SELECT
  database_name,
  blocked_query_fingerprint,
  blocking_query_fingerprint,
  COUNT(*) AS lock_wait_incidents,
  COUNT(DISTINCT transaction_id) AS affected_transactions,
  AVG(wait_ms) AS average_wait_ms,
  MAX(wait_ms) AS maximum_wait_ms,
  SUM(CASE WHEN NOT resolved THEN 1 ELSE 0 END) AS unresolved_incidents,
  SUM(CASE WHEN deadlock_detected THEN 1 ELSE 0 END) AS deadlocks
FROM database_lock_wait_events
WHERE timestamp_utc >= now() - INTERVAL '7 days'
  AND environment = 'production'
GROUP BY
  database_name,
  blocked_query_fingerprint,
  blocking_query_fingerprint
HAVING COUNT(*) >= 3
ORDER BY deadlocks DESC, maximum_wait_ms 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

Maximum lock wait by blocked operation

Order updates show longer waits, unresolved incidents, and detected deadlocks.

database_nameblocked_query_fingerprintblocking_query_fingerprintlock_wait_incidentsaffected_transactionsaverage_wait_msmaximum_wait_msunresolved_incidentsdeadlocks
app_productionUPDATE orders SET status = ?SELECT order FOR UPDATE881,6003,20022
analytics_productionREFRESH customer_summaryINSERT customer_event6650070000

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

Maximum lock wait by blocked operation: static chart of synthetic maximum_wait_ms values from the Find Database Lock Waits and Deadlocks 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, reproducible input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Blocked and blocking fingerprints keep the contention relationship visible without collecting parameterized SQL.
  2. 2Distinct transaction count protects the result from repeated samples of the same wait.
  3. 3Deadlocks and unresolved incidents take priority over a merely high average wait.

Edge cases to decide

  • Polling can emit several observations for one incident. Preserve a transaction or incident identifier.
  • Database lock views can expose sensitive statement text; normalize at collection time before emitting an event.
  • Expected maintenance locks should carry an approved workload category so they can be segmented.

Recommended dashboard

  • Bars: maximum_wait_ms by blocked_query_fingerprint
  • Table: blocker and blocked fingerprint pairs
  • Stats: unresolved incidents and deadlocks

Alert guidance

Page only for deadlocks or sustained unresolved contention affecting a user-visible workflow; route maintenance contention separately.

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