PostgreSQL 8.3.6 Documentation
Prev | Fast Backward | Chapter 52. GIN Indexes | Fast Forward | Next

# 52.2. 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 four user-defined methods, which define the behavior of keys in the tree and the relationships between keys, indexed values, and indexable queries. In short, GIN combines extensibility with generality, code reuse, and a clean interface.

The four methods that an index operator class for GIN must provide are:

int compare(Datum a, Datum b)

  Compares keys (not indexed values!) 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.

Datum* extractValue(Datum inputValue, int32 *nkeys)

  Returns an array of keys given a value to be indexed. The number of returned keys must be stored into `*nkeys`.

Datum* extractQuery(Datum query, int32 *nkeys, StrategyNumber n)

  Returns an 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 (see Section 34.14.2). Often, `extractQuery` will need to consult `n` to determine the data type of `query` and the key values that need to be extracted. The number of returned keys must be stored into `*nkeys`. If number of keys is equal to zero then `extractQuery` should store 0 or -1 into `*nkeys`. 0 means that any row matches the `query` and sequence scan should be produced. -1 means nothing can satisfy `query`. Choice of value should be based on semantics meaning of operation with given strategy number.

bool consistent(bool check[], StrategyNumber n, Datum query)

  Returns TRUE if the indexed value satisfies the query operator with strategy number `n` (or would satisfy, if the operator is marked RECHECK in the operator class). The `check` array has the same length as the number of keys previously returned by `extractQuery` for this query. Each element of the `check` array is TRUE if the indexed value contains the corresponding query key, ie, if (check[i] == TRUE) the i-th key of the `extractQuery` result array is present in the indexed value. The original `query` datum (not the extracted key array!) is passed in case the `consistent` method needs to consult it.

Prev | Home | Next
Introduction | Up | Implementation