GiST Indexes — Complete Guide
GiST Indexes — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 23 of 100
GiST Indexes
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — Indexing & Performance
What is this?
GiST (Generalized Search Tree) supports non-scalar data — geometric shapes, ranges, exclusion constraints, and nearest-neighbor searches. It answers “overlaps”, “contains”, “within distance”.
Why should you care?
Swiggy delivery zones on PostgresVerse use polygon GiST to find which zone contains a customer lat/long point.
See it live — copy this example
Run in pgAdmin or psql.
CREATE TABLE delivery_zones (
zone_id serial PRIMARY KEY,
zone_name text,
area polygon
);
CREATE INDEX idx_zones_area_gist ON delivery_zones USING GIST (area);
SELECT zone_name FROM delivery_zones
WHERE area @> point(77.5946, 12.9716);
What happened?
- polygon column stores zone boundaries.
- GiST index accelerates @> point containment — which zone covers Bangalore MG Road coordinates.
Practice next
- CREATE EXTENSION IF NOT EXISTS postgis is NOT required for built-in polygon/point types.
- Insert a few zones with polygon values.
- CREATE GiST index and EXPLAIN the point query.
- Use daterange column with GiST: WHERE validity && daterange('2026-07-01','2026-07-31');
- Add EXCLUDE USING GIST to prevent overlapping booking slots.
Remember
GiST handles geometry, ranges, and custom types. @> tests containment for polygons and ranges. Also backs EXCLUDE constraints preventing overlaps.
PostgresVerse geo dispatch
Rider assignment query finds zone in one indexed lookup instead of Python loop.
Outcome: ETA API scales when Bangalore adds 200 micro-zones.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!