What are the types of index access methods in PostgreSQL?
Introduction to access methods
An access method is an algorithm used to locate and access any data. Indexing plays a vital role in access methods. As an analogy, we can well imagine the difference in ease between looking for a particular term in a book by reading it sequentially, on the one hand, and finding it from an appendix section or a table of content, on the other. The latter is obviously much easier, and that’s exactly how indexing works.
Types of index access methods
PostgreSQL provides the following six different types of indexing used for different kinds of data:
B-TreeHashGiSTSP-GiSTGINBRIN
The CREATE INDEX command creates B-Tree indexes by default. However, any indexing type can be selected with the USING clause.
CREATE INDEX index_name ON table_name USING HASH (column_name);
This query creates an index by using the hash method.
B-Tree (or balanced tree)
The B-Tree index is the most used index type. It is used by default in the CREATE INDEX query. It can handle all
It supports equality and range queries using <, <=, =, >=, and > operators.
The optimizer can use B-Tree indexes for
Hash
The Hash index of PostgreSQL stores a 4-byte hash code of the indexed values. Since it only stores the hash of the value, there is no limit on the size of the indexed value. Any data type can be indexed using Hash.
It only supports simple equality comparison queries. Hash index whenever a comparison query is performed on the indexed column using the = operator.
GiST (or generalized search tree)
The GiST index is a balanced, tree-structure access method that provides index support for multi-dimensional data like ranges or geometry point values. It has built-in operator classes for box, circle, multirange points, polygon, and other geometry shapes.
CREATE INDEX ON table_name USING GIST (column_name class_name);
GiST has the following built-in classes:
box_opscircle_opsinet_opsmultirange_opspoint_opspoly_opsrange_opstsquery_opstsvector_ops
GiST supports all geometric operations defined in the above built-in classes.
It is optimized for
The following query finds five closest places to the given point:
SELECT * FROM places ORDER BY location <-> point '(113,621)' LIMIT 5;
SP-GiST (or spaced partitioned gist)
The SP-GiST index allows indexing for non-balanced disk-based data structures, such as radix tree, k-d trees, and so on. It supports geometric operations of the following built-in classes.
box_opskd_point_opsnetwork_opspoly_opsquad_point_opsrange_opstest_ops
Due to distance ordering support, it provides ordering operators as well. Like GiST, SP-GiST is optimized for nearest-neighbor searches.
GIN (or generalized inverted index)
GIN indexes are suitable for multiple component values like arrays. Each composite must have a separate entry for its elements in the GIN index. Like GiST, GIN also allows developing custom data types.
It supports operators for the following built-in classes:
array_opsjsonb_opsjsonb_path_opstsvector_ops
BRIN (or block range indexes)
The BRIN index is designed to handle such tables that contain column values that have some natural correlation with their physical locations. For example, the timestamped column can have data placed in the same physical location.
The BRIN index stores a summary of the values located in the consecutive physical ranges of a table. It is the most helpful index type when values of a column are well-correlated with the physical order of the table rows. It has large built-in class libraries that support equality and range queries.
Comparison of index access methods
Index Access Method | Suitable Data Structure | Supported Operations |
B-Tree | Linearly sortable | Equality and range operations |
Hash | All data types | Equality comparison operation only |
GiST | Multi-dimensional | Geometric operations of built-in classes |
SP-GiST | Non-balanced disk-based | Geometric and ordering operators |
GIN | Composite data structures | Operations on arrays, JSON and vector |
BRIN | Physically correlated data | Equality and range operations |
Free Resources