BUG #19031: pg_trgm infinite loop on certain cases - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #19031: pg_trgm infinite loop on certain cases
Date
Msg-id 19031-0638148643d25548@postgresql.org
Whole thread Raw
Responses Re: BUG #19031: pg_trgm infinite loop on certain cases
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      19031
Logged by:          Tim Wood
Email address:      washwithcare@gmail.com
PostgreSQL version: 17.6
Operating system:   MacOS
Description:

When querying against a column with a gin_trgm_ops index, using <% with a
string without any trigrams followed by a string with trigrams causes what
appears to be an infinite loop, and the query cannot be canceled, and the
process must be restarted in order to kill the long running query.

Simplified use case:

```
create extension if not exists pg_trgm with schema public;
create temp table simple_case (name text);
create index simple_case_name_index on simple_case using gin (name
gin_trgm_ops);

-- generate enough records for the optimizer to choose the index
insert into simple_case (name) select 'two and' || i::text from
generate_series(1, 1000000) as t(i);

select * from simple_case; -- returns normally

explain select * from simple_case where (',' <% name);
select * from simple_case where (',' <% name); -- returns normally

explain select * from simple_case where ('a' <% name) and (',' <% name);
select * from simple_case where ('a' <% name) and (',' <% name); -- returns
normally

explain select * from simple_case where ('a' <% name) and (',' <% name) and
('a' <% name);
select * from simple_case where ('a' <% name) and (',' <% name) and ('a' <%
name); -- returns normally

explain select * from simple_case where (',' <% name) and ('a' <% name);
select * from simple_case where (',' <% name) and ('a' <% name); -- infinite
loop
select * from simple_case where ('' <% name) and ('a' <% name); -- infinite
loop
```


pgsql-bugs by date:

Previous
From: Todd Cook
Date:
Subject: Re: Postgres: Queries are too slow after upgrading to PG17 from PG15
Next
From: Tom Lane
Date:
Subject: Re: BUG #19031: pg_trgm infinite loop on certain cases