~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to doc/src/sgml/gin.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!-- $PostgreSQL$ -->
 
2
 
 
3
<chapter id="GIN">
 
4
<title>GIN Indexes</title>
 
5
 
 
6
   <indexterm>
 
7
    <primary>index</primary>
 
8
    <secondary>GIN</secondary>
 
9
   </indexterm>
 
10
 
 
11
<sect1 id="gin-intro">
 
12
 <title>Introduction</title>
 
13
 
 
14
 <para>
 
15
   <acronym>GIN</acronym> stands for Generalized Inverted Index.  It is
 
16
   an index structure storing a set of (key, posting list) pairs, where
 
17
   a <quote>posting list</> is a set of rows in which the key occurs. Each
 
18
   indexed value can contain many keys, so the same row ID can appear in
 
19
   multiple posting lists.
 
20
 </para>
 
21
 
 
22
 <para>
 
23
   It is generalized in the sense that a <acronym>GIN</acronym> index
 
24
   does not need to be aware of the operation that it accelerates.
 
25
   Instead, it uses custom strategies defined for particular data types.
 
26
 </para>
 
27
 
 
28
 <para>
 
29
  One advantage of <acronym>GIN</acronym> is that it allows the development
 
30
  of custom data types with the appropriate access methods, by
 
31
  an expert in the domain of the data type, rather than a database expert.
 
32
  This is much the same advantage as using <acronym>GiST</acronym>.
 
33
 </para>
 
34
 
 
35
 <para>
 
36
  The <acronym>GIN</acronym>
 
37
  implementation in <productname>PostgreSQL</productname> is primarily
 
38
  maintained by Teodor Sigaev and Oleg Bartunov. There is more
 
39
  information about <acronym>GIN</acronym> on their
 
40
  <ulink url="http://www.sai.msu.su/~megera/wiki/Gin">website</ulink>.
 
41
 </para>
 
42
</sect1>
 
43
 
 
44
<sect1 id="gin-extensibility">
 
45
 <title>Extensibility</title>
 
46
 
 
47
 <para>
 
48
   The <acronym>GIN</acronym> interface has a high level of abstraction,
 
49
   requiring the access method implementer only to implement the semantics of
 
50
   the data type being accessed.  The <acronym>GIN</acronym> layer itself
 
51
   takes care of concurrency, logging and searching the tree structure.
 
52
 </para>
 
53
 
 
54
 <para>
 
55
   All it takes to get a <acronym>GIN</acronym> access method working is to
 
56
   implement four (or five) user-defined methods, which define the behavior of
 
57
   keys in the tree and the relationships between keys, indexed values,
 
58
   and indexable queries. In short, <acronym>GIN</acronym> combines
 
59
   extensibility with generality, code reuse, and a clean interface.
 
60
 </para>
 
61
 
 
62
 <para>
 
63
   The four methods that an operator class for
 
64
   <acronym>GIN</acronym> must provide are:
 
65
 </para>
 
66
 
 
67
 <variablelist>
 
68
    <varlistentry>
 
69
     <term>int compare(Datum a, Datum b)</term>
 
70
     <listitem>
 
71
      <para>
 
72
       Compares keys (not indexed values!) and returns an integer less than
 
73
       zero, zero, or greater than zero, indicating whether the first key is
 
74
       less than, equal to, or greater than the second.
 
75
      </para>
 
76
     </listitem>
 
77
    </varlistentry>
 
78
 
 
79
    <varlistentry>
 
80
     <term>Datum *extractValue(Datum inputValue, int32 *nkeys)</term>
 
81
     <listitem>
 
82
      <para>
 
83
       Returns an array of keys given a value to be indexed.  The
 
84
       number of returned keys must be stored into <literal>*nkeys</>.
 
85
      </para>
 
86
     </listitem>
 
87
    </varlistentry>
 
88
 
 
89
    <varlistentry>
 
90
     <term>Datum *extractQuery(Datum query, int32 *nkeys,
 
91
        StrategyNumber n, bool **pmatch, Pointer **extra_data)</term>
 
92
     <listitem>
 
