Can the order of concurrent inserts be guaranteed with some column
0
votes
1
answer
141
views
I have some services, which can execute
insert
to insert new elements into a table of my database (Postgresql). So in the view of the database, many insert
s are executed concurrently.
My question is if it's possible to keep the insertion order with some column of the table.
For example, there are three insert
s:
INSERT INTO Users (UserName, Id) VALUES ('Tom', 1);
INSERT INTO Users (UserName, Id) VALUES ('Jerry', 2);
INSERT INTO Users (UserName, Id) VALUES ('Joey', 3);
They are executing concurrently.
For some reason, the second insert
is delayed. In this case, when the third insert
is now executing, I'm expecting one of the two results as below:
1. the insert
fails;
2. the insert
succeed with a new Id 2
.
Is there some mechanism to achieve this?
**Why do I need this**
There is a ID generator which generate IDs with snowflake algorithm. Concurrent insert
s are executed in multi-threads. when reading data from the table, I want to make range queries like this:
select * from my_table where Id > a limit 5
select * from my_table where Id > b limit 5
select * from my_table where Id > c limit 5
...
a
, b
and c
are the last Id coming from the previous select
.
| --- a --- | --- b --- | --- c --- |
The problem is when the second select
is executing, it still might be possible to make a insert
in the a
zone because of concurrent insert
. If this happen, I will have no chance to read the delayed inserted data.
Asked by Yves
(123 rep)
Aug 27, 2024, 07:40 AM
Last activity: Aug 29, 2024, 06:49 PM
Last activity: Aug 29, 2024, 06:49 PM