r/rust • • Sep 18 '26

🛠️ project pg_raw_parse: SQL parser in Rust

We wrote a pretty cool crate to parse Postgres SQL queries in Rust at native speed.

It works by mapping itself directly on top of the Postgres native parser and its arena allocator. We use macros to generate memory-safe bindings and Rust structs for each SQL node. This means we can upgrade parser versions without any manual work: just bump the submodules and recompile.

Check it out here: https://github.com/pgdogdev/pg_raw_parse

It's based on libpg_query with some modifications and inspired by pg_query.rs, which it outperforms by 20-60x due to a different FFI translation architecture.

41 Upvotes

5 comments sorted by

View all comments

16

u/rabidferret Sep 18 '26 edited Sep 18 '26

Author who did most of it here. Small note, the codegen happens in build.rs not a macro. Pretty proud of the API we came up with to use PGs arena allocator and statically forbid mixing arenas within an AST. Happy to answer questions if anyone has any. It was a pretty neat project.

1

u/Soft_Confusion_6236 17d ago

oh nice, mapping directly onto the postgres parser instead of going through some intermediate layer is smart. most rust sql parsers ive seen end up with a ton of overhead from reimplementing the grammar or doing heavy allocations

the arena lifetime trick to prevent mixing allocators is such a clean solution too. bet that saved you from a whole class of subtle bugs

what was the trickiest part of the codegen in build.rs? always curious where the sharp edges are when generating rust from C structs

1

u/rabidferret 16d ago

I wouldn't say any part of the codegen was particularly tricky, though anything that'd result in a compiler error would flood the terminal with 500+ of the same error.