#28676: Using select_for_update with next save() in multiple threads stucks
-------------------------------------+-------------------------------------
     Reporter:  M1ha Shvn            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  django psycopg       |             Triage Stage:
  PostgreSQL select_for_update       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by M1ha Shvn):

 Replying to [comment:2 M1ha Shvn]:
 > Replying to [comment:1 Tim Graham]:
 > > Can you explain why Django is at fault?
 >
 > 1) According to  [https://www.postgresql.org/docs/9.2/static/monitoring-
 stats.html PostgreSQL docs] state "idle in transaction" means that query
 was executed and control returned to backend code, but it hasn't committed
 the transaction yet. But save() is stuck somewhere (it is incorrect). If I
 place print(123) after it - it will not print anything. So PostgreSQL
 ended its work, but save() method stopped, waiting for something and not
 continuing code execution. The expected behavior of django here is to
 leave save() method, than leave "with transaction.atomic" context manager
 and commit transaction, so other transaction can go in. The proof is
 replacing save() method with QuerySet.update(). As you can see on the
 screen, it generates perfectly the same SQL, but works fine without
 stucking.
 > 2) The second strange factor is duplicate select query, generated inside
 save() method. As you can see from above and screen, it's the same query
 without "FOR UPDATE". But there is no need in this query - conversation
 data has been already selected by "SELECT ... FOR UPDATE". According to
 [https://docs.djangoproject.com/en/1.10/ref/models/instances/#what-
 happens-when-you-save the docs], save() should do only one query - INSERT
 or UPDATE (UPDATE in this situation) without any SELECT queries.
 > P. s. The only reason for save() of doing second SELECT from the docs is
 getting pk value. But it is not INSERT query, pk is already defined.
 Moreover force_update=True doesn't change anything.
 >
 > Replying to [comment:1 Tim Graham]:
 > > propose a change to fix it
 > I'm not quite good in django inner code, I've tried debugging where,
 inside save() method, the problem is, but haven't succeeded

 1) Futher investigation shows that [https://github.com/Suor/django-
 cacheops django-cacheops] invalidated_update() also doesn't work. Seems
 it's bug of this extension.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28676#comment:3>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.c6469564dcd553f2ef9c6248f2dbb697%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to