During a planned PostgreSQL switchover behind PgBouncer 1.26, how do PAUSE, RELOAD and RESUME keep clients connected, and how do you restart PgBouncer itself without -R?
answer
- drain servers, keep clients
- queries queue while paused
- watch idle in transaction
- so_reuseport, one process at a time
basics
~20 sPAUSE waits for every server connection to be released, then disconnects them while clients stay connected and queue; RELOAD repoints the database; RESUME continues. PgBouncer itself restarts by rolling several so_reuseport processes, since 1.26 removed -R.
solid answer
~40 sOn the admin console, `PAUSE ticketing` waits until each server connection is released according to the pool mode, at the end of the current transaction in transaction mode, then disconnects it; it returns only when all are gone. Clients keep their connections, and queries they send wait in `cl_waiting`. You promote the standby, point the `[databases]` entry at it, run `RELOAD`, then `RESUME ticketing`. Keep the pause shorter than `query_wait_timeout` (120 s) and watch for idle-in-transaction clients, which block `PAUSE` until their transaction ends. To restart PgBouncer itself, 1.26 removed the online restart (`-R`, plus `SUSPEND` and `SHOW FDS`); run several processes on one port with `so_reuseport`, each with its own socket directory and pidfile, and stop them one at a time with `SHUTDOWN WAIT_FOR_CLIENTS` or `SIGTERM`.
code
ini · 16 lines; process 1 of 2 (process 2 differs only in unix_socket_dir, pidfile, logfile, peer_id)
[databases]
ticketing = host=10.0.0.5 dbname=ticketing pool_mode=transaction
[peers]
1 = host=/var/run/pgbouncer1
2 = host=/var/run/pgbouncer2
[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
so_reuseport = 1
unix_socket_dir = /var/run/pgbouncer1
pidfile = /var/run/pgbouncer1/pgbouncer.pid
logfile = /var/log/pgbouncer/pgbouncer1.log
peer_id = 1go deeper
Know that PgBouncer's admin console has PAUSE and RESUME, and that PAUSE stops traffic to PostgreSQL without dropping application connections.
Explain the sequence PAUSE, promote, edit [databases], RELOAD, RESUME, and why PAUSE waits for transactions to finish in transaction mode.
Anticipate idle-in-transaction blockers and query_wait_timeout, pause every so_reuseport process, and replace -R with peered rolling restarts on 1.26.
Decide where pooling sits in the failover design and how switchover tooling, PgBouncer layout and application retry behaviour fit together.
## The goal During a planned switchover, PostgreSQL's primary role moves to another node. The promotion itself and its orchestration belong to PostgreSQL and to tools such as Patroni. PgBouncer's job is narrower: **keep the application's client connections open, stop traffic for a few seconds, and send it to the new primary afterwards**. For the ticketing fleet this means functions see a short pause instead of connection errors. ## `PAUSE`, `RELOAD`, `RESUME` All three are **admin console** commands, run on the virtual database `pgbouncer` by a user in `admin_users`. 1. **`PAUSE [db]`**: PgBouncer waits for each server connection to be **released according to the pool mode**, then disconnects it. In transaction mode that means the end of the current transaction; in statement mode, the end of the statement; in session mode, the client disconnecting, which makes `PAUSE` impractical there. The command **does not return until every server connection is gone**. Clients stay connected; queries they send wait in `cl_waiting`, and new clients to the paused database also wait. 2. **Promote** the standby, using whichever tool owns that step. 3. **Repoint**: change `host=` in the `[databases]` entry and run **`RELOAD`**, or send `SIGHUP`. 4. **`RESUME [db]`**: new server connections go to the new primary and the queued queries run. ```bash psql -h /var/run/pgbouncer -p 6432 -U pgb_admin -d pgbouncer -c 'PAUSE ticketing;' # promote the standby, then edit [databases] ticketing = host=10.0.0.6 ... psql -h /var/run/pgbouncer -p 6432 -U pgb_admin -d pgbouncer -c 'RELOAD;' psql -h /var/run/pgbouncer -p 6432 -U pgb_admin -d pgbouncer -c 'RESUME ticketing;' ``` ### What can go wrong - **An idle-in-transaction client blocks `PAUSE`.** Its server is not released until that transaction ends, so `PAUSE` keeps waiting for as long as the client stays idle. PgBouncer's `idle_transaction_timeout` (default 0, off) disconnects such clients, and `KILL_CLIENT` removes one by id from `SHOW CLIENTS`. - **Waiting clients time out.** While paused, queued clients still age; after `query_wait_timeout` (default 120 s) they are disconnected. Keep the pause well under it. - **Several processes.** With `so_reuseport`, each PgBouncer process must be paused separately through its own Unix socket, because TCP connections to the shared port reach an arbitrary process. ## The alternatives and when they fit | Command | Effect | Use when | |---|---|---| | `RELOAD` with a changed connection string | existing servers close when next released; new ones use the new host | traffic may briefly go to both hosts | | `RECONNECT [db]` | close each server after release, even with unchanged config | something downstream of PgBouncer moved, for example a DNS or proxy change | | `WAIT_CLOSE [db]` | wait until no server is still marked `close_needed` | a script must know the change is fully applied | | `KILL [db]` | drop client and server connections immediately | emergency failover, not a planned switchover | `RELOAD` alone is a **gradual** cut-over: for a moment some transactions still reach the old primary. `PAUSE` is the tool when everything must move at once. ## Restarting PgBouncer itself Older guides restart PgBouncer with **`pgbouncer -R`**, an online restart that handed open sockets to a new process via `SUSPEND` and `SHOW FDS`. It was deprecated in 1.20 and **removed in 1.26**, together with those two commands. The replacement is a **rolling restart**: 1. Run two or more PgBouncer processes on the same port with **`so_reuseport = 1`**, so the kernel spreads new connections across them. This has the intended effect on sufficiently recent Linux, FreeBSD and DragonFlyBSD kernels. 2. Give each process its own `unix_socket_dir`, `pidfile` and `logfile`. Add a `peer_id` and a `[peers]` section so a cancel request that reaches the wrong process is forwarded to the right one. 3. Stop one process with **`SHUTDOWN WAIT_FOR_CLIENTS`**, or `SIGTERM`, which since 1.23 means the same thing. It stops accepting connections and exits once its clients have gone. 4. Let clients reconnect; they land on the other processes. Start the stopped process again and repeat for the next one. The signals map onto console commands, which matters for service managers and container stop hooks: - `SIGHUP` is `RELOAD`; - `SIGUSR1` is `PAUSE` and `SIGUSR2` is `RESUME`; - `SIGINT` is `SHUTDOWN WAIT_FOR_SERVERS`, which stops accepting connections and exits once all servers are released; - `SIGTERM` is `SHUTDOWN WAIT_FOR_CLIENTS`; - `SIGQUIT` is the immediate `SHUTDOWN`, the old meaning of `SIGTERM` before 1.23. `so_reuseport`, `listen_port`, `unix_socket_dir` and `pidfile` are read only at start-up, so a `RELOAD` cannot turn this layout on.
- Why is RELOAD alone not enough for an all-at-once switchover in PgBouncer?After `RELOAD` changes a database's connection string, existing server connections close only when they are next released, while new ones use the new host. For a short window some transactions still run on the old node. `PAUSE` first drains every server, so nothing reaches the old primary after `RESUME`.
- What breaks if you run several PgBouncer processes with so_reuseport but no [peers] section?A query cancel request opens a new connection that the kernel may hand to a different process than the one serving the query, and that process does not know the cancel key. With `peer_id` and `[peers]` configured, it forwards the cancel to the right peer.
- RELOAD alone moves every transaction to the new primary at the same moment.
- pgbouncer -R is the way to restart PgBouncer without dropping clients.
- PAUSE disconnects the clients, so every application has to reconnect.
- so_reuseport can be switched on later with a RELOAD.
- SIGTERM makes PgBouncer exit immediately, dropping all clients.
Assumes PgBouncer 1.26: online restart (-R), SUSPEND and SHOW FDS were removed in 1.26, and SIGTERM has meant SHUTDOWN WAIT_FOR_CLIENTS since 1.23.