Hacker News

Best stories

Live mirror
30 storiesupdated just nowView source snapshot
  1. Show HN: An e-ink frame that hears birds and draws them as 1800s illustrations(github.com/arnegiacomo ↗)
    236comments
  2. Introducing System One Models and Jev(typesafe.ai ↗)
    472comments
  3. I can't stop thinking about Papua New Guinea(notnottalmud.substack.com ↗)
    468comments
  4. 25 years of mass surveillance is enough(schneier.com ↗)
    339comments
  5. An update on Wayback Machine access(blog.archive.org ↗)
    350comments
  6. EU chief opens door for Canada to become 'associate member'(bbc.com ↗)
    821comments
  7. Suspected sabotage causes major Netherlands rail disruption(bbc.com ↗)
    470comments
  8. Mistral X Mozilla: Private, Multilingual AI Browsing(mistral.ai ↗)
    182comments
  9. Apple Reference Image: A New Approach for Verified Photography(security.apple.com ↗)
    329comments
  10. Gemini 3.8 Live and 3.8 Live Extended Thinking(blog.google ↗)
    321comments
  11. US confirms for first time it has deployed space weapons(bbc.com ↗)
    359comments
  12. Why I'm still bearish on LLMs after Navier-Stokes(dank.systems ↗)
    593comments
  13. Let's make quality the norm again(forbrukerradet.no ↗)
    483comments
  14. Hackers Got Inside a Flock Camera(wired.com ↗)
    208comments
  15. Building a Linux GPU Driver for the M4 Mac Mini in One Month(codyho.dev ↗)
    257comments
  16. Linux from Scratch(linuxfromscratch.org ↗)
    111comments
  17. Show HN: Capsule – Single-file web apps that save their data into SQLite(withcapsule.app ↗)
    156comments
  18. Small programming tricks(will-keleher.com ↗)
    173comments
  19. Java 27(openjdk.org ↗)
    408comments
  20. Training a 4B model to produce 81% faster query plans than Postgres(rohanbansal.com ↗)
    64comments
  21. The Google Play app review process now regularly takes longer than a week(gultsch.social ↗)
    327comments
  22. We got admin access to Baseten's production GitHub(strix.ai ↗)
    184comments
  23. Israeli Minister Threatens Filmmakers' Citizenship over Gaza Documentary(reutersconnect.com ↗)
    86comments
  24. America's Driver's License Breach Is a National Security Disaster(lawfaremedia.org ↗)
    199comments
  25. PS5 Linux lead quits: "a bunch of noobs using LLMs" that "they don't understand"(frvr.com ↗)
    209comments
  26. German Rheinmetall open-sources its Battlesuite connected weapon system protcol(rheinmetall.github.io ↗)
    104comments
  27. Alternatives to MinIO for single-node local S3(rmoff.net ↗)
    106comments
  28. CSS-Tricks in Limbo(vale.rocks ↗)
    132comments
  29. Tell the speakers that you liked their talks(ohhelloana.blog ↗)
    74comments
  30. Salesforce Global Outage(salesforce.com ↗)
    167comments

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

332 pointsby 4h agorohanbansal.com
63 comments
4h agoHN ↗

But how will you know that the query plan actually does what your query asked for?

4h agoHN ↗

I imagine you can perform operations on query plans to transform them and determine equivalence?

3h agoHN ↗

`pg_hint_plan` has a debug log so you can verify Postgres actually used the hint or not! Used this during evaluations

3h agoHN ↗

I would like to think that pg_hint_plan is designed in such a way that any hint it accepts must be a valid plan for the query. I’m quite confident that schemes with this property that can also express high quality plans are possible and not even excessively complicated.

This is not to say that it’s possible to genetically verify that a proposed algorithm does what you want it to — that would be undecidable or NP-hard or co-NP-hard depending on how you formulate the question.

3h agoHN ↗

I wouldn't be very excited about adding a 4B param model to my database deployment, but using this kind of approach while testing an app to identify query plans where Postgres is leaving performance on the table seems valuable without much risk.

2h agoHN ↗

Given the approach from the article, you can commit the hints to git and run tests for verification. The model would be used during coding.

