Graceful shutdown
Graceful shutdown gives active transactions an opportunity to finish before the proxy closes their client connections. It reduces disruption during a planned stop, but depends on the application’s shutdown deadline and client reconnection behavior.
Stop accepting, then drain
Section titled “Stop accepting, then drain”The application stops accepting new connections and signals existing sessions to drain. In the library, both follow the context passed to server.NewServer: cancel it to drain sessions. Server.Close alone only stops accepting, and Run then returns without waiting for sessions. A session that has no active backend work can close promptly. A busy session continues processing until it reaches an idle state that is safe to release. A connection that has not finished TLS, startup, or authentication when shutdown begins is closed instead: with a startup error, or with none if its TLS handshake has not finished.
At that point, the proxy sends this error and closes the client connection:
FATAL 57P01: Pg-proxy session has been terminated. Reconnect to continue.SQLSTATE 57P01 is admin_shutdown, the same code PostgreSQL sends when its server shuts down. Most drivers and connection pools already treat it as a reason to reconnect. Because the proxy waits until the session is idle, no transaction is lost: the client can reconnect and run its next transaction on another proxy. Sessions are not handed off to another process.
Deadlines still apply
Section titled “Deadlines still apply”The application controls how long it waits for sessions and pools to close. An unfinished transaction, including a client idle inside a transaction, can prevent draining from completing within that time. Forced termination, network failures, and shutdown deadlines can still interrupt work.
The pg-proxy binary handles SIGINT and SIGTERM, stops acceptance, and allows up to proxy.shutdown_grace_period for cleanup (30 seconds by default). Set a positive duration such as 30s or 2m in YAML, or shutdownGracePeriod in JSON. If cleanup exceeds that deadline, it reports an error and exits unsuccessfully. This is the binary’s policy, not a fixed library-wide grace period.
Client behavior matters
Section titled “Client behavior matters”Client pools should discard closed connections and reconnect through the application’s configured endpoint. An interrupted connection does not always reveal whether a write committed. Retry decisions must account for the transaction’s possible outcome; reconnection alone does not make replaying a write safe.
See Timeouts, Transaction pooling, and Local development for related behavior.
