ActiveRecord::Deadlocked: PG::TRDeadlockDetected Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Rails: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected

Two transactions locked rows in opposite orders, so PostgreSQL aborted one.

Exact message you may see ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detectedMysql2::Error: Deadlock found when trying to get lock; try restarting transactionActiveRecord::Deadlocked: Mysql2::Errordeadlock detected DETAIL: Process 123 waits for ShareLock on transaction
Seen on: Ruby PostgreSQL

Meaning

Keep transactions short, update rows in a consistent order (sort ids before update), use lock! consistently, avoid mixing row locks and table scans, and retry on ActiveRecord::Deadlocked (Rails 5.2+ raises it for MySQL/PostgreSQL). Background jobs that update the same rows concurrently are typical.

Common causes

  • Concurrent jobs updating rows in different orders
  • Long transactions
  • update_all/bulk updates overlapping
  • Foreign key locks (parent/child updates)
  • Unordered IN (...) updates

⚡ Quick fix

  1. Sort ids and lock in a consistent order
  2. Shorten transactions
  3. Rescue ActiveRecord::Deadlocked and retry

Detailed fix by platform

Rails

  1. Order.where(id: ids.sort).order(:id).lock.each { |o| o.update!(...) }

How to diagnose

  1. Order — Consistent?
  2. Duration — Short?
  3. Retry — Handled?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

A server is already running. Check /tmp/pids/server.pid — Rails: A server is already running (pid: 12345, file: /app/tmp/pids/server.pid). Exiting (Ruby / Rails)A server is already r…AbstractController::DoubleRenderError — Rails: AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action) (Ruby / Rails)AbstractController::D…ActionController::InvalidAuthenticityToken — Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity) (Ruby / Rails)ActionController::Inv…ActionController::ParameterMissing: param is missing or the value is empty — Rails: ActionController::ParameterMissing: param is missing or the value is empty: user (Ruby / Rails)ActionController::Par…ActionController::RoutingError: No route matches — Rails: ActionController::RoutingError (No route matches [GET] "/users/new") (Ruby / Rails)ActionController::Rou…ActionController::UnknownFormat — Rails: ActionController::UnknownFormat — UsersController#show is missing a template for this request format and variant (Ruby / Rails)ActionController::Unk…ActionController::UrlGenerationError: No route matches {action...} missing required keys — Rails: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] (Ruby / Rails)ActionController::Url…ActionMailer: SMTP From address may not be blank / Net::SMTPAuthenticationError — Rails: ArgumentError: SMTP From address may not be blank: nil / Net::SMTPAuthenticationError: 535 5.7.8 Username and Password not accepted (Ruby / Rails)ActionMailer: SMTP Fr…ActiveRec…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.