Header image: S&R training for SDRF (Uttarakhand) by Nanda Devi Adventure and Outdoor Education Institute_by_Nanda_Devi_Adventure_and_Outdoor_Education_Institute.jpg) by Sunil Kainthola, CC BY-SA 4.0, via Wikimedia Commons — cropped to 16:9 and colour-adjusted.
Key takeaways
- A 4B model produced PostgreSQL query hints 81% faster than default optimizer
- Reinforcement learning optimized for actual execution time, not cost estimates
- Specialized smaller models can outperform general-purpose database optimizers
A 4-billion-parameter language model just generated PostgreSQL query hints that delivered plans 81% faster than the database’s default optimizer. Not 10%. Not 20%. Eighty-one percent. That’s not incremental improvement—that’s a full-scale performance rout.
The researcher didn’t set out to build an AI oracle for databases. The experiment explored whether a small, open-weights model could be post-trained via supervised fine-tuning and agentic reinforcement learning to beat PostgreSQL’s default query plans. The answer wasn’t just yes. It was spectacularly yes.
Why PostgreSQL’s Optimizer Falls Short
PostgreSQL’s query optimizer is a marvel. It juggles cost models, statistical estimates, and heuristic rules to generate execution plans. For simple joins or indexed lookups, it’s brilliant. But introduce complex analytical queries, skewed data, or unpredictable access patterns, and its limitations become glaring.
The problem isn’t that PostgreSQL’s optimizer is bad. It’s that it’s static. It makes decisions based on precomputed statistics and fixed cost models—not real-world execution feedback. If it estimates a full table scan will be cheaper than an index lookup, it generates that plan—even if the index would have been faster in practice. This disconnect between estimated and actual costs isn’t a bug. It’s a fundamental limitation.
And the cost models? They’re simplistic. They assume uniform data distribution, ignore column correlations, and struggle with nested subqueries or complex joins. These gaps create room for alternatives—especially ones that adapt dynamically to execution feedback.
The Experiment: Training a 4B Model to Outsmart PostgreSQL
The approach was straightforward in concept but computationally demanding in execution. He took a 4-billion-parameter language model and post-trained it using supervised fine-tuning followed by reinforcement learning. The goal wasn’t to replace PostgreSQL’s optimizer. It was to generate query hints—directives like /+ HashJoin(t1 t2) / or /+ SeqScan(t1) /—that steer the optimizer toward better decisions.
The training process relies on measuring whether one way of running a query is faster than the PostgreSQL default. Faster plans reinforced the model’s behavior.
The key insight? Reducing the problem to a single, measurable outcome: query execution time. Traditional optimizers rely on cost estimates. This approach learned directly from real-world performance. That shift—optimizing for actual speed, not estimated cost—made all the difference.
The results weren’t just good. They were striking. Across the benchmark queries, the model’s hints produced plans 81% faster than PostgreSQL’s defaults. That’s not a tweak. That’s a transformation.
Reinforcement Learning: The Secret Weapon
Reinforcement learning (RL) isn’t new, but its application to query optimization is still rare. Most database optimizers use cost-based models or rule-based heuristics. RL treats optimization as a learning problem. The model isn’t told what a "good" plan looks like. It discovers it through trial and error, guided by the reward signal of execution time.
This approach has clear advantages:
- Adaptability: The model learns patterns static cost models miss—correlations between columns, workload-specific access patterns.
- Feedback loop: Unlike traditional optimizers, which rely on precomputed statistics, RL models learn from actual execution feedback. If a hint leads to a faster plan, the model doubles down on that behavior.
- Specialization: By focusing on a single objective—execution time—the model develops highly specialized strategies for specific query patterns.
The experiment’s success hinges on this narrow focus. Traditional optimizers juggle multiple objectives—minimizing CPU, I/O, memory—while adhering to heuristic rules. RL, in this case, ignored all that and optimized for one thing: speed. That singular focus is what allowed a relatively small model to outperform a decades-old optimizer.
Why a 4B Model Worked: Specialization Beats Generalization
Before this experiment, it wasn’t obvious that a 4-billion-parameter model could outperform PostgreSQL’s optimizer. Larger models—like hypothetical future versions of GPT-6 Astra or Qwen 3.8 (with its 2.4 trillion parameters)—hadn’t demonstrated this capability. Bansal himself was skeptical:
"If larger models like GPT-6 Astra or Qwen 3.8 2.4T couldn’t improve upon default PostgreSQL query plans, I couldn’t expect the 4B model to either. "
Yet the 4B model succeeded. Why? Because specialization beats generalization in this domain.
PostgreSQL’s optimizer is a general-purpose tool. It’s designed to handle a wide variety of queries, schemas, and workloads. The 4B model, by contrast, was trained for one task: generating hints that lead to faster query plans. It didn’t need to understand SQL syntax, database theory, or query semantics. It just needed to learn which hints correlated with faster execution.
This aligns with a broader trend in AI: smaller, specialized models can outperform larger, general-purpose ones when the task is narrow and the reward signal is clear. We’ve seen this elsewhere—AlphaFold dominating protein folding, MuZero mastering games without prior knowledge. The lesson? Raw model size isn’t everything. What matters is alignment between the training objective and the problem.
The Training Infrastructure: Compute Isn’t Cheap
Training a model to optimize queries isn’t trivial. Bansal rented a 2x H100 node from Lambda—not a gaming rig, but a high-end setup built for large-scale machine learning.
The hardware and software stack were critical. The experiment used Hugging Face’s Optimum library with ONNX Runtime, which can improve training times by 35% or more for transformer-based models. ONNX Runtime alone accelerates large-model training throughput by up to 40%, and when combined with DeepSpeed, the improvement reaches up to 130%.
Here’s why this matters:
- ONNX Runtime: Optimizes the model’s computation graph, reducing overhead during training and inference.
- DeepSpeed: Implements techniques like ZeRO (Zero Redundancy Optimizer) to shard model states across GPUs, enabling larger batch sizes and faster training.
This infrastructure isn’t just about speed. It’s about making reinforcement learning feasible for query optimization. Without these optimizations, the training process would have been prohibitively slow or expensive.
Limitations and Open Questions
The experiment is a proof of concept, not a production-ready system. Several limitations and questions remain:
Query Hints, Not Full Replacement
The model generates hints that influence PostgreSQL’s optimizer, not plans that replace it entirely. This is pragmatic—easier to integrate into existing workflows—but limiting. Some queries might benefit from a more radical overhaul of the execution plan, which hints alone can’t provide.
Execution Time as a Noisy Signal
The training process relies on measuring execution time, which can be noisy. Cache state, background processes, or hardware variability can skew timing measurements. While the experiment controlled for these variables, real-world deployments would need robust mechanisms to handle noise in the reward signal.
Generalization to Unseen Queries
The model was trained on a specific set of queries. How well does it generalize to unseen queries—especially those with different patterns or complexities? The experiment didn’t test this extensively, but it’s critical for real-world adoption.
Computational Cost
Training a 4B model on a 2x H100 node isn’t cheap. The computational cost of reinforcement learning raises questions about scalability. Would this approach be feasible for smaller organizations, or is it limited to well-funded labs and tech giants?
Dynamic Workloads and Schemas
Databases aren’t static. Schemas evolve, workloads shift, data distributions change. A model trained on yesterday’s queries might not perform well on today’s workload. How can this approach adapt to dynamic environments?
Broader Implications: AI for Systems Optimization
This experiment isn’t just about databases. It’s part of a broader trend: using AI to optimize complex systems. We’ve seen this before:
- Compiler optimization: Google’s MLGO uses reinforcement learning to optimize LLVM compiler passes.
- Network routing: DeepMind’s work on Google’s data center cooling systems reduced energy usage by 40%.
- Hardware design: AI is optimizing chip layouts and memory hierarchies.
The common thread? Narrow, well-defined optimization problems with clear reward signals. Query optimization fits this mold perfectly: the goal is singular (execution time), the feedback is immediate (query runtime), and the optimization space is constrained (the set of possible query plans).
This suggests reinforcement learning could be applied to other areas of database management:
- Distributed query execution: Optimizing plans for distributed databases like CockroachDB or Google Spanner.
- Real-time analytics: Dynamically adjusting plans for streaming workloads in systems like Apache Flink or Materialize.
- Index selection: Learning which indexes to create or drop based on query patterns.
The Future: Can This Scale Beyond PostgreSQL?
The experiment focused on PostgreSQL, but the approach could extend to other databases and query engines. Here’s how:
MySQL, SQL Server, and Beyond
Other relational databases have optimizers with similar limitations. A model trained for one database might not transfer directly to another, but the underlying approach—reinforcement learning guided by execution time—should generalize.
Query Engines: Spark SQL, DuckDB, BigQuery
Modern query engines handle massive datasets and complex analytical queries. Their optimizers face many of the same challenges as PostgreSQL’s. Could a reinforcement learning model generate hints or even full plans for these engines? Likely yes, but the training process would need to account for their unique execution models—Spark’s distributed execution, DuckDB’s vectorized processing.
Hybrid Approaches
A pure RL-based optimizer might be too computationally expensive for widespread adoption. A more practical approach could combine traditional optimizers with AI-generated hints:
- Use the default optimizer for simple queries.
- Fall back to AI-generated hints for complex queries where the optimizer struggles.
- Continuously retrain the model on new queries to adapt to evolving workloads.
This hybrid approach would balance performance, cost, and robustness.
Open Questions and Next Steps
The most exciting implication, though, is what this says about the future of database optimization. For decades, query optimizers have relied on static cost models and heuristic rules. This experiment proves that AI can do better—not by replacing the optimizer, but by augmenting it with learned, adaptive strategies.
Specialization Wins
The success of this 4B model challenges a common assumption in AI: that bigger models are always better. In this case, a smaller, specialized model outperformed a general-purpose optimizer by focusing on a single, measurable outcome.
This isn’t to say bigger models don’t have their place. They excel at tasks requiring broad knowledge or creativity—language understanding, code generation, open-ended problem-solving. But when the problem is narrow and the reward signal is clear, specialization trumps generalization.
For database optimization, this is a game-changer. It suggests AI-driven optimizers could become standard, especially for complex analytical workloads. It also raises the possibility of AI optimizing other parts of the database stack—index selection, query scheduling, even physical database design.
The open question is whether this approach can move from research labs to production environments. The computational cost of training is a hurdle, but as hardware improves and training techniques advance, that barrier will lower. The bigger challenge might be integration—convincing database vendors to adopt AI-generated hints or plans alongside their existing optimizers.
One thing is certain: query optimization just got a lot more interesting. And PostgreSQL’s default optimizer? It’s now on notice.