Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update() - Mailing list pgsql-general

From Greg Sabino Mullane
Subject Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
Date
Msg-id CAKAnmmKD2kxF6pD3c5eFhJZ+TMogRS9dHcMWbmiZs2Vn=XvZ0g@mail.gmail.com
Whole thread
In response to Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()  (felix.quintgz@yahoo.com)
Responses Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
List pgsql-general
On Sun, Mar 8, 2026 at 2:23 PM <felix.quintgz@yahoo.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

pgsql-general by date:

Previous
From: felix.quintgz@yahoo.com
Date:
Subject: Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
Next
From: Igor Korot
Date:
Subject: CREATE TABLE fails