2h agoHN ↗

Because P!=NP (very probably IMHO) there's a huge class of problem for which LLMs are useful on the expensive and heuristic-y generation side because the verification is relatively inexpensive.

2h agoHN ↗

you'll have to prove equivalence through some Lean4 code perhaps? or some weird clause tree comparisons... good question indeed.

54m agoHN ↗

You're misunderstanding the setup here. The LLM doesn't modify the query, just some details about how to choose between different ways to break the query into basic operations on the tables. The SQL doesn't change. It's still up to postgres to guarantee that the results match the query. If the proposed plan were nonsense that didn't amount to carrying out the query, postgres would ignore it.

3h agoHN ↗

Aren't optimizations suppose to be deterministic?

3h agoHN ↗

They are not. Choice among several query plans depends on various summary statistics about the data, which might not be the most recent.

3h agoHN ↗

Including the input parameters.

It's not unusual for us to end up with bad query plans because the shape of our data can vary pretty greatly. In many cases, a Foo has 1 Bar. But in some cases, a Foo has a million Bars. That can cause the query optimizer to treat lookups on the bar table as if there are few elements there (causing a scan instead of a seek).

For the general case, the optimizer gets it right. However, the fringe case is one that causes the entire system to crash. It's a bit akin to how an insertion sort can be faster than quick sort when n is small. The optimizer might make a bad assumption about the size of n which makes it pick an expensive n lookup when log(n) is available (but slower for small n).

3h agoHN ↗

I'd like to contribute my amateur hour entry into this thread, although I did administer and develop mssql stuff for awhile.

sure optimizations based on stats, but the stats are the wildcard, in my experience query plans can change suddenly.

Queries are translated into plans according to statistics. However the transforms will be deterministic and should only change one valid plan to another. I could very easily see a neural network manipulate transforms the same way the current programming does, its just that the neural networks are by nature really nicely suitable because the "decisions" are based on training, and this training can be closed world type things like the ai assists that chess engines are now getting. Obviously ai still can't play chess but apparently its very good at ranking board positions just by developing that much statistical info because its training comes not from reading the web, but playing a gazzilian games against itself in a "closed" chess world of its own.

I'm thinking that the ai does "this legal transform of the query plan should be applied to this pattern of data (statistics, cardinality, etc)" simply because the ai encountered it in closed world training, much like the chess thing.

Just a theory tho feel free to correct!

2h agoHN ↗

Obviously ai still can't play chess

I believe you are wrong on that. Do you mean large language models can’t play chess?

2h agoHN ↗

I don't believe that a pure neural network can currently abide by the rules of chess, even with unreasonable amounts of training. Do you have a counter example? I never mind having beliefs challenged with facts lol

edit: I think you could provide an AI with a service or skill that asks "is this move legal" but given all the overhead for llms or whatever to call a "legal move" service external to its process, well then you aren't really searching the tree very efficiently lol.

However if you just let a neural network score boards and the neural network is in the same process well then I think thats the working solution for using neural networks in chess. The net does not need to score all boards either, simple value based heuristics can obviously provide a preliminary list of good boards (moves) at a certain depth or ply and then select the move that produces the board that the neural net scores highest. I kinda sorta think thats whats done today but as usual I could be full of it lol

3h agoHN ↗

I paid ~$800 to rent a 2x H100 SXM node from Lambda for ~95 hours, and ~$400 in OpenAI API fees to generate the Astra trajectory demonstrations.

a tiny 4B model went from not being able to understand the harness it was wrapped in, to achieving a 1.81x geometric mean speedup and a summed latency decrease of 44.7% across a workload of join-heavy SQL queries

I can’t find it in the article (may have skimmed it too much), but I suspect they didn’t include those ~95 hours in the benchmark numbers.

I think all database vendors know their query optimizers could do much better if they could afford to spend lots of time to derive query plans.

⇒ this may be useful for some workloads, but even then, can you afford to spend hours every now and then to update your 4B model to ensure it still picks a good query plan?

3h agoHN ↗

