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 1hanZO-0000hc-Mo for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jun 2019 20:40:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hanZM-0002VR-3X for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jun 2019 20:40:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hanZL-0002RN-Nq for pgsql-hackers@lists.postgresql.org; Tue, 11 Jun 2019 20:40:35 +0000 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hanZI-00067M-EZ for pgsql-hackers@lists.postgresql.org; Tue, 11 Jun 2019 20:40:34 +0000 Received: by mail-qt1-x841.google.com with SMTP id 33so8072149qtr.8 for ; Tue, 11 Jun 2019 13:40:32 -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=3iWbr98hRHXUk7xmmxH6AQbDGN0esd6Jjd8WFpjno24=; b=l/eVMb7bHzJrlnb5FRHn79M02BWTokaHEPtB3RhxlE+1MXu/Y9yPdBEGab/t7lZixx m51cf4wn8t3n+MUel2Q5kYtuKcxj7r9eui30QyA7T1czto+2FO3ifETQXmWJmkDAhuEx VFrIYOFyFRGL54CecGeSDWUmkn5N1pHmug1qaheJrq1lGuZcw9jARaLWiaC9YOVZoRuG 3czj77L+4YwHJleVpWK03Naa9vHOhb0WNQATPJvGaL6+rNYWNX2SozQ5JrKpuCqE4Cgh UrTckrIo/QytPzsH9/x1KTsDEV24+FZn6TKqGxlPiEMwPB/aI9goaYYorMzexj97QQBg qJ/g== 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=3iWbr98hRHXUk7xmmxH6AQbDGN0esd6Jjd8WFpjno24=; b=BBzdm6v8S4wISYPV2UBp4KLJ0u3JcUjqZ8OtzWIbzspjUlwI9wm1Cp3dr7ZxTUzWFl XGD6jjALYGwF5S7eGqL7pCdyS/bplj+Zd/BXfI5PyEttG123tdPJoOjJzCgdh5a41JUi LnEgBEq/zaPxYC1vFIqsxo7LEBOAuQ+/7gu8u1tSW8e8AF1e8u9bO95sNKul10rwzTAT /Ff9v5gLAsOnUmfxb5OvQTnTBpOW7C+LjbCBr7+7rn4jI3evxzBFPbt5p/wGpp4FYxSH sM+GvhCTVTSeeor5TbarJpD6cjQ+zcI8SsFaJtdtOR2RJm7jRp9n0wHrpPX9lTOcb8Vb t6ig== X-Gm-Message-State: APjAAAXe6rAKHXQOfOZfZlbTpq/fpVGEtSzES74agFSfXsCbZhDgFzf/ RDUtIkfZDK+PubTRZt/9Ac+yNA== X-Google-Smtp-Source: APXvYqxCgT+Lu6lTsS8Oy5+GDVZPYpd/ABbrklOgjK1smi8WYg7RbwOrSiOLGo3NOreVbUP4jcNYeQ== X-Received: by 2002:aed:3faa:: with SMTP id s39mr6646282qth.335.1560285631093; Tue, 11 Jun 2019 13:40:31 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([177.154.139.203]) by smtp.gmail.com with ESMTPSA id d25sm6666497qko.96.2019.06.11.13.40.30 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 11 Jun 2019 13:40:30 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 003B0120CB3; Tue, 11 Jun 2019 16:40:25 -0400 (-04) Date: Tue, 11 Jun 2019 16:40:25 -0400 From: Alvaro Herrera To: Oleksii Kliukin Cc: PostgreSQL Hackers , Chris Travers Subject: Re: upgrades in row-level locks can deadlock Message-ID: <20190611204025.GA10702@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello On 2019-May-22, Oleksii Kliukin wrote: > I have recently observed a deadlock on one of our production servers related > to locking only a single row in a job table. There were two functions involved > in the deadlock, the first one acquires a “for key share” lock on the row that > represents the job it works on and subsequently updates it with the job’s end > time (we need multiple jobs to be operating on a single row concurrently, > that’s why there is a "for key share" lock). The other function starts by > acquiring the “for update” lock on the job row and then performs actions that > should not be run in parallel with other jobs. > > The deadlock can be easily reproduced with the following statements. The > queries run against a table job (id integer primary key, name text) with a > single row of (1,'a')) Hmm, great find. > X1: select id from job where name = 'a' for key share; > Y: select id from job where name = 'a' for update; -- starts waiting for X1 > X2: select id from job where name = 'a' for key share; > X1: update job set name = 'b' where id = 1; > X2: update job set name = 'c' where id = 1; -- starts waiting for X1 > X1: rollback; > > At this point, Y is terminated by the deadlock detector: Yeah, this seems undesirable in general terms. Here's a quick isolationtester spec that reproduces the problem: 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 "s1" setup { begin; } step "s1_keyshare" { select id from tlu_job where name = 'a' for key share; } step "s1_update" { update tlu_job set name = 'b' where id = 1; } step "s1_rollback" { rollback; } session "s2" setup { begin; } step "s2_keyshare" { select id from tlu_job where name = 'a' for key share; } step "s2_update" { update tlu_job set name = 'c' where id = 1; } step "s2_commit" { commit; } session "s3" setup { begin; } step "s3_forupd" { select id from tlu_job where name = 'a' for update; } teardown { commit; } # Alexey's permutation permutation "s1_keyshare" "s3_forupd" "s2_keyshare" "s1_update" "s2_update" "s1_rollback" "s2_commit" (X1 is s1, X2 is s2, Y is s3). Permutations such as that one report a deadlock with the original code, and does not report a deadlock after your proposed patch. permutation "s1_keyshare" "s1_update" "s2_keyshare" "s3_forupd" "s2_update" "s1_rollback" "s2_commit" But semantically, I wonder if your transactions are correct. If you intend to modify the row in s1 and s2, shouldn't you be acquiring FOR NO KEY UPDATE lock instead? I don't see how can s1 and s2 coexist peacefully. Also, can your Y transaction use FOR NO KEY UPDATE instead .. unless you intend to delete the tuple in that transaction? I'm mulling over your proposed fix. I don't much like the idea that DoesMultiXactIdConflict() returns that new boolean -- seems pretty ad-hoc -- but I don't see any way to do better than that ... (If we get down to details, DoesMultiXactIdConflict needn't initialize that boolean: better let the callers do.) -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services