Software Ownership

AI Query Optimization vs. Postgres: An Experiment in Owning the Planner

Bao and LLM-QO already beat Postgres's native planner by 20 to 70 percent. The real question is whether a $15 fine-tuned model can be something a team owns, not a feature it rents.

At a glance
  1. 01Postgres allows overriding its native planner via built-in hooks without modifying C source code.
  2. 02Fine-tuning a small 4B parameter model for query planning now costs less than $15 in compute.
  3. 03Learned optimizers can regress catastrophically on outliers, requiring safe fallback constraints.
  4. 04The real cost of AI optimization is engineering time for data collection, not the model compute.
Diagram of a query being routed through multiple candidate execution paths toward one optimized path, illustrating AI-based query optimization compared to a standard database planner.
Illustration generated by Remy for this story.

AI query optimization already beats Postgres's native planner in published benchmarks, by margins from single digits to nearly 70 percent.12 Nobody has proven that a small, cheaply trained model can deliver those gains as something a team owns outright, rather than a feature rented from a managed database vendor. That's the actual open question here: not whether AI can beat the planner, that's settled, but whether you can own the thing that beats it.

Figure 1
LLM-QO's improvement over Postgres's built-in optimizer
improvement in average execution time (%)
5.6%IMDB8.6%JOB-light68.7%DSB
Benchmark

The premise: a planner you train instead of a planner you're issued

Postgres's query planner is free. You don't pay a subscription for it. But its intelligence is fixed at whatever the community shipped in your version. You can tune parameters, add indexes, rewrite queries. You can't teach it anything new about your specific workload the way you'd fine-tune a model.

That's the frame this piece tests. If a learned optimizer can beat Postgres's cost-based planner by double digits, and fine-tuning a small model now costs less than a nice dinner, then "AI query optimization" stops being a research curiosity and becomes an ownership question: build a planner you control, or keep renting one bundled into whatever managed Postgres tier you're paying for.

How does Postgres's planner work, and why is it replaceable?

Postgres uses a cost-based optimizer. For any given query, it enumerates possible execution plans, join orders, index choices, scan types, estimates the cost of each against table statistics, and picks the cheapest one it finds. The catch: those cost estimates are just estimates. They rest on assumptions about data distribution that don't always hold, and the join-ordering search space grows fast enough that the planner has to prune it heavily on complex queries.

Here's what makes this whole experiment possible: Postgres exposes hooks, literal function pointers the planner calls at specific moments, that let you intercept and override its behavior without touching a line of Postgres's C source.3 You don't need to fork Postgres to change how it plans queries. You need to write something that plugs into a hook that already exists.

That architectural fact is why this isn't a thought experiment. The plumbing for AI query optimization already exists inside Postgres. Someone just has to build the model that uses it.

The prior art: Bao, Neo, and LLM-QO already did this

Learned query optimizers aren't hypothetical. They're a small but real research lineage, and the results beat what most DBAs assume.

  • Bao, built at MIT CSAIL and Intel Labs, plugs into Postgres through the same hook architecture described above and steers the native planner with coarse-grained hints, things like disabling nested loop joins for a specific query, chosen through reinforcement learning. Across three real workloads on Google Cloud, Bao cut cost and latency by roughly 50 percent versus native Postgres, and about 20 percent versus a tuned commercial database.1 It trained in about an hour, not days.1
  • Neo, an earlier system from largely the same research group, used reinforcement learning to tailor plan choices to the latency actually observed on a specific database instance, framed explicitly as a tool to improve "thousands of applications which rely on PostgreSQL."4
  • LLM-QO, a 2025 system, drops plan enumeration entirely and reframes query optimization as text generation: a fine-tuned LLM reads the query, table statistics, and a reference plan, then generates the plan directly. Fine-tuned through a two-stage pipeline, Query Instruction Tuning followed by Query Direct Preference Optimization, on open-source model backbones with LoRA, it beat Postgres's built-in optimizer by 5.6 percent on IMDB, 8.6 percent on JOB-light, and 68.7 percent on the DSB benchmark.2
Figure 2
Bao's gains over Postgres's native planner
50%
Cost & latency reduction vs. native Postgres
20%
Reduction vs. a tuned commercial database
1 hr
Training time

None of these systems replace Postgres. All three sit on top of it, steering or generating plans that the same execution engine runs. That distinction matters for everything that follows.

Why a 4B model, not a 175B one

The instinct is to reach for a frontier model. That's the wrong instinct here, and it's a cost argument, not a capability one.

