11 PostgreSQL provides several index types: B-tree, Hash, GiST, SP-GiST,
12 GIN, BRIN, and the extension bloom. Each index type uses a different
13 algorithm that is best suited to different types of indexable clauses.
14 By default, the CREATE INDEX command creates B-tree indexes, which fit
15 the most common situations. The other index types are selected by
16 writing the keyword USING followed by the index type name. For example,
17 to create a Hash index:
18 CREATE INDEX name ON table USING HASH (column);
22 B-trees can handle equality and range queries on data that can be
23 sorted into some ordering. In particular, the PostgreSQL query planner
24 will consider using a B-tree index whenever an indexed column is
25 involved in a comparison using one of these operators:
28 Constructs equivalent to combinations of these operators, such as
29 BETWEEN and IN, can also be implemented with a B-tree index search.
30 Also, an IS NULL or IS NOT NULL condition on an index column can be
31 used with a B-tree index.
33 The optimizer can also use a B-tree index for queries involving the
34 pattern matching operators LIKE and ~ if the pattern is a constant and
35 is anchored to the beginning of the string — for example, col LIKE
36 'foo%' or col ~ '^foo', but not col LIKE '%bar'. However, if your
37 database does not use the C locale you will need to create the index
38 with a special operator class to support indexing of pattern-matching
39 queries; see Section 11.10 below. It is also possible to use B-tree
40 indexes for ILIKE and ~*, but only if the pattern starts with
41 non-alphabetic characters, i.e., characters that are not affected by
42 upper/lower case conversion.
44 B-tree indexes can also be used to retrieve data in sorted order. This
45 is not always faster than a simple scan and sort, but it is often
50 Hash indexes store a 32-bit hash code derived from the value of the
51 indexed column. Hence, such indexes can only handle simple equality
52 comparisons. The query planner will consider using a hash index
53 whenever an indexed column is involved in a comparison using the equal
59 GiST indexes are not a single kind of index, but rather an
60 infrastructure within which many different indexing strategies can be
61 implemented. Accordingly, the particular operators with which a GiST
62 index can be used vary depending on the indexing strategy (the operator
63 class). As an example, the standard distribution of PostgreSQL includes
64 GiST operator classes for several two-dimensional geometric data types,
65 which support indexed queries using these operators:
66 << &< &> >> <<| &<| |&> |>> @> <@ ~= &&
68 (See Section 9.11 for the meaning of these operators.) The GiST
69 operator classes included in the standard distribution are documented
70 in Table 65.1. Many other GiST operator classes are available in the
71 contrib collection or as separate projects. For more information see
74 GiST indexes are also capable of optimizing “nearest-neighbor”
76 SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
79 which finds the ten places closest to a given target point. The ability
80 to do this is again dependent on the particular operator class being
81 used. In Table 65.1, operators that can be used in this way are listed
82 in the column “Ordering Operators”.
86 SP-GiST indexes, like GiST indexes, offer an infrastructure that
87 supports various kinds of searches. SP-GiST permits implementation of a
88 wide range of different non-balanced disk-based data structures, such
89 as quadtrees, k-d trees, and radix trees (tries). As an example, the
90 standard distribution of PostgreSQL includes SP-GiST operator classes
91 for two-dimensional points, which support indexed queries using these
95 (See Section 9.11 for the meaning of these operators.) The SP-GiST
96 operator classes included in the standard distribution are documented
97 in Table 65.2. For more information see Section 65.3.
99 Like GiST, SP-GiST supports “nearest-neighbor” searches. For SP-GiST
100 operator classes that support distance ordering, the corresponding
101 operator is listed in the “Ordering Operators” column in Table 65.2.
105 GIN indexes are “inverted indexes” which are appropriate for data
106 values that contain multiple component values, such as arrays. An
107 inverted index contains a separate entry for each component value, and
108 can efficiently handle queries that test for the presence of specific
111 Like GiST and SP-GiST, GIN can support many different user-defined
112 indexing strategies, and the particular operators with which a GIN
113 index can be used vary depending on the indexing strategy. As an
114 example, the standard distribution of PostgreSQL includes a GIN
115 operator class for arrays, which supports indexed queries using these
119 (See Section 9.19 for the meaning of these operators.) The GIN operator
120 classes included in the standard distribution are documented in
121 Table 65.3. Many other GIN operator classes are available in the
122 contrib collection or as separate projects. For more information see
127 BRIN indexes (a shorthand for Block Range INdexes) store summaries
128 about the values stored in consecutive physical block ranges of a
129 table. Thus, they are most effective for columns whose values are
130 well-correlated with the physical order of the table rows. Like GiST,
131 SP-GiST and GIN, BRIN can support many different indexing strategies,
132 and the particular operators with which a BRIN index can be used vary
133 depending on the indexing strategy. For data types that have a linear
134 sort order, the indexed data corresponds to the minimum and maximum
135 values of the values in the column for each block range. This supports
136 indexed queries using these operators:
139 The BRIN operator classes included in the standard distribution are
140 documented in Table 65.4. For more information see Section 65.5.