OSDN Git Service

Update copyright for 2015.
[pgbigm/pg_bigm.git] / html / pg_bigm_en.html
1 <html>
2 <head>
3 <title>pg_bigm 1.0 Document</title>
4
5 <link rel="stylesheet" type="text/css" href="style.css">
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 </head>
8
9 <body>
10   <div id="navigation">
11     <ul>
12       <li><a href="index_en.html">Home</a></li>
13       <li><a href="http://en.sourceforge.jp/projects/pgbigm/releases/?package_id=13634">Download</a></li>
14       <li><a href="pg_bigm_en.html">Document</a></li>
15       <li><a href="index_en.html#community">Community</a></li>
16       <li><a href="index_en.html#development">Development</a></li>
17       <li><a href="pg_bigm.html">日本語</a></li>
18     </ul>
19   </div>
20
21 <h1 id="pg_bigm">Document (Release 1.0)</h1>
22
23 <div class="index">
24 <ol>
25 <li><a href="#description">Overview</a></li>
26 <li><a href="#pg_trgm">Comparison with pg_trgm</a></li>
27 <li><a href="#requirement">Tested platforms</a></li>
28 <li><a href="#install">Install</a></li>
29 <li><a href="#uninstall">Uninstall</a></li>
30 <li><a href="#fulltext_search">Full text search</a></li>
31 <li><a href="#functions">Functions</a></li>
32 <li><a href="#parametares">Parameters</a></li>
33 <li><a href="#release_notes">Release Notes</a></li>
34 </ol>
35 </div>
36
37 <h2 id="description">Overview</h2>
38 <p>The pg_bigm module provides full text search capability in <a href="http://www.postgresql.org/">PostgreSQL</a>. This module allows a user to create <b>2-gram</b> (bigram) index for faster full text search.</p>
39 <p>The <a href="http://en.sourceforge.jp/projects/pgbigm/">pg_bigm project</a> provides the following one module.</p>
40
41 <table>
42 <thead>
43 <tr>
44 <th>Module</th><th>Description</th><th>Source Archive File Name</th>
45 </tr>
46 </thead>
47 <tbody>
48 <tr><td>pg_bigm</td>
49   <td nowrap>Module that provides full text search capability in PostgreSQL</td>
50   <td>pg_bigm-x.y-YYYYMMDD.tar.gz</td></tr>
51 </tbody>
52 </table>
53
54 <p>
55 The x.y and YYYYMMDD parts of the source archive file name are replaced with its release version number and date, respectively.
56 For example, x.y is 1.1 and YYYYMMDD is 20131122 if the file of the version 1.1 was released on November 22, 2013.
57 </p>
58 <p>The license of pg_bigm is <a href="http://opensource.org/licenses/postgresql">The PostgreSQL License</a> (same as BSD license).</p>
59
60 <h2 id="pg_trgm">Comparison with pg_trgm</h2>
61 <p>The <a href="http://www.postgresql.jp/document/current/html/pgtrgm.html">pg_trgm</a> contrib module which provides full text search capability using 3-gram (trigram) model is included in PostgreSQL. The pg_bigm was developed based on the pg_trgm. They have the following differences:</p>
62
63 <table>
64 <thead>
65 <tr>
66 <th>Functionalities and Features</th><th>pg_trgm</th><th>pg_bigm</th>
67 </tr>
68 </thead>
69 <tbody>
70 <tr><td>Phrase matching method for full text search</td>
71   <td nowrap>3-gram</td>
72   <td>2-gram</td></tr>
73 <tr><td>Available index</td>
74   <td nowrap>GIN and GiST</td>
75   <td>GIN only</td></tr>
76 <tr><td>Full text search for non-alphabetic language<br>(e.g., Japanese)</td>
77   <td nowrap>Not supported (*1)</td>
78   <td>Supported</td></tr>
79 <tr><td>Full text search with 1-2 characters keyword</td>
80   <td nowrap>Slow (*2)</td>
81   <td>Fast</td></tr>
82 <tr><td>Similarity search</td>
83   <td nowrap>Supported</td>
84   <td>Not supported</td></tr>
85 </tbody>
86 </table>
87
88 <ul>
89 <li>(*1) You can use full text search for non-alphabetic language by commenting out KEEPONLYALNUM macro variable in contrib/pg_trgm/pg_trgm.h and rebuilding pg_trgm module. But pg_bigm provides faster non-alphabetic search than such a modified pg_trgm.</li>
90 <li>(*2) Because, in this search, only sequential scan or index full scan (not normal index scan) can run.</li>
91 </ul>
92
93 <p>pg_bigm cannot coexist with pg_trgm in the same database. Please don't load pg_trgm into the database that you want to use pg_bigm in.</p>
94
95 <h2 id="requirement">Tested platforms</h2>
96 <p>pg_bigm has been built and tested on the following platforms:</p>
97 <table>
98 <thead>
99 <tr>
100 <th>Category</th><th>Module Name</th>
101 </tr>
102 </thead>
103 <tbody>
104 <tr>
105   <td>OS</td>
106   <td nowrap>Red Hat Enterprise Linux 6.1 x86_64</td>
107 </tr>
108 <tr>
109   <td>DBMS</td>
110   <td nowrap>PostgreSQL 9.1, 9.2</td>
111 </tr>
112 </tbody>
113 </table>
114
115 <p>pg_bigm requires PostgreSQL 9.1 or later.</p>
116
117 <h2 id="install">Install</h2>
118
119 <h3 id="pg_install">Install PostgreSQL</h3>
120 <p>From the <a href="http://www.postgresql.org/">official site of PostgreSQL</a> download the source archive file "postgresql-X.Y.Z.tar.gz (please replace X.Y.Z with actual version number)" of PostgreSQL, and then build and install it.</p>
121 <pre>
122 $ tar zxf postgresql-X.Y.Z.tar.gz
123 $ cd postgresql-X.Y.Z
124 $ ./configure --prefix=/opt/pgsql-X.Y.Z
125 $ make
126 $ su
127 # make install
128 # exit</pre>
129 <ul>
130 <li>--prefix : Specify the PostgreSQL installation directory. This is optional. By default, PostgreSQL is installed in /usr/local/pgsql.</li>
131 </ul>
132 <p>If PostgreSQL is installed from RPM, the postgresql-devel package must be installed to build pg_bigm.</p>
133
134 <h3 id="bigm_install">Install pg_bigm</h3>
135 <p>Download the source archive file of pg_bigm from <a href="http://en.sourceforge.jp/projects/pgbigm/releases/?package_id=13634">here</a>, and then build and install it.</p>
136 <pre>
137 $ tar zxf pg_bigm-x.y-YYYYMMDD.tar.gz
138 $ cd pg_bigm-x.y-YYYYMMDD
139 $ make USE_PGXS=1 PG_CONFIG=/opt/pgsql-X.Y.Z/bin/pg_config
140 $ su
141 # make USE_PGXS=1 PG_CONFIG=/opt/pgsql-X.Y.Z/bin/pg_config install
142 # exit
143 </pre>
144 <ul>
145 <li>USE_PGXS : USE_PGXS=1 must be always specified when building pg_bigm.</li>
146 <li>PG_CONFIG : Specify the path to <a href="http://www.postgresql.org/docs/current/static/app-pgconfig.html">pg_config</a> (which exists in the bin directory of PostgreSQL installation). If the PATH environment variable contains the path to pg_config, PG_CONFIG doesn't need to be specified.</li>
147 </ul>
148
149 <h3 id="bigm_register">Load pg_bigm</h3>
150 <p>Create the database cluster, modify postgresql.conf, start PostgreSQL server and then load pg_bigm into the database.</p>
151 <pre>
152 $ initdb -D $PGDATA --locale=C --encoding=UTF8
153
154 $ vi $PGDATA/postgresql.conf
155 shared_preload_libraries = 'pg_bigm'
156 custom_variable_classes = 'pg_bigm'
157
158 $ pg_ctl -D $PGDATA start
159 $ psql -d &lt;database name&gt;
160 =# CREATE EXTENSION pg_bigm;
161 =# \dx pg_bigm
162                     List of installed extensions
163   Name   | Version | Schema |              Description
164 ---------+---------+--------+---------------------------------------
165  pg_bigm | 1.0     | public | text index searching based on bigrams
166 (1 row)
167 </pre>
168
169 <ul>
170 <li>Replace $PGDATA with the path to database cluster.</li>
171 <li>pg_bigm supports all PostgreSQL encoding and locale.</li>
172 <li>In postgresql.conf, pg_bigm must be added into <a href="http://www.postgresql.org/docs/current/static/runtime-config-resource.html#GUC-SHARED-PRELOAD-LIBRARIES">shared_preload_libraries</a> and <a href="http://www.postgresql.org/docs/current/static/runtime-config-custom.html#GUC-CUSTOM-VARIABLE-CLASSES">custom_variable_classes</a>.
173   <ul>
174     <li>In PostgreSQL 9.2 or later, you need to set only shared_preload_libraries because custom_variable_classes doesn't exist.</li>
175   </ul>
176 </li>
177 <li><a href="http://www.postgresql.org/docs/current/static/sql-createextension.html">CREATE EXTENSION</a> pg_bigm needs to be executed in all the databases that you want to use pg_bigm in.</li>
178 <li>pg_bigm cannot coexist with pg_trgm in the same database. Please don't load pg_trgm into the database that you want to use pg_bigm in.</li>
179 </ul>
180
181
182 <h2 id="uninstall">Uninstall</h2>
183
184 <h3 id="bigm_uninstall">Delete pg_bigm</h3>
185 <p>Unload pg_bigm from the database and then uninstall it.</p>
186 <pre>
187 $ psql -d &lt;database name&gt;
188 =# DROP EXTENSION pg_bigm CASCADE;
189 =# \q
190
191 $ pg_ctl -D $PGDATA stop
192 $ su
193
194 # cd &lt;pg_bigm source directory&gt;
195 # make USE_PGXS=1 PG_CONFIG=/opt/pgsql-X.Y.Z/bin/pg_config uninstall
196 # exit
197 </pre>
198
199 <ul>
200 <li>pg_bigm needs to be unloaded from all the databases that it was loaded into.</li>
201 <li><a href="http://www.postgresql.org/docs/current/static/sql-dropextension.html">DROP EXTENSION</a> pg_bigm needs to be executed with CASCADE option to delete all the database objects which depend on pg_bigm, e.g., pg_bigm full text search index.</li>
202 </ul>
203
204 <h3 id="delete_conf">Reset postgresql.conf</h3>
205 <p>Delete the following pg_bigm related settings from postgresql.conf.</p>
206 <ul>
207 <li>shared_preload_libraries</li>
208 <li>custom_variable_classes</li>
209 <li>pg_bigm.* (parameters which begin with pg_bigm)</li>
210 </ul>
211
212 <h2 id="fulltext_search">Full text search</h2>
213
214 <h3 id="create_index">Create Index</h3>
215 <p>You can create an index for full text search by using GIN index.</p>
216 <p>The following example creates the table <i>pg_tools</i> which stores the name and description of PostgreSQL related tool, inserts four records into the table, and then creates the full text search index on the <i>description</i> column.</p>
217
218 <pre>
219 =# CREATE TABLE pg_tools (tool text, description text);
220
221 =# INSERT INTO pg_tools VALUES ('pg_hint_plan', 'Tool that allows a user to specify an optimizer HINT to PostgreSQL');
222 =# INSERT INTO pg_tools VALUES ('pg_dbms_stats', 'Tool that allows a user to stabilize planner statistics in PostgreSQL');
223 =# INSERT INTO pg_tools VALUES ('pg_bigm', 'Tool that provides 2-gram full text search capability in PostgreSQL');
224 =# INSERT INTO pg_tools VALUES ('pg_trgm', 'Tool that provides 3-gram full text search capability in PostgreSQL');
225
226 =# CREATE INDEX pg_tools_idx ON pg_tools USING gin (description gin_bigm_ops);
227 </pre>
228
229 <ul>
230 <li><b>gin</b> must be used as an index method. GiST is not available for pg_bigm.</li>
231 <li><b>gin_bigm_ops</b> must be used as an operator class.</li>
232 </ul>
233
234 <p>You can also create multicolumn pg_bigm index and specify GIN related parameters then, as follows.</p>
235
236 <pre>
237 =# CREATE INDEX pg_tools_multi_idx ON pg_tools USING gin (tool gin_bigm_ops, description gin_bigm_ops) WITH (FASTUPDATE = off);
238 </pre>
239
240 <h3 id="do_fulltext_search">Execute full text search</h3>
241 <p>You can execute full text search by using LIKE pattern matching.</p>
242 <pre>
243 =# SELECT * FROM pg_tools WHERE description LIKE '%search%';
244   tool   |                             description                             
245 ---------+---------------------------------------------------------------------
246  pg_bigm | Tool that provides 2-gram full text search capability in PostgreSQL
247  pg_trgm | Tool that provides 3-gram full text search capability in PostgreSQL
248 (2 rows)
249 </pre>
250 <ul>
251 <li>The search keyword must be specified as the pattern string that LIKE operator can handle properly, as discussed in <a href="#likequery">likequery</a>.</li>
252 </ul>
253
254 <h2 id="functions">Functions</h2>
255 <h3 id="likequery">likequery</h3>
256 <p>likequery is a function that converts the search keyword (argument #1) into the pattern string that LIKE operator can handle properly.</p>
257 <ul>
258 <li>Argument #1 (text) - search keyword</li>
259 <li>Return value (text) - pattern string that was converted from argument #1 so that LIKE operator can handle properly</li>
260 </ul>
261
262 <p>If the argument #1 is NULL, the return value is also NULL.</p>
263
264 <p>This function does the conversion as follows:</p>
265 <ul>
266 <li>appends % (single-byte percent) into both the beginning and the end of the search keyword.</li>
267 <li>escapes the characters % (single-byte percent), _ (single-byte underscore) and \ (single-byte backslash) in the search keyword by using \ (single-byte backslash).</li>
268 </ul>
269
270 <p>In pg_bigm, full text search is performed by using LIKE pattern matching. Therefore, the search keyword needs to be converted into the pattern string that LIKE operator can handle properly. Usually a client application should be responsible for this conversion. But, you can save the effort of implementing such a conversion logic in the application by using likequery function.</p>
271
272 <pre>
273 =# SELECT likequery('pg_bigm has improved the full text search performance by 200%');
274                              likequery                             
275 -------------------------------------------------------------------
276  %pg\_bigm has improved the full text search performance by 200\%%
277 (1 row)
278 </pre>
279
280 <p>Using likequery, you can rewrite the full text search query which was used in the example in "Execute full text search" into:</p>
281 <pre>
282 =# SELECT * FROM pg_tools WHERE description LIKE likequery('search');
283   tool   |                             description                             
284 ---------+---------------------------------------------------------------------
285  pg_bigm | Tool that provides 2-gram full text search capability in PostgreSQL
286  pg_trgm | Tool that provides 3-gram full text search capability in PostgreSQL
287 (2 rows)
288 </pre>
289
290 <h3 id="show_bigm">show_bigm</h3>
291 <p>show_bigm returns an array of all the 2-grams in the given string (argument #1).</p>
292
293 <ul>
294 <li>Argument #1 (text) - character string</li>
295 <li>Return value (text[]) - an array of all the 2-grams in argument #1</li>
296 </ul>
297
298 <p>A 2-gram that show_bigm returns is a group of two consecutive characters taken from a string that blank character has been appended into the beginning and the end. For example, the 2-grams of the string "ABC" are "(blank)A" "AB" "BC" "C(blank)".</p>
299
300 <pre>
301 =# SELECT show_bigm('full text search');
302                             show_bigm                             
303 ------------------------------------------------------------------
304  {" f"," s"," t",ar,ch,ea,ex,fu,"h ","l ",ll,rc,se,"t ",te,ul,xt}
305 (1 row)
306 </pre>
307
308 <h3 id="pg_gin_pending_stats">pg_gin_pending_stats</h3>
309 <p>pg_gin_pending_stats is a function that returns the number of pages and tuples in the pending list of GIN index.</p>
310
311 <ul>
312 <li>Argument #1 (regclass) - Name or OID of GIN index</li>
313 <li>Return value #1 (integer) - Number of pages in the pending list</li>
314 <li>Return value #2 (bigint) - Number of tuples in the pending list</li>
315 </ul>
316
317 <p>
318 Note that the return value #1 and #2 are 0 if the argument #1 is the GIN index built with FASTUPDATE option disabled because it doesn't have a pending list.
319 Please see <a href="http://www.postgresql.org/docs/current/static/gin-implementation.html#GIN-FAST-UPDATE">GIN Fast Update Technique</a> for details of the pending list and FASTUPDATE option.
320 </p>
321
322 <pre>
323 =# SELECT * FROM pg_gin_pending_stats('pg_tools_idx');
324  pages | tuples
325 -------+--------
326      1 |      4
327 (1 row)
328 </pre>
329
330 <h2 id="parametares">Parameters</h2>
331 <h3 id="last_update">pg_bigm.last_update</h3>
332 <p>pg_bigm.last_update is a parameter that reports the last updated date of the pg_bigm module. This parameter is read-only. You cannot change the value of this parameter at all.</p>
333
334 <pre>
335 =# SHOW pg_bigm.last_update;
336  pg_bigm.last_update
337 ---------------------
338  2013.11.22
339 (1 row)
340 </pre>
341
342 <h3 id="enable_recheck">pg_bigm.enable_recheck</h3>
343 <p>pg_bigm.enable_recheck is a parameter that specifies whether to perform Recheck which is an internal process of full text search. The default value is on, i.e., Recheck is performed. Not only superuser but also any user can change this parameter value in postgresql.conf or by using SET command. This parameter must be enabled if you want to obtain the correct search result.</p>
344
345 <p>PostgreSQL and pg_bigm internally perform the following processes to get the search results:</p>
346
347 <ul>
348 <li>retrieve the result candidates from full text search index.</li>
349 <li>choose the correct search results from the candidates.</li>
350 </ul>
351
352 <p>The latter process is called Recheck. The result candidates retrieved from full text search index may contain wrong results. Recheck process gets rid of such wrong results.</p>
353
354 <p>For example, imagine the case where two character strings "He is awaiting trial" and "It was a trivial mistake" are stored in a table. The correct search result with the keyword "trial" is "He is awaiting trial". However, "It was a trivial mistake" is also retrieved as the result candidate from the full text search index because it contains all the 2-grams ("al", "ia", "ri", "tr") of the search keyword "trial". Recheck process tests whether each candidate contains the search keyword itself, and then chooses only the correct results.</p>
355
356 <p>How Recheck narrows down the search result can be observed in the result of EXPLAIN ANALYZE.</p>
357
358 <pre>
359 =# CREATE TABLE tbl (doc text);
360 =# INSERT INTO tbl VALUES('He is awaiting trial');
361 =# INSERT INTO tbl VALUES('It was a trivial mistake');
362 =# CREATE INDEX tbl_idx ON tbl USING gin (doc gin_bigm_ops);
363 =# SET enable_seqscan TO off;
364 =# EXPLAIN ANALYZE SELECT * FROM tbl WHERE doc LIKE likequery('trial');
365                                                    QUERY PLAN                                                    
366 -----------------------------------------------------------------------------------------------------------------
367  Bitmap Heap Scan on tbl  (cost=12.00..16.01 rows=1 width=32) (actual time=0.041..0.044 rows=1 loops=1)
368    Recheck Cond: (doc ~~ '%trial%'::text)
369    Rows Removed by Index Recheck: 1
370    -&gt;  Bitmap Index Scan on tbl_idx  (cost=0.00..12.00 rows=1 width=0) (actual time=0.028..0.028 rows=2 loops=1)
371          Index Cond: (doc ~~ '%trial%'::text)
372  Total runtime: 0.113 ms
373 (6 rows)
374 </pre>
375
376 <p>In this example, you can see that Bitmap Index Scan retrieved two rows from the full text search index but Bitmap Heap Scan returned only one row after Recheck process.</p>
377
378 <p>It is possible to skip Recheck process and get the result candidates retrieved from the full text search index as the final results, by disabling this parameter. In the following example, wrong result "It was a trivial mistake" is also returned because the parameter is disabled.</p>
379
380 <pre>
381 =# SELECT * FROM tbl WHERE doc LIKE likequery('trial');
382          doc          
383 ----------------------
384  He is awaiting trial
385 (1 row)
386
387 =# SET pg_bigm.enable_recheck = off;
388 =# SELECT * FROM tbl WHERE doc LIKE likequery('trial');
389            doc            
390 --------------------------
391  He is awaiting trial
392  It was a trivial mistake
393 (2 rows)
394 </pre>
395
396 <p>This parameter must be enabled if you want to obtain the correct search result. On the other hand, you may need to set it to off, for example, for evaluation of Recheck performance overhead or debugging, etc.</p>
397
398 <h3 id="gin_key_limit">pg_bigm.gin_key_limit</h3>
399 <p>pg_bigm.gin_key_limit is a parameter that specifies the maximum number of 2-grams of the search keyword to be used for full text search. If it's set to zero (default), all the 2-grams of the search keyword are used for full text search. Not only superuser but also any user can change this parameter value in postgresql.conf or by using SET command.</p>
400
401 <p>PostgreSQL and pg_bigm basically use all the 2-grams of search keyword to scan GIN index. However, in current implementation of GIN index, the more 2-grams are used, the more performance overhead of GIN index scan is increased. In the system that large search keyword is often used, full text search is likely to be slow. This performance issue can be solved by using this parameter and limiting the maximum number of 2-grams to be used.</p>
402
403 <p>On the other hand, the less 2-grams are used, the more wrong results are included in the result candidates retrieved from full text search index. Please note that this can increase the workload of Recheck and decrease the performance.</p>
404
405 <h2 id="release_notes">Release Notes</h2>
406 <ul>
407 <li><a href="release-1-0-20131122_en.html">Version 1.0, released on 2013-11-22</a></li>
408 </ul>
409
410 <hr>
411 <div align="right">Copyright (c) 2012-2015, NTT DATA Corporation</div>
412
413 </body>
414 </html>