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 1khgJM-00043c-Vf for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 21:57:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khgJL-0001CX-SK for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 21:57:19 +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 1khgJL-0001CQ-Le for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 21:57:19 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khgJJ-0007aD-Hu for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 21:57:19 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 622995C0064; Tue, 24 Nov 2020 16:57:16 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 24 Nov 2020 16:57:16 -0500 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:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=VKHTKoSRb5qKz80tV dPN4E/ot29rfUZabmiNNCkxPFQ=; b=rYMnVG4T3RjsoIrvhEY3B1owJLDVwLDFL bbiI9JbuVcXRPMBy86iabvgxkQHjJBBkA7QI/sRgYO9Yn1gNA7d+Lr9W0tTFrtfQ My2OMzmQ9UsXPPwrD7+QS0356RyiHOHBe53bAyF64TBhnlgiuncws0NLOpWLJkmY 2lXpQhD+nFmuVH9/8CfZGyGbE3+osWxOik4KVfP0PBukpeTzlGCujTZFmjmiDzjZ +EUMyPR3cJ80sVJehtb2RItSiHVJKrjtUtWuyAvWNf2FEern0QnkN/XCQCJcmMrC gF/P/bOdz1dIBOZNMbM2xkzb16YzkdrIXtbimAyUW5Ptla/DR2ANg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegkedgudehjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkgggtuggjfgesthdtredttdervdenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvefgleevgeeujeeghfegteeghfegheeifeehheelteet ieduieeihffggfehffetnecukfhppeduledtrdelhedrudekrdejleenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghl vhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.18.79]) by mail.messagingengine.com (Postfix) with ESMTPA id 74506328005D; Tue, 24 Nov 2020 16:57:15 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 0C9C52A0BB8; Tue, 24 Nov 2020 18:57:13 -0300 (-03) Date: Tue, 24 Nov 2020 18:57:13 -0300 From: Alvaro Herrera To: Tom Lane Cc: Andres Freund , Michael Paquier , Dmitry Dolgov <9erthalion6@gmail.com>, James Coleman , Pg Hackers Subject: Re: remove spurious CREATE INDEX CONCURRENTLY wait Message-ID: <20201124215713.GA16003@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <57665.1606146169@sss.pgh.pa.us> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-Nov-23, Tom Lane wrote: > Alvaro Herrera writes: > > GetCurrentVirtualXIDs, ComputeXidHorizons, GetSnapshotData: > > > In these cases, what we need is that the code computes some xmin (or > > equivalent computation) based on a set of transactions that exclude > > those marked with the flags. The behavior we want is that if some > > transaction is marked as vacuum, we ignore the Xid/Xmin *if there is > > one*. In other words, if there's no Xid/Xmin, then the flag is not > > important. So if we can ensure that the flag is set first, and the > > Xid/xmin is installed later, that's sufficient correctness and we don't > > need to hold exclusive lock. But if we can't ensure that, then we must > > use exclusive lock, because otherwise we risk another process seeing our > > Xid first and not our flag, which would be bad. > > I don't buy this either. You get the same result if someone looks just > before you take the ProcArrayLock to set the flag. So if there's a > problem, it's inherent in the way that the flags are defined or used; > the strength of lock used in this stanza won't affect it. The problem is that the writes could be reordered in a way that makes the Xid appear set to an onlooker before PROC_IN_VACUUM appears set. Vacuum always sets the bit first, and *then* the xid. If the reader always reads it like that then it's not a problem. But in order to guarantee that, we would have to have a read barrier for each pass through the loop. With the LW_EXCLUSIVE lock, we block the readers so that the bit is known set by the time they examine it. As I understand, getting the lock is itself a barrier, so there's no danger that we'll set the bit and they won't see it. ... at least, that how I *imagine* the argument to be. In practice, vacuum_rel() calls GetSnapshotData() before installing the PROC_IN_VACUUM bit, and therefore there *is* a risk that reader 1 will get MyProc->xmin included in their snapshot (because bit not yet set), and reader 2 won't. If my understanding is correct, then we should move the PushActiveSnapshot(GetTransactionSnapshot()) call to after we have the PROC_IN_VACUUM bit set.