Skip to content
PostgreSQL

PostgreSQL Error 57014

canceling statement due to statement timeout

Applies to: PostgreSQL 13 to 18
Last technically reviewed 24 October 2026 on PostgreSQL 18.6 documentation; SQL-level parts in PGlite 0.5.11
The full message
ERROR:  canceling statement due to statement timeout
Other forms of this message
Cancel request (pg_cancel_backend, Ctrl+C in psql, a driver query timeout)
ERROR:  canceling statement due to user request
Autovacuum worker cancelled (server log; same SQLSTATE)
ERROR:  canceling autovacuum task
Not this SQLSTATE: lock_timeout raises 55P03
ERROR:  canceling statement due to lock timeout
SQLSTATE
57014
Condition name
query_canceled
Product
PostgreSQL
Versions
PostgreSQL 13 to 18
Last reviewed
24 October 2026

What it means

SQLSTATE 57014 means the running statement was cancelled, either because it exceeded statement_timeout or because a cancel request arrived from the client or another session.

statement_timeout aborts any statement that runs longer than the set time; zero, the default, disables it. The timer runs from the moment the command reaches the server until the server completes it, and applies to each statement separately. When it expires, the statement fails with canceling statement due to statement timeout. A cancel request has the same effect with a different message, canceling statement due to user request: it comes from pg_cancel_backend(pid) in another session, from Ctrl+C in psql, or from a driver whose own query timeout sends a cancel to the server.

Only the statement is cancelled; the session stays connected. Inside a transaction block the whole transaction is aborted, so later statements fail with 25P02 until you ROLLBACK. Two related settings raise other SQLSTATEs: lock_timeout (55P03, waiting too long for a lock) and transaction_timeout or idle_in_transaction_session_timeout, which end the whole session.

Common causes

  • statement_timeout is set for the role, the database, the connection string or the application, and a slow query exceeds it.
  • A query became slow: a missing index, outdated statistics, more data than before, or a plan that changed.
  • The statement waited a long time for a lock held by another transaction; the wait counts towards statement_timeout.
  • A driver or ORM query timeout (for example a JDBC setQueryTimeout) expired and the driver sent a cancel request.
  • Someone pressed Ctrl+C in psql or a monitoring script ran pg_cancel_backend on the session.

Diagnose it

Read-only These queries only read the system catalogs (pg_catalog, information_schema) and statistics views such as pg_stat_activity and pg_locks. They do not change data, roles or settings, so they are safe to run first.

Which timeouts apply to this session, and where were they set?

SELECT name, setting, unit, source
FROM pg_settings
WHERE name IN ('statement_timeout', 'lock_timeout',
               'idle_in_transaction_session_timeout', 'transaction_timeout');
What to look for

A non-zero statement_timeout and its source: default, configuration file, database, user, session or client. Run it from the same application connection that fails, because each session can have its own value.

Is the timeout set on a role or database?

SELECT r.rolname, d.datname, s.setconfig
FROM pg_db_role_setting AS s
LEFT JOIN pg_roles AS r ON r.oid = s.setrole
LEFT JOIN pg_database AS d ON d.oid = s.setdatabase
ORDER BY r.rolname, d.datname;
What to look for

Entries such as statement_timeout=30s, set with ALTER ROLE or ALTER DATABASE. They apply to new sessions of that role or database.

Which statements are running long right now?

SELECT pid,
       now() - query_start AS running_for,
       state,
       wait_event_type,
       wait_event,
       left(query, 80) AS query
FROM pg_stat_activity
WHERE state = 'active'
  AND pid <> pg_backend_pid()
ORDER BY running_for DESC;
What to look for

The longest-running statements first. A wait_event_type of Lock means the statement is waiting for another transaction rather than working.

See the plan of the slow query

Plain EXPLAIN shows the plan without running the query. Look for sequential scans of large tables and estimated row counts far from reality.

EXPLAIN
SELECT customer_id, sum(amount)
FROM orders
WHERE created_at >= now() - interval '30 days'
GROUP BY customer_id;

How to fix it

Each fix is labelled with what it changes. Try read-only checks and the least invasive fix first, and test changes outside production.

Make the query faster

Changes schema

The lasting fix when a legitimate query times out. Add the index the plan is missing, refresh statistics with ANALYZE, or narrow the query. On a busy table, CREATE INDEX CONCURRENTLY avoids blocking writes.

