Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

415
Views
Can Django's select_for_update deadlock when used on the same record twice?

I have some code that would be a lot simpler with nested calls to select_for_update(), but I'm afraid I'm setting myself up for a deadlock.

Example:

with transaction.atomic():
    uu1 = UniUser.objects.select_for_update().get(pk=uupk)
    with transaction.atomic():
        uu2 = UniUser.objects.select_for_update().get(pk=uupk)  # why no deadlock here??
        uu2.bio = 'i like pizza'
        uu2.save()
    uu1.bio = 'i like pie'
    uu1.save()

I would have expected the second select_for_update() to deadlock since it's trying to lock an already locked record, but this code runs just fine and we end up with 'i like pie' in the bio. Ditto if I remove the inner transaction and just lock the same record twice in the same transaction.

Why is there no deadlock here? Is this safe code?

In case the answer is database specific, I'm using Postgres.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The lock is acquired and held by your transaction.

When you attempt to grab it again, nothing happens as your transaction already hods a lock on that row.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!