Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
get_or_create can still cause IntegrityError
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tomasz Zielinski  
View profile  
 More options May 7 2011, 6:28 am
From: Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>
Date: Sat, 7 May 2011 03:28:27 -0700 (PDT)
Local: Sat, May 7 2011 6:28 am
Subject: get_or_create can still cause IntegrityError

I think that get_or_create is still broken, despite this fix:
http://code.djangoproject.com/ticket/6641

The details, along with a working solution, are in my answer here:
http://stackoverflow.com/questions/2235318/how-do-i-deal-with-this-ra...
I'm not sure if the solution is optimal, trying to INSERT first might be
overally more expensive, but I think that something should be done with
get_or_create.

Best,

Tomasz


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Cramer  
View profile  
 More options May 7 2011, 1:09 pm
From: David Cramer <dcra...@gmail.com>
Date: Sat, 7 May 2011 10:09:12 -0700 (PDT)
Local: Sat, May 7 2011 1:09 pm
Subject: Re: get_or_create can still cause IntegrityError
We hit this same issue in Postgres (it's definitely not MySQL
specific). I'm unsure of the solution or precise conditions we're
hitting it in, but I think by default we use READ COMMITTED.

On May 7, 3:28 am, Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>
wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Cramer  
View profile  
 More options May 7 2011, 2:03 pm
From: David Cramer <dcra...@gmail.com>
Date: Sat, 7 May 2011 11:03:40 -0700 (PDT)
Local: Sat, May 7 2011 2:03 pm
Subject: Re: get_or_create can still cause IntegrityError
Would it help (in PG-world) at least if the selects where in
savepoints as well?

On May 7, 10:09 am, David Cramer <dcra...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Odp: Re: get_or_create can still cause IntegrityError" by Tomasz Zielinski
Tomasz Zielinski  
View profile  
 More options May 8 2011, 7:30 am
From: Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>
Date: Sun, 8 May 2011 04:30:58 -0700 (PDT)
Local: Sun, May 8 2011 7:30 am
Subject: Odp: Re: get_or_create can still cause IntegrityError

W dniu sobota, 7 maja 2011, 20:03:40 UTC+2 użytkownik David Cramer napisał:

> Would it help (in PG-world) at least if the selects where in
> savepoints as well?

In case you asked me - I don't know, I don't use PG on a daily basis...

Anyway, let me list some possible solutions to launch the discussion:
1. Leave it as is, and update the documentation to warn users that
get_or_create is problematic for concurrent writes unless used in READ
COMMITED isolation level
2. Switch to my code and update the documentation to warn users
that get_or_create commits the open transaction
3. Combine 1&2 and somehow allow two modes of operation for get_or_create,
either based on a flag, or based on retrieved isolation level - or maybe add
some get_or_create2 ?
4. Remove get_or_create, because it cannot be fixed to gracefully handle all
common cases

Re savepoint-based solution, I don't list it because I don't "feel" how it
should work.

Tomasz


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "get_or_create can still cause IntegrityError" by Cal Leeming [Simplicity Media Ltd]
Cal Leeming [Simplicity Media Ltd]  
View profile  
 More options May 9 2011, 1:05 pm
From: "Cal Leeming [Simplicity Media Ltd]" <cal.leem...@simplicitymedialtd.co.uk>
Date: Mon, 9 May 2011 18:05:17 +0100
Local: Mon, May 9 2011 1:05 pm
Subject: Re: Re: get_or_create can still cause IntegrityError

Hi guys,

I spent literally *months* trying to find the best way to resolve this
situation. On our high performance sites + backends (around 120qps, 50/50
split between read and write) we were getting IntegrityError raised by
get_or_create (which we now refer to as Object Collisions).

We are able to trigger the issue by having two scripts attempt to
get_or_create() a new object at the same time (it takes a few loop
iterations until it reproduces itself).

When get_or_create() is called, it'll raise a duplicate key exception, but
if you then attempt to do a .get() on the row using the same args/kwargs
straight away afterwards, you'll notice it returns an empty result. This (we
think) is because when you create a row within a transaction, although the
row isn't there, it will still raise a duplicate key error until the row is
either commited or rolled back.

Eventually, we created a modified get_or_create() which *reduce* the chance
of it happening:

http://pastebin.com/X1nvk0uq

However, we are still left with a final problem, which is we either put a
sleep() in the code (eww!) to increase the chance of a successful query, or
you raise an ObjectNotReady exception (custom), and then add logic into your
code to deal with this occurrence. The other option is that you re-factor
your code so that the time between commits on large chunks of code are as
small as possible, reducing the chance of this error. But, from what we can
tell, you can only "reduce" the chance of raising an ObjectNotReady, you
can't get rid of it entirely. (unless you get rid of transactions entirely
lol)

2011/5/8 Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Odp: Re: Re: get_or_create can still cause IntegrityError" by Tomasz Zielinski
Tomasz Zielinski  
View profile  
 More options May 11 2011, 6:40 pm
From: Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>
Date: Wed, 11 May 2011 15:40:23 -0700 (PDT)
Local: Wed, May 11 2011 6:40 pm
Subject: Odp: Re: Re: get_or_create can still cause IntegrityError

W dniu poniedziałek, 9 maja 2011, 19:05:17 UTC+2 użytkownik SleepyCal
napisał:

> Hi guys,

> I spent literally *months* trying to find the best way to resolve this
> situation. (...)

Have you checked my StackOverflow answer I linked in the top post? It
*completely* removes the problem for MySQL/InnoDB,
on REPEATABLE READ isolation level. If you use Postgres with comparable
isolation level then my solution might also work for you.
For READ COMMITED level the problem shouldn't occur anyway.

Tomasz


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomasz Zielinski  
View profile  
 More options May 17 2011, 7:16 am
From: Tomasz Zielinski <tomasz.zielin...@pyconsultant.eu>
Date: Tue, 17 May 2011 04:16:41 -0700 (PDT)
Local: Tues, May 17 2011 7:16 am
Subject: Odp: Re: Re: get_or_create can still cause IntegrityError

There's a related, ongoing discussion here:
http://code.djangoproject.com/ticket/13906
It looks like the general attitude is that MySQL backend should switch to
READ COMMITED isolation level,
which would be the simplest solution.

Tomasz


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »