A 4B AI Model Just Beat Postgres at Query Planning

A 4B AI Model Just Beat Postgres at Query Planning

Postgres has optimized queries for decades, but a tiny 4B model just proved it can pick faster execution plans.

Developer Rohan Bansal trained a 4-billion parameter open-weights model using fine-tuning and reinforcement learning to generate custom query plans. At first, the model couldn't produce a valid plan for 99 out of 113 join-heavy queries. But after RL training—split between a rented dual-H100 node and four desktop Postgres containers—it cut query latency by 44.7%.

Why it matters: Database optimizers rely on heuristics because join ordering is an NP-hard problem. Because execution speed is simple to verify, reinforcement learning can directly reward faster query paths and outperform standard database engines.

Know this: To make it work, Bansal used off-policy distillation from GPT-6 Astra trajectories and built a custom GRPO variant to score rollouts cleanly against Linux cache noise.

Your database optimizer might soon hand off its hardest choices to a small local neural network.

Sources