PostgreSQL Error 57014: Query Canceled

PostgreSQL error 57014 (Query Canceled) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

SQLSTATE 57014 PostgreSQL Last verified 2026-08-19

Quick Answer

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.

Error Code

SQLSTATE: 57014
Official name: Query Canceled
Service: PostgreSQL

What does this error mean?

The query was canceled, typically by a user request, statement_timeout, or idle_in_transaction_timeout.

Common Causes

How to Fix

  1. Distinguish timeout vs user cancel using the error context
  2. Add an index or rewrite the query so it finishes within statement_timeout
  3. Temporarily raise statement_timeout for a known long admin query
  4. Set statement_timeout appropriately per role/operation, not globally low

Code Examples

Diagnose a slow query sql
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.

Framework-Specific Fixes

django

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)
sqlalchemy

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
supabase

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

You Might Also Like

Frequently Asked Questions

Why am I seeing PostgreSQL error 57014?

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.

How do I fix PostgreSQL error 57014?

Distinguish timeout vs user cancel using the error context.

Which frameworks have documented fixes for error 57014?

This page documents fixes for: django, sqlalchemy, supabase.

Official Sources

This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.