Query Timeout
Error message
canceling statement due to statement timeout
Quick summary
The query exceeded the configured statement timeout — it is taking too long to execute.
Why this happens
The query is not using an available index (full table scan)
The query joins large tables without proper indexes
The statement_timeout is set too low for the query
The database is under heavy load, slowing all queries
Minimal example
-- Slow query without an index
SELECT * FROM orders WHERE customer_email = 'ada@example.com';
-- Full table scan on millions of rows → timeout-- Add an index, then the query is fast
CREATE INDEX idx_orders_email ON orders(customer_email);
SELECT * FROM orders WHERE customer_email = 'ada@example.com';Adding an index on the filtered column turns a full table scan into an instant lookup.
How to diagnose
Run EXPLAIN ANALYZE on the query to see the execution plan
Check if the query is using indexes or doing full table scans
Check the statement_timeout setting
Check the database load and active queries
How to fix
Add indexes on columns used in WHERE, JOIN, and ORDER BY
Rewrite the query to be more efficient
Increase the statement_timeout if the query legitimately needs more time
Run the query during off-peak hours or batch the work
How to prevent
Index columns used frequently in WHERE and JOIN clauses
Use EXPLAIN to verify query plans before deploying
Set appropriate statement timeouts for different query types
Monitor slow queries and optimize them proactively
Related Resources
Related Lessons
- Query Optimization
Learn to optimize slow queries.