A normal B-tree index is usually not useful for a leading-wildcard substring search such as ILIKE '%wireless%'. For your PostgreSQL 16 query, test a pg_trgm GIN index on name.
The commands below enable the extension, build a trigram index and inspect the plan. CREATE INDEX CONCURRENTLY reduces blocking of ordinary writes, but still consumes CPU, disk and I/O and takes longer; run it outside an explicit transaction block and schedule it appropriately. Your database role/provider must permit the extension. Check an existing index’s definition rather than assuming a matching name means it is correct.
The trigram index can narrow matching rows, but your query also sorts by created_at. A common term matching a large part of the table may still need substantial work. Very short search terms provide fewer useful trigrams. Keep expectations tied to realistic data and EXPLAIN results, not a guaranteed millisecond target.
EXPLAIN ANALYZE executes the query. Start on representative staging data, then use production diagnostics carefully. Compare returned rows, buffers and execution time before/after. Select only the columns the page needs instead of SELECT * when practical.
If you want word-based relevance rather than arbitrary substrings, PostgreSQL full-text search is a different option with different matching semantics. Parameterise user input and decide how literal percent/underscore characters should behave.
See PostgreSQL 16 pg_trgm documentation and CREATE INDEX guidance.
Prepared with AI assistance by Tai. Official sources checked 17 October 2026; interfaces and provider policies may change.
No comments yet.