Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
LIKE with trailing wildcard in Postgres doesn't use the index when the prefix is a result of a subquery

I have the following table

CREATE TABLE test(id bigint primary key, path varchar);

with the additional index on path column

CREATE INDEX test_path_idx ON test(path varchar_pattern_ops);

With the query like this

SELECT * FROM test WHERE path LIKE 'prefix%';

the index on path is used. It's still used with

SELECT * FROM test WHERE path LIKE 'prefix' || '%';

Index Only Scan using test_path_idx on test  (cost=0.41..8.43 rows=1 width=511)
  Index Cond: ((path ~>=~ 'prefix'::text) AND (path ~<~ 'prefiy'::text))
  Filter: ((path)::text ~~ 'prefix%'::text)

However, if the prefix is a result of a subquery

SELECT * FROM test WHERE path LIKE (SELECT 'prefix')::varchar || '%';

Seq Scan on test  (cost=0.01..359.93 rows=20 width=511)
  Filter: ((path)::text ~~ ((($0)::character varying)::text || '%'::text))
  InitPlan 1 (returns $0)
    ->  Result  (cost=0.00..0.01 rows=1 width=32)

the index is not used. Of course, in reality the subquery is more complicated but the above suffices to make the optimizer "ignore" the index.

I assume it happens because the optimizer cannot be sure there are no wildcards in the subquery result so it takes the "worst case" path. Is there a way to optimize this? Unfortunately, executing the inner select in a separate query and plugging in the result is not an option.

P.S. I have tested this on 9.6.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!