Fix pgAdmin Query Timeout โ Statement Cancelled
In this tutorial, you'll learn about Fix pgAdmin Query Timeout. We cover key concepts, practical examples, and best practices.
You run a heavy analytical query in pgAdmin, wait a few seconds, and get "canceling statement due to statement timeout". Your data never arrives.
Wrong โ
-- A long-running query that exceeds the default timeout
SELECT generate_series(1, 10000000) AS numbers,
md5(random()::text) AS hash;
Error in pgAdmin:
ERROR: canceling statement due to statement timeout
You try increasing statement_timeout in the session:
SET statement_timeout = '0';
-- but the tool's own timeout still fires
Right โ
Option A โ perโsession (in pgAdmin query tool):
-- Run before your heavy query
SET statement_timeout = '300s'; -- 5 minutes
-- or disable entirely
SET statement_timeout = '0';
SELECT generate_series(1, 10000000) AS numbers,
md5(random()::text) AS hash;
Option B โ pgAdmin tool preference:
- File โ Preferences โ Query Tool
- Set Statement Timeout to e.g.
300000(ms) - Tick Autoโcommit if needed
- Save
Now your query runs to completion:
numbers | hash
-----------+----------------------------------
1 | 5d41402abc4b2a76b9719d911017c592
2 | 7d793037a0760186574b0282f2f435e7
...
(10000000 rows)
Root Cause
pgAdmin sends every query with a statement_timeout (default 30โ60 s). The PostgreSQL server enforces this at the database level. Either side can kill the query prematurely.
Prevention
- Set
statement_timeoutin<a href="/databases/postgresql/">postgresql</a>.confto a generous default (e.g.5min) for reporting workloads. - Use pgAdmin's Query Tool preferences to raise the tool-level timeout globally.
- For oneโoff heavy queries, prepend
SET statement_timeout = '0'. - Monitor active queries with
SELECT * FROM pg_stat_activity WHERE state = 'active'to spot timeouts early.
Common Mistakes with query timeout
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world PGADMIN code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Learn more about query tuning in the [DodaTech PostgreSQL Performance course](https://dodatech.com/courses/postgresql-performance).
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro