Sobes.tech
Junior — Middle

Explain the differences between hash indexes and B-Tree indexes in PostgreSQL database.

sobes.tech AI

Answer from AI

In PostgreSQL, there are different types of indexes, among which hash indexes and B-Tree indexes are the most common.

B-Tree indexes:

  • Used by default for most data types.
  • Support comparison operations: equality, range (>, <, BETWEEN).
  • The tree structure allows for fast searching, inserting, and deleting records.
  • Well-suited for sorting and range searches.

Hash indexes:

  • Optimized only for equality operations (=).
  • Use a hash function for quick lookup.
  • Do not support range searches.
  • Previously in PostgreSQL, they were less reliable and did not support WAL (transaction log), but the situation has improved in newer versions.