When you need PgBouncer
Every connection an Atomic process opens counts against PostgreSQL’smax_connections (count your connections):
- Agent process: up to
maxConnections + readerMaxConnections - Intercom broker: up to
maxConnections + readerMaxConnections + 1(for wake-ups)
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 withpool_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.
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
6432or6543: Atomic may not infer a pooler automatically—setlistenUrlormaxPreparedStatements: 0.
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’smax_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
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):
too many connections for role "atomic_runtime".
A role for password lookups
Instead of duplicating user passwords in PgBouncer, configureauth_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 directlistenUrl wake-ups), and your migration host over TLS in pg_hba.conf, then reload with SELECT pg_reload_conf();:
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:
pgbouncer_auth’s password in /etc/pgbouncer/userlist.txt, readable only by the PgBouncer service user:
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 foratomic_runtime. Must not exceed PostgreSQL’s remaining capacity (max_connectionsminus reserved slots, broker wake-ups, other clients, andreserve_pool_size—132 in the 150-runner example). Start near 2× server CPU cores (for example32on a 16-core server;80above assumes ~40 cores) and raise only ifcl_waiting > 0inSHOW POOLSwhile 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 waitsreserve_pool_timeoutseconds.max_db_connections: Hard cap on total server connections foratomic(default_pool_size + reserve_pool_size).max_client_conn: Client connections accepted by PgBouncer. Set well aboveprocesses × (maxConnections + readerMaxConnections), and raise the OS open-file limit (ulimit -n/LimitNOFILE) abovemax_client_conn.- Lock waits hold pool slots: Transactions waiting on locks retain their PostgreSQL backend connection. If
cl_waitingclimbs whilesv_activehits the cap, raisedefault_pool_sizeandmax_db_connectionswithinmax_connections.
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.
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
Pointurl 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
atomic storage status reports wake-ups: LISTEN/NOTIFY.~/.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):
?sslrootcert=/path/to/root.crt if signed by a private CA (see TLS).
Equivalent SDK configuration:
Check that it works
Atomic: Runatomic 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 inStorageUnavailableError or StorageTransientError:
Managed services
Related pages
- Tuning for many concurrent agents: connection arithmetic and
maxConnectionssizing. - Tune PostgreSQL for many agents: server memory,
pg_stat_statements, autovacuum, andlz4compression. - Connection poolers and Wake-ups.
- PgBouncer configuration reference.