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 1k8Eoj-0008Po-W6 for pgsql-committers@arkaria.postgresql.org; Wed, 19 Aug 2020 03:31:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k8Eoi-0005BF-Tx for pgsql-committers@arkaria.postgresql.org; Wed, 19 Aug 2020 03:31:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k8Eoi-0005B8-OV for pgsql-committers@lists.postgresql.org; Wed, 19 Aug 2020 03:31:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k8Eoe-0000Tm-MT for pgsql-committers@lists.postgresql.org; Wed, 19 Aug 2020 03:31:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 07J3V4l31366054; Tue, 18 Aug 2020 23:31:04 -0400 From: Tom Lane To: David Rowley cc: Andres Freund , pgsql-committers@lists.postgresql.org Subject: Re: pgsql: Fix race condition in snapshot caching when 2PC is used. In-reply-to: References: <1355915.1597794204@sss.pgh.pa.us> <20200818235152.4hxyb3hrvcm7pwom@alap3.anarazel.de> <1356510.1597794950@sss.pgh.pa.us> <20200819003713.cm2dg7o7knqnxdny@alap3.anarazel.de> Comments: In-reply-to David Rowley message dated "Wed, 19 Aug 2020 15:11:20 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1366052.1597807864.1@sss.pgh.pa.us> Date: Tue, 18 Aug 2020 23:31:04 -0400 Message-ID: <1366053.1597807864@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Rowley writes: >> On 2020-08-18 19:55:50 -0400, Tom Lane wrote: >>> On Wed, 19 Aug 2020 at 12:37, Andres Freund 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. > Couldn't it be done by creating two inline functions, one to call to > atomically increment and the other to just increment? Can backup that > the correct version of the function is being called with an > Assert(LWLockHeldByMeInMode(ProcArrayLock, ...)); On reflection I agree with Andres' thought that just taking the lock exclusively in ProcArrayClearTransaction is the right solution. It's silly to imagine that a 2PC commit (plus all the other stuff that needs to happen around that) is fast enough that that'll be a serious performance hit. Keeping things simple for the other code paths is a more useful goal. regards, tom lane