I have a table with structure:
CREATE TABLE public.users_storages
(
user_id bigint NOT NULL,
item_type integer NOT NULL,
count integer,
CONSTRAINT users_storages_pkey PRIMARY KEY (user_id, item_type)
)
And I have such query: INSERT INTO users_storages SELECT $1,$2,$3 WHERE (SELECT sum(count) FROM users_storages WHERE user_id = $1 GROUP BY user_id) <= $4
But, such query have a race condition, how i can lock inserting rows for user_id = $1?