Graceful locking for internal_id records

In order to generate internal ids (iid), we use a model instance with the right scope (for example, project and issues - after all, we want a iid sequence for this project and its issues). In order to increment the sequence, we SELECT ... FOR UPDATE the corresponding record in internal_ids.

Now, with competing transactions and especially long transactions holding that lock, we run into statement timeouts. That is because FOR UPDATE is blocking until the lock is granted.

The idea here is to do FOR UPDATE SKIP LOCKED instead and retry in case the lock wasn't granted. This will greatly reduce the time spent on this query and allow transactions to complete because they're not aborted at the statement_timeout.