Skip to main content
PgBouncer is a lightweight connection pooler that sits between Atomic and PostgreSQL. Thousands of client connections from Atomic terminate at PgBouncer, which multiplexes their transactions over a few dozen server connections to PostgreSQL. This guide builds on PostgreSQL setup and uses these placeholder names:

When you need PgBouncer

Every connection an Atomic process opens counts against PostgreSQL’s max_connections (count your connections):
  • Agent process: up to maxConnections + readerMaxConnections
  • Intercom broker: up to maxConnections + readerMaxConnections + 1 (for wake-ups)
If PostgreSQL cannot spare at least maxConnections: 2 per process across your fleet, deploy PgBouncer. Example: 150 CI runners each run 1 agent and 1 Intercom broker (300 processes, 150 brokers) against a server with max_connections = 300, 3 reserved admin slots, and 10 non-Atomic clients (287 slots available). Even at maxConnections: 1 and readerMaxConnections: 0, the fleet needs 300 + 150 = 450 connections. With PgBouncer, all 300 processes connect to PgBouncer, the 150 brokers hold 1 direct wake-up connection each (287 − 150 = 137 slots left), and PgBouncer multiplexes all 300 processes over an 85-connection server pool (80 + 5 reserve, totaling 235 server connections).

How Atomic works with PgBouncer

Transaction mode. Run PgBouncer with pool_mode = transaction. PgBouncer lends a PostgreSQL backend connection to a client for one transaction and returns it to the pool as soon as the transaction commits or rolls back. Atomic scopes every session setting (search_path, timeouts) and advisory lock to the transaction, so nothing depends on pinning a backend connection between transactions.
Never use pool_mode = statement with Atomic—statement pooling breaks multi-statement transactions.
Direct wake-ups via listenUrl. Intercom brokers receive instant message notices via PostgreSQL LISTEN/NOTIFY, which requires a persistent backend connection that transaction pooling cannot maintain. Point listenUrl directly at PostgreSQL so each broker opens one direct atomic-listen connection while all other queries flow through PgBouncer. If you omit listenUrl, Atomic detects the pooler and falls back to polling wake-up hints every 0.5 seconds (instead of instant notifications with a 5-second hint poll and 30-second backup outbox scan). Messages still arrive, at slightly higher latency and query volume. Prepared statements. PostgreSQL plans a prepared statement once and reuses the plan. PgBouncer 1.21+ tracks prepared statements across transaction pooling when max_prepared_statements > 0 (PgBouncer 1.24+ defaults this to 200). Atomic probes PgBouncer on its first connection and configures prepared statements automatically:
  • PgBouncer 1.21+ with max_prepared_statements > 0: enabled automatically.
  • Older PgBouncer, max_prepared_statements = 0, Supavisor, or unknown poolers: disabled automatically to avoid missing-statement errors.
  • Pooler on a port other than 6432 or 6543: Atomic may not infer a pooler automatically—set listenUrl or maxPreparedStatements: 0.
Setting maxPreparedStatements explicitly overrides auto-detection (0 forces off, 256 forces on). If PgBouncer’s max_prepared_statements is later lowered to 0 at runtime, Atomic retries the failed query once and disables prepared statements for the remainder of the process.

Set up PostgreSQL

Connection limits

Size PostgreSQL’s max_connections (requires restart) to cover:
  • PgBouncer’s server pool (default_pool_size + reserve_pool_size)
  • One wake-up connection per Intercom broker (when using listenUrl)
  • Administrators, monitoring, and other applications
Cap atomic_runtime so a runaway fleet cannot exhaust admin slots. The cap includes both PgBouncer’s pooled connections for atomic_runtime and direct broker wake-up connections (for the 150-runner example: 85 + 150 = 235, plus margin):
Raise this cap if PgBouncer logs too many connections for role "atomic_runtime".

A role for password lookups

Instead of duplicating user passwords in PgBouncer, configure auth_query with a restricted lookup role (pgbouncer_auth). Creating this function requires a superuser; on managed services without superuser access, use the provider’s built-in pooler or list roles in PgBouncer’s auth_file. Connected to the atomic database as a superuser, run:
SECURITY DEFINER lets pgbouncer_auth look up only the allowlisted non-superuser roles without direct access to pg_authid. List every role Atomic machines log in as, and keep RETURNS TABLE—a scalar/record null row causes PgBouncer to log auth_query response contained null user name. Password changes via ALTER ROLE take effect immediately without reloading PgBouncer. Verify SCRAM encoding with SHOW password_encoding; (scram-sha-256).

