INSERT without duplicates in PostgreSQL?

Good day, there is a table:
ec2aaaac5811453489168e3719a7b8b9.png

Example query:
UPDATE users_data ud
SET
 count = (ud.count + 1),
 updated_at = now()
WHERE
 user_id = :uid AND
 command = :command AND
 created_at::date = :now;

INSERT INTO users_data (user_id, command)
 SELECT :uid :command
 WHERE NOT EXISTS (
 SELECT 1
 FROM users_data
WHERE
 user_id = :uid AND
 command = :command AND
 created_at::date = :now
 LIMIT 1
 );


But here is the next, despite the fact that there is a unique index..
bbe6c672943a433dbb79ceb4968b51b2.png

Can someone tell me what ...
July 2nd 19 at 18:09
1 answer
July 2nd 19 at 18:11
207234 != 20725
Yes, I know, but the select with ::date.. I do not understand why slips - Yasmeen.Funk53 commented on July 2nd 19 at 18:14
: and why should not slip? - marilyne_Roh commented on July 2nd 19 at 18:17
INSERT WHERE NOT EXIST - Yasmeen.Funk53 commented on July 2nd 19 at 18:20
: to ensure that the request is not passed we need now occurred in the same clock cycle as the insert previous values - marilyne_Roh commented on July 2nd 19 at 18:23
: just the thing is that everything works OK, but when the difference between INSERT'AMI (in time) is small (in the thousandths of milliseconds) - then undergoing a second insert - Yasmeen.Funk53 commented on July 2nd 19 at 18:26
:
as an option to remove milisec
stackoverflow.com/questions/12060546/remove-timest... - marilyne_Roh commented on July 2nd 19 at 18:29
: decided to just add a date column and have been with him to build a unique index, that would be no transformation will be faster) Thank you - Yasmeen.Funk53 commented on July 2nd 19 at 18:32

Find more questions by tags SQLPostgreSQL