Re: Am I locking more than I need to? - Mailing list pgsql-general

From Christopher Browne
Subject Re: Am I locking more than I need to?
Date
Msg-id m31xlecwne.fsf@wolfe.cbbrowne.com
Whole thread Raw
In response to Am I locking more than I need to?  (Jeff Davis <jdavis-pgsql@empires.org>)
Responses Re: Am I locking more than I need to?  (Jeff Davis <jdavis-pgsql@empires.org>)
List pgsql-general
The world rejoiced as jdavis-pgsql@empires.org (Jeff Davis) wrote:
> The problem is, when you add the first item to "cart_items" you have to
> do an INSERT with a quantity of 1, but after that you need to do
> UPDATEs. That would seem to create a potential race condition, so in
> order for that to work it would seem you would need to do an ACCESS
> EXCLUSIVE lock on the table to make sure no other process was reading
> the table at the same time.

Various sorts of race conditions are possible in multi-user
multi-tasking systems; what _actual_ problem are you expecting to have
here?

What I would expect is that putting a unique index onto cart_items
based on (cart_id, prod_id) would prevent getting the confusing
situation of having multiple quantities of a single product in a
single cart.

I imagine that is the best thing to try to prevent, and that is
readily done without any "locks" by adding a UNIQUE constraint.  But
perhaps I am imagining a different error condition.

Can you describe the nature of the error condition that you are
thinking about?  That may help indicate what foreign key checks and/or
uniqueness constraints might be worth adding.
--
let name="cbbrowne" and tld="ntlug.org" in String.concat "@" [name;tld];;
http://cbbrowne.com/info/internet.html
This login session:  only $23.95!

pgsql-general by date:

Previous
From: Jeff Davis
Date:
Subject: Am I locking more than I need to?
Next
From: In. Martin Prášek
Date:
Subject: Unix timestamp , unix timestamp with microseconds