Skip to content
Database

Connection management

Using your connections resourcefully

Connections#

Every Compute Add-On has a pre-configured direct connection count and Supavisor pool size. This guide discusses ways to observe and manage them resourcefully.

Configuring Supavisor's pool size#

You can change how many database connections Supavisor can manage by altering the pool size in the "Connection pooling" section of the Database Settings:

Connection Info and Certificate.

The general rule is that if you are heavily using the PostgREST database API, you should be conscientious about raising your pool size past 40% of the Database Max Connections. Otherwise, you can commit 80% to the pool. This leaves adequate room for the Authentication server and other utilities.

These numbers are generalizations and depends on other Supabase products that you use and the extent of their usage. The actual values depend on your concurrent peak connection usage. For instance, if you were only using 80 connections in a week period and your database max connections is set to 500, then realistically you could allocate the difference of 420 (minus a reasonable buffer) to service more demand.

Monitoring connections#

Capturing historical usage#

Dashboard monitoring charts#

Database client connections chart

For Teams and Enterprise plans, Supabase provides Advanced Telemetry charts directly within the Dashboard. The Database client connections chart displays historical connection data broken down by connection type:

  • Postgres: Direct connections from your application
  • PostgREST: Connections from the PostgREST API layer
  • Reserved: Administrative connections for Supabase services
  • Auth: Connections from Supabase Auth service
  • Storage: Connections from Supabase Storage service
  • Other roles: Miscellaneous database connections

This chart helps you monitor connection pool usage, identify connection leaks, and plan capacity. It also shows a reference line for your compute size's maximum connection limit.

For more details on using these monitoring charts, see the Reports guide.

Grafana Dashboard#

Supabase offers a Grafana Dashboard that records and visualizes over 200 project metrics, including connections. For setup instructions, check the metrics docs.

Its "Client Connections" graph displays connections for both Supavisor and Postgres client connection graph

Observing live connections#

pg_stat_activity is a special view that keeps track of processes being run by your database, including live connections. It's particularly useful for determining if idle clients are hogging connection slots.

Query to get all live connections:

1
SELECT
2
pg_stat_activity.pid as connection_id,
3
ssl,
4
datname as database,
5
usename as connected_role,
6
application_name,
7
client_addr as IP,
8
query,
9
query_start,
10
state,
11
backend_start
12
FROM pg_stat_ssl
13
JOIN pg_stat_activity
14
ON pg_stat_ssl.pid = pg_stat_activity.pid;

Interpreting the query:

ColumnDescription
connection_idconnection id
sslIndicates if SSL is in use
databaseName of the connected database (usually postgres)
usenameRole of the connected user
application_nameName of the connecting application
client_addrIP address of the connecting server
queryLast query executed by the connection
query_startTime when the last query was executed
stateQuerying state. See Session states for the full list of values and what each one means.
backend_startTimestamp of the connection's establishment

The username can be used to identify the source:

RoleAPI/Tool
supabase_adminUsed by Supabase for monitoring and by Realtime
authenticatorData API (PostgREST)
supabase_auth_adminAuth
supabase_storage_adminStorage
supabase_replication_adminSynchronizes Read Replicas
postgresSupabase Dashboard and External Tools (e.g., Prisma, SQLAlchemy, PSQL...)
Custom roles defined by userExternal Tools (e.g., Prisma, SQLAlchemy, PSQL...)

Diagnosing stuck and blocked queries#

If your application is slow or hanging, the cause is usually visible right now in pg_stat_activity - a query that's stuck, one session blocking others, or a transaction left open by mistake. This section covers how to read a session's state, find out what's blocking a query, and stop the session responsible.

Session states#

The state column on pg_stat_activity can be one of six values:

StateMeaning
activeThe session is currently executing a query. A query running longer than expected is worth investigating, but a nonzero count of active sessions is normal on its own.
idleThe session is connected and waiting for the client to send a new command. Normal for pooled or long-lived connections.
idle in transactionThe session has an open transaction but isn't currently running a query. This holds locks and blocks table cleanup for as long as it stays open, and almost always means the application forgot to commit or roll back.
idle in transaction (aborted)The last statement in the transaction failed. Postgres releases the transaction's ordinary locks as part of the abort - the exception is session-level advisory locks (pg_advisory_lock, not pg_advisory_xact_lock), which are held until explicitly unlocked or the session ends. The client still needs to send rollback to close the transaction before the session will accept new commands.
fastpath function callThe session is executing a function through Postgres's low-level fastpath protocol, most commonly for large object reads or writes. Rare, and normally brief.
disabledActivity tracking (track_activities) is turned off for this session, so Postgres isn't recording its state or query.

idle in transaction is the state most worth watching. Unlike a slow active query, which is at least making progress, an idle-in-transaction session is holding its locks indefinitely while doing nothing.

Finding blocked queries#

Postgres tracks which sessions are waiting on a lock held by another session. Query pg_stat_activity and pg_blocking_pids() together to see this directly:

1
select
2
a.pid,
3
a.usename as role_name,
4
a.application_name,
5
a.state,
6
a.query,
7
a.wait_event_type,
8
a.wait_event,
9
a.xact_start as transaction_start,
10
a.query_start,
11
a.state_change,
12
pg_blocking_pids(a.pid) as blocked_by
13
from pg_stat_activity as a
14
where
15
a.datname = current_database()
16
and a.pid != pg_backend_pid()
17
and a.backend_type = 'client backend'
18
order by a.query_start asc nulls last;

The where clause excludes the query's own session and Postgres's internal background workers (autovacuum, the WAL writer, extensions like pg_cron), so the results only show sessions a client opened.

blocked_by is an array, not a single value, for two reasons:

  • A session can be waiting on more than one session at once, if several sessions hold a lock that all conflict with what it's requesting.
  • Blocking can chain: session A holds a lock, session B waits on A, session C waits on B. blocked_by only ever lists the direct blocker, so tracing a long queue back to its root cause may mean following the chain through more than one session.

Cancelling or terminating a session#

Postgres gives you two ways to stop a session, and they behave differently:

  • select pg_cancel_backend(pid); cancels the session's currently running query but leaves the connection open. The application gets one failed query back and can continue using that connection.
  • select pg_terminate_backend(pid); ends the session's connection entirely.

Which one to use depends on the session's state:

  • For an active session that's stuck waiting on a lock or running longer than expected, cancel it first. It's the less disruptive option, and it's usually enough to unstick the wait or stop the runaway query.
  • For a session that's idle in transaction, cancelling does nothing - there's no running query to cancel, and the open transaction stays open regardless. Terminating the session is the only way to force the transaction closed and release its locks.
  • For a session that's idle in transaction (aborted), its ordinary locks were already released when the transaction aborted. The correct fix is for the client to issue rollback, which closes the transaction cleanly. Reserve pg_terminate_backend for a client that's unresponsive or a connection that needs to close regardless - it won't release anything further in this case, aside from a session-level advisory lock, which persists until the session ends.

Both functions require you to either be a superuser, hold the pg_signal_backend role, or be signalling your own session - and even pg_signal_backend can't be used to stop a session belonging to an actual superuser role. If you hit a permission error while terminating a session, see this troubleshooting guide.


If you're on Supabase, you can see all of this - session states, blocking chains, and a way to terminate a stuck session - on your project's Database Connections page in the dashboard, without writing any SQL.