Skip to content
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 . We checked the SQL syntax, required event fields, sample results, and limits on using the query. Who reviews this page

Question answered

Which database operations create the most serious lock contention?

Record which operation is blocked, the blocker fingerprint, how long it waited, and how the wait ended. Use those fields to find recurring blockers.

Event schema

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. We review the synthetic sample output separately. Check field types, thresholds, and counting rules 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
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, 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. 2Counting distinct transactions prevents repeated samples of one wait from inflating the result.
  3. 3Deadlocks and unresolved incidents take priority over a merely high average wait.

Edge cases to check

  • 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

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