DEV Community

Sadeed Ahmad
Sadeed Ahmad

Posted on

PostgreSQL indexing: Hash Index

PostgreSQL offers a wide array of options for database indexing, hash indexing being one of them. In this article, we'll explore the fundamental theory and structure of hash indexing and its implementation in PostgreSQL.

A hash index is a database indexing method that utilizes a hash function to map keys to corresponding values. Hashing is a technique to associate a small number with a value of any data type to access data for situations where keys are relatively small and the data set remains static, in PostgreSQL.

Architecture of Hash Index

Meta Page is the initial page in the hash index and holds essential information about the index's contents. It also has Bucket Pages which are the primary pages of the index, where data is stored in the form of "hash code - TID" pairs. TID stands for "tuple identifier" and helps locate the actual data in the table. Overflow Pages have a structure similar to bucket pages and are used when a single page is insufficient to hold all the data for a specific bucket. Finally, Bitmap Pages maintain a record of overflow pages that are currently clear and can be reused for other buckets, improving the efficiency of the index.

Hash Index in PostgreSQL

Hash indexing in PostgreSQL uses the hash function to calculate an integer hash code for each value in the indexed column. At first, the hash index contains a small number of buckets, but this dynamically increases as the index grows to accommodate data efficiently. When inserting data into the hash index, the hash code determines the specific bucket where the data is stored. The data is organized as "hash code - TID" pairs, with the TID serving as a pointer to the actual data location in the table.

If different values produce the same hash code, PostgreSQL uses either open addressing or chaining. Open addressing involves searching for the next available bucket in a specific sequence until an empty one is found, while chaining stores multiple values in the same bucket or overflow pages. Bitmap pages are employed to manage the allocation of overflow pages effectively.

Hash indexing is most effective for static datasets with relatively small keys, providing rapid data access for read-intensive workloads in PostgreSQL.

Top comments (0)