Senior
What index is used by default in PostgreSQL and why B-tree and not a red-black tree?
sobes.tech AI
Answer from AI
In PostgreSQL, the default index type used is B-tree.
Reasons for choosing B-tree:
- B-tree is a balanced tree with ordered keys, which provides efficient search, insertion, and deletion in logarithmic time.
- Supports range queries (e.g., BETWEEN, >, <), which are important for most use cases.
- Scales well with increasing data volume.
Why not red-black tree?
- Red-black tree is a binary balanced tree, suitable for main memory, but not optimal for disk structures.
- B-tree is optimized for working with large blocks of data on disk, minimizing read/write operations.
- Minimizing disk accesses is crucial in databases, making B-tree more suitable for indexes.
Thus, B-tree is a compromise between search efficiency and optimization for disk subsystems, making it an ideal choice for indexes in PostgreSQL.