U
U
User992021-10-07 15:26:48
PostgreSQL
User99, 2021-10-07 15:26:48

How to make RETURNING id with lock?

When inserting data into the database, an ID is returned using the built-in function RETURNING id;
But it turned out that about 50 people inserted the record at once. And accordingly, the ID of the one who last inserted the record was returned to half.
This is probably an isolated case. But the confidential material was received by another person.
How can this be fixed? How to return the ID of only the person who inserted the entry, and not the last entry?
Example:

spoiler

Маша вставил в базу запись: "Привет, я Маша!" ->ID: 1
Саша вставил в базу запись: "Привет, я Саша!" -> ID: 2
Так получилось что они сохранили одновременно. Но на миллисекунд позже сохранил Саша.

И так получилось что Маше возвращается ID Саши, то есть ID=2;



How can this be fixed?

Over 3 million entries. About 1 thousand concurrent users. PostgreSQL database

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-10-07
@galaxy

And it so happened that Sasha's ID is returned to Masha, that is, ID=2;

It couldn't happen. INSERT RETURNING returns exactly what it inserted (not some "last record")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question