Supabase REST API returns 504 PGRST003 "Timed out acquiring connection from connection pool" (or 503 PGRST002)
Your Supabase queries hang and then fail with 504 PGRST003, or 503 PGRST002 "Could not query the database for the schema cache", while SQL in the dashboard still works. What is holding the Data API's connections, how to find it, and when a restart or support ticket is the right move.
Free · no card
Paste your error. Get a free diagnosis by email.
Tell us what broke and we’ll email you a short diagnosis: the likely cause and what to try first. Usually within a day.
What you are seeing
- supabase-js calls hang for several seconds, then fail with
{"code":"PGRST003","message":"Timed out acquiring connection from connection pool."}and HTTP 504. - Or the REST API returns 503 with
PGRST002(could not connect when building the schema cache), sometimes after a restart. - Queries in the dashboard SQL editor still work, and Auth may still work, but every page that reads data through the API fails.
- It comes in bursts at busy times, during an import or a cron job, or right after you shipped a feature that makes many requests per page.
Why it happens
- Every Data API request borrows one database connection from PostgREST's pool for the length of its transaction. If all of them are busy, new requests wait, and after the acquisition timeout PostgREST gives up with 504 PGRST003. PostgREST's docs call this an indicator of a performance problem, not a bug in itself.
- Slow queries hold connections: a filter or order on a column with no index, a big table scanned on every page load, an RLS policy that runs a subquery per row, or an RPC function that does heavy work.
- Too many requests: a
useEffectthat refetches in a loop, one request per row in a list instead of one query with a join (resource embedding), inserts one row at a time in a loop, or aggressive polling. AI-written code does all of these. - Locks: a migration, a long transaction, or a session left
idle in transactionholds a lock, so API queries on that table queue behind it and keep their connections. - PGRST002 means PostgREST could not reach Postgres to load its schema cache. That happens when the database is down, overloaded, out of connections, or out of disk, and it can persist after a fast database reboot because that restarts only Postgres, not the other project services.
How to fix it
1. Confirm which error it is
curl -s -o /dev/null -w '%{http_code}\n' \
"https://<project-ref>.supabase.co/rest/v1/<a_table>?select=id&limit=1" \
-H "apikey: <anon-or-publishable-key>"
# 504 + PGRST003: pool exhausted. 503 + PGRST000-002: PostgREST cannot reach Postgres.
# Also check status.supabase.com for an incident in your region.
2. See what is holding connections right now
select pid, usename, application_name, state,
now() - query_start as running_for,
wait_event_type,
pg_blocking_pids(pid) as blocked_by,
left(query, 120) as query
from pg_stat_activity
where datname = current_database() and state <> 'idle'
order by query_start;
-- PostgREST connects as the authenticator role
select state, count(*) from pg_stat_activity
where usename = 'authenticator' group by state;
3. End stuck sessions (carefully)
-- try cancel first, then terminate only if it stays stuck
select pg_cancel_backend(<pid>);
select pg_terminate_backend(<pid>);
-- a session in 'idle in transaction' for minutes, or a migration
-- blocking others, is the usual culprit
4. Find and fix the slow queries
-- slowest statements by total time
-- (needs pg_stat_statements: Database, Extensions, enable it if it is off)
select calls, round(total_exec_time) as total_ms,
round(mean_exec_time) as mean_ms, left(query, 120) as query
from extensions.pg_stat_statements
order by total_exec_time desc limit 10;
-- then explain the worst one and add the missing index
explain analyze select * from public.orders where user_id = '<some-uuid>' order by created_at desc;
create index if not exists orders_user_id_created_at_idx on public.orders (user_id, created_at desc);
5. Make RLS policies cheap
-- wrap auth.uid() in a select so it runs once per query, not once per row,
-- and index the column the policy filters on
alter policy "owner can read" on public.orders
using ((select auth.uid()) = user_id);
create index if not exists orders_user_id_idx on public.orders (user_id);
6. Cut the number of requests
// one request with embedding instead of one per row
const { data } = await supabase
.from('orders')
.select('id, total, customer:customers(name)')
.limit(50);
// one bulk insert instead of a loop
await supabase.from('events').insert(rows);
// and make sure effects have dependency arrays so they do not refetch forever
7. If nothing is busy and it still fails
Restart project in Settings, General restarts every service, including the Data API. Fast database reboot restarts only Postgres. If a full restart does not clear PGRST002 or PGRST003 and pg_stat_activity shows nothing busy, open a support ticket with the project ref, the time window in UTC, the error codes, and your PostgREST logs. Raise the Data API pool size (API settings) only after the queries are fixed, and keep it well below your compute size's max connections.
How to know it is fixed
The curl check returns 200 several times in a row, pages that read data load normally at your busiest time, and the PostgREST logs show no new PGRST003 or PGRST002 entries. In pg_stat_activity the authenticator connections are mostly idle between requests, and nothing sits in idle in transaction. Sources: PostgREST: connection pool and acquisition timeout, PostgREST: error codes, Supabase: Connection management, Supabase: Timeouts, Supabase: pg_stat_statements, Supabase: RLS performance, supabase/supabase #54047, supabase/supabase #54075. Related: Supabase project Unhealthy or hibernated, Supabase Free project read-only or disk full.