A four-billion-parameter model outperforms Postgres plans, with reinforcement learning breaking the query ordering dilemma
Listen to this article
Read by Anchor
Computer science researchers have posed a fundamental question since 2015 about the true efficiency of query-optimizing engines in databases, and the same team, a decade later, confirms that traditional engines remain unable to deliver optimal performance. A recent engineering experiment by researcher Rohan Bansal shows that small, open-weight language models can overcome this long-standing gap, after successfully training a four-billion-parameter model from the Qwen family using fine-tuning and reinforcement learning, achieving a 44.7 % reduction in query response time across 113 complex queries, clearly outperforming the default plans of the Postgres engine.
The engineering difficulty in database engines lies in the ordering of join operations, a problem mathematically classified among non-deterministic, NP-hard problems. The query engine experiences a massive combinatorial explosion when new tables are added to a query: a query joining two tables has 96 possible execution paths, which jumps to 4,608 paths with three tables, exceeds 442,000 for four tables, and surpasses 532 billion execution paths for just seven tables. These options branch across join ordering in the execution tree, the direction of inner and outer tables, the choice of hash versus nested-loop join algorithms, as well as variations of sequential or index scans. Although Postgres relies on dynamic programming and genetic algorithms to prune the search space, its assumptions of uniform distribution break down against actual filter predicates, inflating the processed data volume fourfold without justification.
Verifying a query plan’s efficiency is a direct measurement, while searching for the optimal plan within a vast probabilistic space carries the larger computational burden.Bansal relied on this paradox to train a language model that excels at tasks with easily verifiable outputs, using a single axis: actual execution time. The experiment began with behavioral distillation through five hundred training trajectories from GPT-6 Astra agents, followed by fine-tuning a model that failed in its initial version to produce an acceptable plan for 99 of 113 queries. The system then trained the model using the Generalized Relative Policy Optimization (GRPO) reinforcement-learning algorithm, distributing the workload across a rented cloud server equipped with H100 processors for training and inference, and four local Postgres containers for performance measurement while isolating cache contention on Linux.
The learning cycle generated four exploratory attempts per query, with the model proposing an execution strategy each round, which was sent to the engine to measure its time against the default plan; performance differences were then translated into numerical rewards that adjusted the model’s weights toward faster plans. This experiment produced plans that outpaced the traditional engine by up to 81 % on certain queries, allowing the model to act as a specialized inference guide that surpasses statistical shortcomings in relational databases.
This experiment positions the regional engineering gap as a new approach to managing data-infrastructure efficiency.Banking platforms and fintech companies in the Gulf, as well as commerce and logistics projects in Egypt and the Levant, rely on large Postgres databases whose complex queries consume compute resources, often prompting organizations to pursue costly server upgrades or resort to intricate data sharding. Running specialized four-billion-parameter models locally enables acceleration of sensitive analytical queries without exporting table schemas or data to external platforms, in line with data-sovereignty requirements. This path requires infrastructure leaders to develop their teams’ skills from manual index writing to building isolated measurement environments and employing reinforcement-learning frameworks to inevitably improve operational systems.