BRIN Indexes — Complete Guide
BRIN 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 24 of 100
BRIN Indexes
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — Indexing & Performance
What is this?
BRIN (Block Range Index) stores min/max summaries per block range of a table — tiny index size for very large, naturally ordered data like time-series logs.
Why should you care?
PostgresVerse event_logs append-only by created_at — BRIN on timestamp skips terabytes of blocks when querying one day.
See it live — copy this example
Run in pgAdmin or psql.
CREATE TABLE event_logs (
log_id bigserial PRIMARY KEY,
created_at timestamptz NOT NULL,
payload jsonb
);
CREATE INDEX idx_event_logs_created_brin
ON event_logs USING BRIN (created_at);
SELECT count(*) FROM event_logs
WHERE created_at >= '2026-07-19'::date
AND created_at < '2026-07-20'::date;
What happened?
- Rows insert in time order so each BRIN range summarizes contiguous timestamps.
- Day filter excludes most heap blocks using a small index.
Practice next
- Insert ordered timestamps into event_logs (bulk COPY helps).
- Compare pg_relation_size of BRIN vs B-Tree on created_at.
- EXPLAIN ANALYZE the day-bound query.
- Tune pages_per_range with WITH (pages_per_range=32).
- Query month range and compare blocks read with B-Tree index.
Remember
BRIN is minimal storage for sequential data. Works best when physical order correlates with indexed column. Combine with partitioning for warehouse-scale logs.
PostgresVerse audit trail
Compliance queries one day of 500M audit rows using BRIN on logged_at.
Outcome: Index fits in RAM; nightly reports finish on time.
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!