Home Explore Blog CI



postgresql

7th chunk of `doc/src/sgml/pgtrgm.sgml`
b821560ba9bb2167be0ce39f6d077f26795e1de8e0b9a73a00000001000009e8
 '<replaceable>word</replaceable>' &lt;&lt;% t
  ORDER BY sml DESC, t;
</programlisting>
   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 <replaceable>word</replaceable>,
   sorted from best match to worst.  The index will be used to make this
   a fast operation even over very large data sets.
  </para>

  <para>
   Possible variants of the above queries are:
<programlisting>
SELECT t, '<replaceable>word</replaceable>' &lt;&lt;-&gt; t AS dist
  FROM test_trgm
  ORDER BY dist LIMIT 10;
</programlisting>
   and
<programlisting>
SELECT t, '<replaceable>word</replaceable>' &lt;&lt;&lt;-&gt; t AS dist
  FROM test_trgm
  ORDER BY dist LIMIT 10;
</programlisting>
   This can be implemented quite efficiently by GiST indexes, but not
   by GIN indexes.
  </para>


  <para>
   Beginning in <productname>PostgreSQL</productname> 9.1, these index types also support
   index searches for <literal>LIKE</literal> and <literal>ILIKE</literal>, for example
<programlisting>
SELECT * FROM test_trgm WHERE t LIKE '%foo%bar';
</programlisting>
   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.
  </para>

  <para>
   Beginning in <productname>PostgreSQL</productname> 9.3, these index types also support
   index searches for regular-expression matches
   (<literal>~</literal> and <literal>~*</literal> operators), for example
<programlisting>
SELECT * FROM test_trgm WHERE t ~ '(foo|bar)';
</programlisting>
   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.
  </para>

  <para>
   For both <literal>LIKE</literal> and regular-expression searches, keep in mind
   that a pattern with no extractable trigrams will degenerate to a full-index
   scan.
  </para>

  <para>
   The choice between GiST and GIN indexing depends on the relative
   performance characteristics of GiST and GIN, which are discussed elsewhere.
  </para>
 </sect2>

 <sect2 id="pgtrgm-text-search">
  <title>Text Search Integration</title>

Title: pg_trgm: Advanced Index Searches and Performance Considerations
Summary
This section details how pg_trgm indexes, starting from PostgreSQL 9.1 and 9.3, support index searches for LIKE/ILIKE patterns and regular expressions, respectively. It explains the mechanism of extracting trigrams from search strings for efficient lookups, noting the importance of the number of trigrams for search effectiveness and the potential for full-index scans with patterns lacking extractable trigrams. Finally, it touches on the choice between GiST and GIN indexing based on their respective performance characteristics.