PostgreSQL error: canceling statement due to statement timeout

ERROR:  canceling statement due to statement timeout

The statement ran longer than statement_timeout allows, measured in wall-clock time from when the server received it — including any time spent waiting on locks. PostgreSQL cancelled it and rolled back its effects.

What this error means

SQLSTATE 57014 (query_canceled) covers all statement cancellations; the message tells you which one you got:

The subtlety that matters: statement_timeout counts lock waits too. A query that normally takes 50 ms can hit a 30 s timeout because it sat behind someone's ALTER TABLE. Before "optimizing" the query, find out which case you're in.

Common causes

How to diagnose it

-- Da dove viene il valore attivo:
SELECT name, setting, source
FROM pg_settings
WHERE name = 'statement_timeout';

-- Override per ruolo/database:
SELECT * FROM pg_db_role_setting;

The cancelled statement is written to the server log together with the error, so you know exactly which query it was. Then split the two cases:

How to fix it

🔍 Related tool: the EXPLAIN ANALYZE visualizer — paste the plan of the query that timed out and see which node eats the time.
🔍 Related reading: How to read EXPLAIN ANALYZE.