In general, we need to review how database transactions are being used. The dequeue loop is way too large of a transaction.
But given what we have, if the transaction dies with a locking error, we should retry the entire thing a few times.
If there's a deadlock, that should result in an uppercase failmail.