93
      <para>
 
94
       Returns an array of keys given a value to be queried; that is,
 
95
       <literal>query</> is the value on the right-hand side of an
 
96
       indexable operator whose left-hand side is the indexed column.
 
97
       <literal>n</> is the strategy number of the operator within the
 
98
       operator class (see <xref linkend="xindex-strategies">).
 
99
       Often, <function>extractQuery</> will need
 
100
       to consult <literal>n</> to determine the data type of
 
101
       <literal>query</> and the key values that need to be extracted.
 
102
       The number of returned keys must be stored into <literal>*nkeys</>.
 
103
       If the query contains no keys then <function>extractQuery</>
 
104
       should store 0 or -1 into <literal>*nkeys</>, depending on the
 
105
       semantics of the operator.  0 means that every
 
106
       value matches the <literal>query</> and a sequential scan should be
 
107
       performed.  -1 means nothing can match the <literal>query</>.
 
108
       <literal>pmatch</> is an output argument for use when partial match
 
109
       is supported.  To use it, <function>extractQuery</> must allocate
 
110
       an array of <literal>*nkeys</> booleans and store its address at
 
111
       <literal>*pmatch</>.  Each element of the array should be set to TRUE
 
112
       if the corresponding key requires partial match, FALSE if not.
 
113
       If <literal>*pmatch</> is set to NULL then GIN assumes partial match
 
114
       is not required.  The variable is initialized to NULL before call,
 
115
       so this argument can simply be ignored by operator classes that do
 
116
       not support partial match.
 
117
       <literal>extra_data</> is an output argument that allows
 
118
       <function>extractQuery</> to pass additional data to the
 
119
       <function>consistent</> and <function>comparePartial</> methods.
 
120
       To use it, <function>extractQuery</> must allocate
 
121
       an array of <literal>*nkeys</> Pointers and store its address at
 
122
       <literal>*extra_data</>, then store whatever it wants to into the
 
123
       individual pointers.  The variable is initialized to NULL before
 
124
       call, so this argument can simply be ignored by operator classes that
 
125
       do not require extra data.  If <literal>*extra_data</> is set, the
 
126
       whole array is passed to the <function>consistent</> method, and
 
127
       the appropriate element to the <function>comparePartial</> method.
 
128
      </para>
 
129
 
 
130
     </listitem>
 
131
    </varlistentry>
 
132
 
 
133
    <varlistentry>
 
134
     <term>bool consistent(bool check[], StrategyNumber n, Datum query,
 
135
                           int32 nkeys, Pointer extra_data[], bool *recheck)</term>
 
136
     <listitem>
 
137
      <para>
 
138
       Returns TRUE if the indexed value satisfies the query operator with
 
139
       strategy number <literal>n</> (or might satisfy, if the recheck
 
140
       indication is returned).  The <literal>check</> array has length
 
141
       <literal>nkeys</>, which is the same as the number of keys previously
 
142
       returned by <function>extractQuery</> for this <literal>query</> datum.
 
143
       Each element of the
 
144
       <literal>check</> array is TRUE if the indexed value contains the
 
145
       corresponding query key, ie, if (check[i] == TRUE) the i-th key of the
 
146
       <function>extractQuery</> result array is present in the indexed value.
 
147
       The original <literal>query</> datum (not the extracted key array!) is
 
148
       passed in case the <function>consistent</> method needs to consult it.
 
149
       <literal>extra_data</> is the extra-data array returned by
 
150
       <function>extractQuery</>, or NULL if none.
 
151
       On success, <literal>*recheck</> should be set to TRUE if the heap
 
152
       tuple needs to be rechecked against the query operator, or FALSE if
 
153
       the index test is exact.
 
154
      </para>
 
155
     </listitem>
 
156
    </varlistentry>
 
157
 
 
158
  </variablelist>
 
159
 
 
160
 <para>
 
161
  Optionally, an operator class for
 
162
  <acronym>GIN</acronym> can supply a fifth method:
 
163
 </para>
 
164
 
 
165
  <variablelist>
 
166
 
 
167
    <varlistentry>
 
