Sub topic 18
Limitations and When to Reach for an External Engine
What PostgreSQL Full-Text Search Does Not Solve
Assume you know basic SELECT, WHERE, ORDER BY, joins, and PostgreSQL indexes. You should also know how to run queries in psql or another SQL client.
PostgreSQL full-text search is strong when searchable documents already belong in your database. It tokenizes text, removes stop words, applies stemming, matches tsquery values, and ranks results. It does not provide every kind of search behavior.
Common limitations include:
- Stemming does not correct spelling. A search for
databsewill not normally matchdatabase. - Full-text search is not ideal for arbitrary substrings. Searching for
catdoes not naturally findeducate. - Ranking is configurable but not automatically the best ranking for every product.
- Built-in search has limited support for autocomplete, typo suggestions, synonyms based on user behavior, and complex facets.
- Large distributed collections can make index size, replication, and query latency harder to manage.
- Search indexes can become temporarily stale when documents are copied to another system.
pg_trgm complements full-text search. It compares three-character sequences, called trigrams. It helps with misspellings, prefixes, and substring matching.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX articles_title_trgm_idx
ON articles USING gin (title gin_trgm_ops);
SELECT id, title, similarity(title, 'postgre') AS score
FROM articles
WHERE title % 'postgre'
ORDER BY score DESC
LIMIT 10;This query can find titles similar to postgre. Trigram matching is different from linguistic matching: it compares character patterns rather than normalized words.
Choosing the Right Search System
Use PostgreSQL alone when the corpus is moderate, search is closely tied to transactional data, and consistency matters. A generated tsvector column with a GIN index keeps the design simple and makes document changes visible in the same transaction.
Consider an external engine when you need features such as:
- Distributed indexing across very large collections.
- Fast autocomplete and typo correction at high request volume.
- Faceting, highlighting, synonyms, query suggestions, or custom analyzers.
- Search-specific ranking experiments and analytics.
- Independent scaling of search traffic and database traffic.
An external engine adds operational cost. You must move records into it, handle failed synchronization, monitor another service, and decide how much stale data users can tolerate.
| Requirement | Good starting point | Main concern |
|---|---|---|
| Stemmed word search over database rows | PostgreSQL FTS | Ranking and linguistic configuration |
| Typos or substring matching | `pg_trgm` | Index size and similarity thresholds |
| Autocomplete, facets, and distributed scale | External engine | Synchronization and operations |
Make the Boundary Deliberate
Measure real queries with EXPLAIN (ANALYZE, BUFFERS). Check latency, index growth, update volume, and result quality before changing systems. An external engine should solve a measured requirement, not merely provide more features.
Exercise
A product needs stemming and transactional consistency, but users also expect minor title misspellings to match. What should you try first?
Show answer
Keep PostgreSQL full-text search for word matching and add a pg_trgm index for title similarity. Reassess with measured latency and relevance data before adopting an external engine.