PostgreSQL error 57014 (Query Canceled) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Distinguish timeout vs user cancel using the error context. If that does not apply, add an index or rewrite the query so it finishes within statement_timeout — the full checklist is below.
SQLSTATE: 57014
Official name: Query Canceled
Service: PostgreSQL
The query was canceled, typically by a user request, statement_timeout, or idle_in_transaction_timeout.
EXPLAIN (ANALYZE, BUFFERS)
SELECT count(*) FROM orders WHERE created_at > now() - interval '1 year';
EXPLAIN ANALYZE reveals seq scans and missing indexes that make queries exceed statement_timeout and get canceled with 57014.
Set per-query timeouts via SET LOCAL statement_timeout inside a transaction.
with connection.cursor() as cur:
cur.execute("SET LOCAL statement_timeout = '30s'")
cur.execute(long_query)
Catch StatementTimeout and report a friendly 'took too long' error to the caller.
from sqlalchemy.exc import OperationalError
try:
session.execute(text('SELECT slow()'))
except OperationalError as e:
if getattr(e.orig, 'sqlstate', None) == '57014':
raise TimeoutError('query too slow')
raise
Adjust the project's statement_timeout and the per-request timeout on the client.
await supabase.from('t').select()
// server statement_timeout still applies; tune in Dashboard > Database settings
Most often this happens when statement_timeout is set and the query exceeded it, or when a user/admin ran pg_cancel_backend() or pressed Ctrl-C in psql.
Distinguish timeout vs user cancel using the error context.
This page documents fixes for: django, sqlalchemy, supabase.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.