168
     <term>int comparePartial(Datum partial_key, Datum key, StrategyNumber n,
 
169
                              Pointer extra_data)</term>
 
170
     <listitem>
 
171
      <para>
 
172
       Compare a partial-match query to an index key.  Returns an integer
 
173
       whose sign indicates the result: less than zero means the index key
 
174
       does not match the query, but the index scan should continue; zero
 
175
       means that the index key does match the query; greater than zero
 
176
       indicates that the index scan should stop because no more matches
 
177
       are possible.  The strategy number <literal>n</> of the operator
 
178
       that generated the partial match query is provided, in case its
 
179
       semantics are needed to determine when to end the scan.  Also,
 
180
       <literal>extra_data</> is the corresponding element of the extra-data
 
181
       array made by <function>extractQuery</>, or NULL if none.
 
182
      </para>
 
183
     </listitem>
 
184
    </varlistentry>
 
185
 
 
186
  </variablelist>
 
187
 
 
188
 <para>
 
189
  To support <quote>partial match</> queries, an operator class must
 
190
  provide the <function>comparePartial</> method, and its
 
191
  <function>extractQuery</> method must set the <literal>pmatch</>
 
192
  parameter when a partial-match query is encountered.  See
 
193
  <xref linkend="gin-partial-match"> for details.
 
194
 </para>
 
195
 
 
196
</sect1>
 
197
 
 
198
<sect1 id="gin-implementation">
 
199
 <title>Implementation</title>
 
200
 
 
201
 <para>
 
202
  Internally, a <acronym>GIN</acronym> index contains a B-tree index
 
203
  constructed over keys, where each key is an element of the indexed value
 
204
  (a member of an array, for example) and where each tuple in a leaf page is
 
205
  either a pointer to a B-tree over heap pointers (PT, posting tree), or a
 
206
  list of heap pointers (PL, posting list) if the list is small enough.
 
207
 </para>
 
208
 
 
209
 <sect2 id="gin-fast-update">
 
210
  <title>GIN fast update technique</title>
 
211
 
 
212
  <para>
 
213
   Updating a <acronym>GIN</acronym> index tends to be slow because of the
 
214
   intrinsic nature of inverted indexes: inserting or updating one heap row
 
215
   can cause many inserts into the index (one for each key extracted
 
216
   from the indexed value). As of <productname>PostgreSQL</productname> 8.4,
 
217
   <acronym>GIN</> is capable of postponing much of this work by inserting
 
218
   new tuples into a temporary, unsorted list of pending entries.
 
219
   When the table is vacuumed, or if the pending list becomes too large
 
220
   (larger than <xref linkend="guc-work-mem">), the entries are moved to the
 
221
   main <acronym>GIN</acronym> data structure using the same bulk insert
 
222
   techniques used during initial index creation.  This greatly improves
 
223
   <acronym>GIN</acronym> index update speed, even counting the additional
 
224
   vacuum overhead.  Moreover the overhead can be done by a background
 
225
   process instead of in foreground query processing.
 
226
  </para>
 
227
 
 
228
  <para>
 
229
   The main disadvantage of this approach is that searches must scan the list
 
230
   of pending entries in addition to searching the regular index, and so
 
231
   a large list of pending entries will slow searches significantly.
 
232
   Another disadvantage is that, while most updates are fast, an update
 
233
   that causes the pending list to become <quote>too large</> will incur an
 
234
   immediate cleanup cycle and thus be much slower than other updates.
 
235
   Proper use of autovacuum can minimize both of these problems.
 
236
  </para>
 
237
 
 
238
  <para>
 
239
   If consistent response time is more important than update speed,
 
240
   use of pending entries can be disabled by turning off the
 
241
   <literal>FASTUPDATE</literal> storage parameter for a
 
242
   <acronym>GIN</acronym> index.  See <xref linkend="sql-createindex"
 
243
   endterm="sql-createindex-title"> for details.
 
244
  </para>
 
245
 </sect2>
 
246
 
 
247
 <sect2 id="gin-partial-match">
 
248
  <title>Partial match algorithm</title>
 
249
 
 
250
  <para>
 
