/
Ao
/
MaxReport
Обзор
Документация
Войти
/
Ao
/
MaxReport
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
pgsql/doc/src/sgml/html/gin.html
420 строк
37 KB
AoAnima
first_commit
13 июл 2026, 17:47
13 июл 2026, 17:47
f1a670a
Код
Авторство
О чём код?
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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>65.4. GIN Indexes</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="spgist.html" title="65.3. SP-GiST Indexes" /><link rel="next" href="brin.html" title="65.5. BRIN 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">65.4. GIN Indexes</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spgist.html" title="65.3. SP-GiST Indexes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indextypes.html" title="Chapter 65. Built-in Index Access Methods">Up</a></td><th width="60%" align="center">Chapter 65. Built-in Index Access Methods</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="brin.html" title="65.5. BRIN Indexes">Next</a></td></tr></table><hr /></div><div class="sect1" id="GIN"><div class="titlepage"><div><div><h2 class="title" style="clear: both">65.4. GIN Indexes <a href="#GIN" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="gin.html#GIN-INTRO">65.4.1. Introduction</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-BUILTIN-OPCLASSES">65.4.2. Built-in Operator Classes</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-EXTENSIBILITY">65.4.3. Extensibility</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-IMPLEMENTATION">65.4.4. Implementation</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-TIPS">65.4.5. GIN Tips and Tricks</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-LIMIT">65.4.6. Limitations</a></span></dt><dt><span class="sect2"><a href="gin.html#GIN-EXAMPLES">65.4.7. Examples</a></span></dt></dl></div><a id="id-1.10.17.5.2" class="indexterm"></a><div class="sect2" id="GIN-INTRO"><div class="titlepage"><div><div><h3 class="title">65.4.1. Introduction <a href="#GIN-INTRO" class="id_link">#</a></h3></div></div></div><p> <acronym class="acronym">GIN</acronym> stands for Generalized Inverted Index. <acronym class="acronym">GIN</acronym> is designed for handling cases where the items to be indexed are composite values, and the queries to be handled by the index need to search for element values that appear within the composite items. For example, the items could be documents, and the queries could be searches for documents containing specific words. </p><p> We use the word <em class="firstterm">item</em> to refer to a composite value that is to be indexed, and the word <em class="firstterm">key</em> to refer to an element value. <acronym class="acronym">GIN</acronym> always stores and searches for keys, not item values per se. </p><p> A <acronym class="acronym">GIN</acronym> index stores a set of (key, posting list) pairs, where a <em class="firstterm">posting list</em> is a set of row IDs in which the key occurs. The same row ID can appear in multiple posting lists, since an item can contain more than one key. Each key value is stored only once, so a <acronym class="acronym">GIN</acronym> index is very compact for cases where the same key appears many times. </p><p> <acronym class="acronym">GIN</acronym> is generalized in the sense that the <acronym class="acronym">GIN</acronym> access method code does not need to know the specific operations that it accelerates. Instead, it uses custom strategies defined for particular data types. The strategy defines how keys are extracted from indexed items and query conditions, and how to determine whether a row that contains some of the key values in a query actually satisfies the query. </p><p> One advantage of <acronym class="acronym">GIN</acronym> is that it allows the development of custom data types with the appropriate access methods, by an expert in the domain of the data type, rather than a database expert. This is much the same advantage as using <acronym class="acronym">GiST</acronym>. </p><p> The <acronym class="acronym">GIN</acronym> implementation in <span class="productname">PostgreSQL</span> is primarily maintained by Teodor Sigaev and Oleg Bartunov. There is more information about <acronym class="acronym">GIN</acronym> on their <a class="ulink" href="http://www.sai.msu.su/~megera/wiki/Gin" target="_top">website</a>. </p></div><div class="sect2" id="GIN-BUILTIN-OPCLASSES"><div class="titlepage"><div><div><h3 class="title">65.4.2. Built-in Operator Classes <a href="#GIN-BUILTIN-OPCLASSES" class="id_link">#</a></h3></div></div></div><p> The core <span class="productname">PostgreSQL</span> distribution includes the <acronym class="acronym">GIN</acronym> operator classes shown in <a class="xref" href="gin.html#GIN-BUILTIN-OPCLASSES-TABLE" title="Table 65.3. Built-in GIN Operator Classes">Table 65.3</a>. (Some of the optional modules described in <a class="xref" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Appendix F</a> provide additional <acronym class="acronym">GIN</acronym> operator classes.) </p><div class="table" id="GIN-BUILTIN-OPCLASSES-TABLE"><p class="title"><strong>Table 65.3. Built-in <acronym class="acronym">GIN</acronym> Operator Classes</strong></p><div class="table-contents"><table class="table" summary="Built-in GIN Operator Classes" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Name</th><th>Indexable Operators</th></tr></thead><tbody><tr><td rowspan="4" valign="middle"><code class="literal">array_ops</code></td><td><code class="literal">&& (anyarray,anyarray)</code></td></tr><tr><td><code class="literal">@> (anyarray,anyarray)</code></td></tr><tr><td><code class="literal"><@ (anyarray,anyarray)</code></td></tr><tr><td><code class="literal">= (anyarray,anyarray)</code></td></tr><tr><td rowspan="6" valign="middle"><code class="literal">jsonb_ops</code></td><td><code class="literal">@> (jsonb,jsonb)</code></td></tr><tr><td><code class="literal">@? (jsonb,jsonpath)</code></td></tr><tr><td><code class="literal">@@ (jsonb,jsonpath)</code></td></tr><tr><td><code class="literal">? (jsonb,text)</code></td></tr><tr><td><code class="literal">?| (jsonb,text[])</code></td></tr><tr><td><code class="literal">?& (jsonb,text[])</code></td></tr><tr><td rowspan="3" valign="middle"><code class="literal">jsonb_path_ops</code></td><td><code class="literal">@> (jsonb,jsonb)</code></td></tr><tr><td><code class="literal">@? (jsonb,jsonpath)</code></td></tr><tr><td><code class="literal">@@ (jsonb,jsonpath)</code></td></tr><tr><td valign="middle"><code class="literal">tsvector_ops</code></td><td><code class="literal">@@ (tsvector,tsquery)</code></td></tr></tbody></table></div></div><br class="table-break" /><p> Of the two operator classes for type <code class="type">jsonb</code>, <code class="literal">jsonb_ops</code> is the default. <code class="literal">jsonb_path_ops</code> supports fewer operators but offers better performance for those operators. See <a class="xref" href="datatype-json.html#JSON-INDEXING" title="8.14.4. jsonb Indexing">Section 8.14.4</a> for details. </p></div><div class="sect2" id="GIN-EXTENSIBILITY"><div class="titlepage"><div><div><h3 class="title">65.4.3. Extensibility <a href="#GIN-EXTENSIBILITY" class="id_link">#</a></h3></div></div></div><p> The <acronym class="acronym">GIN</acronym> interface has a high level of abstraction, requiring the access method implementer only to implement the semantics of the data type being accessed. The <acronym class="acronym">GIN</acronym> layer itself takes care of concurrency, logging and searching the tree structure. </p><p> All it takes to get a <acronym class="acronym">GIN</acronym> access method working is to implement a few user-defined methods, which define the behavior of keys in the tree and the relationships between keys, indexed items, and indexable queries. In short, <acronym class="acronym">GIN</acronym> combines extensibility with generality, code reuse, and a clean interface. </p><p> There are two methods that an operator class for <acronym class="acronym">GIN</acronym> must provide: </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">Datum *extractValue(Datum itemValue, int32 *nkeys, bool **nullFlags)</code></span></dt><dd><p> Returns a palloc'd array of keys given an item to be indexed. The number of returned keys must be stored into <code class="literal">*nkeys</code>. If any of the keys can be null, also palloc an array of <code class="literal">*nkeys</code> <code class="type">bool</code> fields, store its address at <code class="literal">*nullFlags</code>, and set these null flags as needed. <code class="literal">*nullFlags</code> can be left <code class="symbol">NULL</code> (its initial value) if all keys are non-null. The return value can be <code class="symbol">NULL</code> if the item contains no keys. </p></dd><dt><span class="term"><code class="function">Datum *extractQuery(Datum query, int32 *nkeys, StrategyNumber n, bool **pmatch, Pointer **extra_data, bool **nullFlags, int32 *searchMode)</code></span></dt><dd><p> Returns a palloc'd array of keys given a value to be queried; that is, <code class="literal">query</code> is the value on the right-hand side of an indexable operator whose left-hand side is the indexed column. <code class="literal">n</code> is the strategy number of the operator within the operator class (see <a class="xref" href="xindex.html#XINDEX-STRATEGIES" title="36.16.2. Index Method Strategies">Section 36.16.2</a>). Often, <code class="function">extractQuery</code> will need to consult <code class="literal">n</code> to determine the data type of <code class="literal">query</code> and the method it should use to extract key values. The number of returned keys must be stored into <code class="literal">*nkeys</code>. If any of the keys can be null, also palloc an array of <code class="literal">*nkeys</code> <code class="type">bool</code> fields, store its address at <code class="literal">*nullFlags</code>, and set these null flags as needed. <code class="literal">*nullFlags</code> can be left <code class="symbol">NULL</code> (its initial value) if all keys are non-null. The return value can be <code class="symbol">NULL</code> if the <code class="literal">query</code> contains no keys. </p><p> <code class="literal">searchMode</code> is an output argument that allows <code class="function">extractQuery</code> to specify details about how the search will be done. If <code class="literal">*searchMode</code> is set to <code class="literal">GIN_SEARCH_MODE_DEFAULT</code> (which is the value it is initialized to before call), only items that match at least one of the returned keys are considered candidate matches. If <code class="literal">*searchMode</code> is set to <code class="literal">GIN_SEARCH_MODE_INCLUDE_EMPTY</code>, then in addition to items containing at least one matching key, items that contain no keys at all are considered candidate matches. (This mode is useful for implementing is-subset-of operators, for example.) If <code class="literal">*searchMode</code> is set to <code class="literal">GIN_SEARCH_MODE_ALL</code>, then all non-null items in the index are considered candidate matches, whether they match any of the returned keys or not. (This mode is much slower than the other two choices, since it requires scanning essentially the entire index, but it may be necessary to implement corner cases correctly. An operator that needs this mode in most cases is probably not a good candidate for a GIN operator class.) The symbols to use for setting this mode are defined in <code class="filename">access/gin.h</code>. </p><p> <code class="literal">pmatch</code> is an output argument for use when partial match is supported. To use it, <code class="function">extractQuery</code> must allocate an array of <code class="literal">*nkeys</code> <code class="type">bool</code>s and store its address at <code class="literal">*pmatch</code>. Each element of the array should be set to true if the corresponding key requires partial match, false if not. If <code class="literal">*pmatch</code> is set to <code class="symbol">NULL</code> then GIN assumes partial match is not required. The variable is initialized to <code class="symbol">NULL</code> before call, so this argument can simply be ignored by operator classes that do not support partial match. </p><p> <code class="literal">extra_data</code> is an output argument that allows <code class="function">extractQuery</code> to pass additional data to the <code class="function">consistent</code> and <code class="function">comparePartial</code> methods. To use it, <code class="function">extractQuery</code> must allocate an array of <code class="literal">*nkeys</code> pointers and store its address at <code class="literal">*extra_data</code>, then store whatever it wants to into the individual pointers. The variable is initialized to <code class="symbol">NULL</code> before call, so this argument can simply be ignored by operator classes that do not require extra data. If <code class="literal">*extra_data</code> is set, the whole array is passed to the <code class="function">consistent</code> method, and the appropriate element to the <code class="function">comparePartial</code> method. </p></dd></dl></div><p> An operator class must also provide a function to check if an indexed item matches the query. It comes in two flavors, a Boolean <code class="function">consistent</code> function, and a ternary <code class="function">triConsistent</code> function. <code class="function">triConsistent</code> covers the functionality of both, so providing <code class="function">triConsistent</code> alone is sufficient. However, if the Boolean variant is significantly cheaper to calculate, it can be advantageous to provide both. If only the Boolean variant is provided, some optimizations that depend on refuting index items before fetching all the keys are disabled. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">bool consistent(bool check[], StrategyNumber n, Datum query, int32 nkeys, Pointer extra_data[], bool *recheck, Datum queryKeys[], bool nullFlags[])</code></span></dt><dd><p> Returns true if an indexed item satisfies the query operator with strategy number <code class="literal">n</code> (or might satisfy it, if the recheck indication is returned). This function does not have direct access to the indexed item's value, since <acronym class="acronym">GIN</acronym> does not store items explicitly. Rather, what is available is knowledge about which key values extracted from the query appear in a given indexed item. The <code class="literal">check</code> array has length <code class="literal">nkeys</code>, which is the same as the number of keys previously returned by <code class="function">extractQuery</code> for this <code class="literal">query</code> datum. Each element of the <code class="literal">check</code> array is true if the indexed item contains the corresponding query key, i.e., if (check[i] == true) the i-th key of the <code class="function">extractQuery</code> result array is present in the indexed item. The original <code class="literal">query</code> datum is passed in case the <code class="function">consistent</code> method needs to consult it, and so are the <code class="literal">queryKeys[]</code> and <code class="literal">nullFlags[]</code> arrays previously returned by <code class="function">extractQuery</code>. <code class="literal">extra_data</code> is the extra-data array returned by <code class="function">extractQuery</code>, or <code class="symbol">NULL</code> if none. </p><p> When <code class="function">extractQuery</code> returns a null key in <code class="literal">queryKeys[]</code>, the corresponding <code class="literal">check[]</code> element is true if the indexed item contains a null key; that is, the semantics of <code class="literal">check[]</code> are like <code class="literal">IS NOT DISTINCT FROM</code>. The <code class="function">consistent</code> function can examine the corresponding <code class="literal">nullFlags[]</code> element if it needs to tell the difference between a regular value match and a null match. </p><p> On success, <code class="literal">*recheck</code> should be set to true if the heap tuple needs to be rechecked against the query operator, or false if the index test is exact. That is, a false return value guarantees that the heap tuple does not match the query; a true return value with <code class="literal">*recheck</code> set to false guarantees that the heap tuple does match the query; and a true return value with <code class="literal">*recheck</code> set to true means that the heap tuple might match the query, so it needs to be fetched and rechecked by evaluating the query operator directly against the originally indexed item. </p></dd><dt><span class="term"><code class="function">GinTernaryValue triConsistent(GinTernaryValue check[], StrategyNumber n, Datum query, int32 nkeys, Pointer extra_data[], Datum queryKeys[], bool nullFlags[])</code></span></dt><dd><p> <code class="function">triConsistent</code> is similar to <code class="function">consistent</code>, but instead of Booleans in the <code class="literal">check</code> vector, there are three possible values for each key: <code class="literal">GIN_TRUE</code>, <code class="literal">GIN_FALSE</code> and <code class="literal">GIN_MAYBE</code>. <code class="literal">GIN_FALSE</code> and <code class="literal">GIN_TRUE</code> have the same meaning as regular Boolean values, while <code class="literal">GIN_MAYBE</code> means that the presence of that key is not known. When <code class="literal">GIN_MAYBE</code> values are present, the function should only return <code class="literal">GIN_TRUE</code> if the item certainly matches whether or not the index item contains the corresponding query keys. Likewise, the function must return <code class="literal">GIN_FALSE</code> only if the item certainly does not match, whether or not it contains the <code class="literal">GIN_MAYBE</code> keys. If the result depends on the <code class="literal">GIN_MAYBE</code> entries, i.e., the match cannot be confirmed or refuted based on the known query keys, the function must return <code class="literal">GIN_MAYBE</code>. </p><p> When there are no <code class="literal">GIN_MAYBE</code> values in the <code class="literal">check</code> vector, a <code class="literal">GIN_MAYBE</code> return value is the equivalent of setting the <code class="literal">recheck</code> flag in the Boolean <code class="function">consistent</code> function. </p></dd></dl></div><p> </p><p> In addition, GIN must have a way to sort the key values stored in the index. The operator class can define the sort ordering by specifying a comparison method: </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">int compare(Datum a, Datum b)</code></span></dt><dd><p> Compares two keys (not indexed items!) and returns an integer less than zero, zero, or greater than zero, indicating whether the first key is less than, equal to, or greater than the second. Null keys are never passed to this function. </p></dd></dl></div><p> Alternatively, if the operator class does not provide a <code class="function">compare</code> method, GIN will look up the default btree operator class for the index key data type, and use its comparison function. It is recommended to specify the comparison function in a GIN operator class that is meant for just one data type, as looking up the btree operator class costs a few cycles. However, polymorphic GIN operator classes (such as <code class="literal">array_ops</code>) typically cannot specify a single comparison function. </p><p> An operator class for <acronym class="acronym">GIN</acronym> can optionally supply the following methods: </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">int comparePartial(Datum partial_key, Datum key, StrategyNumber n, Pointer extra_data)</code></span></dt><dd><p> Compare a partial-match query key to an index key. Returns an integer whose sign indicates the result: less than zero means the index key does not match the query, but the index scan should continue; zero means that the index key does match the query; greater than zero indicates that the index scan should stop because no more matches are possible. The strategy number <code class="literal">n</code> of the operator that generated the partial match query is provided, in case its semantics are needed to determine when to end the scan. Also, <code class="literal">extra_data</code> is the corresponding element of the extra-data array made by <code class="function">extractQuery</code>, or <code class="symbol">NULL</code> if none. Null keys are never passed to this function. </p></dd><dt><span class="term"><code class="function">void options(local_relopts *relopts)</code></span></dt><dd><p> Defines a set of user-visible parameters that control operator class behavior. </p><p> The <code class="function">options</code> function is passed a pointer to a <code class="structname">local_relopts</code> struct, which needs to be filled with a set of operator class specific options. The options can be accessed from other support functions using the <code class="literal">PG_HAS_OPCLASS_OPTIONS()</code> and <code class="literal">PG_GET_OPCLASS_OPTIONS()</code> macros. </p><p> Since both key extraction of indexed values and representation of the key in <acronym class="acronym">GIN</acronym> are flexible, they may depend on user-specified parameters. </p></dd></dl></div><p> </p><p> To support <span class="quote">“<span class="quote">partial match</span>”</span> queries, an operator class must provide the <code class="function">comparePartial</code> method, and its <code class="function">extractQuery</code> method must set the <code class="literal">pmatch</code> parameter when a partial-match query is encountered. See <a class="xref" href="gin.html#GIN-PARTIAL-MATCH" title="65.4.4.2. Partial Match Algorithm">Section 65.4.4.2</a> for details. </p><p> The actual data types of the various <code class="literal">Datum</code> values mentioned above vary depending on the operator class. The item values passed to <code class="function">extractValue</code> are always of the operator class's input type, and all key values must be of the class's <code class="literal">STORAGE</code> type. The type of the <code class="literal">query</code> argument passed to <code class="function">extractQuery</code>, <code class="function">consistent</code> and <code class="function">triConsistent</code> is whatever is the right-hand input type of the class member operator identified by the strategy number. This need not be the same as the indexed type, so long as key values of the correct type can be extracted from it. However, it is recommended that the SQL declarations of these three support functions use the opclass's indexed data type for the <code class="literal">query</code> argument, even though the actual type might be something else depending on the operator. </p></div><div class="sect2" id="GIN-IMPLEMENTATION"><div class="titlepage"><div><div><h3 class="title">65.4.4. Implementation <a href="#GIN-IMPLEMENTATION" class="id_link">#</a></h3></div></div></div><p> Internally, a <acronym class="acronym">GIN</acronym> index contains a B-tree index constructed over keys, where each key is an element of one or more indexed items (a member of an array, for example) and where each tuple in a leaf page contains either a pointer to a B-tree of heap pointers (a <span class="quote">“<span class="quote">posting tree</span>”</span>), or a simple list of heap pointers (a <span class="quote">“<span class="quote">posting list</span>”</span>) when the list is small enough to fit into a single index tuple along with the key value. <a class="xref" href="gin.html#GIN-INTERNALS-FIGURE" title="Figure 65.1. GIN Internals">Figure 65.1</a> illustrates these components of a GIN index. </p><p> As of <span class="productname">PostgreSQL</span> 9.1, null key values can be included in the index. Also, placeholder nulls are included in the index for indexed items that are null or contain no keys according to <code class="function">extractValue</code>. This allows searches that should find empty items to do so. </p><p> Multicolumn <acronym class="acronym">GIN</acronym> indexes are implemented by building a single B-tree over composite values (column number, key value). The key values for different columns can be of different types. </p><div class="figure" id="GIN-INTERNALS-FIGURE"><p class="title"><strong>Figure 65.1. GIN Internals</strong></p><div class="figure-contents"><div class="mediaobject"><object type="image/svg+xml" data="gin.svg" width="100%"></object></div></div></div><br class="figure-break" /><div class="sect3" id="GIN-FAST-UPDATE"><div class="titlepage"><div><div><h4 class="title">65.4.4.1. GIN Fast Update Technique <a href="#GIN-FAST-UPDATE" class="id_link">#</a></h4></div></div></div><p> Updating a <acronym class="acronym">GIN</acronym> index tends to be slow because of the intrinsic nature of inverted indexes: inserting or updating one heap row can cause many inserts into the index (one for each key extracted from the indexed item). <acronym class="acronym">GIN</acronym> is capable of postponing much of this work by inserting new tuples into a temporary, unsorted list of pending entries. When the table is vacuumed or autoanalyzed, or when <code class="function">gin_clean_pending_list</code> function is called, or if the pending list becomes larger than <a class="xref" href="runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT">gin_pending_list_limit</a>, the entries are moved to the main <acronym class="acronym">GIN</acronym> data structure using the same bulk insert techniques used during initial index creation. This greatly improves <acronym class="acronym">GIN</acronym> index update speed, even counting the additional vacuum overhead. Moreover the overhead work can be done by a background process instead of in foreground query processing. </p><p> The main disadvantage of this approach is that searches must scan the list of pending entries in addition to searching the regular index, and so a large list of pending entries will slow searches significantly. Another disadvantage is that, while most updates are fast, an update that causes the pending list to become <span class="quote">“<span class="quote">too large</span>”</span> will incur an immediate cleanup cycle and thus be much slower than other updates. Proper use of autovacuum can minimize both of these problems. </p><p> If consistent response time is more important than update speed, use of pending entries can be disabled by turning off the <code class="literal">fastupdate</code> storage parameter for a <acronym class="acronym">GIN</acronym> index. See <a class="xref" href="sql-createindex.html" title="CREATE INDEX"><span class="refentrytitle">CREATE INDEX</span></a> for details. </p></div><div class="sect3" id="GIN-PARTIAL-MATCH"><div class="titlepage"><div><div><h4 class="title">65.4.4.2. Partial Match Algorithm <a href="#GIN-PARTIAL-MATCH" class="id_link">#</a></h4></div></div></div><p> GIN can support <span class="quote">“<span class="quote">partial match</span>”</span> queries, in which the query does not determine an exact match for one or more keys, but the possible matches fall within a reasonably narrow range of key values (within the key sorting order determined by the <code class="function">compare</code> support method). The <code class="function">extractQuery</code> method, instead of returning a key value to be matched exactly, returns a key value that is the lower bound of the range to be searched, and sets the <code class="literal">pmatch</code> flag true. The key range is then scanned using the <code class="function">comparePartial</code> method. <code class="function">comparePartial</code> must return zero for a matching index key, less than zero for a non-match that is still within the range to be searched, or greater than zero if the index key is past the range that could match. </p></div></div><div class="sect2" id="GIN-TIPS"><div class="titlepage"><div><div><h3 class="title">65.4.5. GIN Tips and Tricks <a href="#GIN-TIPS" class="id_link">#</a></h3></div></div></div><div class="variablelist"><dl class="variablelist"><dt><span class="term">Create vs. insert</span></dt><dd><p> Insertion into a <acronym class="acronym">GIN</acronym> index can be slow due to the likelihood of many keys being inserted for each item. So, for bulk insertions into a table it is advisable to drop the GIN index and recreate it after finishing bulk insertion. </p><p> When <code class="literal">fastupdate</code> is enabled for <acronym class="acronym">GIN</acronym> (see <a class="xref" href="gin.html#GIN-FAST-UPDATE" title="65.4.4.1. GIN Fast Update Technique">Section 65.4.4.1</a> for details), the penalty is less than when it is not. But for very large updates it may still be best to drop and recreate the index. </p></dd><dt><span class="term"><a class="xref" href="runtime-config-resource.html#GUC-MAINTENANCE-WORK-MEM">maintenance_work_mem</a></span></dt><dd><p> Build time for a <acronym class="acronym">GIN</acronym> index is very sensitive to the <code class="varname">maintenance_work_mem</code> setting; it doesn't pay to skimp on work memory during index creation. </p></dd><dt><span class="term"><a class="xref" href="runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT">gin_pending_list_limit</a></span></dt><dd><p> During a series of insertions into an existing <acronym class="acronym">GIN</acronym> index that has <code class="literal">fastupdate</code> enabled, the system will clean up the pending-entry list whenever the list grows larger than <code class="varname">gin_pending_list_limit</code>. To avoid fluctuations in observed response time, it's desirable to have pending-list cleanup occur in the background (i.e., via autovacuum). Foreground cleanup operations can be avoided by increasing <code class="varname">gin_pending_list_limit</code> or making autovacuum more aggressive. However, enlarging the threshold of the cleanup operation means that if a foreground cleanup does occur, it will take even longer. </p><p> <code class="varname">gin_pending_list_limit</code> can be overridden for individual GIN indexes by changing storage parameters, which allows each GIN index to have its own cleanup threshold. For example, it's possible to increase the threshold only for the GIN index which can be updated heavily, and decrease it otherwise. </p></dd><dt><span class="term"><a class="xref" href="runtime-config-client.html#GUC-GIN-FUZZY-SEARCH-LIMIT">gin_fuzzy_search_limit</a></span></dt><dd><p> The primary goal of developing <acronym class="acronym">GIN</acronym> indexes was to create support for highly scalable full-text search in <span class="productname">PostgreSQL</span>, and there are often situations when a full-text search returns a very large set of results. Moreover, this often happens when the query contains very frequent words, so that the large result set is not even useful. Since reading many tuples from the disk and sorting them could take a lot of time, this is unacceptable for production. (Note that the index search itself is very fast.) </p><p> To facilitate controlled execution of such queries, <acronym class="acronym">GIN</acronym> has a configurable soft upper limit on the number of rows returned: the <code class="varname">gin_fuzzy_search_limit</code> configuration parameter. It is set to 0 (meaning no limit) by default. If a non-zero limit is set, then the returned set is a subset of the whole result set, chosen at random. </p><p> <span class="quote">“<span class="quote">Soft</span>”</span> means that the actual number of returned results could differ somewhat from the specified limit, depending on the query and the quality of the system's random number generator. </p><p> From experience, values in the thousands (e.g., 5000 — 20000) work well. </p></dd></dl></div></div><div class="sect2" id="GIN-LIMIT"><div class="titlepage"><div><div><h3 class="title">65.4.6. Limitations <a href="#GIN-LIMIT" class="id_link">#</a></h3></div></div></div><p> <acronym class="acronym">GIN</acronym> assumes that indexable operators are strict. This means that <code class="function">extractValue</code> will not be called at all on a null item value (instead, a placeholder index entry is created automatically), and <code class="function">extractQuery</code> will not be called on a null query value either (instead, the query is presumed to be unsatisfiable). Note however that null key values contained within a non-null composite item or query value are supported. </p></div><div class="sect2" id="GIN-EXAMPLES"><div class="titlepage"><div><div><h3 class="title">65.4.7. Examples <a href="#GIN-EXAMPLES" class="id_link">#</a></h3></div></div></div><p> The core <span class="productname">PostgreSQL</span> distribution includes the <acronym class="acronym">GIN</acronym> operator classes previously shown in <a class="xref" href="gin.html#GIN-BUILTIN-OPCLASSES-TABLE" title="Table 65.3. Built-in GIN Operator Classes">Table 65.3</a>. The following <code class="filename">contrib</code> modules also contain <acronym class="acronym">GIN</acronym> operator classes: </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">btree_gin</code></span></dt><dd><p>B-tree equivalent functionality for several data types</p></dd><dt><span class="term"><code class="filename">hstore</code></span></dt><dd><p>Module for storing (key, value) pairs</p></dd><dt><span class="term"><code class="filename">intarray</code></span></dt><dd><p>Enhanced support for <code class="type">int[]</code></p></dd><dt><span class="term"><code class="filename">pg_trgm</code></span></dt><dd><p>Text similarity using trigram matching</p></dd></dl></div><p> </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spgist.html" title="65.3. SP-GiST Indexes">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indextypes.html" title="Chapter 65. Built-in Index Access Methods">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="brin.html" title="65.5. BRIN Indexes">Next</a></td></tr><tr><td width="40%" align="left" valign="top">65.3. SP-GiST Indexes </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 65.5. BRIN Indexes</td></tr></table></div></body></html>