pg_trgm

text similarity measurement and index searching based on trigrams

Overview

PackageVersionCategoryLicenseLanguage
pg_trgm1.6FTSPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
2390pg_trgmNoYesNoYesNoNo-
Relatedpg_similarity pg_bigm fuzzystrmatch unaccent smlar pgroonga_database rum citext

Version

PG18PG17PG16PG15PG14
1.61.61.61.61.6

Install

Note: This is a built-in contrib extension of PostgreSQL

CREATE EXTENSION pg_trgm;

Usage

pg_trgm: Text similarity measurement and index searching based on trigrams

The pg_trgm module provides functions and operators for determining the similarity of alphanumeric text based on trigram matching, plus index operator classes for fast string similarity searches.

CREATE EXTENSION pg_trgm;

A trigram is a group of three consecutive characters from a string. Two strings are similar if they share many trigrams.

Functions

FunctionDescription
similarity(text, text)realReturns similarity between 0 and 1
show_trgm(text)text[]Returns array of all trigrams in the string
word_similarity(text, text)realSimilarity of first string to most similar word in second
strict_word_similarity(text, text)realSimilar but with stricter word boundary matching
show_limit()real(Deprecated) Returns pg_trgm.similarity_threshold
set_limit(real)real(Deprecated) Sets pg_trgm.similarity_threshold
SELECT similarity('word', 'two words');
-- 0.36363637

SELECT show_trgm('word');
-- {"  w"," wo",ord,"rd ",wor}

Operators

OperatorDescription
text % textbooleanTrue if similarity > pg_trgm.similarity_threshold
text <% textbooleanTrue if word similarity > pg_trgm.word_similarity_threshold
text %> textbooleanCommutator of <%
text <<% textbooleanTrue if strict word similarity > threshold
text %>> textbooleanCommutator of <<%
text <-> textrealDistance (1 - similarity)
text <<-> textrealWord distance (1 - word_similarity)
text <->> textrealCommutator of <<->
text <<<-> textrealStrict word distance
text <->>> textrealCommutator of <<<->

GUC Parameters

ParameterDefaultDescription
pg_trgm.similarity_threshold0.3Threshold for % operator
pg_trgm.word_similarity_threshold0.6Threshold for <% and %> operators
pg_trgm.strict_word_similarity_threshold0.5Threshold for <<% and %>> operators

Index Support

GiST and GIN indexes support the similarity operators:

-- GIN index (faster lookups, slower builds)
CREATE INDEX trgm_idx ON test_trgm USING GIN (t gin_trgm_ops);

-- GiST index (supports distance operators for KNN)
CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops);

-- GiST with custom signature length
CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops(siglen=32));

Text Search Example

Using trigram indexes to speed up LIKE / ILIKE / regex queries:

SELECT t, similarity(t, 'word') AS sml
FROM test_trgm
WHERE t % 'word'
ORDER BY sml DESC, t;

-- KNN search using distance operator
SELECT t, t <-> 'word' AS dist
FROM test_trgm
ORDER BY dist
LIMIT 10;

GIN and GiST trigram indexes also accelerate LIKE, ILIKE, ~, and ~* queries automatically.


Last Modified 2026-03-12: add pg extension catalog (95749bf)