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 1oblAn-0005hb-1n for pgsql-sql@arkaria.postgresql.org; Fri, 23 Sep 2022 16:05:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oblAl-0002X9-Un for pgsql-sql@arkaria.postgresql.org; Fri, 23 Sep 2022 16:05:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oblAl-0002Wz-Ly for pgsql-sql@lists.postgresql.org; Fri, 23 Sep 2022 16:05:03 +0000 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oblAj-0006Up-7a for pgsql-sql@lists.postgresql.org; Fri, 23 Sep 2022 16:05:03 +0000 Received: by mail-pj1-x1041.google.com with SMTP id q35-20020a17090a752600b002038d8a68fbso6334567pjk.0 for ; Fri, 23 Sep 2022 09:05:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date; bh=CwN9tcqlVKer7wCqffYFgjwBCpIiCnFg+3rJVcPTdp0=; b=WOrOFH/KR5hrAAFsTKxwXegDe7RxBqODU2q+001pDBl0zEPni5JOul0Z57Eu63w76X 7C769oOCPzFvnU7G14tCTCww457H3rTFoZ4ywbur6Wf9HJmw06zRrn9Tq5xzPESaBiVm nVGhc93im9YLZmVmYh6amd014DhNDO1wqUYaceCS+cyDxCDL0UAIXF30If+YvmO8o26G kkVcWRDbo3/HZKeavrGlxnZHJBIiOdzRrLFuuKyphRXtz9HhKgcU5RrK7w1mvCwuRIFV AKHSyzFsuPtrfipiXJ3k8Ll2xwUCFdM18IWkVFPy5cQiWXHRs0Rm9p1SKXW0lrh6TIEE FCJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date; bh=CwN9tcqlVKer7wCqffYFgjwBCpIiCnFg+3rJVcPTdp0=; b=HUUTwf6sgvpInQ8xwhrPNenStETF2orTpbZY0NN23fMrt4y9NQmx8Kg5F5q9Qs+mQ8 rqeaCDhp8fPvPCsH4n4Vk4Ue+V6m2alym0AdZnJHkQuK/yaSWCeTCya3EnBiUobVcBiN Q4qKMJX5gX8QHUn3/lz5MLCS1Ln0tJjFK4IzTSz3Em/31Vj3KLLY4iXDUJeq4W5+ipOE C92F5JXJlfeQa6gN75WoQ3FbUPWZ0AtR1IMUkJWraaLcOOu06IrFuyhmsHkhVhSfsBaB eTmU1J3CS+Tq2kqZ2qqXZKqTjga2TAAIf9aUdxoE30VB2suDTwtU57hCj+3NTMdX/xRN OvPw== X-Gm-Message-State: ACrzQf1MJrS9Lbl8LcwWWF/eO+oRFipsIHnkDhSYXNova1D8DxIRzKPN V6gR6v8Z/XDZ7dJM+xpOOjvd6acuOTw= X-Google-Smtp-Source: AMsMyM5lvg+czchtA6QTlwHA9lX7fy/9Ky7zYvFiecVlIjXT3s+7KGCAY3f83S/EfA47LAeinXuNMA== X-Received: by 2002:a17:902:e5c9:b0:178:5a6f:6eb8 with SMTP id u9-20020a170902e5c900b001785a6f6eb8mr9488518plf.42.1663949098490; Fri, 23 Sep 2022 09:04:58 -0700 (PDT) Received: from ?IPV6:2601:681:5a00:f790:ab3e:3f8e:a9ab:39f1? ([2601:681:5a00:f790:ab3e:3f8e:a9ab:39f1]) by smtp.gmail.com with ESMTPSA id t127-20020a632d85000000b0042bf6034b3fsm5828793pgt.55.2022.09.23.09.04.57 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Sep 2022 09:04:57 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------4H0qDf4V3tmFkJQumHJysFS9" Message-ID: <7b30c39f-2d36-e540-7494-c41b7e8b7179@gmail.com> Date: Fri, 23 Sep 2022 10:04:56 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: Delete from locking ordering differences Content-Language: en-CA To: pgsql-sql@lists.postgresql.org References: From: Rob Sargent In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------4H0qDf4V3tmFkJQumHJysFS9 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 9/23/22 01:31, Peter Hendriks wrote: > 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! The "optimizatin fence" nature of CTEs appears to be a win in this case.  Why the "order by"?  I assume these are down within a transaction? --------------4H0qDf4V3tmFkJQumHJysFS9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 9/23/22 01:31, Peter Hendriks wrote:
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! 

The "optimizatin fence" nature of CTEs appears to be a win in this case.  Why the "order by"?  I assume these are down within a transaction?

--------------4H0qDf4V3tmFkJQumHJysFS9--