---
title: "Anatomy of a (Postgres) search engine"
description: "Inverted indexes power full-text search by mapping each term to a sorted, compressed postings list of document IDs, with optional positional data for phrase/span queries and frequency statistics for BM25 scoring."
url: "https://swe.quest/articles/anatomy-of-a-postgres-search-engine-ef0fff51702b4f16"
canonical: "https://planetscale.com/blog/anatomy-of-a-postgres-search-engine"
source: "PlanetScale"
published: "2026-09-22"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 12
tags: ["search","postgres","data-structures","storage-engines"]
---

# Anatomy of a (Postgres) search engine

A post on [PlanetScale](https://swe.quest/sources/planetscale.md), published 2026-09-22, 12 min read, Deep dive. Original: [https://planetscale.com/blog/anatomy-of-a-postgres-search-engine](https://planetscale.com/blog/anatomy-of-a-postgres-search-engine)

## Summary

Inverted indexes power full-text search by mapping each term to a sorted, compressed postings list of document IDs, with optional positional data for phrase/span queries and frequency statistics for BM25 scoring. Postings can be delta-encoded or stored as bitmaps for fast SIMD intersection, and block-level statistics let top-k queries skip whole blocks. Indexes are built as immutable segments plus a mutable segment, with deletes handled by tombstones and periodic merges that trade heavy I/O against per-query overhead. Inside Postgres, such an index must map to ctids, be WAL-logged, reuse freed pages, run merges in background workers, and cooperate with VACUUM.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Search](https://swe.quest/tags/search.md) (Backend & APIs)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Data structures](https://swe.quest/tags/data-structures.md) (Architecture & design)
- [Storage engines](https://swe.quest/tags/storage-engines.md) (Data & databases)

## Prerequisites

- B-tree indexes ([easy posts on Storage engines](https://swe.quest/tags/storage-engines.md?difficulty=easy))
- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres indexes ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PlanetScale TIN, BM25, inverted index, PostgreSQL, VACUUM, ctid

## Related posts

- [pg\_search is Available on Neon](https://swe.quest/articles/pg-search-is-available-on-neon-98d919ff82afb749.md): Neon · 2025-03-18 · Medium · 8 min read · PostgreSQL, Search, Performance engineering
  ParadeDB's pg\_search extension, now available on Neon in AWS regions, adds a Tantivy-powered BM25 inverted index to Postgres, bringing relevance ranking, fuzzy matching, faceting, highlighting and JSON-aware search that native tsvector/GIN full-text search lacks.
- [Introducing Inverted Indices in ClickHouse](https://swe.quest/articles/introducing-inverted-indices-in-clickhouse-ecbc0f773fa09eed.md): ClickHouse · 2023-02-02 · Medium · 11 min read · Search, Storage engines, Data structures, Analytics & OLAP
  ClickHouse v23.1 adds experimental inverted indexes (contributed by IBM) as MergeTree secondary indexes for full-text term search.
- [Beyond FTS5: Building Transactional Full-Text Search in TursoDB](https://swe.quest/articles/beyond-fts5-building-transactional-full-text-search-in-tursodb-07e8d5e009fddab8.md): Turso · 2026-01-27 · Medium · 7 min read · Search, Storage engines, SQLite, Rust
  TursoDB adds native full-text search by embedding Tantivy and implementing its Directory trait on top of Turso's B-tree, storing index "files" as 512KB chunks in an internal table.
- [Comparing Native Postgres, ElasticSearch, and pg\_search for Full-Text Search](https://swe.quest/articles/comparing-native-postgres-elasticsearch-and-pg-search-for-full-text-search-b83c564af59c02c3.md): Neon · 2025-06-13 · Medium · 9 min read · Search, PostgreSQL, Performance engineering
  Native Postgres full-text search (tsvector/tsquery with GIN indexes) breaks down at scale because ranking by ts\_rank must score every matching row and lacks BM25, fuzzy matching and per-field relevance.
- [6 tips to optimize storage costs for your Postgres databases](https://swe.quest/articles/6-tips-to-optimize-storage-costs-for-your-postgres-databases-11da5e875c34786c.md): Neon · 2024-03-15 · Medium · 4 min read · PostgreSQL, Cost optimization, Storage engines
  Six ways to shrink Postgres storage spend: drop unused indexes (found via pg\_stat\_user\_indexes and pg\_index), avoid full database copies for dev and staging by using Neon's copy-on-write branching, choose smaller data types like SMALLINT, order columns largest to smallest to cut alignment padding, shorten Neon's history retention window, and tune autovacuum to limit bloat.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
