Connection pooling and limits
Know how many PostgreSQL connections your plan gives you, who holds them, what a transaction pooler breaks, and how to read the live values of your project.
Mental model#
PostgreSQL gives each connection a process and caps their number with max_connections. Your application should not hold PostgreSQL connections when it does not need to: the SDK and the REST API never open one from your side, and the services behind them share a few pooled connections. Only the direct connection (psql, ORMs) puts connections of your own on the server, and on the cluster of an organization even those go through a pooler.
your app ─HTTP─▶ gateway ─▶ PostgREST of the project (2 pooled connections per replica) ─┐
your CLI ─HTTP─▶ gateway ─▶ aura-db ─▶ PgBouncer (transaction mode) ───────────────────┼─▶ PostgreSQL
psql/ORM ─TLS──▶ router ─────────────▶ PgBouncer (transaction mode) ───────────────────┘ max_connectionsThe pools on each path#
| Path | Pool | Size | When it is full |
|---|---|---|---|
| SDK and REST | Pool of the project's PostgREST, connected to the primary | By default 2 connections per replica on the cluster of an organization, 10 on a dedicated cluster (settings of the deployment) | The request waits up to 3 seconds, then fails with 503 DB_POOL_TIMEOUT and Retry-After |
aura db raw, Studio, schema API | Pool of aura-db, through the PgBouncer of the cluster | Created on demand, closed when idle | PgBouncer queues the transaction |
| Direct connection | PgBouncer of the cluster of your organization (Free, Pro) | default_pool_size of the plan, see below | PgBouncer queues the transaction, up to max_client_conn clients |
| Direct connection, dedicated cluster | None: the router connects to the primary | max_connections of the cluster | PostgreSQL refuses the connection |
What transaction mode changes#
The PgBouncer of a cluster runs in transaction mode: a server connection is yours for one transaction, then goes back to the pool and may serve another client. PgBouncer's documentation puts it this way: in that mode, clients must not use session-based features, since each transaction may run on a different server connection. In practice:
| Works | Does not survive the end of a transaction |
|---|---|
Statements in one transaction (BEGIN … COMMIT) | SET without LOCAL: use SET LOCAL inside the transaction |
SET LOCAL, SET LOCAL ROLE | Named prepared statements created with PREPARE |
| Single statements (autocommit) | LISTEN / NOTIFY subscriptions |
NOTIFY pgrst, 'reload schema' | Session-level advisory locks (pg_advisory_lock) |
aura-db already works this way: it sets the role, the search path, the statement timeout and the claims with SET LOCAL at the start of each transaction. For an ORM on the direct connection, see Connect to your database: Prisma documents pgbouncer=true for this mode, and its migration tool expects a session connection that the endpoint of the cluster does not offer.
Limits per plan#
These values come from the provisioning code and the plan table of the platform. The cluster of an organization is shared by the projects of that organization.
Cluster of the organization (new projects on Free and Pro)
| Plan of the organization | max_connections | Storage | PostgreSQL instances | PgBouncer default_pool_size | max_client_conn | max_db_connections | max_user_connections |
|---|---|---|---|---|---|---|---|
| Free | 50 | 5 Gi | 1 | 10 | 100 | 20 | 20 |
| Pro | 100 | 20 Gi | 1 | 20 | 200 | 50 | 60 |
max_db_connections caps the server connections of one database (one project); max_user_connections caps those of the shared login of the services across the whole cluster. The code also sizes this cluster for a Team organization (200 connections, 50 Gi, a pool of 40); new projects of a Team organization get a dedicated cluster instead.
Dedicated cluster (new projects on Team and Enterprise)
| Plan | max_connections | Storage | PostgreSQL instances |
|---|---|---|---|
| Team | 300 | 50 Gi | 3 |
| Enterprise | 400 | 100 Gi | 3 |
The dedicated cluster has its own PgBouncer (2 instances, pool size 50 and 1,000 clients by default), used by aura-db. These values come from the code and were not replayed on a running dedicated cluster.
Request limits (all paths through the gateway)
| Plan | db.requests per minute | db.requests per month | db.raw_sql_requests per minute | db.raw_sql_requests per month |
|---|---|---|---|---|
| Free | 600 | 1,000,000 | 60 | 2,000 |
| Pro | 30,000 | 50,000,000 | 600 | 100,000 |
| Team | 300,000 | 500,000,000 | 6,000 | 1,000,000 |
| Enterprise | 3,000,000 | 5,000,000,000 | 60,000 | 10,000,000 |
db.requests counts CRUD and RPC calls, db.raw_sql_requests counts calls to /raw. Above a per-minute limit the gateway answers 429. A monthly quota blocks requests only when it is a hard limit: every monthly quota of Free is; the monthly quotas of Pro, Team and Enterprise are counted without blocking. See also Limits.
Statement timeouts#
Each path bounds how long one statement may run. Past the limit, PostgreSQL cancels it (57014, canceling statement due to statement timeout).
| Path | Limit | Set by | Answer |
|---|---|---|---|
| SDK and REST, anon key | 5 s | Setting of the role aura_anon | 504 STATEMENT_TIMEOUT |
SDK and REST, signed-in user or service_role key | 10 s | Setting of aura_authenticator, the login of PostgREST | 504 STATEMENT_TIMEOUT |
aura db raw, Studio SQL editor, db.sql() | 10 s by default (500 ms to 120 s, setting of the deployment) | SET LOCAL statement_timeout by aura-db; a request can raise its own limit, but the gateway stops waiting after about 30 s by default and answers 408 | 400 BAD_REQUEST, message containing the query ran longer than this project's statement timeout (10 s) |
| Direct connection | none |
aura_authenticator is also limited to 200 connections on a cluster (CONNECTION LIMIT 200).
This function counts up to n; with a large n it runs longer than 5 seconds. The anon key hits its limit:
aura db raw "create function slow_count(n bigint) returns bigint language sql stable as \$\$
select count(*) from generate_series(1, n)
\$\$;
grant execute on function slow_count(bigint) to aura_anon" ℹ No results (0 rows).curl -s -w '\nHTTP %{http_code}\n' -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/rpc/slow_count" \
-H "apikey: $AURABASE_ANON_KEY" -H 'content-type: application/json' -d '{"n": 400000000}'{"error":{"code":"STATEMENT_TIMEOUT","details":"","message":"canceling statement due to statement timeout","request_id":"85871810-8838-45fd-9c40-c4a264fb23c1","type":"db_unavailable","upstream_code":"57014"}}
HTTP 504aura db raw "drop function slow_count(bigint)" ℹ No results (0 rows).Read the live values#
max_connections is a setting that aura db raw can read:
aura db raw "show max_connections" max_connections
───────────────
50
ℹ 1 row(s).The value above was captured on a project of a Free organization. aura db stats shows the connections open on the database of your project against that limit, and the size of the project:
aura db statsOutput (your numbers differ):
» Database Statistics
Active connections : 0 / 50
Project size : 224.0 KB
ℹ Endpoint latencies (p50/p95/p99): Studio › Logs.Trade-offs#
- The budget is shared by the organization. On Free and Pro, the
max_connectionsof the cluster serves every project of the organization, plus each awake project's PostgREST pool. A direct connection kept open by one project's ORM uses a share of it. - Transaction mode trades session features for capacity. Many clients share few server connections, at the cost of the session features listed above. Keep session-dependent work on the SDK or
aura db raw, or use a dedicated cluster, where the direct connection reaches the primary without a pooler. - No session-mode endpoint. The direct connection of the cluster of an organization is only offered through the transaction pooler.
- Statement timeouts. Every path through the gateway stops waiting for a long statement, after about 30 seconds by default at the latest (see Statement timeouts); this does not guarantee that PostgreSQL has stopped it. Only the direct connection has no limit. A long migration or backfill should be split into smaller statements.
Related pages#
- Connect to your database
The direct connection, with the rules that follow from the pooler.
- Shared and dedicated clusters
Where these limits apply, and what changes on a dedicated cluster.
- How a request reaches Postgres
The path behind each pool.
- Performance audit
Find the slow queries that hold connections.