⇒ this may be useful for some workloads, but even then, can you afford to spend hours every now and then to update your 4B model to ensure it still picks a good query plan?

I think this would be likely comparable to a scheduled backup, so I think it would be an acceptable maintenance window. However, deterministic algorithms would likely beat re-training (or re-fine-tuning) the model. For example, one could analyze actual distributions or whatever (instead of assuming uniform), and then some plans would automatically be eliminated.

Imo a good thought experiment is to look at places that are hyper-optimized, like compilers. Would LLMs bring anything to the table (architecturally or performance-wise) to a piece of software that has been carefully crafted for decades? (Methinks no.)

2h agoHN ↗

The Postgres query planner has had to operate, for those same decades, in a much more realtime-sensitive and restricted environment than compilers. It can only draw its conclusions from summary statistics on tables in isolation, not on their relationships with each other (and even less so when filters are involved). For many cases this is fine! For many others it isn't.

40m agoHN ↗

There's a good number of heuristic choices in compilation where, maybe, you could get more optimal outcomes with machine learning - but at the cost of compilation resources, both time and space, and possibly determinism too.

As an example, register allocation is graph colouring, and thus NP complete; a model for producing an allocation plan is learning heuristics that might look at more features in combination than the ones hand-crafted into the compiler. An LLM for the job might do better than a more focused model like a GNN, due to sheer size, the effectiveness of transformers, or magic. But it probably won't do an overall better job than the handcrafted heuristics, because those handcrafted heuristics also tend to compile very, very fast with a small memory footprint, and can be debugged (more) easily when they go wrong.

1h agoHN ↗

I think the idea would be making a frontier model that does this. One that is trained on multiple queries

49m agoHN ↗

Could you stitch a specific query plan to your view?

‘create plan llm_optimized …’

‘create view foo (select x, y, z from table bar) with plan llm_optimized’

3h agoHN ↗

Frontier intelligence is extremely powerful; the distillation I did off Astra trajectories is proof enough that large models are not going anywhere

Wouldn't admitting this invite trouble due to accusations of distillation flying around between closed and open models.

3h agoHN ↗

I don't think anybody's denying that small models are doing distillation, the issue at hand is frontier lab vs frontier lab when it comes to big models.

2h agoHN ↗

Frontier model trainers stole almost all the data they've trained on (the whole internet, all copyrighted).

It's very hard for them to claim the moral high ground here.

It's like stealing an apple from the British Colonial Empire.

2h agoHN ↗

Having the moral high ground matters less than having a big warchest of money to spend on lawyers.

2h agoHN ↗

Does it matter if they companies doing are not in the jurisdiction or even if they are, maybe the can't prove it?

2h agoHN ↗

They can't prove it but can force you to spend time and money to disprove it.

50m agoHN ↗

The only thing they're going to try to do is ban Chinese models, and that's not going to fly outside the US, so Americans would be the only losers.

2h agoHN ↗

You'd be shocked at some of the punishments handed out from various empires over history for offences as minor as stealing an apple. Whether they can claim the moral high ground is not question of ethics but a question of power.

2h agoHN ↗

On the flip side, its important to know just how much we would be sacrificing if big frontier gets their way in convincing the courts that distillation is a bad thing

2h agoHN ↗

why is this write-up so long?

Need 5 days just to go through it.

2h agoHN ↗

They forgot to run a (same) model to optimize article for reading speed

2h agoHN ↗

Think of it like a paper. You wouldn't ask why a paper was so long.

Also you can now ask AI to summarise it for you and even probe with questions pertaining to your specific interests.

2h agoHN ↗

Why wouldn't you ask that? That's exactly why papers have supplementary material

2h agoHN ↗

Funny enough I was thinking about something very similar to this based on the Jev model posted yesterday.

2h agoHN ↗

I’ve played with it already. I don’t think this is the use case. I think Jev’s use case is fast, cheap and somewhat easy classification. It’s not trainable in the way you would want here. Even though it’s fast it wont be faster than pgs query optimizer.

At least as I understand things.

How did you plan to use Jev for query optimization?

1h agoHN ↗

I am still struggling to understand a use-case for Jev. Isn't what was explained in this article a classification problem? I.e. find and aggregate data?

