acts-as-taggable-on: PG TR Deadlock Detected after addition of taggings_count

Just upgraded to v3.1.1 and I’m seeing a deadlock. I have 25 sidekiq (threaded) workers running and importing data. Each process is creating many tags.

WARN: PG::TRDeadlockDetected: ERROR:  deadlock detected
DETAIL:  Process 2546 waits for ShareLock on transaction 258000; blocked by process 2547.
Process 2547 waits for ExclusiveLock on tuple (74,46) of relation 45545 of database 19986; blocked by process 2554.
Process 2554 waits for ShareLock on transaction 257999; blocked by process 2546.
HINT:  See server log for query details. 
: UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = 5849

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (3 by maintainers)

Most upvoted comments

This fixed issue for us. The cons of that are that we limit the number of threads but we don’t experience the deadlock again. https://github.com/mperham/sidekiq/wiki/Ent-Rate-Limiting#concurrent

… somehow I’m still getting deadlock errors even after setting ActsAsTaggableOn.tags_counter = false inside my sidekiq job

PG::TRDeadlockDetected: ERROR:  deadlock detected\nDETAIL:  Process 24245 waits for ShareLock on transaction 55696; blocked by process 24246.\nProcess 24246 waits for ShareLock on transaction 55694; blocked by process 24245.\nHINT:  See server log for query details.\nCONTEXT:  while updating tuple (43,158) in relation \"tags\"\n: UPDATE \"tags\" SET \"taggings_count\" = COALESCE(\"taggings_count\", 0) + 1 WHERE \"tags\".\"id\" = 92

my job looks something like this:

class Worker
  include Sidekiq::Worker
  def perform(ids)
    ActsAsTaggableOn.tags_counter = false
    ActiveRecord::Base.transaction do
      MyModel.where(:id => ids).delete_all
      # ... recreating models with tags
    end
  end
end

Am I doing it wrong?