Scaling Postgres artwork

Scaling Postgres · Creston Jamison

pgrust Faster Than Postgres? | Scaling Postgres 426

In this episode of Scaling Postgres, we discuss the progress of the Rust rewrite of Postgres called pgrust, more about the backup tool pg_hardstorage, beware listen / notify and how to fix bad SQL. To get the show notes as well as get notified of new episodes, visit: https://www.scalingpostgres.com/episodes/426-pgrust-faster-than-postgres/ Want to learn more about Postgres performance? Join my FREE training called Postgres Performance Demystified here: https://www.scalingpostgres.com/courses/postgres-performance-demystified/

Jul 192026
15 minlength
25transcript lines
Publishertranscript source

Transcript first 15 of 25 lines

0:00I had heard about pgrust maybe a couple of months ago and I heard someone was rewriting Postgres in Rust and I probably had a little bit of an eye roll with regards to that and I thought it was maybe just a developer being a developer and everything is better in Rust or whatever language you happen to like at the time. But because I'm talking about this as the lead to the show, it might be something to start to pay attention to because it just crossed passing 100% of the Postgres regression tests and you can start it up on your existing Postgres directory.

0:36So we'll talk about that this week, but I hope you, your friends, family and co workers continue to do well. Our first piece of content is the Four Horsemen behind thousands of Postgres outages. This is from malicebird Me and and of course this is a clickbaity title, but these are some of the things he sees and suggests he wants to change or fix in pgrust. And again, this is a Rust rewrite of Postgres. So the first thing he addresses is vacuum and transaction ID wraparound. And transaction ID wrap around is a definite concern as your database grows because we're still using 32 bit transaction IDs and I can only assume he's going to be using 64 bit and his intent is to change how rollbacks happen and use an undo log like Oracle does, probably to avoid having to vacuum the heap. But both of these are goals, they are not present yet, presumably because he has to get the regression tests passing. So he's just trying to match Postgres at this point. These are just future looking things as far as I understand it.

1:45The next is connection limits and query parallelism and he's basically knocking the Postgres process model, although he makes some of these statements that I'm like that doesn't happen because he says quote if more things try to connect to Postgres than the connection limit, they won't be able to. That's true, and your database will go down. I've never seen a database go down due to too many connections. The database stops additional people from connecting, but basically he's going to be using a thread based model and he says this was architected from day one with it. And he says even though the process model is safer, Rust provides safety guarantees for these threads, so it should be fine. The next item he calls out is bad query plans. Now again with pgplannadvice this is not as relevant too much anymore, but again this is all Forward facing. That he wants to do with PGrust is he wants to build an adaptive query planner and that if a Query normally takes 10 milliseconds, suddenly takes 10 seconds, PGRUST would detect the regression, check if the plan changed, or check if the statistics drifted and make a correction. Although in theory you could make this enhancement to Postgres, today it should be tracking sufficient information to detect plan flips. I'm not saying it would be easy, but I could see how that feature could be added. Next he talks about JSON and the fact it does not collect statistics, and that's something he wants to do. But again, Postgres could start tracking some sort of statistics for JSON as well. So this gives you a sense of what he's going for. The next post is PGRust passes 100% of the Postgres regression tests, and that's with Postgres 18.3. And it's disk compatible with Postgres, so you can boot from an existing Postgres data directory, but of course it's not production ready yet, not optimized, not compatible with existing Postgres extensions, but still, this is a significant milestone nonetheless. And then the next blog post, how PGRust was built 4 attempts to rewrite Postgres and Rust with AI now, this is a much longer blog post, but it talks about his technique for using AI to do it. And in terms of the TLDR, after four attempts and dropping $100,000, we succeeded in using Opus to rewrite Postgres and Rust.

4:08So they had four iterations to try to get it and spend $100,000 at the end. But you'll see they've actually added another hundred thousand dollars near the end of this blog post. Now, of course, the issue with using AI for building these is are you going to, I guess, quote unquote, paint yourself into a corner? Is it going to have too much bloat or slop or whatever you want to call the code to be able to move forward? And it's interesting that he says for this first attempt it got completion, but ended up having fundamental issues that made it unsalvageable, which philosophically any software can be altered and improved. So I think it's a pretty significant statement to call it unsalvageable. And they said this attempt was mostly port one feature at a time, and it goes into details about that.

4:57The second attempt was to basically transpile the Postgres code from C to Rust, so basically matching it up line by line with the Postgres code. But the problem with doing that, it basically generated 5.3 million lines of unsafe rust code. Now, what's interesting about this attempt, it completely passed the Postgres test suite and passed 100% of the regression suite and had similar performance to Postgres, but it was even ABI compatible with Postgres, which means the extensions would work as well.

5:29The problem was when they tried to convert the code into idiomatic rust.

5:34That's where things fell apart.

5:37And it was taking hours to do this type of conversion. So the idea for the next attempt was to rewrite the generated C2 rust code crate by crate, because there were approximately a thousand rust crates that was initially output. And he goes through over the skills and some of what he prompted to try and do that, but he had a lot of problems with token usage. And he makes an interesting comment here. Now, I may be a little crazy or maybe very crazy, but I thought if I could complete PGrust for under $100,000, it would be worth it. Now, the problem is in C, the circular dependencies looks to be what might have killed him with this, because rust crates forbid the creation of circular dependencies. And then going back to one of the previous iterations being unsalvageable, he here he says, quote, when Fable came out, he said, all right, let me try to get Fable to resolve this. And he says, even Fable couldn't recover this version that he's calling PGRUST Idiomatic.

6:37Is there any way to save PGRUST Idiomatic? Does it just make sense to start over again? And he had spent a lot on these Claude Dynamic workflows, up to $50,000 at this point. So the fourth attempt, he called PGRUST Fabled, but the spoiler was it was not built by Fable because Fable was released. And then Anthropic did a rug pull, and you could only have Opus. So that's what he did. I kept going with Opus, and he talks about his different skills and how he approached this a little bit differently. And he prioritized implementing seams between the crates and updated his audit skill to give it more things to look for.

7:18So he regularly ran this audit skill and was consistently making sure all the seams were wired.

7:23But ultimately he got this version. PGRUST Fabled, although written in Opus, was able to pass 100% of the Postgres regression suite and using idiomatic rust at the cost of another $50,000. So $100,000 in total.

7:39Now, then he says, okay, what's next? He says quote, the big issue with pgrust fabled is it's really slow, eight times slower than Postgres.

7:49So they're doing the next version presumably based on this code called PGRUST Fast. And this is crazy to me. So far they've put in $200,000 into PGRUST fast on top of the hundred thousand dollars they put into Rust Idiomatic. So it's using a new Vectorize executor, a new JIT compiler that cuts the latency of jit compilation from 50 milliseconds to 5 microseconds, and a scheduler that dynamically adjusts the prioritization of queries. Now this is still an internal test, but he says this altogether the optimizations we've made have made PGrust fast, over 50% faster than Postgres on transactional workloads and about as fast as Clickhouse for analytical ones.

8:37If this is true, that's pretty amazing and they plan on releasing this within the next two weeks.

8:44And this blog post was released today as I'm recording this, so it definitely looks like something to keep an eye on. But what do you think? Feel free to let me know in the comments.

More Scaling Postgres episodes →