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 1m4rql-0006yW-MI for pgsql-bugs@arkaria.postgresql.org; Sat, 17 Jul 2021 21:27:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m4rqj-0007RO-Ku for pgsql-bugs@arkaria.postgresql.org; Sat, 17 Jul 2021 21:27:53 +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 1m4rqj-0007RG-C6 for pgsql-bugs@lists.postgresql.org; Sat, 17 Jul 2021 21:27:53 +0000 Received: from meesny.iki.fi ([195.140.195.201]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m4rqc-0000pZ-Ir for pgsql-bugs@lists.postgresql.org; Sat, 17 Jul 2021 21:27:52 +0000 Received: from [192.168.1.113] (dsl-hkibng22-54faa4-119.dhcp.inet.fi [84.250.164.119]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id B7D6E20553; Sun, 18 Jul 2021 00:27:43 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1626557263; 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=hESMdg9S8h8oKYRX0f8roDLZlewnpquZj+H5EMAqVwc=; b=Gf16ErRiZj431mawZoC1EBY70e+0T5NJ2v8AjtCLZ+IEALllr/5tFJpn4L168afyl+mos6 Ub9S0Jwe0DNrpjWei6VKpq/T1tV12BrUL0cxKWXwDLbB3XAQz8B8KZxGVHdYE1Z13x2Tcf E5gcfUreI5I2KOsyZ2gqVMTbzwar2+0= Subject: Re: IRe: BUG #16792: silent corruption of GIN index resulting in SELECTs returning non-matching rows To: Peter Geoghegan Cc: Alexander Korotkov , Pawel Kudzia , PostgreSQL mailing lists , Tom Lane References: <16792-b1913b6b4e098331@postgresql.org> <970432.1623938874@sss.pgh.pa.us> <1039976.1623959387@sss.pgh.pa.us> <1871889b-f5ef-f4d4-8d42-65a9e863e0a9@iki.fi> From: Heikki Linnakangas Message-ID: <10e8dbad-d21a-b22b-6677-c5c9df9ea6ee@iki.fi> Date: Sun, 18 Jul 2021 00:27:43 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1626557263; 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=hESMdg9S8h8oKYRX0f8roDLZlewnpquZj+H5EMAqVwc=; b=FgGnQLmljMJJgTs+l68oXwISkbeCk+PCncZ45BE8vx+d01iJ3I06r3aVOhDuwxA8fEaiLd mfrQ1FAOtuVvlNktYrzQpV2NZ2JIo7bYyG6rJMsTqY2uUq1ys2zDg/vOFPIu5ktjpIlAx8 fPWpjKYfX0OppAaEgQdxKAPVqe6O9Is= ARC-Seal: i=1; s=meesny; d=iki.fi; t=1626557263; a=rsa-sha256; cv=none; b=xhS2Sm7eY6rmtFdKLRN6T2LP88BnJmW1O/g2AX8qUu1HvUHDwrhXvj1merNBVwOiQlYnFH Sidf2bxIYpV24b/SKRCxsmhKkc6dp2AAUkDmzOdmcWepGssJZevhv9yM4xqdw3SgEqg74W s1mpnRbDwG4+58rBA95Y0h+6cq9K/38= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 17/07/2021 04:56, Peter Geoghegan wrote: > On Fri, Jul 16, 2021 at 5:30 PM Heikki Linnakangas wrote: >> Hmm, seems we should fix that. But could a prematurely recycled deleted >> page cause permanent corruption? > > If scans can find a page that is wholly unrelated to the expected page > (and possibly even in the wrong high level page category), then it's > really hard to predict what might happen. This could lead to real > chaos. Right. Looking at the code, I don't think that can happen. The code that deals with the pending list uses lock coupling, so it should never follow a pointer to a page that was concurrently deleted. Might something funny happen if a pending list page is deleted and immediately reused as another kind of page? I don't see a problem with that. And a gin btree page won't be reused prematurely as a pending list page, because ginNewBuffer() checks GinPageIsRecyclable(), even if the page is being reused as a pending list page. > ginInsertCleanup() makes no attempt to perform basic validation > of its assumptions about what kind of page this is, except for some > assertions. We should have something like a "can't happen" error on > !GinPageIsList() inside ginInsertCleanup() -- if we had that already > then I might be able to reason about this problem. It wouldn't hurt to > have similar checks in other code that deals with posting trees and > entry trees, too. +1 While playing with the trace Pawel sent, I loaded some of the index pages into a local instance so that I could step through the code with gdb. I filled the rest of the pages with zeros. I managed to get the gin btree descend code into an infinite loop with that. When it followed a pointer to an entry leaf page, but that entry leaf page was substituted with zeros, it interpreted the all-zeros page as a page with a right-link to block 0. It then stepped right to read block 0 - which is the metapage! - and incorrectly interpreted it as an internal page. Amazingly, it managed to perform the binary search on it as if it contained index tuples, and came up wth an another block number, which I had also replaced with all-zeros. Long story short, we should sprinkle a lot more sanity checks to the functions in ginbtree.c. It should check that the page is of expected kind; no stepping from internal entry page to posting tree page. And seeing a pointer to block 0 at any step should be an error. But this was just an artifact of the missing pages my test. It doesn't explain the original problem. > ginInsertCleanup() is tolerant of all kinds of things. It's not just > the lack of page-level sanity checks. It's also the basic approach to > crash safety, which relies on the fact that GIN only does lossy index > scans. My guess is that there could be lots of problems without it > being obvious to users. Things really went downhill in > ginInsertCleanup() starting in commit e956808328. Yeah, I wouldn't be surprised if this turns out to be a bug in pending list cleanup. But I haven't been able to come up with a concrete sequence of steps that would cause it. - Heikki