Cheese with Holes (Emmental) - Developing
Cheese with Holes (Emmental) - Developing
Useful note with basic structure, but still has holes to fill.
Click the cheese icon to learn more

GiST Index (Generalized Search Tree)

Author: guiferviz

Created:

Last Modified:

GiST stands for Generalized Search Tree. Unlike a B-Tree index (which is perfect for sorting and equality checks like =, <, >), GiST is a framework that allows indexing complex data types like:

  • Geometric data (PostGIS uses R-Trees implemented via GiST).
  • Full-text search (searching for words inside documents).
  • Range types (Time ranges, integer ranges).

YouTube Video with a nice visual explanation: https://www.youtube.com/watch?v=zw4-Hpm7ysk

Example: Time Ranges

Imagine a booking system, we constantly ask: Does this new time range overlap with any existing booking?

  • B-Tree: Can efficiently find bookings starting after X or ending before Y. But finding overlaps requires checking two conditions simultaneously (start < request_end AND end > request_start), which B-Trees struggle to optimize perfectly.
  • GiST: Can index a Range (like tsrange in PostgreSQL) as a single geometric-like object. It supports operators like && (overlap), @> (contains), and <@ (contained by) natively.

Example in PostgreSQL

-- Create an index on the 'duration' column (tsrange type) of a table containing bookings.
CREATE INDEX booking_duration_idx ON bookings USING GIST (duration);

-- Efficiently find overlaps
SELECT * FROM bookings
WHERE duration && '[2025-01-01 10:00, 2025-01-01 11:00)';