3

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?

4
  • see this question stackoverflow.com/questions/14179713/locking- table-in-postgresql Commented Feb 23, 2016 at 5:16
  • Yes, it's related. But I can't use unique indexes, and the only way It's locking whole table, or use serializable isolation level. But i want lock inserting only for specific primary key part (user_id) Commented Feb 23, 2016 at 11:51
  • then You can use begin and commit with rollback or this one might be helpful to you stackoverflow.com/questions/7395915/… Commented Feb 23, 2016 at 12:23
  • I'm using Postgresql, and lock table is not good. Commented Feb 23, 2016 at 13:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.