1h agoHN ↗

The number of options has to be small and bounded. The query planning is more of a search/optimization problem than a classification problem since the number of options increases wildly based on query size.

2h agoHN ↗

“81% faster query plans than Postgres”…on an 8 GB dataset that fits entirely in memory, with shared_buffers constrained to a fraction of that, queries warmed before measuring, and read-only SELECTs.

I would be cautious about over fitting, it’s tough to say if those query plans would really be more optimal than Postgres heuristics at scale and with a bit more realistic OLTP workloads.

In any case, such is life with profile guided optimization. Many of us appreciate how database workloads can drift over time and with scale.

Kudos to the author for getting their hands dirty and writing up their experiments.

1h agoHN ↗

With a 4B parameter model that probably ran through 8GBs of RAM multiple times to run.

At a certain point we should seriously talk about CUDA accelerating Postgres instead.

1h agoHN ↗

I would think it's possible to make it so that the 4B model only needs to be called during an initial phase, and then the same queries it constructed can just be re-used with values replaced, unless you're generating a lot of unique on-the-fly query shapes.

8m agoHN ↗

pardon but aren't disks usually the bottleneck? im all for CUDA acceleration and CUDA accelerating culture

2h agoHN ↗

Optimal plan construction is math-heavy, algorithm-heavy and vary even by workload. There are options like creating just-in-time indexes, so solution space grows even faster than article presents. Sometimes it is the query planner which is the slow part of total execution time.

LLM is kind of blunt weapon to use here. I am waiting rather for alphago style neural net heuristic.

1h agoHN ↗

What if we use a hybrid model of using both query optimizer and LLM? Whichever produces better result, the database can use?

- a question from someone with lack of DB depth, me.

1h agoHN ↗

The immediate problem: How do you know which one is better without running them?

1h agoHN ↗

Not someone familiar with DB internals, but could you run in parallel, at least for reads?

1h agoHN ↗

Only in the worst case when the plans are equivalent: If one plan is significantly faster, then it'll finish first, and the loser can get canceled before it finishes.

1h agoHN ↗

Good and bad plans can have orders of magnitude performance difference. The bad one can easily do enough damage cutting the performance in half before it is canceled.

1h agoHN ↗

Could you A/B at random, use that to collect data and eventually feed that back in to prefer A or B depending on the shape of the query?

42m agoHN ↗

I also wondered why an LLM would be the right starting point. Why would Balzac or billions of lines of rwir code or reddit be relevant to mapping this smallish, well-defined language (SQL) to this other tiny constrained specification language (the query plan suggestions)? You could make a (relatively) tiny network and then actually pass it some relevant features of the actual data, like as numbers, not just as text returned from a tool call.

40m agoHN ↗

It’s ultimately based on a lot of hand-written heuristics. Google has some non-LLM based machine learning technique to guide optimization heuristics in LLVM; that would be closer to what you are looking for.

1h agoHN ↗

Engineer: "HELP, our production DB is frozen on this query that worked fine before!"

Infra: "Hmm, let's check... Well would you look at that, it seems like your LLM query planner usually works and produces fast queries, but this time when you changed a variable name to trigger query rebuild, it happened to hallucinate and miss an index, would you mind re-running the LLM a few times until you get a faster query?"

42m agoHN ↗

Funnily enough, you could replace "LLM query planner" with just "query planner" and this comment would still hold true

9m agoHN ↗

Our database 12.34 was working great, but 12.35 deployment had some optimizer changed that had regression on exact scenario that you have in your statistics. Shit happens, sorry. Use this hint.

1h agoHN ↗

TL:DR; for people. Index your data properly.

52m agoHN ↗

This was a thoroughly enjoyable read, both the writing and presentation. I really liked the level of writing as it's basically introducing a whole lot of advanced topics but at just the right level for a non-AI researcher type of engineer like myself to be able to understand what's going on, and I felt it made some elements of LLMs actually something I could understand rather than wizardry done by maths PhDs. Probably because it's more like applied engineering rather than hard mathematics here. Thank you for a delightful post.