본문으로 건너뛰기
아우라베이스 로고
아우라베이스

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.

설명8분 읽기레벨 중급

이 페이지는 아직 귀하의 언어로 제공되지 않습니다. 영어로 표시됩니다.

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_connections

The pools on each path#

PathPoolSizeWhen it is full
SDK and RESTPool of the project's PostgREST, connected to the primaryBy 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 APIPool of aura-db, through the PgBouncer of the clusterCreated on demand, closed when idlePgBouncer queues the transaction
Direct connectionPgBouncer of the cluster of your organization (Free, Pro)default_pool_size of the plan, see belowPgBouncer queues the transaction, up to max_client_conn clients
Direct connection, dedicated clusterNone: the router connects to the primarymax_connections of the clusterPostgreSQL 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:

WorksDoes 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 ROLENamed 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 organizationmax_connectionsStoragePostgreSQL instancesPgBouncer default_pool_sizemax_client_connmax_db_connectionsmax_user_connections
Free505 Gi1101002020
Pro10020 Gi1202005060

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)

Planmax_connectionsStoragePostgreSQL instances
Team30050 Gi3
Enterprise400100 Gi3

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)

Plandb.requests per minutedb.requests per monthdb.raw_sql_requests per minutedb.raw_sql_requests per month
Free6001,000,000602,000
Pro30,00050,000,000600100,000
Team300,000500,000,0006,0001,000,000
Enterprise3,000,0005,000,000,00060,00010,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).

PathLimitSet byAnswer
SDK and REST, anon key5 sSetting of the role aura_anon504 STATEMENT_TIMEOUT
SDK and REST, signed-in user or service_role key10 sSetting of aura_authenticator, the login of PostgREST504 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 408400 BAD_REQUEST, message containing the query ran longer than this project's statement timeout (10 s)
Direct connectionnone

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 504
aura 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 stats

Output (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_connections of 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.
@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ 문제 신고