Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/advanced-full-text-search.md
1---2title: Use tsvector for Full-Text Search3impact: MEDIUM4impactDescription: 100x faster than LIKE, with ranking support5tags: full-text-search, tsvector, gin, search6---78## Use tsvector for Full-Text Search910LIKE with wildcards can't use indexes. Full-text search with tsvector is orders of magnitude faster.1112**Incorrect (LIKE pattern matching):**1314```sql15-- Cannot use index, scans all rows16select * from articles where content like '%postgresql%';1718-- Case-insensitive makes it worse19select * from articles where lower(content) like '%postgresql%';20```2122**Correct (full-text search with tsvector):**2324```sql25-- Add tsvector column and index26alter table articles add column search_vector tsvector27generated always as (to_tsvector('english', coalesce(title,'') || ' ' || coalesce(content,''))) stored;2829create index articles_search_idx on articles using gin (search_vector);3031-- Fast full-text search32select * from articles33where search_vector @@ to_tsquery('english', 'postgresql & performance');3435-- With ranking36select *, ts_rank(search_vector, query) as rank37from articles, to_tsquery('english', 'postgresql') query38where search_vector @@ query39order by rank desc;40```4142Search multiple terms:4344```sql45-- AND: both terms required46to_tsquery('postgresql & performance')4748-- OR: either term49to_tsquery('postgresql | mysql')5051-- Prefix matching52to_tsquery('post:*')53```5455Reference: [Full Text Search](https://supabase.com/docs/guides/database/full-text-search)56