Training a 4B Model to Beat Postgres Query Plans

Postgres is brilliant, but its query optimizer still stumbles on complex joins. I trained a compact 4B model to generate faster query plans instead....

Feed
September 17, 2026
Training a 4B Model to Beat Postgres Query Plans


Most developers trust database query optimizers blindly. We write SQL, cross our fingers, and let Postgres handle the heavy lifting under the hood. But query optimization is notoriously brutal. In fact, figuring out the optimal join order is an NP-hard problem that has baffled computer scientists for decades, meaning even mature engines regularly pick suboptimal paths that grind heavy workloads to a halt. When Leis and his team benchmarked query optimizers a decade ago and found them lacking, I figured things would improve dramatically by now. They haven't.

That speed gap made me wonder if we could bypass old heuristics entirely using machine learning. It after all, language models thrive on tasks with clear ground truths, which easily verifiable outputs. And database execution time is about as binary as it gets: either the query finishes fast, or it crawls. So I set out to answer a single question. Could a tiny, open-weights model be post-trained via supervised fine-tuning and agentic reinforcement learning to always outperform Postgres's default planner on punishingly complex joins?

Building the training rig was a mess of engineering constraints. I split the heavy lifting across a rented twin H100 node running vLLM alongside the trainer, while physically tethering four separate Postgres database containers directly to the machine on my desk to handle the execution feedback loop. Because disk I/O and Linux page cache contention will instantly corrupt performance benchmarks if you are not obsessively careful, I had to engineer a custom measurement environment alongside a specialized GRPO variant to accurately score noisy RL rollouts in the wild.

Training a 4B Model to Beat Postgres Query Plans

The results genuinely shocked me. Occasionally, that small 4B model didn't just edge out the database. It slashed latency by over forty percent across a brutal suite of join-heavy IMDb queries where it initially could not even create a valid plan. We spend millions chasing massive foundational models for trivial chat interfaces while ignoring the profound use compact, specialized models offer when applied to deterministic base problems. Sometimes, the best way to fix a stubborn bottleneck is to throw out the rulebook and let an agent learn the terrain the hard way.

Optimization is an endless game of trade-offs, but ignoring machine learning's viability for systems-level routing feels increasingly shortsighted. If a lean model running on commodity hardware can outsmart decades of handcrafted database heuristics, our approach to execution planning is due for a massive rethink.