We built a wrapper on top of the Postgres native SQL parser to allow us to parse and manipulate SQL in Rust.
Our library forks libpg_query [1] and its original Rust bindings, pg_query.rs [2]. By not using Protobuf and mapping our structs directly on top of the Postgres arena allocator, we are able to achieve a pretty massive speedup:
- 20-60x parse/deparse, depending on query size
- 90% memory utilization decrease vs pg_query.rs
This library is used in production in PgDog [3], but has no dependencies to it, so you can use it in your Rust apps without issues.
It also provides access to Postgres internals used to walk and transform query ASTs. Those are actually really cool. Check out the README for details, but we basically use a bunch of Rust macros to generate 100% compatible Rust structs directly from C headers.
This makes it really easy to upgrade major versions, i.e., we just bump the submodules and re-compile.
Let us know what you think!
- PgDog team
[1]: https://github.com/pganalyze/libpg_query