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.

Kindextension
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QHow do similarity() and word_similarity() differ?
Asimilarity() compares the trigram overlap of the two strings as a whole, while word_similarity() looks for the best contiguous occurrence of the search term inside the target string. The latter matches the intuition of "does this field contain this word" and avoids matching 'cat' inside 'concatenate'.
QHow does the % operator differ from LIKE '%x%'?
A% compares similarity, so it also matches strings that are merely close in spelling ('Postgres' is matched by % 'postgre'), whereas LIKE '%x%' is a literal substring test. Both are accelerated by gin_trgm_ops, but they answer different questions: one recalls by similarity, the other matches literal text.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