251
   GIN can support <quote>partial match</> queries, in which the query
 
252
   does not determine an exact match for one or more keys, but the possible
 
253
   matches fall within a reasonably narrow range of key values (within the
 
254
   key sorting order determined by the <function>compare</> support method).
 
255
   The <function>extractQuery</> method, instead of returning a key value
 
256
   to be matched exactly, returns a key value that is the lower bound of
 
257
   the range to be searched, and sets the <literal>pmatch</> flag true.
 
258
   The key range is then searched using the <function>comparePartial</>
 
259
   method.  <function>comparePartial</> must return zero for an actual
 
260
   match, less than zero for a non-match that is still within the range
 
261
   to be searched, or greater than zero if the index key is past the range
 
262
   that could match.
 
263
  </para>
 
264
 </sect2>
 
265
 
 
266
</sect1>
 
267
 
 
268
<sect1 id="gin-tips">
 
269
<title>GIN tips and tricks</title>
 
270
 
 
271
 <variablelist>
 
272
  <varlistentry>
 
273
   <term>Create vs insert</term>
 
274
   <listitem>
 
275
    <para>
 
276
     Insertion into a <acronym>GIN</acronym> index can be slow
 
277
     due to the likelihood of many keys being inserted for each value.
 
278
     So, for bulk insertions into a table it is advisable to drop the GIN
 
279
     index and recreate it after finishing bulk insertion.
 
280
    </para>
 
281
 
 
282
    <para>
 
283
     As of <productname>PostgreSQL</productname> 8.4, this advice is less
 
284
     necessary since delayed indexing is used (see <xref
 
285
     linkend="gin-fast-update"> for details).  But for very large updates
 
286
     it may still be best to drop and recreate the index.
 
287
    </para>
 
288
   </listitem>
 
289
  </varlistentry>
 
290
 
 
291
  <varlistentry>
 
292
   <term><xref linkend="guc-maintenance-work-mem"></term>
 
293
   <listitem>
 
294
    <para>
 
295
     Build time for a <acronym>GIN</acronym> index is very sensitive to
 
296
     the <varname>maintenance_work_mem</> setting; it doesn't pay to
 
297
     skimp on work memory during index creation.
 
298
    </para>
 
299
   </listitem>
 
300
  </varlistentry>
 
301
 
 
302
  <varlistentry>
 
303
   <term><xref linkend="guc-work-mem"></term>
 
304
   <listitem>
 
305
    <para>
 
306
     During a series of insertions into an existing <acronym>GIN</acronym>
 
307
     index that has <literal>FASTUPDATE</> enabled, the system will clean up
 
308
     the pending-entry list whenever it grows larger than
 
309
     <varname>work_mem</>.  To avoid fluctuations in observed response time,
 
310
     it's desirable to have pending-list cleanup occur in the background
 
311
     (i.e., via autovacuum).  Foreground cleanup operations can be avoided by
 
312
     increasing <varname>work_mem</> or making autovacuum more aggressive.
 
313
     However, enlarging <varname>work_mem</> means that if a foreground
 
314
     cleanup does occur, it will take even longer.
 
315
    </para>
 
316
   </listitem>
 
317
  </varlistentry>
 
318
 
 
319
  <varlistentry>
 
320
   <term><xref linkend="guc-gin-fuzzy-search-limit"></term>
 
321
   <listitem>
 
322
    <para>
 
323
     The primary goal of developing <acronym>GIN</acronym> indexes was
 
324
     to create support for highly scalable, full-text search in
 
325
     <productname>PostgreSQL</productname>, and there are often situations when
 
326
     a full-text search returns a very large set of results.  Moreover, this
 
327
     often happens when the query contains very frequent words, so that the
 
328
     large result set is not even useful.  Since reading many
 
329
     tuples from the disk and sorting them could take a lot of time, this is
 
330
     unacceptable for production.  (Note that the index search itself is very
 
331
     fast.)
 
332
    </para>
 
333
    <para>
 
334
     To facilitate controlled execution of such queries
 
335
     <acronym>GIN</acronym> has a configurable soft upper limit on the
 
