Some PostgreSQL syntax and features are currently unsupported. For details, see Unsupported
features.
How do trigram indexes work?
Trigram indexes make substring and similarity matches efficient by indexing the unique trigrams of a string. A trigram is a group of three consecutive characters in a string. To display the trigrams within a string, use theshow_trgm() :
- Exact for an equality (
=) or pattern matching (LIKE/ILIKE) search. - Inexact for a similarity (
%) search.
- The spelling of a search term is not exact.
- The exact search term is not known.
% comparison to perform a fuzzy search. When applied to a STRING column, the % operator matches values that meet a configured similarity threshold.
To search for names like “Steven” in column first_name:
Trigram matching only works if the search term has at least 3 characters.
LIKE and ILIKE pattern matching, can be very slow on large datasets. To make both types of searches more efficient, create a trigram index. For an example, see Use a trigram index to speed up fuzzy string matching.
Creation
To create a trigram index, use the syntax that defines an , specifying aSTRING column and the gin_trgm_ops or gist_trgm_ops opclass. It is necessary to specify an opclass in order to enable trigram-based comparisons.
-
Using the PostgreSQL-compatible syntax:
GIN and GiST indexes are implemented identically on CockroachDB.
GIN and GIST are therefore synonymous when
defining a trigram index.-
Using
CREATE INVERTED INDEX:
Comparisons
Trigram indexes onSTRING columns support the following comparison operators:
- equality: . Note that standard may perform better than trigram indexes for equality searches.
- pattern matching (case-sensitive):
- pattern matching (case-insensitive):
- similarity matching: . This operator returns
trueif the strings in the comparison have a similarity that meets or exceeds the threshold set by thepg_trgm.similarity_threshold.
Examples
Create various trigram indexes
Suppose you have a table with the following columns:w.
A GIN index with trigram matching enabled:
Use a trigram index to speed up fuzzy string matching
-
Create a table with a
STRINGcolumn: -
Populate the table with sample values:
-
See how trigram matching performs without a trigram index. Retrieve the columns with values similar to
word, using the%operator. Sort the results by the output of thesimilarity():Values are not included in the results if their similarities do not meet the threshold set by , which defaults to0.3. For example:Notice that the fuzzy search took 30 milliseconds to execute. Without a trigram index, the statement performs a full scan, which you can verify using : -
To speed up the fuzzy search, create a trigram index on column
w: -
Check that the statement uses the trigram index:
-
Execute the statement again and note the improved performance:
-
Pattern matching with
LIKEandILIKEis also accelerated by a trigram index:
Unsupported features
The following PostgreSQL syntax and features are currently unsupported.word_similarity()built-in function.strict_word_similarity()built-in function.%>and<%comparisons and acceleration.<<%and%>>comparisons and acceleration.<->,<<->,<->>,<<<->, and<->>>comparisons.- Acceleration on .
%comparisons,show_trgm, and trigram index creation on .
See also
- Use cases for trigram indexes (When not to use Full Text Search) (2022 blog post)

