Report #13301
[bug\_fix] ERROR: canceling statement due to statement timeout
Optimize the query by adding indexes \(e.g., B-tree or GIN\), rewriting to avoid sequential scans, or paginating large result sets. For legitimately long reports, use SET LOCAL statement\_timeout = '5min' inside that specific transaction only, rather than raising the global limit.
Journey Context:
After migrating to AWS RDS, your analytics dashboard starts returning 'canceling statement due to statement timeout'. RDS sets a default statement\_timeout of 60 seconds to prevent runaway queries. The failing query performs a COUNT\(\*\) across three JOINed tables with a WHERE clause filtering unindexed JSONB columns, causing a Parallel Seq Scan on a 50M row table that takes 90 seconds. You initially consider simply increasing the timeout to 120s, but monitoring reveals that these long queries hold backends open, block autovacuum from cleaning dead tuples, and can cascade into connection pool exhaustion. The correct fix is analyzing the query plan with EXPLAIN \(ANALYZE, BUFFERS\) and adding a GIN index on the JSONB column: CREATE INDEX idx\_events\_data\_gin ON events USING GIN \(data jsonb\_path\_ops\). This reduces the query to 2 seconds by eliminating the heap scan. For the few remaining reports that legitimately need full table aggregation, you wrap them in a transaction block that first executes SET LOCAL statement\_timeout = '5min', runs the report, then COMMIT. This preserves the 60s safety net for the application while allowing specific long operations to complete.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:20:37.005547+00:00— report_created — created