postgresql_16:9.6.35.4 Index Support
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > pg_trgm — support for similarity of text using trigram matching > Index Support
The pg_trgm module provides GiST and GIN index operator classes that allow you to create an index over a text column for the purpose of very fast similarity searches. These index types support the above-described similarity operators, and additionally support trigram-based index searches for LIKE, ILIKE, ~, ~* and = queries. The similarity comparisons are case-insensitive in a default build of pg_trgm. Inequality operators are not supported. Note that those indexes may not be as efficient as regular B-tree indexes for equality operator.
Example: CREATE TABLE test_trgm (t text); CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops); or CREATE INDEX trgm_idx ON test_trgm USING GIN (t gin_trgm_ops);
gist_trgm_ops GiST opclass approximates a set of trigrams as a bitmap signature. Its optional integer parameter siglen determines the signature length in bytes. The default length is 12 bytes. Valid values of signature length are between 1 and 2024 bytes. Longer signatures lead to a more precise search (scanning a smaller fraction of the index and fewer heap pages), at the cost of a larger index.
Example of creating such an index with a signature length of 32 bytes:
CREATE INDEX trgm_idx ON test_trgm USING GIST (t gist_trgm_ops(siglen=32));
At this point, you will have an index on the t column that you can use for similarity searching. A typical query is SELECT t, similarity(t, '*word*') AS sml FROM test_trgm WHERE t % '*word*' ORDER BY sml DESC, t; This will return all values in the text column that are sufficiently similar to *word*, sorted from best match to worst. The index will be used to make this a fast operation even over very large data sets.
A variant of the above query is SELECT t, t <-> '*word*' AS dist FROM test_trgm ORDER BY dist LIMIT 10; This can be implemented quite efficiently by GiST indexes, but not by GIN indexes. It will usually beat the first formulation when only a small number of the closest matches is wanted.
Also you can use an index on the t column for word similarity or strict word similarity. Typical queries are: SELECT t, word_similarity('*word*', t) AS sml FROM test_trgm WHERE '*word*' <% t ORDER BY sml DESC, t; and SELECT t, strict_word_similarity('*word*', t) AS sml FROM test_trgm WHERE '*word*' <<% t ORDER BY sml DESC, t; This will return all values in the text column for which there is a continuous extent in the corresponding ordered trigram set that is sufficiently similar to the trigram set of *word*, sorted from best match to worst. The index will be used to make this a fast operation even over very large data sets.
Possible variants of the above queries are: SELECT t, '*word*' <<-> t AS dist FROM test_trgm ORDER BY dist LIMIT 10; and SELECT t, '*word*' <<<-> t AS dist FROM test_trgm ORDER BY dist LIMIT 10; This can be implemented quite efficiently by GiST indexes, but not by GIN indexes.
Beginning in PostgreSQL 9.1, these index types also support index searches for LIKE and ILIKE, for example SELECT * FROM test_trgm WHERE t LIKE '%foo%bar'; The index search works by extracting trigrams from the search string and then looking these up in the index. The more trigrams in the search string, the more effective the index search is. Unlike B-tree based searches, the search string need not be left-anchored.
Beginning in PostgreSQL 9.3, these index types also support index searches for regular-expression matches (~ and ~* operators), for example SELECT * FROM test_trgm WHERE t ~ '(foo|bar)'; The index search works by extracting trigrams from the regular expression and then looking these up in the index. The more trigrams that can be extracted from the regular expression, the more effective the index search is. Unlike B-tree based searches, the search string need not be left-anchored.
For both LIKE and regular-expression searches, keep in mind that a pattern with no extractable trigrams will degenerate to a full-index scan.
The choice between GiST and GIN indexing depends on the relative performance characteristics of GiST and GIN, which are discussed elsewhere.