Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1odtx6-0001tl-Kp for pgsql-sql@arkaria.postgresql.org; Thu, 29 Sep 2022 13:51:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1odtx5-0007LL-9d for pgsql-sql@arkaria.postgresql.org; Thu, 29 Sep 2022 13:51:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1odtx4-0007L5-Si for pgsql-sql@lists.postgresql.org; Thu, 29 Sep 2022 13:51:47 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1odtx2-0002DG-0a for pgsql-sql@lists.postgresql.org; Thu, 29 Sep 2022 13:51:45 +0000 Received: by mail-pg1-x536.google.com with SMTP id s26so1505138pgv.7 for ; Thu, 29 Sep 2022 06:51:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindloops-nl.20210112.gappssmtp.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date; bh=sgkKcMTujMJ5KgHzngcNuwaXUuDgHWLxZGRsX8CaeKY=; b=bUNEjysNYxP0Tl+0GIkwdWjYNGrWhGoJK8ja8eaKcwYZgxDY4iPHC8NO2v2/SsA7Cr fq4A9Y0veCYusFD+6jmgRD3S6szYF3ieNkENLs6GZZ7na7ZVa5ZHTHCQeOq49WMRFMlB Ytze8R/EEQL+ZfOPiX+zbDKa7OjRe+Y2bg8wjuN4z16bBr3Ui19bzH+9DT9p9Ib8MQ+D 7zEl+MPYsqiPyst+3qFM/QS2mTGxILSfU3tlIrT8EXLnCnGkJm/gCU+h5pRa+oDJ83rM jPDr9dbC5RnDaSjWPS3ZVEbkOzqKitC/wvQoCKsTAPpgFiDBFInpBPqafArNykShDLnq X1Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date; bh=sgkKcMTujMJ5KgHzngcNuwaXUuDgHWLxZGRsX8CaeKY=; b=BMPF94JQD9B5O1BIGhmCWQJq7lC8Vb22O2CUjji19ASsQ7LH6Tj05ikhgw6s3bEdWU Oncf6jA8Ab5tIAW0mU95h+dDcJYQmEP6ZHAvrB9fKceKj9yjFCOS6jWkJQ/CTWrEg0J2 BJtFv+hhrMayePpJV/uTm7Lc2kOgsfNImLzgNgpHbQM9e9Vb19JQQv4zKFSAWP4HUr3N AiBbOX+zYKBv6qTeBBbM4nYPRTaZbvV5hwffsXik57+L4PFoni8UHdADUNCb6cb8kBxe Ivyw4JNO4KnpSrPnXxmTC4NovmJ/iBSLacz+lxDojKt6tov+xD1w+0gHns6Ncs/Y/4TY lHhg== X-Gm-Message-State: ACrzQf3jKhpSeOP3EZR/mnUoh6/Y3BtvVLjGIv6DRqG/7JZHhWJiA7Hx FZy4JARz8S4AMvpyoXPrNekAbEBB0A/lsXS3bRbqGwjlEeav0g== X-Google-Smtp-Source: AMsMyM72gW+hZ7qtrl0Ozw6MnG0jINMIO5WWU67hqXQQZ+fXnOBNIf2cX3ro1VQFfUkNGJXe73Rq2ufwUTEolRql0Q0= X-Received: by 2002:a05:6a00:b92:b0:545:e8c5:8945 with SMTP id g18-20020a056a000b9200b00545e8c58945mr3625991pfj.56.1664459502270; Thu, 29 Sep 2022 06:51:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Peter Hendriks Date: Thu, 29 Sep 2022 15:51:31 +0200 Message-ID: Subject: Re: Delete from locking ordering differences To: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006054e205e9d12df5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006054e205e9d12df5 Content-Type: text/plain; charset="UTF-8" Yes, we do not really care about order, but to prevent locking issues. Multiple transactions may run this query at the same time. It should never contend because of the skip locked, so maybe we should try this without an order by too. We can not get this query to fail in test so far, just in production, wo we are bit hesitant to change now that it is finally working... Our guess is to why this makes such a difference is that the delete statement in postgres does not guarantee ordering, so maybe the optimizer makes different choices than expected. Asking the question here, so maybe someone with more understanding can explain why we need the CTE. Op vr 23 sep. 2022 om 09:31 schreef Peter Hendriks : > We are wondering if anyone can explain the difference we are having in > production with the following queries: > > DELETE FROM store > WHERE id IN ( > SELECT id FROM store > FOR UPDATE SKIP LOCKED > ORDER BY ID > LIMIT 1000 > ) > RETURNING id, payload > > This query is sometimes executed with high concurrency, and then can hang > indefinitely, we assume because of a locking problem that postgresql is not > detecting as a deadlock. > > This alternative query does not have the hanging problem: > > WITH store_ids AS ( > SELECT id FROM store > FOR UPDATE SKIP LOCKED > ORDER BY ID > LIMIT 1000 > ) > DELETE FROM store s > USING store_ids si > WHERE s.id = si.id > RETURNING s.id, s.payload > > Can anyone explain why the first query is expected to fail (hang), and the > second query does not have this problem? We would be interested in more > understanding on this. Thanks! > -- Met vriendelijke groet, Peter Hendriks Mindloops B.V. T. +31 (0)6 37 23 26 73 E. peter@mindloops.nl KvK: 85061921 --0000000000006054e205e9d12df5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Yes, we do not really care about order, but to prevent loc= king issues. Multiple transactions may run this query at the same time. It = should never contend because of the skip locked, so maybe we should try thi= s without an order by too. We can not get this query to fail in test so far= , just in production, wo we are bit hesitant to change now that it is final= ly working... Our guess is to why this makes such a difference is that the = delete statement in postgres does not guarantee ordering, so maybe the opti= mizer makes different choices than expected. Asking the question here, so m= aybe someone with more understanding can explain why we need the CTE.
=
Op vr = 23 sep. 2022 om 09:31 schreef Peter Hendriks <peter@mindloops.nl>:
We are wondering if anyone can exp= lain the difference we are having in production with the following queries:=

DELETE FROM store
WHERE id IN (
=C2= =A0 SELECT id FROM store=C2=A0
=C2=A0 FOR UPDATE SKIP LOCKED=C2= =A0
=C2=A0 ORDER BY ID
=C2=A0 LIMIT 1000
)
RETURNING id, payload

This query is someti= mes executed with high concurrency, and then can hang indefinitely, we assu= me because of a locking problem that postgresql is not detecting as a deadl= ock.

This alternative query does not have the hang= ing problem:

WITH store_ids AS (
= =C2=A0 SELECT id FROM store=C2=A0
=C2=A0 FOR UPDATE SKIP LOCKED= =C2=A0
=C2=A0 ORDER BY ID
=C2=A0 LIMIT 1000
=
)
DELETE FROM store s
USING store_ids si=C2= =A0
WHERE s.id =3D = si.id
RETURNING s.id, s.payload

Can anyone explain why the first query is expected to fail = (hang), and the second query does not have this problem? We would be intere= sted in more understanding on this. Thanks!=C2=A0


--
= Met vriendelijke groet,

Peter Hendriks
Mindloops B.V.

T. +31 (0)6 37 23 26 73
E.=C2=A0peter@mindloops.nl
KvK:=C2=A085061921
<= /div>
--0000000000006054e205e9d12df5--