Skip to main content
A trigram index is a type of created on a column. Trigram indexes are used to efficiently search for strings in large tables without providing an exact search term. This page describes how to create and use trigram indexes on CockroachDB.
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 the show_trgm() :
A trigram index stores every unique trigram within each string being indexed. When you search a trigram index for a value, the database retrieves all of the entries in the index that match enough of the trigrams of the search value to satisfy the match. The type of match depends on the comparison operator:
  • Exact for an equality (=) or pattern matching (LIKE/ILIKE) search.
  • Inexact for a similarity (%) search.
Trigrams enable pattern matching even when the prefix of the string is not known. For example:
Fuzzy string matching based on text similarity is useful when:
  • The spelling of a search term is not exact.
  • The exact search term is not known.
For example, if you don’t know how to spell a name in your database, you can use a % 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.
Fuzzy string matching, as well as 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 a STRING 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 on STRING 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 true if the strings in the comparison have a similarity that meets or exceeds the threshold set by the pg_trgm.similarity_threshold .
For usage examples, see Use a trigram index to speed up fuzzy string matching.

Examples

Create various trigram indexes

Suppose you have a table with the following columns:
The following examples illustrate how to create various trigram indexes on column w. A GIN index with trigram matching enabled:
A partial index with trigram matching enabled:
A multi-column index with trigram matching enabled:
An expression index with trigram matching enabled:

Use a trigram index to speed up fuzzy string matching

  1. Create a table with a STRING column:
  2. Populate the table with sample values:
  3. 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 the similarity() :
    Values are not included in the results if their similarities do not meet the threshold set by , which defaults to 0.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 :
  4. To speed up the fuzzy search, create a trigram index on column w:
  5. Check that the statement uses the trigram index:
  6. Execute the statement again and note the improved performance:
  7. Pattern matching with LIKE and ILIKE is 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