BUG #19031: pg_trgm infinite loop on certain cases

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: washwithcare(at)gmail(dot)com
Subject: BUG #19031: pg_trgm infinite loop on certain cases
Date: 2025-08-25 21:27:04
Message-ID: 19031-0638148643d25548@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19031
Logged by: Tim Wood
Email address: washwithcare(at)gmail(dot)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
```

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2025-08-26 00:54:23 Re: BUG #19031: pg_trgm infinite loop on certain cases
Previous Message Todd Cook 2025-08-25 12:30:37 Re: Postgres: Queries are too slow after upgrading to PG17 from PG15