How to fix Query canceled in PostgreSQL
A query was canceled due to timeout, user request, or resource limits. Check statement_timeout settings and identify long-running queries to either optimize them or adjust timeout values.
Quick fix
Fixes Query canceled
SHOW statement_timeout;- Check current statement_timeout value
- Identify which query is timing out
- Optimize the slow query
- Increase statement_timeout if appropriate
SHOW statement_timeout;Query canceled
What this error means
The "Query canceled" error (error code 57014) occurs when PostgreSQL terminates an in-flight SQL statement. This commonly happens when a query exceeds the statement_timeout parameter, which defines the maximum execution time allowed for a single query. The cancellation can also be triggered manually by an administrator using pg_cancel_backend(), or by system resource constraints. Unlike a connection termination, query cancellation leaves the session intact so the user can retry or run other queries.
Typical symptoms
- Queries fail intermittently with "canceling statement" error message
- Certain long-running reports or batch operations fail unexpectedly
- Application timeout errors when executing specific queries
- Database logs show "statement timeout" entries
- Operations that succeed in development fail in production (different timeout settings)
Common causes
- statement_timeout parameter is set too low for query complexity
- Query lacks proper indexes, causing full table scans
- Database server is under high load with resource contention
- Slow network connection delays query execution
- Explicit cancellation via pg_cancel_backend() by administrator
- Transaction holding locks blocks query execution until timeout
- PostgreSQL 17+ transaction_timeout applies to entire transaction
How to fix "Query canceled"
Connect to the database and check the current timeout setting:
SHOW statement_timeout;The value is in milliseconds. A value of 0 means no timeout. Typical settings are 30000 (30 seconds) or 60000 (60 seconds). Note your current setting as you may need to adjust it.
Enable query logging to see which statement causes the timeout. Add to postgresql.conf or set at session level:
SET log_min_duration_statement = 0; -- Log all queries
SET log_statement = 'all'; -- Log all SQLAlternatively, query the slow query log or check application error logs for the SQL text that failed. Use EXPLAIN ANALYZE on the problematic query to understand execution time.
Use EXPLAIN ANALYZE to examine the query plan:
EXPLAIN ANALYZE SELECT * FROM large_table WHERE condition;Look for sequential scans where indexes should be used. Add indexes to frequently searched columns:
CREATE INDEX idx_column_name ON table_name(column_name);Rewrite queries to filter early, use JOINs instead of subqueries, and avoid full table scans on large tables.
If the query legitimately requires more time, increase the timeout. Set at database level for all new connections:
ALTER DATABASE mydatabase SET statement_timeout = '120s';Or set per session before running the query:
SET statement_timeout = 120000; -- 120 seconds in milliseconds
SELECT * FROM slow_query;For application code, set timeout per connection in your driver/ORM configuration.
If timeouts occur while waiting for locks from other transactions, set lock_timeout separately:
SET lock_timeout = '5s';
SET statement_timeout = '60s';This fails fast if a lock cannot be acquired, rather than waiting. Investigate which transaction holds the lock and consider breaking work into smaller transactions.
After optimization, re-run the query and verify it completes within the timeout:
\timing on
SELECT ...; -- Check execution timeMonitor application logs and database metrics to confirm no new timeouts occur. For critical queries, consider using a read replica to avoid impacting production traffic.
Advanced notes
PostgreSQL 17 introduces transaction_timeout which cancels entire transactions exceeding a duration, separate from statement_timeout. Idle in transaction sessions can also cause issues; use idle_in_transaction_session_timeout to prevent them. For applications, implement client-side retry logic with exponential backoff. Consider using connection pooling (PgBouncer, pgpool) which can enforce timeouts and manage resource usage. When using pg_cancel_backend(pid), ensure you target the correct PID from pg_stat_activity. If a backend is stuck in a non-interruptible section (e.g., waiting on I/O), pg_cancel_backend() may not work; use pg_terminate_backend() as a last resort, which closes the entire connection. High-concurrency scenarios may benefit from query prioritization using PostgreSQL's workload management features or custom application logic.
Frequently asked questions
- What does "Query canceled" mean?
- The "Query canceled" error (error code 57014) occurs when PostgreSQL terminates an in-flight SQL statement. This commonly happens when a query exceeds the statement_timeout parameter, which defines the maximum execution time allowed for a single query. The cancellation can also be triggered manually by an administrator using pg_cancel_backend(), or by system resource constraints.
- How do I fix "Query canceled"?
- A query was canceled due to timeout, user request, or resource limits. Check statement_timeout settings and identify long-running queries to either optimize them or adjust timeout values.
- What causes "Query canceled"?
- statement_timeout parameter is set too low for query complexity Query lacks proper indexes, causing full table scans Database server is under high load with resource contention
- Is the "Query canceled" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐postgresql.org - Error Codes(Official Docs)
- ๐postgresql.org - statement_timeout Parameter(Official Docs)