// PostgreSQL error reference

PostgreSQL 57014: canceling statement

The query was cancelled, either because it ran past statement_timeout or because a client or administrator sent a cancel request.

SQLSTATE
57014
Name
query_canceled
Category
Operator Intervention
Error message
canceling statement due to statement timeout

What it means

57014 means the running statement was cancelled before it finished. The message says why: canceling statement due to statement timeout when it ran longer than statement_timeout, and canceling statement due to user request when a client pressed cancel (Ctrl+C in psql, a driver's cancel call) or someone ran pg_cancel_backend() on it.

Only the statement is cancelled; the session stays connected. Inside a transaction block the transaction is now failed and must be rolled back.

Common causes

  • A statement_timeout set on the server, database, role or session that the query exceeded.
  • You or your tool cancelled the query.
  • An administrator or monitoring job cancelled it with pg_cancel_backend(pid).
  • A slow query: a missing index, a bad plan, or waiting on a lock until the timeout fired.

How to fix it

  1. Check which timeout applies and where it was set.

    SHOW statement_timeout;
    SELECT name, setting, source FROM pg_settings WHERE name = 'statement_timeout';
  2. Raise it for the one session or transaction that legitimately needs longer, rather than globally.

    SET statement_timeout = '10min';
    -- or only for this transaction:
    BEGIN;
    SET LOCAL statement_timeout = '10min';
    -- long-running work
    COMMIT;
  3. Make the query faster: look at its plan for sequential scans on big tables and add the index it needs.

    EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE customer_id = 42;
  4. If it was waiting on a lock, find the blocker.

    SELECT pid, pg_blocking_pids(pid) AS blocked_by, wait_event_type, query
    FROM pg_stat_activity
    WHERE cardinality(pg_blocking_pids(pid)) > 0;

Reproduce it safely

SET statement_timeout = '1s';
SELECT pg_sleep(5);  -- ERROR: canceling statement due to statement timeout
RESET statement_timeout;

Related errors

More operator intervention errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.