Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uqvPm-00FISE-MK for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Aug 2025 15:16:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uqvPm-007QgI-37 for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Aug 2025 15:16:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uqvPl-007QgA-Rp for pgsql-bugs@lists.postgresql.org; Tue, 26 Aug 2025 15:16:50 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uqvPj-001rSm-3A for pgsql-bugs@lists.postgresql.org; Tue, 26 Aug 2025 15:16:49 +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 57QFGkip1081532; Tue, 26 Aug 2025 11:16:46 -0400 From: Tom Lane To: Arseniy Mukhin cc: washwithcare@gmail.com, pgsql-bugs@lists.postgresql.org, Nikita Glukhov , Alexander Korotkov Subject: Re: BUG #19031: pg_trgm infinite loop on certain cases In-reply-to: References: <19031-0638148643d25548@postgresql.org> <969700.1756169663@sss.pgh.pa.us> Comments: In-reply-to Arseniy Mukhin message dated "Tue, 26 Aug 2025 14:26:58 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1081530.1756221406.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 26 Aug 2025 11:16:46 -0400 Message-ID: <1081531.1756221406@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Arseniy Mukhin writes: > On Tue, Aug 26, 2025 at 3:54 AM Tom Lane wrote: >> However, I don't totally understand *why* it fixes the test case. >> Especially not after I noted that there's already a test case in >> pg_trgm that exercises exactly this situation: >> >> select count(*) from test_trgm where t like '%99%' and t like '%qwerty%'; >> >> If you put an Assert into ginNewScanKey that the first scan key >> isn't excludeOnly (instead of the re-sort), it fails on that query. >> So why do we not see an infinite loop for that test case? I don't >> really understand the GIN code well enough to figure out what is >> the difference. > I debug a little bit and it looks like the reason there's no infinite > loop in your example is because it returns MAYBE for the first > 'excludeOnly' key in: > keyGetItem() > ... > res = key->triConsistentFn(key); Ah-hah! The thing I'd overlooked is that that regression test query uses a different operator: LIKE not %>. So even though the GinScanKey looks pretty similar, the strategy is different, leading gin_trgm_triconsistent to return GIN_MAYBE not GIN_FALSE when nkeys=0. So now I can reproduce the failure with the regression tests' table: select count(*) from test_trgm where t %> '' and t %> '%qwerty%'; > I don't have an opinion on whether it's good or not to move > all the 'excludeOnly' keys to the end, but it seems that simply not > having an "excludeOnly" key as the first key is enough to fix the bug. > Maybe it's enough to just swap any normal key with the first one, if > it's "excludeOnly"? Yeah, that would be enough to get us out of this particular example. But I think the lesson here is that there are under-documented dependencies on the ordering of GinScanKeys, and I want the fix to make that ordering more predictable not less so. For example, after seeing this I have little confidence that GIN wouldn't have issues with an excludeOnly key that precedes the first normal key for its index attribute, even when there are other keys for other attributes appearing ahead of them in the scankey array. So I'd rather that the fix be based on a consistent pattern like "put excludeOnly keys after not-excludeOnly keys", not "let's swap the first key with some randomly-chosen other key". regards, tom lane