The idea · Lab 3.7.1A plain equality or ILIKE search fails in three genuinely different ways: an accented character ('Müller' does not match 'Muller'), a typo ('Robrt' does not match 'Robert'), and a phonetic variant ('Smyth' does not sound different from 'Smith' but is spelled completely differently). Three genuinely different extensions — unaccent, pg_trgm, and fuzzystrmatch — are already installed on this server and fix exactly these three cases, verified directly rather than assumed.
unaccent() is not IMMUTABLE by default, which genuinely blocks a straightforward functional index on it — a real, documented PostgreSQL limitation, fixed here with a small IMMUTABLE wrapper function that pins its own search_path. All three tools are then composed into one real, unified scoring function that ranks results by the best signal any of them find.
Accent-Blind Search, and unaccent()'s Real IMMUTABLE Problem
SELECT * FROM people WHERE name ILIKE '%cafe%'; -- 0 rows: 'Café Central' has an accent
CREATE INDEX ON people USING gin (unaccent(name) gin_trgm_ops);
-- ERROR: functions in index expression must be marked IMMUTABLE
unaccent() depends on a text search dictionary lookup, which PostgreSQL does not consider IMMUTABLE by default — a real, well-known limitation. The genuine fix is a small wrapper function that pins its own search_path so the dictionary always resolves the same way:
CREATE FUNCTION immutable_unaccent(text) RETURNS text AS $$
SELECT unaccent('unaccent', $1)
$$ LANGUAGE sql IMMUTABLE PARALLEL SAFE SET search_path = public, pg_catalog;
CREATE INDEX ON people USING gin (immutable_unaccent(name) gin_trgm_ops);
SELECT * FROM people WHERE immutable_unaccent(name) ILIKE '%cafe%'; -- finds 'Café Central'
Typo-Tolerant Search With pg_trgm
SELECT * FROM people WHERE name = 'Robrt Johnson'; -- 0 rows: real typo, no exact match
SELECT name, similarity(name, 'Robrt Johnson') FROM people WHERE name % 'Robrt Johnson';
name | similarity
------------------+------------
Robert Johnson | 0.7058824
The % operator uses pg_trgm's trigram similarity threshold (default 0.3) directly — no exact match needed at all.
Phonetic Search With fuzzystrmatch
SELECT soundex('Smith'), soundex('Smyth'); -- both genuinely return S530
SELECT levenshtein('Jon Smiht', 'John Smith'); -- a real edit distance of 3
soundex() and metaphone() both encode a name by how it sounds, not how it is spelled — 'Smith' and 'Smyth' genuinely produce the identical code despite looking different. levenshtein() instead scores by literal edit distance, useful for ranking typo-close candidates directly.
One Real, Unified Scoring Function
CREATE FUNCTION fuzzy_search(term text) RETURNS TABLE(name text, score float8) AS $$
SELECT p.name,
GREATEST(similarity(p.name, term), similarity(immutable_unaccent(p.name), immutable_unaccent(term)))
- (levenshtein(p.name, term)::float8 / 100)
FROM people p ORDER BY 2 DESC LIMIT 3
$$ LANGUAGE sql;
SELECT * FROM fuzzy_search('Robrt Johnson'); -- ranks 'Robert Johnson' first, genuinely
Combining a raw trigram score, an accent-folded trigram score, and a small levenshtein penalty into one real, callable function — the same composition a real production search endpoint would use.
Key concepts
unaccent() and IMMUTABLE
unaccent() is genuinely not marked IMMUTABLE by PostgreSQL by default, since its behavior technically depends on which text search dictionary is active — which blocks it from being used directly inside a functional index. The real, standard fix is a small wrapper function that pins search_path explicitly and is itself marked IMMUTABLE, confirmed directly in this lab to make the functional index buildable.
pg_trgm % operator and similarity()
similarity() returns a real score from 0 to 1 based on shared three-character sequences (trigrams) between two strings. The % operator is a shorthand for "similarity() above pg_trgm.similarity_threshold" (0.3 by default), and is the operator a GIN or GiST trigram index can actually accelerate.
soundex() vs metaphone() vs levenshtein()
soundex() and metaphone() both encode how a word sounds, ignoring spelling entirely — useful for names that vary in spelling but not pronunciation. levenshtein() instead counts the real minimum number of single-character edits between two strings, useful for ranking near-miss typos by how close they actually are.
What to remember
- unaccent() is not IMMUTABLE by default — a small wrapper function that pins search_path is the real, standard fix for indexing it
- pg_trgm's % operator and similarity() find real matches with zero exact-match requirement, accelerated by a real trigram GIN/GiST index
- soundex() and metaphone() both encode pronunciation, not spelling — genuinely identical codes for names that sound the same but are spelled differently
- levenshtein() ranks candidates by real edit distance, useful for scoring how close a typo actually is
- All three tools compose cleanly into a single real, callable scoring function — exactly what a production search endpoint would use, not three separate half-fixes