Atomic UPDATE to increment integer in Postgresql Atomic UPDATE to increment integer in Postgresql multithreading multithreading

Atomic UPDATE to increment integer in Postgresql


Yes, that is safe.

While one such statement is running, all other such statements are blocked on a lock. The lock will be released when the transaction completes, so keep your transactions short. On the other hand, you need to keep your transaction open until all your work is done, otherwise you might end up with gaps in your sequence.
That is why it is usually considered a bad idea to ask for gapless sequences.