]> begriffs open source - ai-pg/blob - full-docs/txt/indexes-types.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / indexes-types.txt
1
2 11.2. Index Types #
3
4    11.2.1. B-Tree
5    11.2.2. Hash
6    11.2.3. GiST
7    11.2.4. SP-GiST
8    11.2.5. GIN
9    11.2.6. BRIN
10
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);
19
20 11.2.1. B-Tree #
21
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:
26 <   <=   =   >=   >
27
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.
32
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.
43
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
46    helpful.
47
48 11.2.2. Hash #
49
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
54    operator:
55 =
56
57 11.2.3. GiST #
58
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 <<   &<   &>   >>   <<|   &<|   |&>   |>>   @>   <@   ~=   &&
67
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
72    Section 65.2.
73
74    GiST indexes are also capable of optimizing “nearest-neighbor”
75    searches, such as
76 SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
77
78
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”.
83
84 11.2.4. SP-GiST #
85
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
92    operators:
93 <<   >>   ~=   <@   <<|   |>>
94
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.
98
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.
102
103 11.2.5. GIN #
104
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
109    component values.
110
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
116    operators:
117 <@   @>   =   &&
118
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
123    Section 65.4.
124
125 11.2.6. BRIN #
126
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:
137 <   <=   =   >=   >
138
139    The BRIN operator classes included in the standard distribution are
140    documented in Table 65.4. For more information see Section 65.5.