Re: Am I locking more than I need to?

Поиск
Список
Период
Сортировка
От Jeff Davis
Тема Re: Am I locking more than I need to?
Дата
Msg-id 1085122227.2274.780.camel@jeff
обсуждение исходный текст
Ответ на Re: Am I locking more than I need to?  ("Ed L." <pgsql@bluepolka.net>)
Ответы Re: Am I locking more than I need to?  ("Ed L." <pgsql@bluepolka.net>)
Список pgsql-general
> I'm not sure what potential race condition you see since you haven't said
> much about how your transactions fit in here.  But I would suggest you go
> with your first design and don't worry about any explicit locking
> unless/until it clearly becomes a problem.  I've built numerous things
> similar to this, and in my experience, PostgreSQL is very good about
> managing the locking in an intelligent manner if your transactions are
> reasonably grouped.
>
> HTH.
>

client1=> BEGIN;
-- test to see if there's already a record there. If so, UPDATE
--   if not, INSERT
client1=> SELECT * from cart_items where cart_id=X AND prod_id=Y;
-- no record, so INSERT
client1=> INSERT into cart_items(cart_id,prod_id,quantity)
VALUES(X,Y,1);
client2=> SELECT * from cart_items where cart_id=X AND prod_id=Y;
-- still no record, since client1 didn't commit yet
client1=> COMMIT;
-- now client2 needs to insert
client2=> INSERT into cart_items(cart_id,prod_id,quantity)
VALUES(X,Y,1);
client2=> COMMIT;
-- Oops, now there are two records in there.

That's the condition I was worried about.

Thanks,
    Jeff Davis



В списке pgsql-general по дате отправления:

Предыдущее
От: Mike Nolan
Дата:
Сообщение: Re: Data change logs
Следующее
От: Jeff Davis
Дата:
Сообщение: Re: Am I locking more than I need to?