]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/indexes-types.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / indexes-types.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>11.2. Index Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="indexes-intro.html" title="11.1. Introduction" /><link rel="next" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">11.2. Index Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-intro.html" title="11.1. Introduction">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><th width="60%" align="center">Chapter 11. Indexes</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Next</a></td></tr></table><hr /></div><div class="sect1" id="INDEXES-TYPES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.2. Index Types <a href="#INDEXES-TYPES" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPES-BTREE">11.2.1. B-Tree</a></span></dt><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPES-HASH">11.2.2. Hash</a></span></dt><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPE-GIST">11.2.3. GiST</a></span></dt><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPE-SPGIST">11.2.4. SP-GiST</a></span></dt><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPES-GIN">11.2.5. GIN</a></span></dt><dt><span class="sect2"><a href="indexes-types.html#INDEXES-TYPES-BRIN">11.2.6. BRIN</a></span></dt></dl></div><p>
3    <span class="productname">PostgreSQL</span> provides several index types:
4    B-tree, Hash, GiST, SP-GiST, GIN, BRIN, and the extension <a class="link" href="bloom.html" title="F.6. bloom — bloom filter index access method">bloom</a>.
5    Each index type uses a different
6    algorithm that is best suited to different types of indexable clauses.
7    By default, the <a class="link" href="sql-createindex.html" title="CREATE INDEX"><code class="command">CREATE
8    INDEX</code></a> command creates
9    B-tree indexes, which fit the most common situations.
10    The other index types are selected by writing the keyword
11    <code class="literal">USING</code> followed by the index type name.
12    For example, to create a Hash index:
13 </p><pre class="programlisting">
14 CREATE INDEX <em class="replaceable"><code>name</code></em> ON <em class="replaceable"><code>table</code></em> USING HASH (<em class="replaceable"><code>column</code></em>);
15 </pre><p>
16   </p><div class="sect2" id="INDEXES-TYPES-BTREE"><div class="titlepage"><div><div><h3 class="title">11.2.1. B-Tree <a href="#INDEXES-TYPES-BTREE" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.3.2" class="indexterm"></a><a id="id-1.5.10.5.3.3" class="indexterm"></a><p>
17    B-trees can handle equality and range queries on data that can be sorted
18    into some ordering.
19    In particular, the <span class="productname">PostgreSQL</span> query planner
20    will consider using a B-tree index whenever an indexed column is
21    involved in a comparison using one of these operators:
22
23 </p><pre class="synopsis">
24 &lt;   &lt;=   =   &gt;=   &gt;
25 </pre><p>
26
27    Constructs equivalent to combinations of these operators, such as
28    <code class="literal">BETWEEN</code> and <code class="literal">IN</code>, can also be implemented with
29    a B-tree index search.  Also, an <code class="literal">IS NULL</code> or <code class="literal">IS NOT
30    NULL</code> condition on an index column can be used with a B-tree index.
31   </p><p>
32    The optimizer can also use a B-tree index for queries involving the
33    pattern matching operators <code class="literal">LIKE</code> and <code class="literal">~</code>
34    <span class="emphasis"><em>if</em></span> the pattern is a constant and is anchored to
35    the beginning of the string — for example, <code class="literal">col LIKE
36    'foo%'</code> or <code class="literal">col ~ '^foo'</code>, but not
37    <code class="literal">col LIKE '%bar'</code>. However, if your database does not
38    use the C locale you will need to create the index with a special
39    operator class to support indexing of pattern-matching queries; see
40    <a class="xref" href="indexes-opclass.html" title="11.10. Operator Classes and Operator Families">Section 11.10</a> below. It is also possible to use
41    B-tree indexes for <code class="literal">ILIKE</code> and
42    <code class="literal">~*</code>, but only if the pattern starts with
43    non-alphabetic characters, i.e., characters that are not affected by
44    upper/lower case conversion.
45   </p><p>
46    B-tree indexes can also be used to retrieve data in sorted order.
47    This is not always faster than a simple scan and sort, but it is
48    often helpful.
49   </p></div><div class="sect2" id="INDEXES-TYPES-HASH"><div class="titlepage"><div><div><h3 class="title">11.2.2. Hash <a href="#INDEXES-TYPES-HASH" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.4.2" class="indexterm"></a><a id="id-1.5.10.5.4.3" class="indexterm"></a><p>
50    Hash indexes store a 32-bit hash code derived from the
51    value of the indexed column. Hence,
52    such indexes can only handle simple equality comparisons.
53    The query planner will consider using a hash index whenever an
54    indexed column is involved in a comparison using the
55    equal operator:
56
57 </p><pre class="synopsis">
58 =
59 </pre><p>
60   </p></div><div class="sect2" id="INDEXES-TYPE-GIST"><div class="titlepage"><div><div><h3 class="title">11.2.3. GiST <a href="#INDEXES-TYPE-GIST" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.5.2" class="indexterm"></a><a id="id-1.5.10.5.5.3" class="indexterm"></a><p>
61    GiST indexes are not a single kind of index, but rather an infrastructure
62    within which many different indexing strategies can be implemented.
63    Accordingly, the particular operators with which a GiST index can be
64    used vary depending on the indexing strategy (the <em class="firstterm">operator
65    class</em>).  As an example, the standard distribution of
66    <span class="productname">PostgreSQL</span> includes GiST operator classes
67    for several two-dimensional geometric data types, which support indexed
68    queries using these operators:
69
70 </p><pre class="synopsis">
71 &lt;&lt;   &amp;&lt;   &amp;&gt;   &gt;&gt;   &lt;&lt;|   &amp;&lt;|   |&amp;&gt;   |&gt;&gt;   @&gt;   &lt;@   ~=   &amp;&amp;
72 </pre><p>
73
74    (See <a class="xref" href="functions-geometry.html" title="9.11. Geometric Functions and Operators">Section 9.11</a> for the meaning of
75    these operators.)
76    The GiST operator classes included in the standard distribution are
77    documented in <a class="xref" href="gist.html#GIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.1. Built-in GiST Operator Classes">Table 65.1</a>.
78    Many other GiST operator
79    classes are available in the <code class="literal">contrib</code> collection or as separate
80    projects.  For more information see <a class="xref" href="gist.html" title="65.2. GiST Indexes">Section 65.2</a>.
81   </p><p>
82    GiST indexes are also capable of optimizing <span class="quote">“<span class="quote">nearest-neighbor</span>”</span>
83    searches, such as
84 </p><pre class="programlisting">
85 SELECT * FROM places ORDER BY location &lt;-&gt; point '(101,456)' LIMIT 10;
86
87 </pre><p>
88    which finds the ten places closest to a given target point.  The ability
89    to do this is again dependent on the particular operator class being used.
90    In <a class="xref" href="gist.html#GIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.1. Built-in GiST Operator Classes">Table 65.1</a>, operators that can be
91    used in this way are listed in the column <span class="quote">“<span class="quote">Ordering Operators</span>”</span>.
92   </p></div><div class="sect2" id="INDEXES-TYPE-SPGIST"><div class="titlepage"><div><div><h3 class="title">11.2.4. SP-GiST <a href="#INDEXES-TYPE-SPGIST" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.6.2" class="indexterm"></a><a id="id-1.5.10.5.6.3" class="indexterm"></a><p>
93    SP-GiST indexes, like GiST indexes, offer an infrastructure that supports
94    various kinds of searches.  SP-GiST permits implementation of a wide range
95    of different non-balanced disk-based data structures, such as quadtrees,
96    k-d trees, and radix trees (tries).  As an example, the standard distribution of
97    <span class="productname">PostgreSQL</span> includes SP-GiST operator classes
98    for two-dimensional points, which support indexed
99    queries using these operators:
100
101 </p><pre class="synopsis">
102 &lt;&lt;   &gt;&gt;   ~=   &lt;@   &lt;&lt;|   |&gt;&gt;
103 </pre><p>
104
105    (See <a class="xref" href="functions-geometry.html" title="9.11. Geometric Functions and Operators">Section 9.11</a> for the meaning of
106    these operators.)
107    The SP-GiST operator classes included in the standard distribution are
108    documented in <a class="xref" href="spgist.html#SPGIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.2. Built-in SP-GiST Operator Classes">Table 65.2</a>.
109    For more information see <a class="xref" href="spgist.html" title="65.3. SP-GiST Indexes">Section 65.3</a>.
110   </p><p>
111    Like GiST, SP-GiST supports <span class="quote">“<span class="quote">nearest-neighbor</span>”</span> searches.
112    For SP-GiST operator classes that support distance ordering, the
113    corresponding operator is listed in the <span class="quote">“<span class="quote">Ordering Operators</span>”</span>
114    column in <a class="xref" href="spgist.html#SPGIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.2. Built-in SP-GiST Operator Classes">Table 65.2</a>.
115   </p></div><div class="sect2" id="INDEXES-TYPES-GIN"><div class="titlepage"><div><div><h3 class="title">11.2.5. GIN <a href="#INDEXES-TYPES-GIN" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.7.2" class="indexterm"></a><a id="id-1.5.10.5.7.3" class="indexterm"></a><p>
116    GIN indexes are <span class="quote">“<span class="quote">inverted indexes</span>”</span> which are appropriate for
117    data values that contain multiple component values, such as arrays.  An
118    inverted index contains a separate entry for each component value, and
119    can efficiently handle queries that test for the presence of specific
120    component values.
121   </p><p>
122    Like GiST and SP-GiST, GIN can support
123    many different user-defined indexing strategies, and the particular
124    operators with which a GIN index can be used vary depending on the
125    indexing strategy.
126    As an example, the standard distribution of
127    <span class="productname">PostgreSQL</span> includes a GIN operator class
128    for arrays, which supports indexed queries using these operators:
129
130 </p><pre class="synopsis">
131 &lt;@   @&gt;   =   &amp;&amp;
132 </pre><p>
133
134    (See <a class="xref" href="functions-array.html" title="9.19. Array Functions and Operators">Section 9.19</a> for the meaning of
135    these operators.)
136    The GIN operator classes included in the standard distribution are
137    documented in <a class="xref" href="gin.html#GIN-BUILTIN-OPCLASSES-TABLE" title="Table 65.3. Built-in GIN Operator Classes">Table 65.3</a>.
138    Many other GIN operator
139    classes are available in the <code class="literal">contrib</code> collection or as separate
140    projects.  For more information see <a class="xref" href="gin.html" title="65.4. GIN Indexes">Section 65.4</a>.
141   </p></div><div class="sect2" id="INDEXES-TYPES-BRIN"><div class="titlepage"><div><div><h3 class="title">11.2.6. BRIN <a href="#INDEXES-TYPES-BRIN" class="id_link">#</a></h3></div></div></div><a id="id-1.5.10.5.8.2" class="indexterm"></a><a id="id-1.5.10.5.8.3" class="indexterm"></a><p>
142    BRIN indexes (a shorthand for Block Range INdexes) store summaries about
143    the values stored in consecutive physical block ranges of a table.
144    Thus, they are most effective for columns whose values are well-correlated
145    with the physical order of the table rows.
146    Like GiST, SP-GiST and GIN,
147    BRIN can support many different indexing strategies,
148    and the particular operators with which a BRIN index can be used
149    vary depending on the indexing strategy.
150    For data types that have a linear sort order, the indexed data
151    corresponds to the minimum and maximum values of the
152    values in the column for each block range.  This supports indexed queries
153    using these operators:
154
155 </p><pre class="synopsis">
156 &lt;   &lt;=   =   &gt;=   &gt;
157 </pre><p>
158
159    The BRIN operator classes included in the standard distribution are
160    documented in <a class="xref" href="brin.html#BRIN-BUILTIN-OPCLASSES-TABLE" title="Table 65.4. Built-in BRIN Operator Classes">Table 65.4</a>.
161    For more information see <a class="xref" href="brin.html" title="65.5. BRIN Indexes">Section 65.5</a>.
162   </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-intro.html" title="11.1. Introduction">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 11.3. Multicolumn Indexes</td></tr></table></div></body></html>