On 5 syys, 10:09, lucky <[email protected]> wrote: > Session A Session B > CREATE TABLE t (k INT, v INT, PRIMARY KEY (k)); > > SET autocommit=0; SET autocommit=0; > time > | SELECT * FROM t; > | empty set > | INSERT INTO t VALUES (1, 2); > | > v SELECT * FROM t; > empty set > > SELECT * FROM t LOCK IN SHARE MODE; > (it blocks here) > COMMIT; > --------------------- > | 1 | 2 | > --------------------- > 1 row in set > > SELECT * FROM t; > empty set
This is going to be a problem. The .save() logic is implemented as SELECT, if found UPDATE, else INSERT. There also seems to be a possibility of a deadlock. These comments contain more information about this problem: https://code.djangoproject.com/ticket/13906#comment:22, https://code.djangoproject.com/ticket/13906#comment:25. The proposal in that ticket is to use FOR UPDATE select, but it seems FOR SHARE could be better. Maybe the deadlock problem is solved by this, too. There are two ways to solve the .save() issue: either use SELECT FOR SHARE in .save() when trying to save an object fetched with FOR SHARE, or change the logic to UPDATE, if nothing updated, INSERT. The latter choice is implemented in https://code.djangoproject.com/ticket/16649 Unfortunately we have documented explicitly that .save() does SELECT, if found UPDATE, else INSERT. > > UPDATE t SET v=3 WHERE k=1; > 1 row affected > > SELECT * FROM t; > > --------------------- > | 1 | 3 | > --------------------- > 1 row in set Apart of the above .save() problem this looks promising. - Anssi -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
