I rewrote PostHog's SQL parser, 70x faster, while barely looking at the code
After the success of using agents to improve query performance through autoresearch , I wanted to try something more ambitious.
I rewrote PostHog's SQL parser using multiple long-running Claude Code sessions in parallel. The result was 16K lines of "hand"-rolled parser code, 5K lines of tooling, a few more K of tests, and a ~70x speed up.
The new parser is equivalent to the previous one for all realistic queries, only differing for a tiny subset of queries written by an evil trickster deity (thereβs a test for SELECT SELECT FROM FROM WHERE WHERE AND AND which is completely valid SQL).
Here's how I did it and what I learned along the way.
Why does PostHog even have an SQL parser?
PostHog lets you access your data directly with SQL . We transpile your SQL to raw ClickHouse SQL because: