70.3. Extensibility
The GIN interface has a high level of abstraction, requiring the access method implementer only to implement the semantics of the data type being accessed. The GIN layer itself takes care of concurrency, logging and searching the tree structure.
All it takes to get a GIN 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 for GIN 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*nkeysboolfields, store its address at*nullFlags, and set these null flags as needed.*nullFlagscan be leftNULL(its initial value) if all keys are non-null. The return value can beNULLif 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,
queryis the value on the right-hand side of an indexable operator whose left-hand side is the indexed column.nis the strategy number of the operator within the operator class (see Section 38.16.2). Often,extractQuerywill need to consultnto determine the data type ofqueryand 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*nkeysboolfields, store its address at*nullFlags, and set these null flags as needed.*nullFlagscan be leftNULL(its initial value) if all keys are non-null. The return value can beNULLif thequerycontains no keys.searchModeis an output argument that allowsextractQueryto specify details about how the search will be done. If*searchModeis 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*searchModeis 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*searchModeis 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.pmatchis an output argument for use when partial match is supported. To use it,extractQuerymust allocate an array of*nkeysbools 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*pmatchis set toNULLthen GIN assumes partial match is not required. The variable is initialized toNULLbefore call, so this argument can simply be ignored by operator classes that do not support partial match.extra_datais an output argument that allowsextractQueryto pass additional data to theconsistentandcomparePartialmethods. To use it,extractQuerymust allocate an array of*nkeyspointers and store its address at*extra_data, then store whatever it wants to into the individual pointers. The variable is initialized toNULLbefore call, so this argument can simply be ignored by operator classes that do not require extra data. If*extra_datais set, the whole array is passed to theconsistentmethod, and the appropriate element to thecomparePartialmethod.
An operator class must also provide a function to check if an indexed item matches the query. It comes in two flavors, a Boolean consistent function, and a ternary triConsistent function. triConsistent covers the functionality of both, so providing triConsistent 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 number
n(or might satisfy it, if the recheck indication is returned). This function does not have direct access to the indexed item's value, since GIN 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. Thecheckarray has lengthnkeys, which is the same as the number of keys previously returned byextractQueryfor thisquerydatum. Each element of thecheckarray is true if the indexed item contains the corresponding query key, i.e., if (check[i] == true) the i-th key of theextractQueryresult array is present in the indexed item. The originalquerydatum is passed in case theconsistentmethod needs to consult it, and so are thequeryKeys[]andnullFlags[]arrays previously returned byextractQuery.extra_datais the extra-data array returned byextractQuery, orNULLif none.When
extractQueryreturns 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. Theconsistentfunction can examine the correspondingnullFlags[]element if it needs to tell the difference between a regular value match and a null match.On success,
*recheckshould 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*recheckset to false guarantees that the heap tuple does match the query; and a true return value with*recheckset 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[])triConsistentis similar toconsistent, but instead of Booleans in thecheckvector, there are three possible values for each key:GIN_TRUE,GIN_FALSEandGIN_MAYBE.GIN_FALSEandGIN_TRUEhave the same meaning as regular Boolean values, whileGIN_MAYBEmeans that the presence of that key is not known. WhenGIN_MAYBEvalues are present, the function should only returnGIN_TRUEif the item certainly matches whether or not the index item contains the corresponding query keys. Likewise, the function must returnGIN_FALSEonly if the item certainly does not match, whether or not it contains theGIN_MAYBEkeys. If the result depends on theGIN_MAYBEentries, i.e., the match cannot be confirmed or refuted based on the known query keys, the function must returnGIN_MAYBE.When there are no
GIN_MAYBEvalues in thecheckvector, aGIN_MAYBEreturn value is the equivalent of setting therecheckflag in the Booleanconsistentfunction.
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:
int compare(Datum a, Datum b)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.
Alternatively, if the operator class does not provide a compare 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 array_ops) typically cannot specify a single comparison function.
An operator class for GIN can optionally supply the following methods:
int comparePartial(Datum partial_key, Datum key, StrategyNumber n, Pointer extra_data)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
nof the operator that generated the partial match query is provided, in case its semantics are needed to determine when to end the scan. Also,extra_datais the corresponding element of the extra-data array made byextractQuery, orNULLif none. Null keys are never passed to this function.void options(local_relopts *relopts)Defines a set of user-visible parameters that control operator class behavior.
The
optionsfunction is passed a pointer to alocal_reloptsstruct, which needs to be filled with a set of operator class specific options. The options can be accessed from other support functions using thePG_HAS_OPCLASS_OPTIONS()andPG_GET_OPCLASS_OPTIONS()macros.Since both key extraction of indexed values and representation of the key in GIN are flexible, they may depend on user-specified parameters.
To support “partial match” queries, an operator class must provide the comparePartial method, and its extractQuery method must set the pmatch parameter when a partial-match query is encountered. See Section 70.4.2 for details.
The actual data types of the various Datum values mentioned above vary depending on the operator class. The item values passed to extractValue are always of the operator class's input type, and all key values must be of the class's STORAGE type. The type of the query argument passed to extractQuery, consistent and triConsistent 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 query argument, even though the actual type might be something else depending on the operator.