Query planning is narrow and repetitive. The input format is constrained (SQL plus statistics), the output format is constrained (a plan), and the domain doesn't require general reasoning about the world. NVIDIA's own research on agentic tasks argues that small language models, roughly under 10 billion parameters, are sufficiently capable for exactly this kind of narrow job, and run 10 to 30 times cheaper than frontier models while fine-tuning in a few GPU hours instead of days.

That economic gap compounds with how cheap fine-tuning has gotten in general. QLoRA fine-tuning of a 7B-8B model on a marketplace GPU now runs $3 to $15 in compute.5 A 4B model targeted at one narrow task, plan generation over one schema, sits well inside that range. This is the same math behind tuning a local model to reason like the API it replaced: the model isn't the bottleneck. The configuration and training data are.

Figure 3
Fine-tuning a small model costs less than a dinner out
$3
QLoRA fine-tuning cost, low end (7B-8B model)
$15
QLoRA fine-tuning cost, high end (7B-8B model)
Source: Medium

The experiment: training a 4B model to plan queries

Here's what building this actually looks like, following the LLM-QO recipe scaled down to a model one team could own.

  1. Build the training corpus. Collect real queries against your actual schema, along with table statistics and reference execution plans, mirroring the QInstruct data format LLM-QO used to encode SQL, stats, and plans as structured text.2
  2. Instruction-tune first. Run a Query Instruction Tuning pass so the model learns the mapping from query-plus-stats to a valid plan, the same first stage LLM-QO used before preference tuning.2
  3. Preference-tune second. Apply a DPO-style pass, ranking candidate plans by actual execution cost so the model learns to prefer cheaper plans over merely valid ones, following LLM-QO's QDPO stage.2
  4. Wire it into Postgres via the hook. Deploy the model's output as hints through the same planner_hook mechanism Bao and pg_hint_plan already use, so the native planner still executes but takes direction from the model.136
  5. Evaluate against a held-out workload, not the training set. This is where most of the field has cut corners, and it's the subject of the next section.

Where it breaks: tail catastrophe and the trust problem

This is where the promotional version of this story falls apart, and where the honest version starts.

A rigorous independent benchmarking study out of Zurich University of Applied Sciences re-evaluated six published learned query optimizers, Neo, Balsa, Lero, LEON, RTOS, and HybridQO, and found that once train/test splits and covariate shift are properly controlled for, Postgres's native optimizer wins in almost all experiments.7 The headline numbers in individual papers often hold up on the benchmark they were tuned against and nowhere else.

Bao's own paper is candid about a second failure mode: learned optimizers, on average, beat traditional ones, but sometimes regress catastrophically on individual queries, up to 100 times worse than the traditional planner would have chosen.8 Bao's fix is architectural. It constrains its own hint space to a limited set of options, which bounds how far a bad choice can drift from optimal, though the source notes this also means Bao isn't always able to find the best possible plan.18

There's a third problem, less about numbers and more about operations. A black-box model's plan choice is harder for a DBA to debug than a cost-based optimizer's explainable cost estimate. When the traditional planner picks a bad plan, you read EXPLAIN ANALYZE and see why. When a fine-tuned model picks a bad plan, you're debugging a neural network's judgment call at 2am.

What does owning your planner cost compared to renting Postgres?

Set the ownership case against real rent. One team reported paying $12,000 a month for a single multi-AZ RDS Postgres instance with 10TB of storage, and they were still burning engineering hours fighting IOPS limits on top of that bill.9 Postgres itself runs 49 percent of developers, per the 2024 Stack Overflow survey, the most popular database for the second year in a row.10 This isn't a niche exercise. It's the default database for most teams reading this.

Figure 4
The monthly cost of renting Postgres at scale
$12,000/mo
Monthly cost of one multi-AZ RDS Postgres instance (10TB)

Against that, the marginal cost of an owned, fine-tuned 4B planning model is close to zero once it's trained. Training itself runs in the tens of dollars.5 The real cost isn't compute, it's the engineering time to build the training corpus, wire in the hook, and monitor for tail regressions, the same tradeoff covered in the playbook for self-hosting a company's AI stack: the model is cheap, the plan for owning it is the actual work. Teams already running their own inference layer instead of renting it from a vendor, using something like Remy to host fine-tuned models internally, are the ones positioned to actually run this experiment instead of just reading about it.

Verdict: is this a real contest yet?

Partially. AI query optimization already wins clearly in the steering role. Bao's hint-based approach delivers real, reproducible gains on top of Postgres's own planner, with low training cost and bounded downside.18 LLM-QO shows a small fine-tuned model can generate competitive or better plans on specific benchmarks.2 Neither requires replacing Postgres. Both require owning a layer on top of it.

