sequenced icon indicating copy to clipboard operation
sequenced copied to clipboard

Deadlock when running concurrent transactions

Open hashwin opened this issue 4 years ago • 1 comments

I am running into an issue where I am getting an ActiveRecord::Deadlocked error

PG::TRDeadlockDetected: ERROR:  deadlock detected
DETAIL:  Process 15304 waits for ExclusiveLock on relation 87680265 of database 87679929; blocked by process 16055.
Process 16055 waits for ExclusiveLock on relation 87680265 of database 87679929; blocked by process 15304.

This looks to be because there is an exclusive lock being put on the entire table. I understand the idea here, but when using a scope, it prevents distributed jobs running in different scopes. Is there any way around this? Is it possible to limit the lock based on scope?

hashwin avatar May 28 '20 13:05 hashwin

Hi! Not using the gem yet, but looking into it and this issue is of interest to me.

Could you provide more context to that situation? When you get that deadlock, are you inserting multiple records in each transaction or just one record per transaction?

If my understanding of PostgreSQL is correct, you should not end up with deadlock if you just insert 1 record in each transaction as one of the two will wait for the other insert to complete, it only ends up being an issue when you have multiples inserts/selects in each transaction. I understand there would be cases where it would be legitimate to do so (ex. inserting an invoice and associated line items that you want to sequence). I think it could also be a problem if your transactions are taking too much time, because the transaction block includes a call to an external API for example ; is that the case?

Thanks

j15e avatar Apr 26 '21 16:04 j15e