Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

PlanetScale 12 min read Deep dive mediumAdept

Anatomy of a (Postgres) search engine

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.

Related posts

  • pg_search is Available on Neon

    Neon mediumAdept

    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. The BM25 index is a covering index across multiple columns, so filters and aggregations are pushed into a single index scan, and it is MVCC-integrated so new data is searchable on commit without ETL. On a 10-million-row log dataset, it built faster than a GIN index (1.63 vs 2.60 min) but was larger (647 vs 176 MB). It beat a GIN/B-tree-tuned Postgres on every query tested, with the largest gains on ranked top-N, count and filtered queries, e.g. 81 ms vs 38.8 s for BM25-ranked top-10.

  • Introducing Inverted Indices in ClickHouse

    ClickHouse mediumAdept

    ClickHouse v23.1 adds experimental inverted indexes (contributed by IBM) as MergeTree secondary indexes for full-text term search. Dictionaries are stored as minimized finite state transducers mapping terms to posting-list offsets, and posting lists are compressed with roaring bitmaps for fast unions and intersections. Indexes are built per part with single-pass in-memory inversion into segments stored across metadata, dictionary and posting-list files, so lookups never load the whole index. On 28.7M Hacker News comments, a hasToken query drops from 0.843s to 0.248s (about 3.4x), though part merges currently rebuild the index from scratch.

  • Beyond FTS5: Building Transactional Full-Text Search in TursoDB

    Turso mediumAdept

    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. Because of this design, index updates share the SQL transaction, WAL, rollback and single-file storage, and indexes are created with Postgres-style `CREATE INDEX ... USING fts` syntax with configurable tokenizers, BM25 column weights, MATCH and highlighting. A hybrid cache keeps metadata resident, caches hot small files (term dictionaries, fast fields) whole, and lazy-loads large segment data by chunk. Background merges conflict with single-writer transactional storage, so automatic merging is disabled via NoMergePolicy. An explicit `OPTIMIZE INDEX` command handles compaction, similar to VACUUM or ANALYZE.

  • Comparing Native Postgres, ElasticSearch, and pg_search for Full-Text Search

    Neon mediumAdept

    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. ElasticSearch fixes search quality and speed but adds a second source of truth, a sync pipeline, eventual consistency and heavy cluster operations. pg_search, a Rust extension built on Tantivy, embeds a BM25 inverted index inside Postgres with transactional updates, fuzzy and phrase queries, snippets and aggregations. On a 10-million-row benchmark it beat native FTS by 20-1000x (e.g. top-N ranked results in 81ms vs 38.8s), and it is pitched for datasets that fit on a single Postgres instance.

  • 6 tips to optimize storage costs for your Postgres databases

    Neon mediumAdept

    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. Shorter retention saves storage but narrows the point-in-time recovery window. For bloat, look in pg_stat_activity for long-running transactions that block autovacuum.

The quest for software engineering knowledge.

Look:
Layout: