I have a Postgres database table (users) with the schema as shown below:
id user_id status
1 1 Green
2 2 Green
3 1 Blue
4 1 Green
A few unexpected concurrent insert queries are being executed on this table as below:
(We don't have control over query triggers)
17:38:31.927 -> INSERT INTO members (user_id, status) VALUES (1, 'Green');
17:38:31.927 -> INSERT INTO members (user_id, status) VALUES (1, 'Green');
This results in two new records added to the table:
id user_id status
1 1 Green
2 2 Green
3 1 Blue
4 1 Green
5 1 Green
6 1 Green
I want to avoid these duplicate records getting created. Also, the record with a user_id and status should not be created if the latest record from all the records with this same user_id is having this same status.
I cannot manage this logic at the code level as these are concurrent queries.
I have explored unique constraints and upsert in Postgres, but didn't find a solution for the above scenario.
EDIT -
Why cannot add a unique constraint on user_id and status?
Ans. There can be duplicate user_id and status. Suppose 'Green' is the latest status of user_id 1 (as shown in the first table) then we cannot insert status as 'Green' for the same user_id. But we can insert 'Blue' and then again we can insert 'Green', this way there will be two records with user_id as 1 and status as 'Green'. Similarly, there are multiple same queries getting triggered concurrently so if one of those queries executes first then other remaining queries should fail because they the have same user_id and status.