← Wed, Sep 16 digest

Training a 4B model to produce 81% faster query plans than Postgres

read article ↗ hn thread ↗

summary

A small 4B parameter language model can be trained using supervised fine-tuning and agentic reinforcement learning to produce Postgres query plans that significantly outperform default system plans. By treating query optimization as a task with a verifiable output—execution time—the model can learn to generate optimal hints for complex, join-heavy queries. This approach is particularly effective for analytic workloads where queries are run repeatedly, amortizing the initial cost of finding the best plan. The experiment demonstrates that a 4B model can achieve a a substantial latency reduction across a wide range of queries.

key points

  1. Postgres's default query optimizer often fails on complex joins because it relies on uniform distribution assumptions for cardinality estimation, which can lead to suboptimal execution plans.
  2. The author developed a custom agent harness that allows the model to interact with the database, inspect statistics, and iteratively test candidate query plans using pg_hint_plan to find the fastest execution path.
  3. Ensuring measurement accuracy was critical; the author minimized 'noise' from Linux page cache and Postgres shared buffers to prevent the reinforcement learning process from being fooled by phantom speedups.

who should read it

Database engineers and AI researchers interested in applying reinforcement learning to systems optimization and query planning.

discussion skeptical

consensus

Commenters are generally skeptical of using LLMs for query planning, arguing that the benchmarks used small, memory-resident datasets that do not reflect real-world OLTP scales. They believe that the lack of determinism and the potential for hallucinations could lead to catastrophic performance regressions in production. Most agree that traditional cost-based optimization and improved statistics are more reliable and efficient solutions.

top insight

The discussion highlights that while LLMs can find optimal plans, the overhead of generating them—including training costs and inference time—often outweighs the benefits compared to handcrafted heuristics. A key perspective is that query planning is a constrained mapping problem where a small, specialized network or a GNN would be more appropriate than a massive LLM trained on general text. This suggests a shift toward 'neural heuristics' rather than generative AI for database internals.

Generated by an LLM from the article and thread. Check the sources before relying on it.