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 1hb6Op-0001GE-Gp for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Jun 2019 16:46:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hb6Oo-0001Qm-6s for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Jun 2019 16:46:58 +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 1hb6On-0001Qe-PD for pgsql-hackers@lists.postgresql.org; Wed, 12 Jun 2019 16:46:57 +0000 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hb6Ok-0000VZ-CG for pgsql-hackers@lists.postgresql.org; Wed, 12 Jun 2019 16:46:57 +0000 Received: by mail-qk1-x743.google.com with SMTP id i125so10777646qkd.6 for ; Wed, 12 Jun 2019 09:46:54 -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=/U8CNmag3mk0IU4iItTg5w3/jFejBcRcdTuu1hl+8B4=; b=ufFT7xJKnTM6wJDj9ijRAQXr3u2t6HgAduYzbsK4ojHi8sZJLwaeYterkMtntHim2Z Ymoibf381/w6vtBbmo0b871BEYFhxv1qYXdo9z5aGNFG2TyEUdl66Wzw/md8uruerEcC g6LvZbuZC6rmfaO76r6/ip0gGGUZ1aaqn0caNhVilXZZ3I1nA2wm+NtlUS41+JEi/jSS 5palH0yb1OS0R2wxWFUGL7b9aRt1X5DQfd7TAsbZ109wpWx9SAAjkRwOjj6lBxYirPvq 7/RGofwkSk3DJ4jwNQU6Nrn09X3tIfxwv21Js5OPy88xZP4AepMgdrdYUgMGGmdS9Apu g5kA== 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=/U8CNmag3mk0IU4iItTg5w3/jFejBcRcdTuu1hl+8B4=; b=qZTSs+aNHMzGL3rf/s+Uu2hN5REJfpj3Bo1SQQ3ubOL3jkG6ojS3gfU9etAL6KnHHa 4n+Pry3ibi1zu/hR+Xnxq00enWu3e+/SS/LUBhBhvKQz1IgzaobKPdNDGvieayaJgA6F rpISwXSZ99JRZAmNSfrYypRLK5uraYz2vb3QJiRTwIDzHfFMYMJ2DKLYwzolFBxroyjb 0PFJpj148/33mGAIou6F/wx5Wx4RCgEQ3pWlme4zDiw7s5XjpgQO3BO/i01IZvnTWWP7 8FA/DamrAyJk/mqScX/qr/v7FJr1eAQ3/IlMUi6ab8LS4fF7aBOZIoRXGtDulcWp3YKA MtCw== X-Gm-Message-State: APjAAAW2+4Gg5eYjAFLj9/lNVc2f3mTrr/DxyggR0v8UVEuY11ZvLrIu Ws64ztEJ8tCXIpnSV8olHCN9FhyR9tn57w== X-Google-Smtp-Source: APXvYqxbX+xOjTxfYJtuCSQJTotWx6BRhaUsHqvCsCIP9h6/jEeU5uBDr7ZXEoFtY59J4a7jOvdolQ== X-Received: by 2002:a37:670d:: with SMTP id b13mr65259509qkc.47.1560358011781; Wed, 12 Jun 2019 09:46:51 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id d123sm72993qkb.94.2019.06.12.09.46.51 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 12 Jun 2019 09:46:51 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 5D6DF120859; Wed, 12 Jun 2019 12:46:49 -0400 (-04) Date: Wed, 12 Jun 2019 12:46:49 -0400 From: Alvaro Herrera To: Oleksii Kliukin Cc: PostgreSQL Hackers Subject: Re: upgrades in row-level locks can deadlock Message-ID: <20190612164649.GA10230@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <62A83BCF-1B90-44F8-BADF-69F750E677B6@hintbits.com> 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-12, Oleksii Kliukin wrote: > Thank you! I can make it even simpler; s1 just acquires for share lock, s3 > gets for update one and s2 takes for share lock first, and then tries to > acquire for update one; once s1 finishes, s3 deadlocks. Cool. I think it would be worthwhile to include a number of reasonable permutations instead of just one, and make sure they all work correctly. I don't think we need to include all possible permutations, just a few. I think we need at least enough permutations to cover the two places of the code that are modified by the patch, for both values of have_current_xid (so there should be four permutations, I think). Please don't simplify the table name to just "t" -- the reason I used another name is that we want these tests to be able to run concurrently at some point; ref. https://postgr.es/m/20180124231006.z7spaz5gkzbdvob5@alvherre.pgsql > > 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? > > It is correct. I wanted to make sure jobs that acquire for key share lock > can run concurrently most of the time; they only execute one update at the > end of the job, and it is just to update the last run timestamp. I see. Under READ COMMITTED it works okay, I suppose. > > 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.) > > I am also not happy about the new parameter to DoesMultiXactIdConflict, but > calling a separate function to fetch the presence of the current transaction > in the multixact would mean doing the job of DoesMultiXactIdConflict twice. > I am open to suggestions on how to make it nicer. Yeah, I didn't find anything better either. We could make things more complex that we resolve the multixact once and then extract the two sepraate bits of information that we need from that ... but it ends up being uglier and messier for no real gain. So let's go with your original idea. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services