skip to content

In PostgreSQL, how do superuser_reserved_connections and reserved_connections keep an operator able to log in when application connections fill max_connections?

level: middleimportance: should knowfreq 34%

answer

  1. slots carved from the top
  2. two bands, two audiences
  3. a predefined role, not superuser
  4. pg_use_reserved_connections

basics

~10 s

Both reserves come out of max_connections. The last superuser_reserved_connections slots (default 3) admit only superusers; the reserved_connections band below them (default 0) also admits roles granted pg_use_reserved_connections, such as an on-call or monitoring role.

solid answer

~40 s

PostgreSQL keeps the top of `max_connections` back for emergencies. An ordinary role is admitted only while at least `superuser_reserved_connections` + `reserved_connections` slots would stay free. Inside that margin, roles with privileges of the predefined role `pg_use_reserved_connections` can still connect until only the superuser band is left, and the final `superuser_reserved_connections` slots (default 3) are for superusers alone. So with `max_connections = 200`, 3 and 10, applications get 187 slots, the on-call role 197 and superusers all 200. Both settings have `postmaster` context, and a rejected client gets a FATAL `remaining connection slots are reserved for …` error with SQLSTATE `53300`. Grant `pg_use_reserved_connections` to operator and monitoring roles, never to the application role.

code

sql · 9 lines
sql
ALTER SYSTEM SET reserved_connections = 10;  -- applies after restart

CREATE ROLE dispatch_oncall LOGIN;
GRANT pg_use_reserved_connections TO dispatch_oncall;
GRANT pg_monitor TO dispatch_oncall;

SELECT name, setting, context, pending_restart
FROM pg_settings
WHERE name IN ('reserved_connections', 'superuser_reserved_connections');

go deeper

for a junior

Remember that a few top slots of max_connections are kept for superusers, three by default, so an administrator can usually still log in.

for a middle

Explain the two bands, the predefined role that unlocks the shared one, the admission arithmetic and the FATAL messages each refusal produces.

for a senior

Design the on-call access path: who gets pg_use_reserved_connections, how big the band is, and why the monitoring agent should not run as superuser.

for a principal

Treat reserved capacity as an incident-response guarantee, and pair it with per-service connection budgets so the reserve is rarely touched.

## The incident the reserves exist for Picture the order-dispatch database of a food-delivery fleet at the lunch peak. Autoscaling adds pods, every pod opens its connection pool, and the client backends climb to the cap. Now the on-call engineer needs to log in to see what is happening and end the stuck sessions, and so does the monitoring agent that should be raising the alert. Without a reserve, they get the same rejection as the application. PostgreSQL's answer is to hold the last few slots of `max_connections` back for privileged roles. ## Two bands carved out of max_connections Two settings define the reserve, and both are taken **from** `max_connections`, not added to it: - **`superuser_reserved_connections`** (default **3**) — the final emergency slots, usable only by superusers. - **`reserved_connections`** (default **0**) — a band just below them, usable by superusers and by roles with privileges of the predefined role **`pg_use_reserved_connections`**. When a regular client backend starts, the server checks how many process slots are still free. An ordinary role is admitted only if at least `superuser_reserved_connections + reserved_connections` slots would remain free. Superusers skip the check entirely. The admission thresholds for one example configuration: | `max_connections = 200`, `superuser_reserved_connections = 3`, `reserved_connections = 10` | Slots the role can fill | |---|---| | Ordinary role (the application) | 187 | | Role granted `pg_use_reserved_connections` | 197 | | Superuser | 200 | The manual words the same rule from the other side: while the number of free slots is above `superuser_reserved_connections` but no more than the sum of both settings, only superusers and `pg_use_reserved_connections` holders get in; once only `superuser_reserved_connections` or fewer slots are free, only superusers do. ## What a rejected client sees Both refusals are FATAL errors with SQLSTATE **`53300`** (`too_many_connections`), issued before the session starts: - In the shared band, a role without the privilege gets `remaining connection slots are reserved for roles with privileges of the "pg_use_reserved_connections" role`. - In the superuser band, any non-superuser gets `remaining connection slots are reserved for roles with the SUPERUSER attribute`. - With no slot at all left, even a superuser gets `sorry, too many clients already`. Reading which message the application logs is a quick way to tell how close to the absolute cap the server is. ## Setting it up 1. Choose the band size: enough for the monitoring agent, one or two on-call sessions and a scripted kill job. The value must be less than `max_connections` minus `superuser_reserved_connections`. 2. Set it with `ALTER SYSTEM` or in `postgresql.conf`; both settings have **`postmaster`** context, so plan a restart. 3. Grant `pg_use_reserved_connections` to the operator and monitoring roles. 4. Keep the application roles out of it, and keep the superuser login for genuine emergencies. The reserves apply only to regular client backends. Autovacuum workers, background workers and WAL senders have their own pools and are not affected. ## Pitfalls - **Granting the privilege to the application role** moves the cliff instead of protecting anything: the fleet fills the reserved band too, and the operators are locked out again. - **Running the monitoring agent as a superuser** lets it consume the three emergency slots exactly when they are needed; `reserved_connections` closes that gap without handing out superuser. - **`pg_monitor` is not the same thing**: it grants read access to statistics, not admission to reserved slots. A monitoring role usually needs both. - **The reserve does not prevent exhaustion**. Capping each service with a per-role `CONNECTION LIMIT`, sizing pools and putting a pooler in front are what keep the fleet below the cap; the reserve only guarantees the people fixing it can get in. - **Per-role limits are approximate and skip superusers**: the server claims a process slot before it counts the role's sessions, so two simultaneous logins can race. ## Checking the setup before you need it An operator band that has never been tested tends to fail on the day it matters. Two checks are cheap. First, confirm the values and that none is still waiting for a restart: `pg_settings` shows `reserved_connections`, `superuser_reserved_connections` and their `pending_restart` flag. Second, confirm membership: the on-call and monitoring roles should be members of `pg_use_reserved_connections`, and the application roles should not. During a load test that fills the application's share, log in as the on-call role and see that it is admitted while the application receives the reserved-slots error.

  • Why not simply let the on-call engineer and the monitoring agent log in as a superuser?
    Because the superuser band is the last-resort reserve, by default only 3 slots, and anything running as superuser can consume it. A monitoring agent or migration tool holding those slots leaves nothing for the human. Superuser is also far more privilege than watching and ending sessions needs. `reserved_connections` with `pg_use_reserved_connections` gives a separate, least-privilege band, keeping superuser slots for true emergencies.
  • Would granting pg_use_reserved_connections to the application role stop the lunch-peak failures?
    No. It only lets the application fill the reserved band as well, so failures start a few connections later and operators lose their guaranteed way in. The fix for exhaustion is budgeting: per-role `CONNECTION LIMIT`, smaller per-pod pools and a pooler in front. The reserve exists for the people repairing the incident, not for the workload causing it.

  • reserved_connections slots are reserved for superusers only.
  • Granting pg_monitor lets a role use the reserved connection slots.
  • Changing reserved_connections takes effect after a configuration reload.
  • Giving the application role pg_use_reserved_connections fixes slot exhaustion.
  • Superusers are limited to the superuser_reserved_connections slots.