You either need to lock the table while creating a new sending_object_id, or, if you don't care exactly what the number is create your own sequence and call nextval on it.
Postgresql lock shared table with multiple users
-
01-07-2022 - |
Вопрос
So, I'm having a synchronization issue here; Let's explain the situation:
I have a table called Sendings
, that consists of these columns:
id sending_object_id type destinatary
The id is the automatic serial id postgresql gives to each table. The sending object_id is a custom identifier, and type and destinatary...don't really matter for this particular question.
This is an example of how the table would look like with data:
id sending_object_id type destinatary
1 1 1 A7
2 1 1 B1
3 2 2 A1
4 2 2 A2
5 3 1 B8
6 4 1 N1
The same "object_id
" can be sent to multiple destinataries, as you can see. Now my synchronization problem comes when two different users, with two different computers, try to insert a new sending.
User A introduces the new sending information and the DB engine makes a SelectMax(sending_object_id)
in order to create the new sending_object_id for the registry.
User B, at the same time, introduces a new sending, making another SelectMax(sending_object_id).
How could I avoid duplicated sending_object_id
s when inserting new data, at the same time?
Thanks in advance.
Решение
Другие советы
I think need to use sequences for this. They guaranteed to be unique in any cases.