#27193: ORDER BY clause not included in subqueries using select_for_update()
-------------------------------+--------------------------------------
     Reporter:  sqwishy        |                    Owner:  nobody
         Type:  Uncategorized  |                   Status:  new
    Component:  Uncategorized  |                  Version:  1.9
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Changes (by sqwishy):

 * status:  closed => new
 * resolution:  needsinfo =>


Comment:

 The documentation at the following link may suggest that rows are locked
 while a query is running rather than all at once atomically; see
 particularly the second paragraph under "13.2.1. Read Committed Isolation
 Level" https://www.postgresql.org/docs/current/static/transaction-iso.html
 #XACT-READ-COMMITTED

 This is a bit of code where adding the order by seems to prevent
 deadlocks.

 {{{#!python
 from django.db import transaction
 from django.contrib.auth.models import Group
 from django.db.models import F

 def setup():
     for i in range(20):
         Group.objects.get_or_create(name='test_27193_%i' % i)

 def test():
     for _ in range(20):
         with transaction.atomic():
             Group.objects.filter(
 id__in=Group.objects.filter(name__startswith='test_27193').select_for_update()
             ).update(name=F('name'))
             #Group.objects.filter(
             #
 
id__in=Group.objects.filter(name__startswith='test_27193').select_for_update().order_by('id')[1:]
             #).update(name=F('name'))

 setup()
 test()
 }}}

 I have that saved to a file aptly named `script.py` and run it 20 times at
 once (I'm sure there is a less ugly way to do this):

 {{{
 seq 20 | parallel --ungroup -j 20 "echo {}; ./manage.py shell --plain <
 script.py"
 }}}

 Doing this with the script as shown, exceptions are raised in python about
 deadlocks and PostgreSQL logs about it. If you comment out that first
 query and uncomment the second one everything runs fine. The slice there
 is just to convince django not to reset the order-by value in the
 queryset.

--
Ticket URL: <https://code.djangoproject.com/ticket/27193#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.027283d40a7f4d43747d707af6a5e23b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to