Technology1 min read

Programming at Light Speed: Building the Gravity Query Engine

By · Published by Everything Blog

In short

Gravity achieves its substantial performance by compiling queries down to raw assembly through a multi-stage compilation process. To find the true ceiling of their hardware, developers benchmarked heavily optimized, Claude-written Rust programs using SIMD against raw columnar storage. Gravity's compiled machine code now runs within 20% of those handwritten baseline tests. Furthermore, the engine retains complete compatibility with standard Postgres query plans, parsers, and native data types.

Key points

  • The new engine operates within twenty percent of the speed achieved by heavily optimized…: The new engine operates within twenty percent of the speed achieved by heavily optimized handwritten Rust baselines.
  • Gravity transforms native Postgres query plans into raw assembly using an intermediate pi…: Gravity transforms native Postgres query plans into raw assembly using an intermediate pipeline graph and a Lisp representation.
  • Full compatibility with the existing Postgres parser, planner, and SQL dialect is preserv…: Full compatibility with the existing Postgres parser, planner, and SQL dialect is preserved without sacrificing low-level execution speed.

Sources checked by our analysis pipeline: 1

With the release of pgrust v0.4-beta, our new query engine, Gravity, is now as much as 3x faster than ClickHouse on ClickBench, the benchmark that ClickHouse themselves designed to show off how fast ClickHouse is. Even better, Gravity achieves that speed while supporting the full Postgres SQL dialect and type system.

In a couple of previous posts, I wrote about how we used batching, operator fusion, and SIMD to make pgrust hundreds of times faster than Postgres for analytical queries, and how we built a copy-and-patch JIT compiler that compiles stencils in 5 microseconds. All of that work, built up over several releases, is what became Gravity.

Copy-and-patch was a great starting point, but gluing pre-compiled templates together only gets us so far. Because we did not have a real intermediate representation or register allocation across the whole loop, we ended up leaving a lot of performance on the table compared to an optimizing compiler like rustc or Clang. For pgrust v0.4, we wanted to go all the way and build a query compiler that generates optimal machine code for…

Technology