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 1qDhDC-0000Tr-Lm for pgsql-bugs@arkaria.postgresql.org; Mon, 26 Jun 2023 08:04:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qDhDB-0007HT-84 for pgsql-bugs@arkaria.postgresql.org; Mon, 26 Jun 2023 08:04:37 +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 1qDhDA-0007FX-Rx for pgsql-bugs@lists.postgresql.org; Mon, 26 Jun 2023 08:04:36 +0000 Received: from meesny.iki.fi ([2001:67c:2b0:1c1::201]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qDhD8-000Xri-Jv for pgsql-bugs@lists.postgresql.org; Mon, 26 Jun 2023 08:04:36 +0000 Received: from [192.168.10.13] (31-22-251.netrun.cytanet.com.cy [31.153.22.251]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 4QqL3J2p6JzybD; Mon, 26 Jun 2023 11:04:28 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1687766670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=odMuCX4sllbUluFn2Jx1lbo/mYQWWxKgEgqDtpl0Ig8=; b=PNlUUPllDOlN5OeOCk7uUle9qsw1YBxbmDiv2qTvQv8KwiEHKXfs1AfOtdalR3VZW6DhJM a+pp0YcXZD6aQeCn/j2S5Gocea/JURoLlbxQyzNO1jWq1nmisYgtCt2lyIctKgbN8Org9z W+azTLdrxEldN2G2i8rZzGHSRlhP4CU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1687766670; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=odMuCX4sllbUluFn2Jx1lbo/mYQWWxKgEgqDtpl0Ig8=; b=ftXqoNsdPs5AJlj7gYh9MsKFUj26/g4DC922YzpRYVPsjOJLOQZG+Wau/CmEkOBBgSSc4B AKHTP2SNo8claKXz9qzImbN1gZQNdARaV0HIEN5gQ2xaypJfpuYLb9Vgqo2rXcr+0gZstB +lF59BUgOHuuQLkGHbQL4Y9RqXHME5A= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=meesny; d=iki.fi; t=1687766670; a=rsa-sha256; cv=none; b=Patsx7JAtWp2sqCPYZZz191/Dce1VRYQ/R1XOMArO/mmgK0hOL48by4EMSbPn/4tdjbpGT 2uUKGQVMAjChDIlw15SEBn0gWrK3uRWCvmRni2Gbd4vMC1XNfFVVQXRs8v0Y6e6IWSqZQT sxb9n4XRc1HLJbWDm3QzYy+33ev+S78= Message-ID: <4638fef6-ea51-0e0e-d463-d5269575b021@iki.fi> Date: Mon, 26 Jun 2023 11:04:26 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: BUG #17949: Adding an index introduces serialisation anomalies. Content-Language: en-US To: Thomas Munro , Dmitry Dolgov <9erthalion6@gmail.com> Cc: artem.anisimov.255@gmail.com, pgsql-bugs@lists.postgresql.org, Teodor Sigaev References: <20230619121757.gjvuvypp25ngwkux@ddolgov.remote.csb> <20230621090421.4rr55ybndttng7xl@ddolgov.remote.csb> <20230623140542.5tbeg5ikz3cupag3@ddolgov.remote.csb> <20230624135910.zufuollwp57otawo@ddolgov.remote.csb> From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 26/06/2023 03:25, Thomas Munro wrote: > On Sun, Jun 25, 2023 at 1:59 AM Dmitry Dolgov <9erthalion6@gmail.com> wrote: >> I've managed to resolve it, or at least reduce the chances for the issue to >> appear, via semi-randomly adding more CheckForSerializableConflictIn / >> PredicateLock around the new sublist that has to be created in >> ginHeapTupleFastInsert. I haven't seen the reproducer failing with this >> changeset after running it multiple times for a couple of minutes, where on the >> main branch, with the two fixes from Thomas included, it was failing within a >> couple of seconds. > > Ahh, right, thanks. I don't think we need to lock all those pages as > you showed, as this whole "fast" path is supposed to be covered by the > meta page (in other words, GIN is expected to have the highest > possible serialisation failure rate under SSI unless you turn fast > updates off). But there is an ordering bug with the existing > predicate lock in that code, which allows this to happen: > > S1: CheckForSerializableConflictIn(meta) > > S2: PredicateLockPage(meta) > S2: scan, find no tuples > > S1: BufferLock(EXCLUSIVE > S1: modify stuff... > > CheckForSerializableConflictIn() was written with the assumption that > you're inserting a tuple (ie you have the page containing the tuple > locked), so you'll either conflict with a reader who already has a > predicate lock at that point OR you'll insert first and then the > reader will see your (invisible-to-snapshot) tuples, but here we're > doing some fancy footwork with a meta page, and we screwed up the > ordering and left a window where neither of those things happens. > Perhaps it was coded that way because there is drop-then-reacquire > dance, but it's easy enough to move the check in both branches. Does > that make sense? Yes, +1 on the patches. Any chance of constructing test cases for these? The above race condition is hard to reach, but some of these other bugs seem more testable. Some minor nits: In v3-0001-Fix-race-in-SSI-interaction-with-empty-btrees.patch: > /* > - * We only get here if the index is completely empty. Lock relation > - * because nothing finer to lock exists. > + * Since we have no pages locked, it's possible for another > + * transaction to insert data between _bt_search() and > + * PredicateLockRelation(). We have to try again after taking a > + * relation-level predicate lock, to close a narrow window where we > + * wouldn't scan concurrently inserted tuples, but the writer wouldn't > + * see our predicate lock. > */ I'd like to keep the old comment here, it's good context, and add the new text in addition to the old. v3-0002-Fix-race-in-SSI-interaction-with-bitmap-heap-scan.patch: Can we keep the optimization when not using SSI? -- Heikki Linnakangas Neon (https://neon.tech)