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 1lwOww-0001FH-2D for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Jun 2021 12:59:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lwOwu-0008Pp-Ni for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Jun 2021 12:59:16 +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 1lwOwu-0008Pg-EX for pgsql-bugs@lists.postgresql.org; Thu, 24 Jun 2021 12:59:16 +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 1lwOwr-0001CE-Nd for pgsql-bugs@lists.postgresql.org; Thu, 24 Jun 2021 12:59:15 +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) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by meesny.iki.fi (Postfix) with ESMTPSA id 16C1B2055B; Thu, 24 Jun 2021 15:59:10 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1624539550; 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=JhFVDx8wjyJ+Zf/Fs9O+ya+0rHjaMlhBvjHQY/jV+FU=; b=tVt394mEi8Gr9b6dQhBrMfVyqYKYEkvTmbzTU6nH9+W1OSpKpfFGBOw7Idn9fXVvaxS++c B5hL45wU+YwMOG8eGxjZGZGrRBLYVesU41EKrAvFrutUgswAvMOy+ryFqsv4/zZGLB9VlA B4eaCKOwEE8fE6KR5Ovl5LOQ4XkR8M0= Subject: IRe: BUG #16792: silent corruption of GIN index resulting in SELECTs returning non-matching rows To: Alexander Korotkov , Pawel Kudzia Cc: pgsql-bugs@lists.postgresql.org, Tom Lane References: <16792-b1913b6b4e098331@postgresql.org> <970432.1623938874@sss.pgh.pa.us> <1039976.1623959387@sss.pgh.pa.us> From: Heikki Linnakangas Message-ID: Date: Thu, 24 Jun 2021 15:59:09 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <1039976.1623959387@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit 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=1624539550; a=rsa-sha256; cv=none; b=LdtwPA6hJgPs0J9M7vOchAwZCo0IV3rpNNQpaq9fcLXJJaLBOallr35v/7esm5jWpx5C1Q RJl4Y6xcibu3sKXojjhMCLwI2n1kFum0C62FzPcouv1jqEG9maroxdq72dbfZM4/ev6uO2 MnsK32QErqcVZtWudHIGdFYdAdLocE4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1624539550; 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=JhFVDx8wjyJ+Zf/Fs9O+ya+0rHjaMlhBvjHQY/jV+FU=; b=XHox0KGkhXiSUcI4cW1mGijHVdkwfdJoaxs2abb6e751fhFnaVNGjq73fv7RpghxithQTg +O9kD5cew9r/+lVL2yP/K0bDO5r6BldWhwMO7MitjVEPT15/5wosVIw2o6g1Uxa9gZv7fE rRlmoayKYse4qLsI/a7bN2JTBPkKWoY= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 17/06/2021 22:49, Tom Lane wrote: > I wrote: >> Pawel Kudzia writes: >>> with help from IRC we've found that decreasing work_mem from 1MB to 256kB >>> or less makes the problem go away: > >> Hmm. So that suggests that the index itself is *not* corrupt, >> but the problem is associated with a bug in the indexscan >> algorithms. > > After staring at the code I think there is at least one bug, and > possibly two, in shimTriConsistentFn. That's likely to be implicated > here because intarray's GIN opclass only provides a bool consistent > function. I'm not very clear on the circumstances that lead to the scan > code inventing GIN_MAYBE inputs, so I haven't been able to construct a > test case. > > The definite bug is triggered because intarray relies on the API > specification that says that if the search mode is > GIN_SEARCH_MODE_DEFAULT, then the consistentFn will only be called > when there's at least one TRUE key: > > case RTOverlapStrategyNumber: > /* result is not lossy */ > *recheck = false; > /* at least one element in check[] is true, so result = true */ > res = true; > break; > > shimTriConsistentFn ignores this and calls it on all-FALSE inputs, > for which it'll incorrectly get a TRUE result, as it will also for > all the following checks. The upshot is that shimTriConsistentFn > will convert any case with a MAYBE input to a hard TRUE result, > with no recheck requirement. This'd easily explain the reported > misbehavior. That's subtle. The documentation says: > If *searchMode is set to > GIN_SEARCH_MODE_DEFAULT (which is the value it is > initialized to before call), only items that match at least one of > the returned keys are considered candidate matches. I guess you can read that as "consistentFn will only be called when there is at least one matching item", but that didn't occur to me when I read that at first. But I agree we need to fix shimTriConsistentFn to respect that. > The other thing that I'm unsure about, because the code is horribly > underdocumented, is that it's not very clear whether > shimTriConsistentFn is accurately converting between the bool and > the tristate APIs. That's because it's not very clear what the > tristate API actually *is*. In particular, is the end state of > key->recheckCurItem meaningful in the tristate case? If it's not, > then the short-circuit case for no MAYBE inputs is broken, because > it'll return TRUE when the bool consistentFn is trying to tell us > to recheck. But if it is meaningful, then the multiway case is broken, > because it will return the recheckCurItem result set by the last call to > the bool consistentfn; which might be false even though other calls said > true. (So in that case I think we'd need "key->recheckCurItem = recheck" > at the end.) I also wonder how any of that works correctly for real > triconsistent functions, which don't have access to the recheckCurItem > flag. > > Anyway, I'm punting this to the code authors. I'd like to see > some comments clarifying what the API really is, not just a > quick-n-dirty code fix. I've been trying to create a test case for this, but no luck so far. I cannot find a codepath that would hit these bugs with the kind of query that Pawel used. The search condition is very simple: "column && '{constant}'", with only one key and one constant to search for. There are three calls to triConsistentFn: 1. In startScanKey, but that's only reached if (key->nentries > 1), so not reachable with Pawel's query. 2. In keyGetItem, when haveLossyEntry==true. But AFAICS, haveLossyEntry is never true with Pawel's query. That would require curItem to be a lossy pointer, and that in turn can only happen when matchBitmap is used, and that's only used with partial match queries and with queries that need a full-index scan. 3. The second call in keyGetItem is reached, but it is only passed GIN_MAYBE when curItem is lossy. Which isn't possible with this query, see point 2. AFAICS the recheckCurItem bug should also not cause wrong results with that query. I must be missing something. I could probably construct a test case with a different query, one involving multiple elements in the search key, but I'd like to have a solid explanation for the original report. Pawel, is this a production system, or would it be possible for you to build from sources with a patch with some extra debugging output? - Heikki