Skip to main content

Timeouts

When one SQL statement runs longer than the statement timeout, PostgreSQL cancels it. The usual error message is canceling statement due to statement timeout.

Through the SDK, HTTP API, and RPC, the default statement timeouts are 3 seconds for anon and 8 seconds for authenticated (see database roles). This is a per-statement database timeout, not an HTTP request or client timeout.

Handle timeouts​

Read the error message to identify which layer timed out:

Error messageMeaningWhat to do
canceling statement due to statement timeoutThe statement exceeded the timeoutOptimize the query, then extend the timeout if needed
canceling statement due to lock timeoutThe statement is waiting for a lockCheck for long uncommitted transactions, see Diagnose timeouts
The client times out but the database reports no errorThe limit is on the request side and the statement may still be runningCheck the client and gateway timeouts instead of only changing the database

Optimize the query​

Most timeouts come from scanning too many rows or missing indexes. Check in this order:

  • Whether filter and join columns have matching indexes.
  • Whether the query returns too many rows. Add limit to list and export endpoints, and avoid large offset values when paginating.
  • Whether subqueries and function calls in RLS policies run per row, and whether they can compare auth.uid() directly instead.
  • Whether a view or function can push the computation into the database and return fewer rows.

See Query optimization for details.

Extend the timeout​

When a longer timeout is genuinely required, choose the method by access path.

Run long tasks over a direct connection. Session settings last only for the connection and suit migrations, bulk backfills, and index creation.

set statement_timeout = '10min';
show statement_timeout;

Run the following statements on the same connection. Close the connection when done, or run reset statement_timeout; to restore the default. The platform connection pool does not guarantee that separate requests share a database session, so requests from the SDK or HTTP API cannot rely on a single set to affect the next call.

A single database function. PostgREST can apply a statement_timeout declared on a function to that call. Whether it takes effect and the actual limit depend on the instance configuration. Verify the change with a real request in a test environment.

create or replace function public.export_orders()
returns setof public.orders as $$
select * from public.orders;
$$ language sql
set statement_timeout to '30s';

Role-level and database-level defaults. alter role and alter database ... set apply only to sessions created afterwards, not to existing connections. Reconnect before checking the value with show. Do not use the show result from a direct connection to infer the effective timeout of an API request.

Diagnose timeouts​

The SQLSTATE of the error is 57014. Other cancellations return the same code, so check the message as well.

Inspect running statements and lock waits:

select pid, state, now() - query_start as duration, query
from pg_stat_activity
where state <> 'idle'
order by duration desc
limit 20;

For monitoring entry points and slow query diagnostics, see Logs and monitoring.

References: PostgreSQL client connection defaults. This guide draws on Supabase's timeout guide and adapts the approach for CloudBase.