Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzXKf-0015wx-15 for pgsql-general@arkaria.postgresql.org; Mon, 09 Mar 2026 09:55:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vzXKd-00FOt5-2L for pgsql-general@arkaria.postgresql.org; Mon, 09 Mar 2026 09:55:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzXKd-00FOsx-0u for pgsql-general@lists.postgresql.org; Mon, 09 Mar 2026 09:55:23 +0000 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vzXKb-00000001GTu-428B for pgsql-general@lists.postgresql.org; Mon, 09 Mar 2026 09:55:22 +0000 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-439ac15f35fso8027571f8f.0 for ; Mon, 09 Mar 2026 02:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773050120; cv=none; d=google.com; s=arc-20240605; b=Lo4SB4VVYsOTFenF+kBMDdUIoi3Cvhh/XskUGVmIYk35XBnrbDAwg3oIMCNH1ItxVd c7Fu2EPtlhg5lrrR1ruFdL1btgHBKq/7YNhs5VF0AkGxrX5CCgodZi4sIj2l795Ivwph 2uQ/0Ihnfa+WcDzlh+s3jyh4DDJFLsvJ0Kz0Hzf79hsGJxllDK0D3voYFcAzcH1T1UHN jJDQSKc6nWObmPTfnhXUovPQRH+zCQRPJxNy/IIda59JSNpT5OJ2uquf3XGsCA9dEDAv pMz5bXhHkNhhqPEPbcD66Edc2iNB6aZpFnn3CwegfKtSMWj4QqULkU4Z+m2EEDMVGk5D M6nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=JpUexu4rwhhnR8IvVZ3P3VWJGm3aAr6Se2gub2WS7zo=; fh=LtdGd/l7HVkP/rJgR6joie5VrupW6GMRZ+7m8tWeQBU=; b=ZCiReFjsDkI7RnK3iGsSw85RXueyfid2Lt0Kig7MBL3TJRk8dU5EEMy/jtGg+1oWZi gW1e0bZmNEweHItY7Zolj0zSJpI5OAhlD0yZZak+ndZMURBOPxus9OYKGxpP8xjzjcj1 IByqdCZU6yLBjirHuNpcpDckxHmdW4RUJLlSoeg9sbo75XMifho+I1oZp/NT+d7CHB3E ruPdnvTVwWc8tbRrD9I8gP2H2hV5fmvvUJadqMwVpWhQl1BsNr7Sn+pJvhA10VYPnNNk 8WZw8tO+Lsfg9zGMZaobF3ATWTHFa/7dP1QkFDULWnmG0uuqz35Gvj0gcJ12cgWpRJT8 RxIg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773050120; x=1773654920; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=JpUexu4rwhhnR8IvVZ3P3VWJGm3aAr6Se2gub2WS7zo=; b=bsGgR5Aez6LVRKrbbvGbeGXI14Y4jrNvkatuuA9SDO9OewwCsZ8/TMkrUso5M24zU9 JBsvbNHExjUhAHnpdfT14bkzCAcjen7kQNJ8AeYQU2VAg/jXelTKqgHGLUey5U36DXCb 6585ouzF24efxHU/ERn9ZWkstZTPHDzrDmW+1QAyG2J0o30z1IqnqFbDUBGttgFBZWZr FPM8u8+sB7+cnWFOyXj7nzMR5kF+kdtqa37dS9KB9F6qZARRT50yMvz/JACsfETdswVJ phRGQ6iFZBHjh5B8O9aen3SER3kQ1i5iW0zrn0/+OOwyHsQz5UnMCVvj0cnFn/TCq49H mdzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773050120; x=1773654920; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JpUexu4rwhhnR8IvVZ3P3VWJGm3aAr6Se2gub2WS7zo=; b=QLOGhJQOD4OycPCERhcW4gQK2DoGFswzjIgd+A0N/cKNhV5Z8BU5SA2N7rG9JMagkm a7crU1gW82sEH6Fp+f9+NZu4hCxy5mNPu9ZrdjreVxNVMIhELssbVtkpBEU9vKAQTKec m9vkpYtF2CPNdoVxSw5/TyvT9P3ELeqP2Mc03fvLtBqPfLKfB2bBAjWOiZNlWk1t/0kS dagYdetsPAV6NmfdgNdRyHxLV2ssxcv+qe/ZDQyLORKDevJc332vqNFjIe7Kjb1E5ihm MLZ2sfNvR02lvpH1uq4AmUVBW4Rz0I2j9s2TkXOuACHtixg5Tw6o0ooIz155KFrfadxm DgVA== X-Forwarded-Encrypted: i=1; AJvYcCU5NNyEXARF+fI6tCtH4IQKKhttqAVEFi5yCVlIO2y8qO3eRpekiFyC4Ut/U9fGJu1TedN7hkWC21q73QpS@lists.postgresql.org X-Gm-Message-State: AOJu0YyGZJPDP82ZPgABaXkHem5OIwbTiKzdi6jSayTSPJEnJWPnczof Q0xIVLjZk+hMJrlw+1UE//n9wpFAEm8Pb9IrvMnWNAvwSUPFgTZzQYppv0QkE5qwcftDyVTKH2V PWwlCh0CaWd9NCgCTkmK4T7CjxRqamfI= X-Gm-Gg: ATEYQzyZa8Hf6GhnvikDP6cZD/HcAztmRVMusUyBtTv22Lohl7tevLpiK3ODRzDjkIa K2abRsF1Q1HAW62+at5CWrg1Hr323gSDl32fb0MtOHq+zmo+pSYzrOZcrI2JcNWK8GmNq4gqa90 ilW7NeKk2b8vkw1rkfl9HAtHl+5qbXdKyaylfMSvzjojm/99kIE21+P0E6yVGZKAAyPLV+f+w+d JoD5LxgUY5oRwI+mgywsloFrFvrA5yuXZC781cC5Nk3x8MHBfQ6t1Of7/S0Yh7bLKEnViX2HSGU B5FS0YU= X-Received: by 2002:a05:6000:1447:b0:439:beba:300 with SMTP id ffacd0b85a97d-439da31e274mr18843016f8f.1.1773050119561; Mon, 09 Mar 2026 02:55:19 -0700 (PDT) MIME-Version: 1.0 References: <1164079167.6346688.1772982934392@mail.yahoo.com> <1656643760.2116109.1772994207524@mail.yahoo.com> In-Reply-To: From: Shaheed Haque Date: Mon, 9 Mar 2026 09:55:08 +0000 X-Gm-Features: AaiRm51nkZuoBS8cvetUHNHnNN1IUfjTUT8_-1vO5xay3fKTA_dJkIjJLVjli8c Message-ID: Subject: Re: Unexpected deadlock across two separate rows, using Postgres 17 and Django's select_for_update() To: Greg Sabino Mullane Cc: felix.quintgz@yahoo.com, pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000008ba4ac064c9466cc" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000008ba4ac064c9466cc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thank you for the replies. On Sun, 8 Mar 2026 at 22:08, Greg Sabino Mullane wrote= : > On Sun, Mar 8, 2026 at 2:23=E2=80=AFPM 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 pla= ce >> 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 =3D 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 =E2=80=9Cpaiyroll_endpoin= t=E2=80=9D > > > The ShareLocks are on the transaction, because each backend is waiting fo= r > the other to finish their transaction, and thus release the lock(s) it ma= y > 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 > > --0000000000008ba4ac064c9466cc Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thank you for the replies.

On Sun, 8 Mar 2026 at 22:08, Greg Sabino Mull= ane <htamfids@gmail.com> wr= ote:
On Sun, Mar 8, 2026 at 2:23=E2=80=AFPM <felix.quintgz@yahoo.com> w= rote:
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 t= hat type of lock on the table it's selecting.

This looks 100% like a normal, multi-row de= adlock situation. The CONTEXT shows it is a row-level problem:
<= /blockquote>

I'm not sure I understand. The two quer= ies are referencing separate, single rows in the child table (primary keys = payroll_endpoint.id =3D 1 and 2)= , so where does the multi-row bit come in?=C2=A0Is it because the two paren= t tables are also being locked, in possibly different orders?
=C2=A0
<= div dir=3D"ltr">

CONTEXT: while locking tuple (7,15) in relation =E2= =80=9Cpaiyroll_endpoint=E2=80=9D

The ShareL= ocks 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 wil= l find that this is a classic failing to lock the rows in the same order pr= oblem.

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

Thanks, Shaheed
=C2=A0
Cheers,
Greg

--0000000000008ba4ac064c9466cc--