How to fix Query canceled in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 28, 2025ยท 216 views

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;
  1. Check current statement_timeout value
  2. Identify which query is timing out
  3. Optimize the slow query
  4. Increase statement_timeout if appropriate
postgresql โ€” error
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

  1. statement_timeout parameter is set too low for query complexity
  2. Query lacks proper indexes, causing full table scans
  3. Database server is under high load with resource contention
  4. Slow network connection delays query execution
  5. Explicit cancellation via pg_cancel_backend() by administrator
  6. Transaction holding locks blocks query execution until timeout
  7. PostgreSQL 17+ transaction_timeout applies to entire transaction

How to fix "Query canceled"

1Check current statement_timeout value

Connect to the database and check the current timeout setting:

sql
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.

2Identify which query is timing out

Enable query logging to see which statement causes the timeout. Add to postgresql.conf or set at session level:

sql
SET log_min_duration_statement = 0;  -- Log all queries
SET log_statement = 'all';          -- Log all SQL

Alternatively, 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.

3Optimize the slow query

Use EXPLAIN ANALYZE to examine the query plan:

sql
EXPLAIN ANALYZE SELECT * FROM large_table WHERE condition;

Look for sequential scans where indexes should be used. Add indexes to frequently searched columns:

sql
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.

4Increase statement_timeout if appropriate

If the query legitimately requires more time, increase the timeout. Set at database level for all new connections:

sql
ALTER DATABASE mydatabase SET statement_timeout = '120s';

Or set per session before running the query:

sql
SET statement_timeout = 120000;  -- 120 seconds in milliseconds
SELECT * FROM slow_query;

For application code, set timeout per connection in your driver/ORM configuration.

5Use lock_timeout to prevent lock waits

If timeouts occur while waiting for locks from other transactions, set lock_timeout separately:

sql
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.

6Monitor and validate

After optimization, re-run the query and verify it completes within the timeout:

sql
\timing on
SELECT ...;  -- Check execution time

Monitor 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