Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()

From: Greg Sabino Mullane <htamfids(at)gmail(dot)com>
To: felix(dot)quintgz(at)yahoo(dot)com
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
Date: 2026-03-08 22:08:01
Message-ID: CAKAnmmKD2kxF6pD3c5eFhJZ+TMogRS9dHcMWbmiZs2Vn=XvZ0g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Mar 8, 2026 at 2:23 PM <felix(dot)quintgz(at)yahoo(dot)com> wrote:

> What makes me suspect it's a lock on the parent table is the word
> "ShareLock" in the logs. A SELECT ... FOR UPDATE statement shouldn't place
> that type of lock on the table it's selecting.
>

This looks 100% like a normal, multi-row deadlock situation. The CONTEXT
shows it is a row-level problem:

CONTEXT: while locking tuple (7,15) in relation “paiyroll_endpoint”

The ShareLocks are on the transaction, because each backend is waiting for
the other to finish their transaction, and thus release the lock(s) it may
have.

If you implement Tom's suggestion, I think you will find that this is a
classic failing to lock the rows in the same order problem.

Cheers,
Greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Igor Korot 2026-03-08 22:10:06 CREATE TABLE fails
Previous Message Andrew Dunstan 2026-03-08 19:44:51 Re: Emitting JSON to file using COPY TO