Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcaSU-0002Tr-Eq for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Jun 2019 19:04:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hcaST-0003xH-9H for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Jun 2019 19:04:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcaSS-0003uB-UB for pgsql-hackers@lists.postgresql.org; Sun, 16 Jun 2019 19:04:52 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hcaSL-0005AL-Ek for pgsql-hackers@lists.postgresql.org; Sun, 16 Jun 2019 19:04:52 +0000 Received: by mail-qk1-x741.google.com with SMTP id a27so4930977qkk.5 for ; Sun, 16 Jun 2019 12:04:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=VC/y/i8GxWqGFNPBiioV+5MRuRH9vioMEGQWmTIEAFU=; b=okh4dOBNtZMcJptmQPAJgZRIMmdJjMdZ5WxXuS71yWgj+NfszSgrBi3vN5nZUh7oSO 5UYJjs7/KkkGmPUWE0z+XPjXQ0nKhdlaUNXJYBZxB2BpCHw38AIIZVbN4jKausgGqfkf iU43zzXLBS5ChkPprAjdoYfG9QOvsG7pSEcxJesKtGOV+EZXymEOTlDm3321l4P8vp7S NV7lXoz6tGjyphcVUXu4Y2aGzxu2BRfXaWRUuL1ldF/VhqIaGPKlBD/QLItYVYkVII+Z WnmrEVreHS6lxdOCzXQYvypwvMPBhFKwqVZDCQsYloF+yc4krsivIQOJSc47ogPWW47d /E2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=VC/y/i8GxWqGFNPBiioV+5MRuRH9vioMEGQWmTIEAFU=; b=WXpDO9p9b8lPgZ6105KYR5TJDUpqW4Y2oiHN6YCY/GnDQ0tiqGr+5H1KDKivJD7Cuf ihYjOkpLrrodWT+WewFOg989rBfiY3qaDe+OBOI04/RKQNRx79EJ2hZJU33go+Qkr5C3 7nFuNm03kTrDgL+Wy8GhSDOoddavSZWIwiKBN/qewYzl9A1S0Sh5zbiGCS7N/oRO+3AR icrEWYR9jzvVh9uZNbwv8naeRYL3LBoES/ueVUqERfqZndPWnOQarjN1NeihzOc9i3Qf IPObvo40twuxqGKx5SAtuogwkPAqx+UNwjT5b/keQuCoALDVyjBjXFxwIb8W781eoxKi 9xmg== X-Gm-Message-State: APjAAAUwRiAk7pF0HiGcVD0a0FTKZYWVQMUqWy6bDvdTuvzmaWjCRABN +4mJxiA+JWT6kMwt6w0F5DLKtA== X-Google-Smtp-Source: APXvYqwMbsVuwFPam0a3krdx4kA8/rtoeP9GIMDnlGCBm566SMSLAWZnq+TdFJEMcexddd2gCJV77w== X-Received: by 2002:a05:620a:13b9:: with SMTP id m25mr23878584qki.246.1560711883379; Sun, 16 Jun 2019 12:04:43 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([179.56.48.227]) by smtp.gmail.com with ESMTPSA id a139sm5222881qkb.48.2019.06.16.12.04.42 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 16 Jun 2019 12:04:43 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 4779D120870; Sun, 16 Jun 2019 15:04:41 -0400 (-04) Date: Sun, 16 Jun 2019 15:04:41 -0400 From: Alvaro Herrera To: Oleksii Kliukin Cc: Tom Lane , pgsql-hackers@lists.postgresql.org Subject: Re: pgsql: Avoid spurious deadlocks when upgrading a tuple lock Message-ID: <20190616190441.GA30993@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190615224713.GA9414@alvherre.pgsql> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Jun-15, Alvaro Herrera wrote: > But that's not the danger ... with the current coding, it's initialized > to false every time through that block; that means the tuple lock will > never be skipped if we jump back to l3. So the danger is that the first > iteration sets the variable, then jumps back; second iteration > initializes the variable again, so instead of skipping the lock, it > takes it, causing a spurious deadlock. So, I'm too lazy today to generate a case that fully reproduces the deadlock, because you need to stall 's2' a little bit using the well-known advisory lock trick, but this one hits the code that would re-initialize the variable. I'm going to push the change of lifetime of the variable for now. setup { drop table if exists tlu_job; create table tlu_job (id integer primary key, name text); insert into tlu_job values(1, 'a'); } teardown { drop table tlu_job; } session "s0" setup { begin; set deadlock_timeout=1} step "s0_fornokeyupdate" { select id from tlu_job where id = 1 for no key update; } step "s0_update" { update tlu_job set name = 's0' where id = 1; } step "s0_commit" { commit; } session "s1" setup { begin; set deadlock_timeout=1} step "s1_for_key_share" { select id from tlu_job where id = 1 for key share; } step "s1_for_update" { select id from tlu_job where id = 1 for update; } step "s1_rollback" { rollback; } session "s2" setup { begin; set deadlock_timeout=1} step "s2_for_key_share" { select id from tlu_job where id = 1 for key share; } step "s2_for_share" { select id from tlu_job where id = 1 for share; } step "s2_rollback" { rollback; } session "s3" setup { begin; set deadlock_timeout=1} step "s3_update" { update tlu_job set name = 'c' where id = 1; } step "s3_rollback" { rollback; } permutation "s1_for_key_share" "s2_for_key_share" "s0_fornokeyupdate" "s2_for_share" "s0_update" "s0_commit" "s1_rollback" "s2_rollback" "s3_rollback" -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services