2 12.9. Preferred Index Types for Text Search #
4 There are two kinds of indexes that can be used to speed up full text
5 searches: GIN and GiST. Note that indexes are not mandatory for full
6 text searching, but in cases where a column is searched on a regular
7 basis, an index is usually desirable.
9 To create such an index, do one of:
11 CREATE INDEX name ON table USING GIN (column);
12 Creates a GIN (Generalized Inverted Index)-based index. The
13 column must be of tsvector type.
15 CREATE INDEX name ON table USING GIST (column [ { DEFAULT |
16 tsvector_ops } (siglen = number) ] );
17 Creates a GiST (Generalized Search Tree)-based index. The column
18 can be of tsvector or tsquery type. Optional integer parameter
19 siglen determines signature length in bytes (see below for
22 GIN indexes are the preferred text search index type. As inverted
23 indexes, they contain an index entry for each word (lexeme), with a
24 compressed list of matching locations. Multi-word searches can find the
25 first match, then use the index to remove rows that are lacking
26 additional words. GIN indexes store only the words (lexemes) of
27 tsvector values, and not their weight labels. Thus a table row recheck
28 is needed when using a query that involves weights.
30 A GiST index is lossy, meaning that the index might produce false
31 matches, and it is necessary to check the actual table row to eliminate
32 such false matches. (PostgreSQL does this automatically when needed.)
33 GiST indexes are lossy because each document is represented in the
34 index by a fixed-length signature. The signature length in bytes is
35 determined by the value of the optional integer parameter siglen. The
36 default signature length (when siglen is not specified) is 124 bytes,
37 the maximum signature length is 2024 bytes. The signature is generated
38 by hashing each word into a single bit in an n-bit string, with all
39 these bits OR-ed together to produce an n-bit document signature. When
40 two words hash to the same bit position there will be a false match. If
41 all words in the query have matches (real or false) then the table row
42 must be retrieved to see if the match is correct. Longer signatures
43 lead to a more precise search (scanning a smaller fraction of the index
44 and fewer heap pages), at the cost of a larger index.
46 A GiST index can be covering, i.e., use the INCLUDE clause. Included
47 columns can have data types without any GiST operator class. Included
48 attributes will be stored uncompressed.
50 Lossiness causes performance degradation due to unnecessary fetches of
51 table records that turn out to be false matches. Since random access to
52 table records is slow, this limits the usefulness of GiST indexes. The
53 likelihood of false matches depends on several factors, in particular
54 the number of unique words, so using dictionaries to reduce this number
57 Note that GIN index build time can often be improved by increasing
58 maintenance_work_mem, while GiST index build time is not sensitive to
61 Partitioning of big collections and the proper use of GIN and GiST
62 indexes allows the implementation of very fast searches with online
63 update. Partitioning can be done at the database level using table
64 inheritance, or by distributing documents over servers and collecting
65 external search results, e.g., via Foreign Data access. The latter is
66 possible because ranking functions use only local information.