public inbox for [email protected]  
help / color / mirror / Atom feed
From: Shaheed Haque <[email protected]>
To: Greg Sabino Mullane <[email protected]>
Cc: [email protected]
Cc: [email protected]
Subject: Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
Date: Mon, 9 Mar 2026 09:55:08 +0000
Message-ID: <CAHAc2jcTLhGPFYNUC++e9HXG2kt-KuCu9tuvGpd-2Hi+ry4yDg@mail.gmail.com> (raw)
In-Reply-To: <CAKAnmmKD2kxF6pD3c5eFhJZ+TMogRS9dHcMWbmiZs2Vn=XvZ0g@mail.gmail.com>
References: <CAHAc2jd=x-6hM=CoEYOaJ6ST5p-nGkTxDmh=PrwxacnkUp=31A@mail.gmail.com>
	<[email protected]>
	<CAHAc2jeME7KPPP3TgP9qWC5LMTOs4gYTEJ1MBDwnj7J2EvJTdA@mail.gmail.com>
	<[email protected]>
	<CAKAnmmKD2kxF6pD3c5eFhJZ+TMogRS9dHcMWbmiZs2Vn=XvZ0g@mail.gmail.com>

Thank you for the replies.

On Sun, 8 Mar 2026 at 22:08, Greg Sabino Mullane <[email protected]> wrote:

> On Sun, Mar 8, 2026 at 2:23 PM <[email protected]> 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:
>

I'm not sure I understand. The two queries are referencing separate, single
rows in the child table (primary keys payroll_endpoint.id = 1 and 2), so
where does the multi-row bit come in? Is it because the two parent tables
are also being locked, in possibly different orders?



>
> 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.
>

I'm not seeing "Tom's suggestion". Is there a way to specify that the
parent tables need not be locked? Perhaps by omitting them from the query?

Thanks, Shaheed


>
> Cheers,
> Greg
>
>


reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update()
  In-Reply-To: <CAHAc2jcTLhGPFYNUC++e9HXG2kt-KuCu9tuvGpd-2Hi+ry4yDg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox