Supabase “canceling statement due to statement timeout” (57014) – fix the query, then the limit

By Abheesht Tripathi · Published

ERROR: canceling statement due to statement timeout   (SQLSTATE 57014)

What it means

The statement ran longer than the statement_timeout for the database role it ran as, so Postgres cancelled it. Supabase sets short defaults on purpose, so one slow API request can't hold a connection for minutes:

RoleDefault timeoutUsed by
anon3 sAPI requests without a signed-in user
authenticated8 sAPI requests with a user JWT
service_rolenone set; falls back to the authenticator role's 8 sserver code with the secret key
postgresnone, capped by a 2 min global defaultSQL Editor, direct connections

Supabase's docs also say Dashboard and client queries can be raised to at most 60 seconds; longer work belongs on a direct or pooled connection.

Step 1: find out why it is slow

Run the same query with explain analyze in the SQL Editor. For an API query, also run it as the API role, because RLS policies are part of the cost:

begin;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"<user-uuid>","role":"authenticated"}', true);
explain analyze
select * from public.orders where status = 'open' order by created_at desc limit 50;
rollback;

Look for Seq Scan on a big table, a large Rows Removed by Filter, and a SubPlan that runs once per row (often an RLS policy). The dashboard's Query Performance report lists the slowest statements over time.

Step 2: the usual fixes

create index concurrently if not exists orders_status_created_at_idx
  on public.orders (status, created_at desc);
create index concurrently if not exists orders_user_id_idx
  on public.orders (user_id);

Run each create index concurrently on its own: it can't run inside a transaction block, so don't combine it with other statements in one run.

Step 3: raise the limit only where it is justified

If a query is as fast as it can be and still needs more time, raise the timeout for that role, then tell the API layer to reload its settings:

alter role authenticated set statement_timeout = '15s';
notify pgrst, 'reload config';

This applies to every request from that role, so keep it modest. For one long admin or migration job, set it for that transaction only with SET LOCAL inside BEGIN … COMMIT. That works over a direct connection, session mode, and the transaction-mode pooler on port 6543 (one transaction keeps one connection). A plain session-level SET is the one that doesn't stick through the transaction-mode pooler:

begin;
set local statement_timeout = '10min';
-- the long statement
commit;

Where it shows up besides the API

The SQL Editor runs as postgres, so a heavy one-off query is limited by the global cap. Cron jobs run as the role that scheduled them, so they hit that role's limit too. Creating an index on a large table can exceed it; use a direct connection for that kind of migration.

How to verify

explain analyze shows an index scan and an execution time well under the role's limit. The same API call returns data instead of 57014, and the timeout no longer appears when you search the Postgres logs for statement timeout.

FAQ

What is the default statement timeout in Supabase?

3 seconds for anon, 8 seconds for authenticated, and service_role falls back to the authenticator role's 8 seconds. The postgres role has no role-level timeout but is capped by a 2 minute global default.

Related guides

More in Postgres errors and performance · all guides