---
title: "Replacing Protobuf with Rust to go 5 times faster"
description: "PgDog forked pg_query.rs and replaced libpg_query's Protobuf serialization layer with direct bindgen-generated C-to-Rust conversions, making SQL parsing 5.45x faster and deparsing 9.64x faster, with a 25% gain in pgbench throughput."
url: "https://swe.quest/articles/replacing-protobuf-with-rust-to-go-5-times-faster-080cb277a22d2431"
canonical: "https://pgdog.dev/blog/replace-protobuf-with-rust"
source: "PgDog"
published: "2026-01-22"
difficulty: "medium"
kind: "Case study"
reading_minutes: 6
tags: ["performance","rust","postgres","ai-coding"]
---

# Replacing Protobuf with Rust to go 5 times faster

A post on [PgDog](https://swe.quest/sources/pgdog.md), published 2026-01-22, 6 min read, Case study. Original: [https://pgdog.dev/blog/replace-protobuf-with-rust](https://pgdog.dev/blog/replace-protobuf-with-rust)

## Summary

PgDog forked pg\_query.rs and replaced libpg\_query's Protobuf serialization layer with direct bindgen-generated C-to-Rust conversions, making SQL parsing 5.45x faster and deparsing 9.64x faster, with a 25% gain in pgbench throughput. Profiling with samply showed the Protobuf entrypoint dominated CPU time while the Postgres parser itself barely registered, and an LRU AST cache keyed on query text broke down with ORM-generated unique statements and drivers lacking prepared statements. The roughly 6,000-line recursive converter was generated by Claude and checked automatically by comparing its output byte-for-byte against the existing Protobuf-based parse via PartialEq. A recursive per-node walk beat an iterative version, which lost to Prost because of extra allocations, hashmap lookups and repeated tree passes.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Rust](https://swe.quest/tags/rust.md) (Languages)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [AI for coding](https://swe.quest/tags/ai-coding.md) (AI & ML)

## Prerequisites

- Rust unsafe and FFI basics ([easy posts on Rust](https://swe.quest/tags/rust.md?difficulty=easy))
- Protobuf serialization ([easy posts on RPC & gRPC](https://swe.quest/tags/rpc.md?difficulty=easy))
- abstract syntax trees

## Keywords

PgDog, libpg\_query, pg\_query.rs, Protobuf, bindgen, Prost, samply, Claude Code

## Related posts

- [Postgres Language Server: implementing the Parser](https://swe.quest/articles/postgres-language-server-implementing-the-parser-3a3e33745c9c0915.md): Supabase · 2023-12-08 · Hard · 14 min read · PostgreSQL, Compilers, Rust, Developer tools, SQL
  A Postgres language server's parser builds on libpg\_query rather than a custom grammar, and has to work around two gaps in it.
- [We're building Postgres in Rust. Using the LLVM of databases](https://swe.quest/articles/we-re-building-postgres-in-rust-using-the-llvm-of-databases-e44d34208278a99e.md): Turso · 2026-07-16 · Easy · 11 min read · PostgreSQL, SQLite, Rust, Query engines, Open source
  Turso, a Rust rewrite of SQLite, is adding a Postgres frontend by parsing Postgres SQL into a common AST and compiling it to Turso's VDBE bytecode, turning the engine into an "LLVM of databases" with pluggable frontends.
- [pg\_graphql v1.0](https://swe.quest/articles/pg-graphql-v1-0-5a4770d60b17d945.md): Supabase · 2022-12-16 · Medium · 5 min read · GraphQL, PostgreSQL, Rust, Performance engineering
  pg\_graphql, a PostgreSQL extension that compiles GraphQL requests into a single SQL query, reaches 1.0 with a SemVer backwards-compatibility guarantee.
- [Building a faster, lazier router for Postgres](https://swe.quest/articles/building-a-faster-lazier-router-for-postgres-bc9a70ce219128ed.md): PlanetScale · 2026-10-01 · Hard · 15 min read · PostgreSQL, Distributed databases, Performance engineering, Go, Query engines
  Neki, PlanetScale's Postgres sharding router, cuts per-query overhead by keeping PostgreSQL wire-protocol bytes intact and decoding only as much as each query requires.
- [Postgres Language Server: Initial Release](https://swe.quest/articles/postgres-language-server-initial-release-4b7148c84b3bcff0.md): Supabase · 2025-03-29 · Medium · 13 min read · PostgreSQL, Developer tools, Rust, SQL, Static analysis
  Postgres Language Server is an LSP implementation for Postgres offering autocompletion, syntax error highlighting, type-checking via PREPARE/EXPLAIN against a live database, and a Squawk-inspired linter, available for VSCode, Neovim and as a CLI.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
