]> granicus.if.org Git - postgresql/blob - doc/src/sgml/gist.sgml
GiST improvements:
[postgresql] / doc / src / sgml / gist.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/gist.sgml,v 1.18 2005/05/17 00:59:30 neilc Exp $
3 -->
4
5 <chapter id="GiST">
6 <title>GiST Indexes</title>
7
8 <sect1 id="intro">
9  <title>Introduction</title>
10
11  <para>
12    <indexterm>
13     <primary>index</primary>
14     <secondary>GiST</secondary>
15    </indexterm>
16    <indexterm>
17     <primary>GiST</primary>
18     <see>index</see>
19    </indexterm>
20    <acronym>GiST</acronym> stands for Generalized Search Tree.  It is a
21    balanced, tree-structured access method, that acts as a base template in
22    which to implement arbitrary indexing schemes. B+-trees, R-trees and many
23    other indexing schemes can be implemented in <acronym>GiST</acronym>.
24  </para>
25
26  <para>
27   One advantage of <acronym>GiST</acronym> is that it allows the development
28   of custom data types with the appropriate access methods, by
29   an expert in the domain of the data type, rather than a database expert.
30  </para>
31
32   <para>
33     Some of the information here is derived from the University of California at
34     Berkeley's GiST Indexing Project<ulink
35     url="http://gist.cs.berkeley.edu/">web site</ulink> and 
36     <ulink url="http://citeseer.nj.nec.com/448594.html">
37     Marcel Kornacker's thesis, Access Methods for Next-Generation Database Systems
38     </ulink>.   The <acronym>GiST</acronym>
39     implementation in <productname>PostgreSQL</productname> is primarily
40     maintained by Teodor Sigaev and Oleg Bartunov, and there is more
41     information on their<ulink url="http://www.sai.msu.su/~megera/postgres/gist/">
42     website</ulink>.
43   </para>
44
45 </sect1>
46
47 <sect1 id="extensibility">
48  <title>Extensibility</title>
49
50  <para>
51    Traditionally, implementing a new index access method meant a lot of
52    difficult work.  It was necessary to understand the inner workings of the
53    database, such as the lock manager and Write-Ahead Log.  The
54    <acronym>GiST</acronym> interface has a high level of abstraction,
55    requiring the access method implementor to only implement the semantics of
56    the data type being accessed.  The <acronym>GiST</acronym> layer itself
57    takes care of concurrency, logging and searching the tree structure.
58  </para>
59  
60  <para>
61    This extensibility should not be confused with the extensibility of the
62    other standard search trees in terms of the data they can handle.  For
63    example, <productname>PostgreSQL</productname> supports extensible B+-trees
64    and R-trees. That means that you can use
65    <productname>PostgreSQL</productname> to build a B+-tree or R-tree over any
66    data type you want. But B+-trees only support range predicates
67    (<literal>&lt;</literal>, <literal>=</literal>, <literal>&gt;</literal>),
68    and R-trees only support n-D range queries (contains, contained, equals).
69  </para>
70  
71  <para>
72    So if you index, say, an image collection with a
73    <productname>PostgreSQL</productname> B+-tree, you can only issue queries
74    such as <quote>is imagex equal to imagey</quote>, <quote>is imagex less
75    than imagey</quote> and <quote>is imagex greater than imagey</quote>?
76    Depending on how you define <quote>equals</quote>, <quote>less than</quote>
77    and <quote>greater than</quote> in this context, this could be useful.
78    However, by using a <acronym>GiST</acronym> based index, you could create
79    ways to ask domain-specific questions, perhaps <quote>find all images of
80    horses</quote> or <quote>find all over-exposed images</quote>.
81  </para>
82
83  <para>
84    All it takes to get a <acronym>GiST</acronym> access method up and running
85    is to implement seven user-defined methods, which define the behavior of
86    keys in the tree. Of course these methods have to be pretty fancy to
87    support fancy queries, but for all the standard queries (B+-trees,
88    R-trees, etc.) they're relatively straightforward. In short,
89    <acronym>GiST</acronym> combines extensibility along with generality, code
90    reuse, and a clean interface.
91   </para>
92
93 </sect1>
94
95 <sect1 id="implementation">
96  <title>Implementation</title>
97  
98  <para>
99    There are seven methods that an index operator class for
100    <acronym>GiST</acronym> must provide:
101  </para>
102
103  <variablelist>
104     <varlistentry>
105      <term>consistent</term>
106      <listitem>
107       <para>
108        Given a predicate <literal>p</literal> on a tree page, and a user
109        query, <literal>q</literal>, this method will return false if it is
110        certain that both <literal>p</literal> and <literal>q</literal> cannot
111        be true for a given data item.
112       </para>
113      </listitem>
114     </varlistentry>
115
116     <varlistentry>
117      <term>union</term>
118      <listitem>
119       <para>
120        This method consolidates information in the tree.  Given a set of
121        entries, this function generates a new predicate that is true for all
122        the entries.
123       </para>
124      </listitem>
125     </varlistentry>
126
127     <varlistentry>
128      <term>compress</term>
129      <listitem>
130       <para>
131        Converts the data item into a format suitable for physical storage in
132        an index page.
133       </para>
134      </listitem>
135     </varlistentry>
136
137     <varlistentry>
138      <term>decompress</term>
139      <listitem>
140       <para>
141        The reverse of the <function>compress</function> method.  Converts the
142        index representation of the data item into a format that can be
143        manipulated by the database.
144       </para>
145      </listitem>
146     </varlistentry>
147
148     <varlistentry>
149      <term>penalty</term>
150      <listitem>
151       <para>
152        Returns a value indicating the <quote>cost</quote> of inserting the new
153        entry into a particular branch of the tree.  items will be inserted
154        down the path of least <function>penalty</function> in the tree.
155       </para>
156      </listitem>
157     </varlistentry>
158
159     <varlistentry>
160      <term>picksplit</term>
161      <listitem>
162       <para>
163        When a page split is necessary, this function decides which entries on
164        the page are to stay on the old page, and which are to move to the new
165        page.
166       </para>
167      </listitem>
168     </varlistentry>
169
170     <varlistentry>
171      <term>same</term>
172      <listitem>
173       <para>
174        Returns true if two entries are identical, false otherwise.
175       </para>
176      </listitem>
177     </varlistentry>
178
179   </variablelist>
180
181 </sect1>
182
183 <sect1 id="limitations">
184  <title>Limitations</title>
185
186  <para>
187   The current implementation of <acronym>GiST</acronym> within
188   <productname>PostgreSQL</productname> has some major limitations:
189   <acronym>GiST</acronym> access is not concurrent; the
190   <acronym>GiST</acronym> interface doesn't allow the development of certain
191   data types, such as digital trees (see papers by Aoki et al); and there
192   is not yet any support for write-ahead logging of updates in
193   <acronym>GiST</acronym> indexes.
194  </para>
195
196  <para>
197   Solutions to the concurrency problems appear in Marcel Kornacker's
198   thesis; however these ideas have not yet been put into practice in the
199   <productname>PostgreSQL</productname> implementation.
200  </para>
201
202  <para>
203   The lack of write-ahead logging is just a small matter of programming,
204   but since it isn't done yet, a crash could render a <acronym>GiST</acronym>
205   index inconsistent, forcing a <command>REINDEX</command>.
206  </para>
207
208 </sect1>
209
210 <sect1 id="examples">
211  <title>Examples</title>
212
213  <para>
214   To see example implementations of index methods implemented using
215   <acronym>GiST</acronym>, examine the following contrib modules:
216  </para>
217  
218  <variablelist>
219   <varlistentry>
220    <term>btree_gist</term>
221    <listitem>
222     <para>B-Tree</para>
223    </listitem>
224   </varlistentry>
225
226   <varlistentry>
227    <term>cube</term>
228    <listitem>
229     <para>Indexing for multi-dimensional cubes</para>
230    </listitem>
231   </varlistentry>
232
233   <varlistentry>
234    <term>intarray</term>
235    <listitem>
236     <para>RD-Tree for one-dimensional array of int4 values</para>
237    </listitem>
238   </varlistentry>
239
240   <varlistentry>
241    <term>ltree</term>
242    <listitem>
243     <para>Indexing for tree-like stuctures</para>
244    </listitem>
245   </varlistentry>
246
247   <varlistentry>
248    <term>rtree_gist</term>
249    <listitem>
250     <para>R-Tree</para>
251    </listitem>
252   </varlistentry>
253
254   <varlistentry>
255    <term>seg</term>
256    <listitem>
257     <para>Storage and indexed access for <quote>float ranges</quote></para>
258    </listitem>
259   </varlistentry>
260
261   <varlistentry>
262    <term>tsearch and tsearch2</term>
263    <listitem>
264     <para>Full text indexing</para>
265    </listitem>
266   </varlistentry>
267  </variablelist>
268
269 </sect1>
270
271 </chapter>