pg_trgm: Trigram Fuzzy Matching and GIN Indexes
Last updated: 2026-11-04
pg_trgm: Trigram Fuzzy Matching and GIN Indexes
pg_trgm splits text into three-character trigrams and indexes them, which enables similarity() scoring, the % operator, and accelerated LIKE / ILIKE matching. Searching for 'Postgre' hits both 'Postgres' and 'PostgreSQL' — the standard technique for typo-tolerant search and near-duplicate detection. With a GIN index using gin_trgm_ops these fuzzy queries go through a bitmap index scan instead of a sequential scan.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE EXTENSION IF NOT EXISTS pg_trgm;
SELECT similarity('Postgres', 'PostgreSQL'); -- similarity from 0 to 1
SELECT * FROM t WHERE name % 'postgre'; -- match when similarity clears the threshold
CREATE INDEX idx_name_trgm ON t USING gin (name gin_trgm_ops);
Returns:CREATE EXTENSION returns success on completion. similarity() returns a real score between 0 and 1; the % operator returns true when the similarity reaches pg_trgm.similarity_threshold (0.3 by default); word_similarity() scores a best contiguous word-segment match.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT name, similarity(name, 'Postgre') AS sim
FROM t_trgm_demo
WHERE name IN ('PostgreSQL', 'Postgres', 'MySQL', 'MongoDB')
ORDER BY sim DESC, name
name | sim
------------+---------
Postgres | 0.7
PostgreSQL | 0.5833333
MongoDB | 0
MySQL | 0
(4 rows)
SELECT name, round(similarity(name, 'Postgres')::numeric, 4) AS sim
FROM t_trgm_demo WHERE name % 'Postgres' ORDER BY sim DESC, name
name | sim
------------+---------
Postgres | 1.0000
PostgreSQL | 0.6667
(2 rows)
EXPLAIN (COSTS OFF) SELECT name FROM t_trgm_demo WHERE name % 'Postgres'
QUERY PLAN
-------------------------------------------------------------------
Bitmap Heap Scan on t_trgm_demo
Recheck Cond: (name % 'Postgres'::text)
-> Bitmap Index Scan on idx_t_trgm_demo_name
Index Cond: (name % 'Postgres'::text)
(4 rows)
📌 Key Points
- In browser PGlite it must be loaded on demand: import { pg_trgm } from '@electric-sql/pglite/contrib/pg_trgm', then new PGlite({ extensions: { pg_trgm } }).
- Size: pg_trgm is about 15 KB — a small extension in the same league as btree_gist (~22 KB) and uuid-ossp (~17 KB); pgcrypto at ~1.1 MB is far larger.
- similarity() scores the overlap of trigram sets and returns a real between 0 and 1. The default threshold, pg_trgm.similarity_threshold = 0.3, is exactly what the % operator compares against.
- The Bitmap Index Scan only happens once a GIN index with gin_trgm_ops exists. This example inserts 5000 rows so the planner reliably picks the index; on a very small table PostgreSQL may fall back to a Seq Scan.
- gin_trgm_ops also supports exact comparisons such as = and !=, but a GIN index is larger and slower to write than a btree — index only the columns that really need fuzzy matching.
- word_similarity() and strict_word_similarity() compare whole words, which suits "does this search term appear inside one word of the field"; similarity() compares the strings as a whole.