Who may connect

Allow PgBouncer, agent hosts (for direct listenUrl wake-ups), and your migration host over TLS in pg_hba.conf, then reload with SELECT pg_reload_conf();:
Here 10.0.1.5 is PgBouncer, 10.0.2.0/24 is the agent subnet, and 10.0.3.10 runs atomic storage migrate directly against PostgreSQL on port 5432 (never through PgBouncer).

Configure PgBouncer

Save this configuration (for PgBouncer 1.21+) as /etc/pgbouncer/pgbouncer.ini:
Save pgbouncer_auth’s password in /etc/pgbouncer/userlist.txt, readable only by the PgBouncer service user:
Only pgbouncer_auth needs its password in userlist.txt; all other role passwords remain solely inside PostgreSQL.

Choose the pool sizes

  • default_pool_size: Server connections PgBouncer opens for atomic_runtime. Must not exceed PostgreSQL’s remaining capacity (max_connections minus reserved slots, broker wake-ups, other clients, and reserve_pool_size—132 in the 150-runner example). Start near 2× server CPU cores (for example 32 on a 16-core server; 80 above assumes ~40 cores) and raise only if cl_waiting > 0 in SHOW POOLS while PostgreSQL CPU has headroom. Higher network latency between agents and PostgreSQL requires a slightly larger pool.
  • reserve_pool_size: Burst slots lent after a client waits reserve_pool_timeout seconds.
  • max_db_connections: Hard cap on total server connections for atomic (default_pool_size + reserve_pool_size).
  • max_client_conn: Client connections accepted by PgBouncer. Set well above processes × (maxConnections + readerMaxConnections), and raise the OS open-file limit (ulimit -n / LimitNOFILE) above max_client_conn.
  • Lock waits hold pool slots: Transactions waiting on locks retain their PostgreSQL backend connection. If cl_waiting climbs while sv_active hits the cap, raise default_pool_size and max_db_connections within max_connections.
Behind PgBouncer, keep Atomic’s maxConnections at its default of 8 or higher—PgBouncer queues client transactions for its server pool.

Start PgBouncer

Start PgBouncer (pgbouncer -d /etc/pgbouncer/pgbouncer.ini or systemctl restart pgbouncer). After subsequent edits to pgbouncer.ini, run RELOAD; on PgBouncer’s admin console.
Ubuntu and Debian default LimitNOFILE=1024. The distro unit starts PgBouncer with only 1,024 file descriptors—too low for max_client_conn = 5000 (logged as kernel file descriptor limit: 1024). Raise the limit with a systemd drop-in, then run systemctl daemon-reload && systemctl restart pgbouncer:
If PgBouncer runs on the PostgreSQL host and connects via 127.0.0.1, PostgreSQL’s certificate typically does not include 127.0.0.1. Use server_tls_sslmode = verify-ca, or connect via the hostname on the server certificate.

Configure Atomic

Point url at PgBouncer and listenUrl directly at PostgreSQL (both use atomic_runtime and the same password). On each agent machine:
1

Join the database through PgBouncer

2

Add listenUrl to ~/.atomic/agent/settings.json

atomic storage join does not set listenUrl, so add the direct PostgreSQL endpoint:
3

Restart Intercom and verify wake-ups

Confirm atomic storage status reports wake-ups: LISTEN/NOTIFY.
The resulting ~/.atomic/agent/settings.json entry looks like this (keep the exact storage-token.<id> filename that atomic storage join created on your machine; storage-token.example is a placeholder):
Atomic verifies the certificate and hostname of both PgBouncer and PostgreSQL; append ?sslrootcert=/path/to/root.crt if signed by a private CA (see TLS). Equivalent SDK configuration:

Check that it works

Atomic: Run atomic storage status on an agent host and confirm wake-ups: LISTEN/NOTIFY (or wake-ups: connecting immediately after restart). If it shows polling only, notifications do not pass the URL's transaction pooler, check that listenUrl points directly to PostgreSQL. PgBouncer: Inspect the atomic / atomic_runtime row via PgBouncer’s console:
Use SHOW CLIENTS and SHOW STATS for per-client details and query/wait totals. PostgreSQL: Verify pooled (atomic) and direct wake-up (atomic-listen) counts on the database server:
atomic connections should stay at or below PgBouncer’s max_db_connections, with one atomic-listen connection per broker.

Common errors

Atomic wraps these causes in StorageUnavailableError or StorageTransientError:

Managed services