← Retour au blog
tech 25 June 2026

I Rewrote PostHog's SQL Parser, 70x Faster, While Barely Looking at the Code

Explore how an intelligent rewrite of PostHog's SQL parser achieved a 70x speed increase, while significantly reducing development time using advanced tools and AI sessions.

Article inspired by the original source
I rewrote PostHog's SQL parser, 70x faster, while barely looking at the code ↗ posthog.com

Introduction

In the realm of modern technology, optimizing every component of your IT infrastructure can make a huge difference. PostHog, a product analytics platform, understood this by rewriting its SQL parser to be 70 times faster. But what's even more impressive is that this feat was accomplished without spending hours scrutinizing the code line by line.

Why an SQL Parser at PostHog?

PostHog allows its users to directly access their data via SQL. For this, it is crucial to transpile the SQL into raw ClickHouse queries. This helps present a logical view of the data, independent of the physical database structure. In other words, the SQL parser plays a fundamental role in converting SQL queries into an Abstract Syntax Tree (AST), which is then used for performance optimizations and access controls.

Generating a Parser with ANTLR

To avoid the complexity and time-consuming maintenance of a manually written parser, PostHog chose ANTLR, an open-source parser generator. ANTLR allows you to declare grammar in a .g4 file, automatically generating most of the parser code. While powerful, ANTLR is flexible but can be heavy, thus requiring additional optimizations.

Leveraging AI Sessions for the Rewrite

Robbie Coomber, an engineer at PostHog, used parallel sessions of Claude Code, an AI tool, to rewrite the parser. The result? 16,000 lines of parser code and about 5,000 additional lines of tooling. The speed gain reached a factor of 70, while maintaining functional equivalence for all realistic queries.

Property-Based Testing

One of the innovative approaches employed was the use of property-based testing. These tests ensure that the parser works correctly for a wide range of scenarios, thus increasing the system's robustness and limiting potential errors.

Impact and Future Prospects

The new parser not only improved performance but also opened up prospects for future optimizations. With this solid foundation, PostHog can consider continuous improvements and respond to more complex demands.

Conclusion

Rewriting a key component like an SQL parser is a challenge, but with the right tools and an innovative approach, the results can be spectacular. If you want to explore how to optimize your infrastructure using AI and other advanced technologies, let's discuss your project in 15 minutes.

SQL parser PostHog ANTLR AI performance optimization
Deepthix newsletter · 100% AI · every Monday 8am

An AI agent reads tech for you.

Our AI agent scans ~200 sources per week and ships the best articles to your inbox Monday 8am. Free. One click to unsubscribe.

Visit the newsletter page →

Want to automate your operations?

Let's talk about your project in 15 minutes.

Book a call