# Karpathy's Autoresearch found a 3-year-old bug in our query engine (and improved performance by 11%)

DevFeed: [Karpathy's Autoresearch found a 3-year-old bug in our query engine (and improved performance by 11%)](<https://devfeed.tech/articles/karpathy-s-autoresearch-found-a-3-year-old-bug-in-our-query-engine-and-improved-performance-by-11-80554.md>)

Original publisher: [Read original article](<https://posthog.com/blog/karpathy-autoresearch-query-engine-bug>)

Author: Robbie Coomber

Published: 2026-06-01T00:00:00Z

Content type: article

Language: en

Sources: [PostHog](<https://devfeed.tech/sources/posthog.md>)

Topics: [database-optimization](<https://devfeed.tech/topics/database-optimization.md>), [table-partitioning](<https://devfeed.tech/topics/table-partitioning.md>), [Query (disambiguation)](<https://devfeed.tech/topics/query.md>), [ReAct agent Python](<https://devfeed.tech/topics/react-agent-python.md>), [Production Engineering](<https://devfeed.tech/topics/production-engineering.md>)

Tags: [ai-agent](<https://devfeed.tech/tags/ai-agent.md>), [analytics](<https://devfeed.tech/tags/analytics.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [bug](<https://devfeed.tech/tags/bug.md>), [clickhouse](<https://devfeed.tech/tags/clickhouse.md>), [data-warehouse](<https://devfeed.tech/tags/data-warehouse.md>), [performance](<https://devfeed.tech/tags/performance.md>), [query-engine](<https://devfeed.tech/tags/query-engine.md>)

## AI overview

An overnight AI investigation uncovered a long-standing ClickHouse query-planning issue: wrapping `timestamp` in `toTimeZone()` prevented partition pruning and stopped primary-key use after `team_id` and `event`. Rewriting the comparison with a bare `timestamp` and timezone-adjusted constant preserved semantics and improved performance; the benchmark reported 62% fewer granules scanned, with the largest speedups on short date ranges. The setup used `pi`, `pi-autoresearch`, structured campaigns with lanes and explicit experiment reviews, and an anonymized throwaway ClickHouse cluster. Slow queries that timed out were narrowed to a 1-10 second range for iteration, then candidates were retested at full range. PostHog says it is building an automated pipeline to find slow queries, run sandboxed investigations, deduplicate suggestions, and prepare PRs for human review; those broader results are not yet reported.

## Source excerpt

A few weeks ago at a team offsite in Lisbon, we pointed an AI agent at our query engine, fed it slow queries from production, and let it run overnight...