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 1k8C6c-0001nc-5Y for pgsql-committers@arkaria.postgresql.org; Wed, 19 Aug 2020 00:37:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k8C6b-0001zk-3O for pgsql-committers@arkaria.postgresql.org; Wed, 19 Aug 2020 00:37:29 +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 1k8C6Z-0001zX-FU for pgsql-committers@lists.postgresql.org; Wed, 19 Aug 2020 00:37:28 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k8C6S-0001i1-1I for pgsql-committers@lists.postgresql.org; Wed, 19 Aug 2020 00:37:27 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 79D53F1C; Tue, 18 Aug 2020 20:37:15 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Tue, 18 Aug 2020 20:37:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=3whbvet+AkGVJvmwEOME6TsTcV3 VjhGsX2k1SszzPWQ=; b=Mg3L2oFaMtWApRxrC+a+X9DmhXZ0N6vxrY4HMSQSfAq towoWPpv+9D5FOPeJBl5VUtJzF+P1j0Oz19Fx6TlOw2tumbd9MAKka+/UHbpp/ow nfPqQ9S1It6oEFG0OksinGvUgOzvp2da3EBN/cDgr3rI0rLM5I+fvPyd0mwDdqv0 nomxWBXfMPSLd66h0ptCyT+CEcUz0Q4OUvTJDptCxfUtmoUNFu9YSKnzsy70bFoj tLlVFiDmX45wV9HgmRj/BNbSfcoNCQbAvUOtQaeuIOCRuuugjz1ahpFnoJSrYvWt igG0CeWkrbUVIbiyv1B9jIau7xeW3k8rACSz5y9t7NQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=3whbve t+AkGVJvmwEOME6TsTcV3VjhGsX2k1SszzPWQ=; b=sHRANpVHHb/RJBvzp3O/Az aDHCcQxWJ8FNcgZO0Vx7zWXU4ssZ+quq2gbiJeVl/quXdAbQlW2McQn2noqmnqWy pDI2YOpHLa/2NuE5VF6OYXh4yVIWlml9mIy45nLssfOSl44cmXf3RZUNpoaDnR6R /dHb6uI6Iy42FgaeYW7j1CHIVQ4Ck0zIW+zOtLch7xYb8mhI6k08bomQc8lRuWFN Qwmx8iBQK8Qt8RZ1q7PEUFKZHBvWLdXHhrIkD4dfaPdU4PXDGdSStvrTNyOV3WQd ePgPa4igPznIpiwaJYtj6HjYd6sqO2XunflTqeQKjKgPfp/4IUrIq72xlYcw3KlQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedruddtjedgvdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesthdtre dttddtvdenucfhrhhomheptehnughrvghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghn rghrrgiivghlrdguvgeqnecuggftrfgrthhtvghrnhepudekhfekleeugeevteehleffff ejgeelueduleeffeeutdelffeujeffhfeuffdunecukfhppeeijedrudeitddrvddujedr vdehtdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grnhgurhgvshesrghnrghrrgiivghlrdguvg X-ME-Proxy: Received: from intern.anarazel.de (c-67-160-217-250.hsd1.ca.comcast.net [67.160.217.250]) by mail.messagingengine.com (Postfix) with ESMTPA id 54529306005F; Tue, 18 Aug 2020 20:37:14 -0400 (EDT) Date: Tue, 18 Aug 2020 17:37:13 -0700 From: Andres Freund To: Tom Lane Cc: pgsql-committers@lists.postgresql.org Subject: Re: pgsql: Fix race condition in snapshot caching when 2PC is used. Message-ID: <20200819003713.cm2dg7o7knqnxdny@alap3.anarazel.de> References: <1355915.1597794204@sss.pgh.pa.us> <20200818235152.4hxyb3hrvcm7pwom@alap3.anarazel.de> <1356510.1597794950@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1356510.1597794950@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2020-08-18 19:55:50 -0400, Tom Lane wrote: > > I'm inclined to just make ClearTransaction take an exclusive lock - the > > rest of the 2PC operations are so heavyweight that I can't imagine > > making a difference. When I tested the locking changes in > > ProcArrayAdd()/Remove() the more heavyweight locking wasn't at all > > visible. > > I was wondering if it'd be sensible to convert that counter into an > atomic variable. That's not real clear, but worth thinking about. I had it that way originally, but because xactCompletionCount otherwise needs to be modified with ProcArrayLock exclusively held, I moved it to a plain variable. It'd be a bad deal to actually modify it atomically unless we got something out of that ([1]), because that'd increase the number of atomic operations for commits, something we surely don't want. We could make the locking rules be that modifications with a shared ProcArrayLock have to be done with an atomic op, and operations with ProcArrayLock held exclusively may do it without an atomic op. But that seems a bit weird. A slightly bigger hammer that we could use would be to remove the somewhat weird split of 2PC commit between ProcArrayClearTransaction() and ProcArrayAdd(). As far as I can tell we could have EndPrepare()/MarkAsPrepared() do both operations with one exclusive ProcArrayLock acquisition. On balance I'd change it to an exclusive lock with a comment mentioning that it'd not be too hard to downgrade to a shared lock, should it ever become a performance issue. Greetings, Andres Freund [1] e.g. not needing to hold ProcArrayLock to check whether a cached snapshot can be used. The complexity there is that I think it adds a "race condition" where the global xmin horizon can go backward. It'd be detected and "reverted", but that still could make the horizon appear to go backwar for a concurrent horizon determination.