Postgres provides several index types: B-tree, R-tree, and Hash. Each index type is more appropriate for a particular query type because of the algorithm it uses. By default, the CREATE INDEX command will create a B-tree index, which fits the most common situations. In particular, the Postgres query optimizer will consider using a B-tree index whenever an indexed column is involved in a comparison using one of these operators: <, <=, =, >=, >
R-tree indices are especially suited for spacial data. To create an R-tree index, use a command of the form
CREATE INDEX name ON table USING RTREE (column);The Postgres query optimizer will consider using an R-tree index whenever an indexed column is involved in a comparison using one of these operators: <<, &<, &>, >>, @, ~=, && (Refer to Section 4.8 about the meaning of these operators.)
The query optimizer will consider using a hash index whenever an indexed column is involved in a comparison using the = operator. The following command is used to create a hash index:
CREATE INDEX name ON table USING HASH (column);
Note: Because of the limited utility of hash indices, a B-tree index should generally be preferred over a hash index. We do not have sufficient evidence that hash indices are actually faster than B-trees even for = comparisons. Moreover, hash indices require coarser locks; see Section 9.7.
The B-tree index is an implementation of Lehman-Yao high-concurrency B-trees. The R-tree index method implements standard R-trees using Guttman's quadratic split algorithm. The hash index is an implementation of Litwin's linear hashing. We mention the algorithms used solely to indicate that all of these access methods are fully dynamic and do not have to be optimized periodically (as is the case with, for example, static hash access methods).