The AI-Driven Rewrite: How PostHog Achieved a 70x Performance Leap in SQL Parsing

In the rapidly evolving landscape of software engineering, the "build vs. buy" debate has recently gained a new, disruptive contender: AI-assisted manual implementation. A recent engineering breakthrough at PostHog—the popular product analytics platform—demonstrates that the gap between complex, library-dependent systems and high-performance, custom-built code is shrinking. By leveraging AI agents to rewrite their core SQL parser, the team achieved a staggering 70x performance improvement, proving that complex, "hand-rolled" systems are no longer the exclusive domain of months-long manual labor.

The Technical Necessity: Why Parsers Matter

At the heart of PostHog’s data processing engine lies a critical component: the SQL parser. Because PostHog allows users to access their data directly via SQL, the platform must transpile these user-submitted queries into raw ClickHouse SQL. This transpilation process is essential for powering the company’s core features, including product analytics, session replay, and error tracking.

Before the transpilation can occur, however, the input must be converted into an Abstract Syntax Tree (AST). The parser is the "gatekeeper"—the first entity to touch untrusted user input. Everything downstream, from security-critical access controls to performance-enhancing query optimizations, relies entirely on the accuracy of the AST generated by this parser.

Historically, the difficulty of maintaining a robust, secure parser meant that manual implementation was often discarded in favor of parser generators like ANTLR. ANTLR allows developers to define a grammar file (a .g4 file) and automatically generates the necessary code. While this approach is reliable, it introduces significant overhead. Because ANTLR uses a generic, graph-walking interpreter to traverse the Augmented Transition Network (ATN), it incurs a performance penalty for every token processed. In an environment where p95 response times are critical, this layer of abstraction represents a significant bottleneck.

Chronology: A Multi-Agent Development Lifecycle

The decision to abandon the safety of ANTLR was not made lightly. The engineering team initiated a parallel development process, utilizing Claude Code sessions to architect a new, high-performance parser in Rust. The project was not a simple task of "telling the AI to code"; it was a sophisticated, iterative orchestration of human oversight and machine execution.

Phase 1: Establishing the Oracle

The team’s primary goal was functional equivalence with the original C++ ANTLR-based parser. The existing parser acted as an "oracle." If the new parser produced an AST identical to the original for any given input, it was considered correct. This provided a rigid framework for Test-Driven Development (TDD).

Phase 2: Property-Based Testing (PBT)

To ensure the new parser was robust, the team employed the Hypothesis library for property-based testing. Instead of writing individual tests, the developers defined the "property" of the system: The new parser must always agree with the original. By generating a tool to create an SQL generator based on the original ANTLR grammar, the team effectively taught the system how to "fuzz" its own logic.

Phase 3: Mitigating Brittle Fixes

Early in the process, the AI agents struggled with "brittle" coding patterns—making narrow, one-off fixes that didn’t address underlying structural issues. The breakthrough came when the developers shifted their prompt engineering strategy. By forcing the AI to load the entire grammar file and relevant reference C++ source code into its context window before every fix, the agents began to produce more coherent, architecturally sound solutions.

Phase 4: The Shadow Mode Deployment

Once the new parser reached a state of high accuracy, the team deployed it in "shadow mode." This involved running both the old and new parsers side-by-side in production. While the original parser processed live traffic, the new parser performed the same operations in the background, with the system flagging any discrepancies. After millions of successful parses with zero divergences, the team transitioned the new parser to handle production traffic.

I wrote a 70x faster SQL parser while barely looking at the code - PostHog

Supporting Data: The Performance Metrics

The results of the rewrite were, by all accounts, transformative. While the internal team estimated a 70x speedup in local benchmarks, the production impact was even more pronounced. On standard production queries, the new parser proved to be, on average, 454x faster than its predecessor.

The performance gains are attributed to the transition from a generic, graph-walking interpreter to a highly optimized, predictive recursive-descent parser. The final product features a Pratt expression core and an LL(2) cursor—a design that, in the past, would have required a specialized team of compiler engineers months to refine and debug.

Implications for the Future of Software Engineering

The success of the PostHog parser project signals a paradigm shift in how we perceive the "manual" implementation of complex systems. The term "vibe-coding"—a derisive label for AI-generated code that lacks rigor—is clearly inapplicable here. The project utilized state-of-the-art fuzzing techniques, coverage-guided test generation, and rigorous, cross-verified validation.

The Death of the "Black Box" Library?

For years, engineers have been told that writing their own parsers, crypto libraries, or complex data structures is a fool’s errand. The logic was simple: the surface area for bugs is too large, and the maintenance cost is too high. However, if an AI agent, guided by a human expert using property-based testing, can generate a 16,000-line, high-performance, and verifiable parser in a matter of days, the "buy vs. build" calculus changes.

This suggests that the next generation of software may be characterized by "bespoke, AI-generated infrastructure." Instead of relying on heavy, general-purpose libraries that do "everything for everyone," companies may choose to generate lightweight, custom-optimized modules that do one thing perfectly.

The Role of the Engineer as "Orchestrator"

The PostHog experiment highlights that the developer’s role is evolving. The engineer of the future is less of a line-by-line typist and more of an "orchestrator of truth." By defining the properties of the system, establishing the testing infrastructure (the "oracle"), and managing the context in which AI agents operate, the engineer can ensure the safety and reliability of code that they did not technically "write" in the traditional sense.

Conclusion: A New Normal

The PostHog parser rewrite, completed in May 2026 by Claude Opus 4.7, stands as a landmark case study in the power of human-AI collaboration. It demonstrates that with the right guardrails—specifically, robust PBT and a reliable reference oracle—AI can perform complex systems engineering that was previously considered impractical.

As these tools continue to mature, we are likely to see a decline in the dominance of massive, opaque dependencies in favor of lean, AI-optimized codebases. For the software engineering industry, this is not just about speed; it is about reclaiming the ability to understand, customize, and optimize the very foundations of our digital infrastructure. The era of the "hand-rolled" parser is back, and this time, it is being written by the machines.

By Asro