Full replacement of the cost-based planner isn't there yet. The Zurich benchmarking work is a fair warning that most learned optimizers don't generalize past the workload they were built on, and the tail-catastrophe risk is real enough that nobody serious is ripping out their cost-based optimizer for a model.7 "Extreme software ownership" today looks less like replacing Postgres's core and more like this: a cheap, narrow, owned model sitting inside Postgres's own extension points, steering decisions the native planner still makes, with a human able to override it the moment it goes wrong. That's not a smaller ambition than replacing the planner outright. It's the version that survives contact with a production incident.

Figure 5
Four ways to plan a query, compared
Four ways to plan a query, compared
Upfront costDegree of ownershipTail-regression riskDebuggabilityTraining time
Native Postgres plannerteams that want zero training risk$0LowLowHighNone
Bao-style hint steeringbounded, low-risk gains on top of PostgresLow (~$ few)MediumLowMedium~1 hour
LLM-QO-style plan generationteams chasing the largest published gains$3-$15 computeHighMediumLowHours (two-stage fine-tune)
RecommendedOwned, fine-tuned 4B planner (this experiment)teams willing to own the model and the monitoring$3-$15 + engineering timeHighMediumLowFew GPU-hours
Ratings are relative across these options, not absolute. Synthesized from the sources discussed in this piece.
Source: Remy analysis
Frequently asked
Questions readers ask
Does AI query optimization actually beat PostgreSQL's built-in planner?

In controlled research settings, yes, sometimes by wide margins. Bao improved cost and latency by roughly 50 percent over native Postgres and about 20 percent over a tuned commercial database across three real workloads. LLM-QO reported 5.6 to 68.7 percent execution-time improvements depending on the benchmark. But an independent benchmarking study found that most published learned optimizers don't reliably beat Postgres once evaluation methodology is tightened.

Can you add a learned query optimizer to Postgres without forking it?

Yes. Postgres exposes a planner_hook and related hooks that let external code intercept and override plan choices without modifying Postgres's source. This is the same mechanism pg_hint_plan and pg_stat_statements use, and it's how Bao integrates as an extension.

Why use a small 4B model instead of a large general-purpose LLM for query planning?

Query planning is a narrow, repetitive task, not a general reasoning problem, so a small model is sufficient. NVIDIA's research argues small language models under roughly 10B parameters can run 10 to 30 times cheaper than frontier models for tasks like this, and fine-tuning a model in that range costs as little as $3 to $15 in compute on a marketplace GPU.

What's the biggest risk of using a learned query optimizer in production?

Tail catastrophe: learned optimizers can regress performance on specific queries by up to 100 times compared to a traditional planner, even while improving average performance. There's also a debugging cost, since a black-box model's plan choice is harder to explain than a cost-based optimizer's estimate.

Is Postgres still the right database to build this on?

For most teams, yes. PostgreSQL is used by 55.6 percent of developers per the 2025 Stack Overflow Developer Survey, making it the most common target for this kind of optimization work rather than a niche case.

Sources
  1. 1Bao: Making Learned Query Optimization PracticalSIGMOD '21 / MIT CSAIL & Intel Labs (Ryan Marcus et al.)
  2. 2Can Large Language Models Be Query Optimizer for Relational Databases?arXiv (Jie Tan, Kangfei Zhao, et al. — CUHK / HKBU / Alibaba DAMO)
  3. 3Getting on a hook or PostgreSQL extensibility (FOSDEM slides)FOSDEM / Postgres Professional (Alexey Kondratov)
  4. 4Neo: A Learned Query OptimizerPVLDB (Ryan Marcus et al.)
  5. 5How to Fine-Tune LLMs for Under $20 (Step-by-Step)Medium
  6. 6pg_hint_plan — get the right plan without surprisesGitHub (ossc-db)
  7. 7Is Your Learned Query Optimizer Behaving As You Expect? A Machine Learning PerspectivearXiv (Claude Lehmann, Pavel Sulimov, Kurt Stockinger — Zurich University of Applied Sciences)
  8. 8Bao: Making Learned Query Optimization Practical (ACM abstract/summary with tail-latency and limitations discussion)ACM Digital Library (SIGMOD '21)
  9. 9Postgres RDS is too expensiveReddit r/devops
  10. 10Technology | 2024 Stack Overflow Developer SurveyStack Overflow
Portrait of Dana Whitfield
Dana Whitfield
SaaS Economics
Dana breaks down where software budgets actually go, one line item at a time.
More from Dana Whitfield
© 2026 The Official Remy BlogDrafted by AI authors, reviewed by human editors.