Tech News
← Back to articles

Optimizing Top K in Postgres

read original related products more articles

How We Optimized Top K in Postgres

By Ming Ying on February 13, 2026

In databases, Top K means “give me the K best rows, ordered by some column or value.” Commonly that means “the most recent rows,” “the highest scores,” or “the largest values.”

It feels like a basic problem that Postgres should solve. After all, can’t we just create an index? Yet in many production Postgres deployments, Top K is deceptively hard. This post examines where Postgres’ Top K optimizations shine, where they falter, and why search libraries like Lucene/Tantivy or databases like ParadeDB that specialize in Top K take a fundamentally different approach.

Postgres' Solution: A Sorted B-Tree

Let’s begin with a single table containing 100M rows.

CREATE TABLE benchmark_logs ( id SERIAL PRIMARY KEY , message TEXT, country VARCHAR ( 255 ), severity INTEGER , timestamp TIMESTAMP , metadata JSONB );

We want to return the top 10 most recent rows by timestamp :

SELECT * FROM benchmark_logs ORDER BY timestamp DESC LIMIT 10 ;

Without an index, this query takes 15 seconds. To accelerate it, we can create a B-Tree index on timestamp .

... continue reading