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.
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.
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.
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.
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL