PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 57014: canceling statement due to statement timeout

The query ran longer than statement_timeout, so PostgreSQL cancelled it.

PostgreSQL 17.12
57014: canceling statement due to statement timeout

Jump to a cause

  1. 01A role-level timeout stops a long report
  2. 02SET statement_timeout = 5 means 5 milliseconds
  3. 03Slow query with no usable index (Seq Scan)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

SQLSTATE 57014 means the statement was cancelled. With the message canceling statement due to statement timeout, the cause is the statement_timeout setting: any statement that runs longer is stopped and the transaction is aborted.

The value can come from the session, the role, the database or postgresql.conf. SHOW statement_timeout; shows the value in effect. The three runs below show where the limit came from and how each case was fixed.

01A role-level timeout stops a long report

The DBA ran ALTER ROLE lab_a SET statement_timeout = '1s'. A report that counts 50 million rows failed with 57014. In the session, SHOW statement_timeout returned 1s, and pg_db_role_setting showed {statement_timeout=1s} for lab_a.

For that one job, BEGIN; SET LOCAL statement_timeout = '60s'; ... COMMIT; let the count finish (50000000). After COMMIT, SHOW statement_timeout was 1s again, so the role setting stays in force for everything else.

Setup
Setuprun as postgres
ALTER ROLE lab_a SET statement_timeout = '1s';
OUTPUT
ALTER ROLE
Reproducerun as lab_a
SELECT count(*) AS n FROM generate_series(1, 50000000);
✕ ERROR
ERROR:  57014: canceling statement due to statement timeout
Checkrun as lab_a
SHOW statement_timeout;
OUTPUT
 statement_timeout
-------------------
 1s
(1 row)
Checkrun as postgres
SELECT r.rolname, s.setconfig FROM pg_db_role_setting s JOIN pg_roles r ON r.oid = s.setrole WHERE r.rolname = 'lab_a';
OUTPUT
 rolname |       setconfig
---------+------------------------
 lab_a   | {statement_timeout=1s}
(1 row)

SET LOCAL lasts only until the end of the transaction. Use it to give one batch job more time without raising the limit for the whole role.

After the fixrun as lab_a
BEGIN;
SET LOCAL statement_timeout = '60s';
SELECT count(*) AS n FROM generate_series(1, 50000000);
COMMIT;
SHOW statement_timeout;
✓ SUCCESS
BEGIN
SET
    n
----------
 50000000
(1 row)

COMMIT
 statement_timeout
-------------------
 1s
(1 row)

02SET statement_timeout = 5 means 5 milliseconds

SET statement_timeout = 5 followed by a 5-million-row count failed with 57014. SHOW statement_timeout returned 5ms: a number without a unit is milliseconds, not seconds.

With SET statement_timeout = '5s', SHOW returned 5s and the count returned 5000000.

Reproducerun as lab_a
SET statement_timeout = 5;
SELECT count(*) AS n FROM generate_series(1, 5000000);
✕ ERROR
SET
ERROR:  57014: canceling statement due to statement timeout
Checkrun as lab_a
SET statement_timeout = 5;
SHOW statement_timeout;
OUTPUT
SET
 statement_timeout
-------------------
 5ms
(1 row)
After the fixrun as lab_a
SET statement_timeout = '5s';
SHOW statement_timeout;
SELECT count(*) AS n FROM generate_series(1, 5000000);
✓ SUCCESS
SET
 statement_timeout
-------------------
 5s
(1 row)

    n
---------
 5000000
(1 row)

03Slow query with no usable index (Seq Scan)

members has 2 million rows. With a 100ms timeout, WHERE lower(email) = '...' failed with 57014. EXPLAIN showed Parallel Seq Scan on members with the filter on lower(email): every row was read.

After CREATE INDEX members_lower_email_idx ON members (lower(email)), the plan became Index Scan using members_lower_email_idx and the same query returned id 1999999 within the same 100ms limit.

Setup
Setuprun as lab_a
CREATE TABLE members (id int PRIMARY KEY, email text NOT NULL);
INSERT INTO members SELECT g, 'User' || g || '@Example.com' FROM generate_series(1, 2000000) g;
ANALYZE members;
OUTPUT
CREATE TABLE
INSERT 0 2000000
ANALYZE
Reproducerun as lab_a
SET statement_timeout = '100ms';
SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
✕ ERROR
SET
ERROR:  57014: canceling statement due to statement timeout
Checkrun as lab_a
EXPLAIN (COSTS OFF) SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
OUTPUT
                            QUERY PLAN
------------------------------------------------------------------
 Gather
   Workers Planned: 2
   ->  Parallel Seq Scan on members
         Filter: (lower(email) = 'user1999999@example.com'::text)
(4 rows)
Fixrun as lab_a
CREATE INDEX members_lower_email_idx ON members (lower(email));
ANALYZE members;
OUTPUT
CREATE INDEX
ANALYZE

An index on email alone does not help a filter on lower(email); the index has to be on the same expression.

After the fixrun as lab_a
SET statement_timeout = '100ms';
EXPLAIN (COSTS OFF) SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
✓ SUCCESS
SET
                           QUERY PLAN
----------------------------------------------------------------
 Index Scan using members_lower_email_idx on members
   Index Cond: (lower(email) = 'user1999999@example.com'::text)
(2 rows)

   id
---------
 1999999
(1 row)

FAQ

How do I fix canceling statement due to statement timeout?

Find where the limit comes from with SHOW statement_timeout, then either make the query faster (an index in the third case) or give that job more time with SET LOCAL statement_timeout (first case).

Where is statement_timeout set for my user?

Role settings are in pg_db_role_setting. In the first case it showed {statement_timeout=1s} for lab_a.

Is statement_timeout in seconds or milliseconds?

A bare number is milliseconds. SET statement_timeout = 5 showed 5ms; write '5s' for seconds.

How do I raise the timeout for one query only?

Wrap it in a transaction with SET LOCAL statement_timeout = '60s'. In the first case the setting went back to 1s after COMMIT.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12