← all news

A 4B model that beats the Postgres query planner

AI · · · source (rohanbansal.com)

Rohan Bansal set out to beat the Postgres query planner with a small model, and documented the whole run. He fine-tuned a 4B model, a Qwen 3.8 distillation, with LoRA, touching only 21 million parameters, then wrapped it in an agent with six tools for inspecting tables and testing query plans. Training had two stages: distill reasoning traces from GPT-6 Astra as a teacher, then improve with reinforcement learning using a custom GRPO variant whose reward is measured against Postgres's own default plan.

The results are specific. On the Join Order Benchmark, 113 queries over the IMDb dataset, the tuned model reached a 1.81x geometric mean speedup with best-of-three sampling, improved 68 of 113 queries by more than 5%, and regressed none. The raw 4B model, by contrast, failed on 99 of those queries before training. Much of the writeup is about fighting measurement noise: he raised Postgres shared_buffers from 128 MB to 2 GB to cut timing variance from about 5% to 0.9%, and clipped speedup rewards so a single lucky run could not fool the model.

The economics are the quiet punchline. The full project cost about $1,200: roughly $800 for 95 hours on a rented 2xH100 node and $400 in API fees. The writeup walks through the reward design and the failure cases, not just the wins.

Why it matters

If you have a narrow, measurable optimization problem, this is a worked recipe for training a small specialized model that beats a hand-tuned system for the price of a laptop. The hard part is not the model, it is designing a reward that noise cannot game.

EngineeringReinforcement LearningDatabases