336
     number of rows returned, the
 
337
     <varname>gin_fuzzy_search_limit</varname> configuration parameter.
 
338
     It is set to 0 (meaning no limit) by default.
 
339
     If a non-zero limit is set, then the returned set is a subset of
 
340
     the whole result set, chosen at random.
 
341
    </para>
 
342
    <para>
 
343
     <quote>Soft</quote> means that the actual number of returned results
 
344
     could differ slightly from the specified limit, depending on the query
 
345
     and the quality of the system's random number generator.
 
346
    </para>
 
347
   </listitem>
 
348
  </varlistentry>
 
349
 </variablelist>
 
350
 
 
351
</sect1>
 
352
 
 
353
<sect1 id="gin-limit">
 
354
 <title>Limitations</title>
 
355
 
 
356
 <para>
 
357
  <acronym>GIN</acronym> doesn't support full index scans: because there are
 
358
  often many keys per value, each heap pointer would be returned many times,
 
359
  and there is no easy way to prevent this.
 
360
 </para>
 
361
 
 
362
 <para>
 
363
  When <function>extractQuery</function> returns zero keys,
 
364
  <acronym>GIN</acronym> will emit an error.  Depending on the operator,
 
365
  a void query might match all, some, or none of the indexed values (for
 
366
  example, every array contains the empty array, but does not overlap the
 
367
  empty array), and <acronym>GIN</acronym> cannot determine the correct
 
368
  answer, nor produce a full-index-scan result if it could determine that
 
369
  that was correct.
 
370
 </para>
 
371
 
 
372
 <para>
 
373
  It is not an error for <function>extractValue</> to return zero keys,
 
374
  but in this case the indexed value will be unrepresented in the index.
 
375
  This is another reason why full index scan is not useful &mdash; it would
 
376
  miss such rows.
 
377
 </para>
 
378
 
 
379
 <para>
 
380
  It is possible for an operator class to circumvent the restriction against
 
381
  full index scan.  To do that, <function>extractValue</> must return at least
 
382
  one (possibly dummy) key for every indexed value, and
 
383
  <function>extractQuery</function> must convert an unrestricted search into
 
384
  a partial-match query that will scan the whole index.  This is inefficient
 
385
  but might be necessary to avoid corner-case failures with operators such
 
386
  as <literal>LIKE</>.
 
387
 </para>
 
388
</sect1>
 
389
 
 
390
<sect1 id="gin-examples">
 
391
 <title>Examples</title>
 
392
 
 
393
 <para>
 
394
  The <productname>PostgreSQL</productname> source distribution includes
 
395
  <acronym>GIN</acronym> operator classes for <type>tsvector</> and
 
396
  for one-dimensional arrays of all internal types.  Prefix searching in
 
397
  <type>tsvector</> is implemented using the <acronym>GIN</> partial match
 
398
  feature.
 
399
  The following <filename>contrib</> modules also contain
 
400
  <acronym>GIN</acronym> operator classes:
 
401
 </para>
 
402
 
 
403
 <variablelist>
 
404
  <varlistentry>
 
405
   <term>btree-gin</term>
 
406
   <listitem>
 
407
    <para>B-Tree equivalent functionality for several data types</para>
 
408
   </listitem>
 
409
  </varlistentry>
 
410
 
 
411
  <varlistentry>
 
412
   <term>hstore</term>
 
413
   <listitem>
 
414
    <para>Module for storing (key, value) pairs</para>
 
415
   </listitem>
 
416
  </varlistentry>
 
417
 
 
418
  <varlistentry>
 
419
   <term>intarray</term>
 
420
   <listitem>
 
421
    <para>Enhanced support for int4[]</para>
 
422
   </listitem>
 
423
  </varlistentry>
 
424
 
 
425
  <varlistentry>
 
426
   <term>pg_trgm</term>
 
427
   <listitem>
 
428
    <para>Text similarity using trigram matching</para>
 
429
   </listitem>
 
430
  </varlistentry>
 
431
 </variablelist>
 
432
</sect1>
 
433
 
 
434
</chapter>