CREATE INDEX CONCURRENTLY ix_orders_created_at ON orders (created_at);

Raise the timeout for one transaction only

Changes configuration

For a known long job (a report, a migration step), raise the limit just for that transaction with SET LOCAL; it reverts at COMMIT or ROLLBACK, so the protective default stays in place for everything else.

BEGIN;
SET LOCAL statement_timeout = '10min';
SELECT customer_id, sum(amount) FROM orders GROUP BY customer_id;
COMMIT;

Set different limits per role

Changes configuration

Give interactive application roles a short timeout and reporting roles a longer one. The documentation advises against setting statement_timeout in postgresql.conf because it would affect all sessions. The new value applies to new sessions.

ALTER ROLE app_web SET statement_timeout = '15s';
ALTER ROLE app_reporting SET statement_timeout = '5min';

Check the client-side timeout

Client or network side

If the message says due to user request and nobody cancelled by hand, the driver or ORM probably sent the cancel because its own query timeout expired. Raise or remove that timeout for the operation, or make the query faster; changing statement_timeout will not help.

Break large updates into batches

Changes data

A single UPDATE or DELETE over millions of rows can exceed any sensible timeout and holds locks for the whole time. Process a limited number of rows per transaction and repeat until none remain.

UPDATE orders
SET amount = round(amount, 2)
WHERE id IN (
    SELECT id FROM orders
    WHERE amount <> round(amount, 2)
    ORDER BY id
    LIMIT 10000
);

Examples

A statement that runs longer than statement_timeout

The documentation describes statement_timeout (Client Connection Defaults) but does not print the message, so the text is quoted from the PostgreSQL 18.6 source code. This example needs a running PostgreSQL server (statement_timeout does not fire in PGlite, where pg_sleep runs to completion whatever the timeout), so it was not reproduced here.

SET statement_timeout = '2s';
SELECT pg_sleep(5);
Result
ERROR:  canceling statement due to statement timeout

Cancelling a query from another session

Session 1 receives the error; in session 2 pg_cancel_backend returns true. The documentation describes pg_cancel_backend (Server Signaling Functions) but does not print the message, so the text is quoted from the PostgreSQL 18.6 source code; the pid is illustrative. This example needs a running PostgreSQL server (two concurrent sessions), which PGlite does not provide, so it was not reproduced here.

-- Session 1 (pid 12345)
SELECT pg_sleep(600);

-- Session 2
SELECT pg_cancel_backend(12345);
Result
ERROR:  canceling statement due to user request

How to prevent it

Set statement_timeout deliberately per role or per application connection rather than server-wide, choose values that match each workload, and use SET LOCAL for the few jobs that need more. Monitor slow queries (for example with pg_stat_statements) so a query that is creeping towards the limit is tuned before it starts failing.

Treat 57014 in the application as "the work was not done": the statement (and an open transaction) was rolled back. Retry only if the operation is idempotent and the cause was temporary, such as a lock wait; retrying a query that is simply too slow just repeats the timeout.

Common questions

How do I fix "canceling statement due to statement timeout"?

Find where statement_timeout is set (pg_settings shows the source), then either make the query faster with an index or a better plan, or raise the limit for that job only with SET LOCAL statement_timeout inside its transaction.

What does "canceling statement due to user request" mean if I did not cancel anything?

A cancel request reached the server: usually the driver or ORM sent it when its own query timeout expired, or a monitoring script called pg_cancel_backend. Check the client timeout settings first.

Does a statement timeout roll back my transaction?

The cancelled statement is rolled back. If it ran inside BEGIN ... COMMIT, the whole transaction is aborted and every later statement fails with 25P02 until you issue ROLLBACK. The connection itself stays open.

Is statement_timeout the same as lock_timeout?

No. statement_timeout limits the whole statement, including time spent waiting for locks, and raises 57014. lock_timeout limits only the wait for a lock and raises 55P03 with "canceling statement due to lock timeout".

Learn more on CodeWithSQL

Official documentation

Last technically reviewed 24 October 2026; examples and diagnostics run on PostgreSQL 18.6 documentation; SQL-level parts in PGlite 0.5.11. SQL-level examples were run in PGlite (PostgreSQL compiled to WebAssembly); examples that need a running server are shown as documented in the PostgreSQL documentation.

Still stuck on a PostgreSQL error?

Browse the full PostgreSQL error library, or get hands-on help from an experienced SQL professional.