Andrei Prigorshnev
84e13e9b1c
FIX: avoid race condition when setting user status ( #19817 )
...
We caught it in logs, race condition led to this error:
ActiveRecord::RecordNotUnique
(PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "user_statuses_pkey"
DETAIL: Key (user_id)=(15) already exists.)
The reason the problem happened was that we were checking if a user has status and if not inserting status:
if user_status
...
else
self.user_status = UserStatus.create!(status)
end
The problem is that it's possible that another request will insert status just after we check if status exists and just before our request call `UserStatus.create!(status)`. Using `upsert` fixes the problem because under the hood `upsert` generates the only SQL request that uses "INSERT ... ON CONFLICT DO UPDATE". So we do everything in one SQL query, and that query takes care of resolving possible conflicts.
2023-02-06 18:56:28 +04:00
..
2023-02-03 12:17:52 +01:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2020-07-20 15:23:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-27 08:05:29 -07:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-10-17 16:58:21 +11:00
2019-10-17 16:58:21 +11:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2020-09-10 21:37:48 +02:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-12 11:45:37 +10:00
2019-05-13 09:31:32 +08:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-30 08:53:17 +08:00
2023-01-09 14:14:59 +00:00
2020-08-20 17:10:33 -07:00
2023-01-09 14:14:59 +00:00
2023-02-01 09:55:21 +00:00
2022-06-09 09:24:30 +10:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-24 09:10:24 +05:30
2019-05-13 09:31:32 +08:00
2021-02-04 11:27:34 +11:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-25 13:50:45 +02:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-12 09:41:39 +10:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2019-05-13 09:31:32 +08:00
2023-01-25 13:50:33 +02:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2021-07-06 10:11:06 +01:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2021-03-09 00:15:14 +02:00
2021-03-06 13:29:35 +02:00
2022-10-18 18:20:10 +01:00
2023-01-09 14:14:59 +00:00
2020-11-02 12:18:48 +05:30
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-12 14:07:42 -03:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-26 17:17:00 -03:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2022-05-12 15:34:35 +01:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2020-10-27 23:42:33 +05:30
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-25 09:45:36 +01:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2021-07-06 10:11:06 +01:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2021-07-06 10:11:06 +01:00
2023-02-03 14:44:40 +11:00
2023-02-03 14:44:40 +11:00
2023-02-03 14:44:40 +11:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2019-05-13 09:31:32 +08:00
2023-01-17 15:50:21 +08:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-20 09:50:24 +08:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2022-06-09 09:24:30 +10:00
2022-02-21 09:45:14 +00:00
2023-01-16 18:06:46 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-12 14:03:26 -06:00
2019-05-13 09:31:32 +08:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2021-07-06 10:11:06 +01:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-20 12:52:49 -06:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-20 09:50:24 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-02-01 07:18:45 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-27 16:32:11 +10:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-24 13:28:21 +10:00
2023-01-24 13:28:21 +10:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2022-09-01 13:04:00 +03:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2021-07-30 13:50:47 -04:00
2023-01-09 14:14:59 +00:00
2023-01-19 15:09:01 -06:00
2020-10-27 23:42:33 +05:30
2023-01-09 14:14:59 +00:00
2020-02-07 17:32:35 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-20 12:52:49 -06:00
2019-05-13 09:31:32 +08:00
2023-02-02 14:39:44 -03:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-30 10:49:08 +02:00
2023-01-09 14:14:59 +00:00
2019-05-13 09:31:32 +08:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-02-06 18:56:28 +04:00
2023-01-20 12:52:49 -06:00
2023-01-20 12:52:49 -06:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00
2023-01-09 14:14:59 +00:00