In one line: On the 113 queries of the IMDb Join Order Benchmark, a 4B open model post-trained by one person for about $1,200 (LoRA SFT on about 400 GPT-6 Astra agent trajectories, then RL with a noise-aware reward) writes pg_hint_plan hints whose plans average 1.40x the speed of Postgres 18's own in one search (geometric mean); the headline 1.81x, or 44.7% less summed time, is the best of three searches picked by single timings, with the search time excluded and the planner at its default cost settings.
How far to trust it: Believe the method and the measurement work: the noise rig, the reward fixes and the released code and per-query results are careful and honest, and the repository itself flags the caveats. Read 1.40x (one search) as the model's result and 1.81x as a search budget's: both on a benchmark watched during development, against Postgres with random_page_cost at its default 4.0 while the model often set 1.1, and with checkpoint differences smaller than the 1.33x to 1.47x spread between repeated searches. The evidence, judged.
The idea in one sentence: a plan's quality can be checked by timing it, so a 4B model can be taught by distillation and then reinforcement learning to write hints that steer Postgres's planner, provided the timing noise is engineered out first; on the Join Order Benchmark the trained model alone averages 1.40 times faster plans, and spending three searches per query, keeping the best by a single timing, reaches the title's 1.81.
You need: how Postgres picks a plan from estimated row counts, and what pg_hint_plan can force (Query planning and performance, sections 1 to 5 and the pinning block); GRPO and RLOO advantages (RL for LLMs, section 3); LoRA (PEFT).
New terms: JOB (Join Order Benchmark: 113 hand-written queries over the real IMDb data, 33 templates, 4 to 17 tables each); CEB (Cardinality Estimation Benchmark: 13,646 generated queries on the same data, the training set); PlanAction (the model's structured output: join tree, join and scan methods, settings, compiled into a pg_hint_plan comment); no-op error rate (how often a plan identical to the default is measured as more than 5% faster or slower); anchored GRPO (the article's advantage rule: a rollout is compared with Postgres's default and with its siblings' mean, never below zero).
The article's premise: judging a plan is easy (run it and time it), producing a good one is hard, and that asymmetry is what reinforcement learning on verifiable rewards exploits. Join ordering alone is NP-hard (in the article), and Leis et al. found in 2015 and again in 2025 that optimizers still pick poor plans, mostly from bad row-count estimates.
Its running example shows why order matters. Joining titles, movie companies and company names, with filters to Japanese companies (5% of 100,000) and the 2000s (20% of 1M titles): joining the companies first passes about 100,000 rows to the second join, joining the titles first about 400,000, four times the work. Counting every choice (two join trees times four input orientations, three join algorithms per join, four scan types per table) gives 8 × 3² × 4³ = 4,608 plans for three tables (in the article), and Postgres prunes them with dynamic programming, or a genetic search from 12 tables. It cannot count rows while planning, so it estimates them from per-column statistics (in the article) and assumes the filters on different tables are independent; if the Japanese companies made half the movies instead of 5%, the "cheap" order would pass a million rows. How estimates are made and fail, and why one bad estimate low in the tree spoils every join above it: Query planning and performance, sections 3 to 5.
The lever is pg_hint_plan, an extension that reads a comment such as /*+ HashJoin(a b) SeqScan(a) */ above a query and makes the planner use those methods and that join order (in the article). The SQL is unchanged, so a hinted plan still computes the same result; only the route differs.
The model does not replace the planner on every query; it is spent on queries worth optimising once and running many times. Giving a model the planner's own statistics would mean competing with decades of cardinality-estimation research, and its inference would cost more than Postgres's millisecond planning. A heavy analytic query run thousands of times is different: tens to hundreds of executions up front pay off over all later runs (in the article). That framing decides what the results mean: search time and training cost are excluded from every speedup, which is fair only for such repeated queries.
The policy is a small open model in an agent loop with six tools. The model is empero-ai/Qwen3.8-4B-Distill, a third party's distillation of Qwen 3.8 2.4T into Qwen 3.5 4B (4.66 billion parameters), chosen because it could be trained on two RTX 3090s at home (in the article). The harness, qo-agent, lets it inspect tables and indexes, read planner statistics for up to eight columns, read the default plan's estimates, submit a candidate for timing, keep the default, or finish with a chosen candidate.
The model never writes hint syntax: it emits a PlanAction JSON object, which the harness validates, compiles into pg_hint_plan hints and prepends to the query (in the article). The figure is one search as the final evaluation ran it (settings from the repository's experiment 033); each candidate's feedback is one timed run, the final score three interleaved pairs.
Training uses CEB's 13,646 queries (16 templates, 12 join shapes); testing uses JOB's 113 (33 templates, each its own shape), both on IMDb. Same database on purpose: the intended user optimises one company's own workload, so learning IMDb well is the point (in the article). What must not leak is query structure, so the author reduced every query to its join graph (tables as nodes, joins as edges, filters ignored) and checked for overlap between the two sets: there was none (in the article).
src/recompute.py. At three timings and 5%: 39.7%, the article's "~40%".The same query does not take the same time twice, and the whole method rests on telling a faster plan from noise. The rig ran four Postgres containers on a 16-core, 64 GB desk machine, 4 cores and 8 GB each, against the 8.5 GB IMDb slice, with a shared queue of queries (in the article). Each query was warmed until Postgres's buffer counters (pages found in its own cache, pages asked of Linux) changed by under 2% between runs, at least two and at most five warm-ups, then timed 20 times. That warm-up rule is the cold-against-warm trap of Query planning and performance, made into a protocol.
Noise was not a blur but two speeds. At Postgres's default 128 MB of shared_buffers, job-13b ran 14 times between 186 and 204 ms and 6 times between 227 and 253 ms (in the article): the counters were stable but not zero, because the cache was far smaller than the data and pages kept coming back from Linux. A no-op candidate scored on medians of three can then land one side in the slow clump.
The toy answers it exactly: 39.7% with three, 25.9% with five, 15.5% with seven. Repetition helps slowly against two clumps; the article's text figure, "~20%", is the share of triples whose median sits in the slow clump (230 of 1,140, in the article), not the rate at which the reward is fooled, which needs only one of the two sides to land there; its figure's caption gives the right number, "~40%". The author's conclusion follows: minimise the fooling rate itself, not the coefficient of variation, since two queries with the same spread can be fooled at very different rates (in the article).
The fix was memory, not more repeats. Measured over all 113 queries with three interleaved pairs, the no-op error rate was 5.0% and 5.4% (two calibrations) at 128 MB of shared_buffers, and 1.8% and 1.2% at 2 GB; the 90th-percentile query went from fooled 13% to 20% of the time to 0% to 1.3%; work_mem (4 or 32 MB) made no difference (in the article). Job-13b's coefficient of variation fell from 10.3% to 0.9%, and the 113 default plans got faster, 95 s to 60 s in total, from cache residency alone. Everything after this ran at 2 GB and 4 MB (in the article). Training's timings had to move to the desk machine too: on the rented GPU server the noise was "off the charts", most likely from sharing its CPUs.
Phantom wins and losses are equal because the draw is symmetric. The article's own metric is different: it slides a window of six consecutive runs over the 20 (15 windows), forms three interleaved pairs, and tries both roles in each pair (8 cases), 120 cases a query. On job-13b that gives 68 of 120 = 56.7%, higher than the 39.7% of random draws, because all six slow runs fall in the first 13 (runs 3, 5, 7, 9, 12 and 13), so most windows hold two or three of them where a random draw of six holds 1.8 on average. The calibration table's 5.0% is that metric averaged over all 113 queries.
Every result is reported two ways. The geometric mean of per-query speedups weights every query equally: a 2x and a 0.5x cancel to 1x (in the article). The total workload speedup divides summed default time by summed chosen time, so long queries dominate. The author reads the first as learning and the second as practical value, since one lucky plan on a long query can carry a workload total.
Frontier models showed the task is solvable. On a 10-query JOB sample, GPT-6 Astra (medium reasoning) reached 2.54x geometric mean with five candidates and 0.85x with one; Qwen 3.8 2.4T 2.26x and 2.02x (in the article). Single runs on ten queries, as the article says of one odd result.
The untrained 4B model mostly could not drive the harness. Of 113 searches, 81 produced no valid candidate, 16 failed at selection and 1 timed out; only six measured a new plan, five at 1.02x to 1.30x and one at 0.05x (in the article). Its PlanActions were malformed, its join trees missed or repeated tables or joined tables the query never joins, and it called tools at the wrong time or tools that did not exist.
First the model had to speak the harness; that came from copying GPT-6 Astra's trajectories. This is off-policy distillation: the student is trained with next-token loss on the teacher's tool calls and replies, never on its own samples (in the article; the method: Distillation and Small Language Models). Three practical steps: rendering the OpenAI Responses-format traces into Qwen's token format (Prime Intellect's renderers), masking the loss to the model's own turns, and unrolling each trajectory into one example per assistant reply, each with everything before it as context. The weights were adapted with a LoRA of 21.2 million parameters, 42.5 MB against 9.32 GB of bf16 weights (in the article; PEFT).
Astra was the teacher although its API returns reasoning summaries, not reasoning. Its trajectories were shorter (22,009 tokens of final context on average against 45,144 for Qwen 3.8 2.4T, all under the ~50,000 a 24 GB card could train on) and faster; trace inversion (expanding summaries into plausible reasoning) was the fallback if summaries hurt. They did not, by the results.
Two epochs helped, a third hurt, and validation loss saw neither. On 100 Astra trajectories from CEB (382 training rows after unrolling), one epoch lifted queries with a valid candidate from 14 to 48 of 113 but scored 0.72x; two epochs 1.08x; three 0.82x, while held-out loss sat at 0.305 to 0.306 through epoch 2 and rose to 0.322 by the end of epoch 3 (in the article). A further 300 trajectories (6 that kept the default without trying dropped) and two more epochs gave the RL starting point: 1.16x geometric mean, 1.06x workload, 20 wins and 5 regressions. The harness also changed between epochs one and two (keeping the default became allowed), so the first two rows are not like for like.
The first reward taught the model to play safe. It was ln(speedup) minus 0.1 per invalid candidate and 0.05 for repeating the default plan, and a flat −3 for a search with no valid candidate. Against −3, returning Postgres's own plan for −0.05 was the best bet, and the model learned it (in the article).
Plain GRPO made it worse, because it rewards beating the group, not beating Postgres. prime-rl's GRPO subtracts the group's mean reward (it does not divide by the spread). In the article's four-rollout example two rollouts are rejected (−3 each), one finds a slower plan (0.80x, ln 0.80 = −0.23) and one repeats the default (−0.05): the mean is −1.57, so the slower plan gets +1.34 and the copy of Postgres's plan +1.52, the most reinforced of all (in the article). Group-relative advantages in general: RL for LLMs, section 3.
The reworked rule anchors every rollout to the default. A rollout's quality is ln of its clipped speedup, soft-thresholded by 0.05 (anything within about 5% counts as zero and real gains or losses shrink by 0.05: the noise floor of section 5, built into the reward); keeping the default scores exactly zero. Its baseline is the mean quality of its scored siblings, leaving itself out (RLOO's leave-one-out baseline), floored at zero, so a rollout is never credited for being the least bad. Advantage is quality minus baseline, minus 0.02 if it picked a duplicate of the default; a search with no valid candidate gets −0.1 instead of −3 (in the article). In the same example all four rollouts are pushed down: −0.10, −0.10, −0.18 and −0.02. The figure recomputes both tables.
src/recompute.py; they match the article's tables.RL ran across two machines: generation and training on a rented 2 × H100 node, timing on the four Postgres containers at home, joined over Tailscale. The SFT adapter was merged into the base and a fresh LoRA trained. A first run (learning rate 1e-6, batch 8, four rollouts a query, 120 updates) changed nothing: 1.14x against the SFT model's 1.16x (in the article). The second setting (1e-5, batch 16, eight rollouts, 600 updates, twice in a row) worked. Since 92% of a rollout's time was inference, rollouts were changed to lease a Postgres worker only while timing, which let 20 run at once on four workers (in the article).
After 600 updates: 1.35x geometric mean, 1.16x workload, 34 wins, 0 regressions, a valid candidate on 99 of 113 queries; after 1,200: 1.41x, 1.29x, 38 wins, 2 regressions. The share of training rollouts that beat Postgres rose steadily through both runs. All these rows are one search per query, and the hero's band shows how far one search moves on its own.
The final checkpoint was run three times on every JOB query and scored three ways (in the article):
"81% faster" and "44.7% latency reduction" are the same measurement. The chosen plans' medians sum to 31.756 s against 57.398 s for the defaults (repository, experiment 033): 57.398 / 31.756 = 1.81 times the speed, 1 − 31.756 / 57.398 = 44.7% less time. The selection used only the single feedback timings; the score comes from the separate three-pair measurement, so the 1.81 is not inflated by picking the luckiest timing. It is, by the author's design, a different system from the trained model: three searches and up to 15 executed candidates per query, whose time is not counted.
What the model does, from the 339 final searches: it inspected tables, statistics or the default plan first in 295 of 337 searches that submitted candidates and used all five candidates in 235. Of 1,347 actions it gave scan hints 1,141 times, join trees 917 times, parallelism hints 572 times and row-count corrections only 146 times; it often forced nested loops and index scans, and often set enable_sort=off and random_page_cost=1.1 (in the article). Its gains came mostly from rewriting the join order, from a single scan fix, and from parallelism; job-01d went 90 times faster with a forced bitmap scan.
Cost: $1,200, about $800 for some 95 hours of the 2 × H100 node and $400 of API fees for the Astra trajectories (in the article). The author's conclusion: frontier models plus a small budget let a company train a small open model on its own niche task, and he lists a Bao-style hint sweep, on-policy distillation, trace inversion and noise measurements on dedicated cloud machines as next steps.
What holds up: the measurement work, the method, and a real gain from training.
What it does not show.
random_page_cost at 4.0 (with 2 GB of shared_buffers and an 8.5 GB database on NVMe), while one of the model's favourite moves was to set it to 1.1. Postgres's documentation suggests lowering it when data is likely fully cached (Query planning and performance, section 9). How much of the gain one configuration line would give is not measured; a Hacker News reader raised it and got no answer. The same goes for max_parallel_workers_per_gather = 2 against 572 parallelism hints.Verdict. Believe that the method works and copy the measurement discipline: a learned or LLM optimizer should be judged with a fooling rate, a noise floor in its reward and selection kept apart from scoring. Read 1.40x as what the trained 4B model does and 1.81x as what three searches buy, both against an untuned planner on a benchmark used during development; before believing any of it on your own workload, rerun the default with random_page_cost lowered and compare against a hint sweep like Bao's.
No replication or written rebuttal was found; the substantive responses are the author's own results file and a long Hacker News thread. Searched on 27 November 2026: the web, GitHub (no issues on the repository; two forks without changes), Lobsters and arXiv. Hacker News: 702 points and 144 comments (read 27 November 2026), mostly database practitioners; secondary sites restated the numbers, one of them as "up to 81% on the hardest cases", which the post does not say.
Separates the model's choice (1.4048x), the within-search rule (1.4355x) and best-of-three (1.8054x); notes that 1.805x "is not an 80.5% latency reduction", that comparing it with a one-search SFT baseline "would mix policy improvement with additional search compute", that JOB "has been inspected during development", and that only 17 queries win in all three searches.
Warns of overfitting and asks whether the plans would hold at scale and on more realistic workloads; database statistics and workloads drift.
A real planner must minimise planning plus execution on live statistics, and the 95 hours of training and the search are outside the benchmark numbers. The post's own framing (repeated analytic queries) answers part of this.
Answering whether a hinted plan still computes the query: the hints only choose among plans for the same SQL, and the debug log was used during evaluation to confirm Postgres applied them. The author's only comment in the thread.
The model's favourite setting, random_page_cost=1.1, suggests the baseline's cost settings were not tuned for fast storage, which "could easily explain the difference by itself". The repository confirms the baseline ran at the default 4.0; no reply.
Where the responses are right. The random_page_cost point is the strongest: the configuration confirms the default 4.0, and the model's own habits suggest part of its edge is a setting a database administrator would change once for everything. The planning-cost objection is right about live queries and answered by the post's scope (repeated analytic queries), but the speedups should be read with the search cost beside them. One reply was wrong: the database does have indexes on the foreign keys (the repository's load script creates 23).
random_page_cost 4.0, the 1.33x to 1.47x spread between repeated searches, and that "best" was chosen by one timing per candidate.The post was fetched on 27 November 2026 and its main text extracted to src/inputs/article.txt (13,432 words with its code blocks and figure labels, 58 minutes at 230 words a minute; about 11,300 words and 50 minutes of prose, the reading time on the card); every "in the article" link is a text fragment or a section id checked against that extract at build time. The page's metadata gives publication on 16 September 2026. The first Wayback Machine copy (16 September, 19:41 UTC) was extracted the same way and compared: the only change is an added Acknowledgements section. The reward and advantage formulas, which render as KaTeX without their TeX source, were read from the page's rendered text and its notes on each term.
The repository polyphilz/qorl was read on GitHub at its state of 27 November (last push 17 September): the README, src/qorl/evaluation/selection.py, the Postgres configuration docker/postgres/configs/001-pgconf-2gb-sb/pg.conf (Postgres 18.6, random_page_cost 4.0, effective_cache_size 4 GB, max_parallel_workers_per_gather 2), scripts/imdb/load.sql (23 foreign-key indexes), and the READMEs and results of experiments 032 and 033. Job-13b's 20 runs are the data values of the post's median-draw figure.
Figures: the post carries no open licence, so none is embedded; the results and the reward example are redrawn from its tables and credited, and the toy is this page's own, computed from the post's data. Responses: the Hacker News item and all 141 visible comments through its API (author polyphilz; submitted 16 September 18:50 UTC). Not found: any replication, a reply from the author on the cost settings, a Lobsters thread.
Every link opens in a new tab; links checked 27 November 2026. Reading time of the resources below: 5h 07m.
Rohan Bansal (independent; written at the Recurse Center): The post itself (rohanbansal.com, published 16 September 2026). Its interactive figures (the search-space counter, the calibration queue, the job-13b median draw, the reward formulas with notes on every term) are worth opening there; this page redraws only the results and the reward example, and recomputes the median draw exactly.
(50 min)
Rohan Bansal (independent; written at the Recurse Center): The author's analysis behind the 1.81x: how best-of-three was selected, the spread between repeated searches, and the caveats the post leaves out ("The 1.805x ratio is not an 80.5% latency reduction").
(20 min)
the authors: Harness, measurement, SFT and RL code, configs, every experiment's README and results, per-query results of the final run. No licence file; no weights or adapters released.
(30 min)
702 points, 144 comments (read 27 November 2026), submitted 16 September 2026.
(30 min)
Rohan Bansal (the author), 14 September 2026, author's analysis: Separates the model's choice (1.4048x), the within-search rule (1.4355x) and best-of-three (1.8054x); notes that 1.805x "is not an 80.5% latency reduction", that comparing it with a one-search SFT baseline "would mix policy improvement with additional search compute", that JOB "has been inspected during development", and that only 17 queries win in all three searches.
(20 min)
refibrillator, Hacker News (the top comment), 16 September 2026, critique: Warns of overfitting and asks whether the plans would hold at scale and on more realistic workloads; database statistics and workloads drift.
(2 min)
perrygeo and Someone, Hacker News, 16 September 2026, critique: A real planner must minimise planning plus execution on live statistics, and the 95 hours of training and the search are outside the benchmark numbers. The post's own framing (repeated analytic queries) answers part of this.
(2 min)
polyphilz (the author), Hacker News, 16 September 2026, author's reply: Answering whether a hinted plan still computes the query: the hints only choose among plans for the same SQL, and the debug log was used during evaluation to confirm Postgres applied them. The author's only comment in the thread.
(1 min)
zacmps, Hacker News, 17 September 2026, critique: The model's favourite setting, random_page_cost=1.1, suggests the baseline's cost settings were not tuned for fast storage, which "could easily explain the difference by itself". The repository confirms the baseline ran at the default 4.0; no reply.
(1 min)
27 November 2026: Last push 17 September 2026; 51 stars, 2 forks, no issues opened (read 27 November 2026). No adapter, merged weights or rollout logs released (outputs/ is not in the repository).
(5 min)
16 September 2026: The first copy (16 September, 19:41 UTC) and the post of 27 November differ only by an added Acknowledgements section; no result or claim changed.
(1 min)
Leis et al., PVLDB 2015 (third-party): The Join Order Benchmark and the evidence that cardinality estimates, not cost models, decide plan quality; the test set here.
(1 hour)
Marcus et al., SIGMOD 2021 (third-party): The same idea with a small model: keep the planner and learn which hint set to give it per query. The post lists a Bao-style sweep as its next experiment; no comparison was run.
(45 min)
Negi et al., PVLDB 2021 (third-party): Introduces the Cardinality Estimation Benchmark, the 13,646 training queries.
(45 min)
NTT OSS Center (maker): The extension the model writes for: scan, join, Leading, Rows, Parallel and Set hints.
(15 min)
Shao et al., 2024 (third-party): Where GRPO's group-normalised advantage comes from; the anchored variant here drops the standard deviation and uses a leave-one-out mean floored at zero.
(40 min)
Owns the planner, cost settings (random_page_cost), statistics and estimation errors, pg_hint_plan, and the learned-optimizer timeline with QORL in it.
Owns GRPO and RLOO advantages (section 3); the anchored variant is RLOO's leave-one-out baseline floored at zero.
Owns LoRA; already cites QORL's 21.2M-parameter adapter.
Owns off-policy distillation from a teacher's outputs.
This page is one of the Articles: articles worth a page of their own, each with its argument, its evidence judged and what others replied.