Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
60.3. Extensibility
Prev UpChapter 60. GIN IndexesHome Next

60.3. Extensibility

TheGIN interface has a high level of abstraction, requiring the access method implementer only to implement the semantics of the data type being accessed. TheGIN layer itself takes care of concurrency, logging and searching the tree structure.

All it takes to get aGIN 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,GIN combines extensibility with generality, code reuse, and a clean interface.

There are two methods that an operator class forGIN must provide:

Datum *extractValue(Datum itemValue, int32 *nkeys, bool **nullFlags)

Returns a palloc'd array of keys given an item to be indexed. The number of returned keys must be stored into*nkeys. If any of the keys can be null, also palloc an array of*nkeysbool fields, store its address at*nullFlags, and set these null flags as needed.*nullFlags can be leftNULL (its initial value) if all keys are non-null. The return value can beNULL if the item contains no keys.

Datum *extractQuery(Datum query, int32 *nkeys, StrategyNumber n, bool **pmatch, Pointer **extra_data, bool **nullFlags, int32 *searchMode)

Returns a palloc'd array of keys given a value to be queried; that is,query is the value on the right-hand side of an indexable operator whose left-hand side is the indexed column.n is the strategy number of the operator within the operator class (seeSection 35.14.2). Often,extractQuery will need to consultn to determine the data type ofquery and the method it should use to extract key values. The number of returned keys must be stored into*nkeys. If any of the keys can be null, also palloc an array of*nkeysbool fields, store its address at*nullFlags, and set these null flags as needed.*nullFlags can be leftNULL (its initial value) if all keys are non-null. The return value can beNULL if thequery contains no keys.

searchMode is an output argument that allowsextractQuery to specify details about how the search will be done. If*searchMode is set toGIN_SEARCH_MODE_DEFAULT (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*searchMode is set toGIN_SEARCH_MODE_INCLUDE_EMPTY, 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*searchMode is set toGIN_SEARCH_MODE_ALL, 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 inaccess/gin.h.

pmatch is an output argument for use when partial match is supported. To use it,extractQuery must allocate an array of*nkeys booleans and store its address at*pmatch. Each element of the array should be set to TRUE if the corresponding key requires partial match, FALSE if not. If*pmatch is set toNULL then GIN assumes partial match is not required. The variable is initialized toNULL before call, so this argument can simply be ignored by operator classes that do not support partial match.

extra_data is an output argument that allowsextractQuery to pass additional data to theconsistent andcomparePartial methods. To use it,extractQuery must allocate an array of*nkeys pointers and store its address at*extra_data, then store whatever it wants to into the individual pointers. The variable is initialized toNULL before call, so this argument can simply be ignored by operator classes that do not require extra data. If*extra_data is set, the whole array is passed to theconsistent method, and the appropriate element to thecomparePartial method.

An operator class must also provide a function to check if an indexed item matches the query. It comes in two flavors, a booleanconsistent function, and a ternarytriConsistent function.triConsistent covers the functionality of both, so providingtriConsistent 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.

bool consistent(bool check[], StrategyNumber n, Datum query, int32 nkeys, Pointer extra_data[], bool *recheck, Datum queryKeys[], bool nullFlags[])

Returns TRUE if an indexed item satisfies the query operator with strategy numbern (or might satisfy it, if the recheck indication is returned). This function does not have direct access to the indexed item's value, sinceGIN 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. Thecheck array has lengthnkeys, which is the same as the number of keys previously returned byextractQuery for thisquery datum. Each element of thecheck array is TRUE if the indexed item contains the corresponding query key, i.e., if (check[i] == TRUE) the i-th key of theextractQuery result array is present in the indexed item. The originalquery datum is passed in case theconsistent method needs to consult it, and so are thequeryKeys[] andnullFlags[] arrays previously returned byextractQuery.extra_data is the extra-data array returned byextractQuery, orNULL if none.

WhenextractQuery returns a null key inqueryKeys[], the correspondingcheck[] element is TRUE if the indexed item contains a null key; that is, the semantics ofcheck[] are likeIS NOT DISTINCT FROM. Theconsistent function can examine the correspondingnullFlags[] element if it needs to tell the difference between a regular value match and a null match.

On success,*recheck 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*recheck set to FALSE guarantees that the heap tuple does match the query; and a TRUE return value with*recheck 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.

GinTernaryValue triConsistent(GinTernaryValue check[], StrategyNumber n, Datum query, int32 nkeys, Pointer extra_data[], Datum queryKeys[], bool nullFlags[])

triConsistent is similar toconsistent, but instead of booleans in thecheck vector, there are three possible values for each key:GIN_TRUE,GIN_FALSE andGIN_MAYBE.GIN_FALSE andGIN_TRUE have the same meaning as regular boolean values, whileGIN_MAYBE means that the presence of that key is not known. WhenGIN_MAYBE values are present, the function should only returnGIN_TRUE if the item certainly matches whether or not the index item contains the corresponding query keys. Likewise, the function must returnGIN_FALSE only if the item certainly does not match, whether or not it contains theGIN_MAYBE keys. If the result depends on theGIN_MAYBE entries, i.e., the match cannot be confirmed or refuted based on the known query keys, the function must returnGIN_MAYBE.

When there are noGIN_MAYBE values in thecheck vector, aGIN_MAYBE return value is the equivalent of setting therecheck flag in the booleanconsistent function.

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:

Alternatively, if the operator class does not provide acompare 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 asarray_ops) typically cannot specify a single comparison function.

Optionally, an operator class forGIN can supply the following method:

To supportpartial match queries, an operator class must provide thecomparePartial method, and itsextractQuery method must set thepmatch parameter when a partial-match query is encountered. SeeSection 60.4.2 for details.

The actual data types of the variousDatum values mentioned above vary depending on the operator class. The item values passed toextractValue are always of the operator class's input type, and all key values must be of the class'sSTORAGE type. The type of thequery argument passed toextractQuery,consistent andtriConsistent 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 thequery argument, even though the actual type might be something else depending on the operator.


Prev Up Next
60.2. Built-in Operator Classes Home 60.4. Implementation
epubpdf
Go to Postgres Pro Standard 10
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp