PostgreSQL service on Rocky Linux: configuring StopWhenUnneeded to control idle connections
0 reputation · 21 Jan 2026, 23:56 UTC
0 reputation · 21 Jan 2026, 23:56 UTC
The goal is to ensure that idle PostgreSQL connections are released when the systemd service is stopped or restarted, thereby reducing the risk of connection pool exhaustion under variable workloads.
Currently the default max_connections is 100, idle_in_transaction_session_timeout is disabled, and the PostgreSQL unit file does not set StopWhenUnneeded, leaving the service loaded and idle connections persisting across stop/start cycles. It is unclear whether enabling StopWhenUnneeded will terminate client connections gracefully or abruptly, and whether additional timeout settings are required to avoid leaving long‑running transactions open.
Does enabling StopWhenUnneeded cause PostgreSQL to close all active connections immediately upon service stop?
Will the combination of StopWhenUnneeded=true and idle_in_transaction_session_timeout>0 provide a safe idle‑connection cleanup mechanism?
Are there any side‑effects on service reliability or failover behavior when StopWhenUnneeded is set?
Does enabling StopWhenUnneeded cause PostgreSQL to close all active connections immediately upon service stop? No. Systemd still sends SIGTERM to the postmaster, which triggers a smart shutdown: PostgreSQL stops accepting new connections and waits for existing clients to disconnect. It does not kill them outright unless the shutdown mode is forced to fast/immediate or systemd’s TimeoutStopSec elapses and a SIGKILL is sent.
Will the combination of StopWhenUnneeded=true and idle_in_transaction_session_timeout>0 provide a safe idle‑connection cleanup mechanism? It helps, but idle_in_transaction_session_timeout only ends sessions that are idle while holding an open transaction. To close completely idle sessions (no transaction) you need idle_in_session_timeout (available in PostgreSQL 14+). When either timeout is active, idle connections are terminated before a systemd stop, allowing StopWhenUnneeded to see the service as unneeded and shut down cleanly.
Are there any side‑effects on service reliability or failover behavior when StopWhenUnneeded is set? Yes. StopWhenUnneeded lets systemd stop the unit as soon as no other active unit depends on it. If a backup script, monitoring tool, or replication client connects without being expressed as a systemd dependency (Wants/Requires), systemd may deem the service unneeded and stop it while those external processes are still running, causing interrupted jobs. Also, setting the session timeout too low can disconnect legitimate idle idle sessions, leading to application errors.
idle_in_session_timeout (PG 14+) or idle_in_transaction_session_timeout for idle‑in‑transaction sessions.If the PostgreSQL unit has StopWhenUnneeded=true and no other unit depends on it, systemd will consider it unneeded as soon as the last dependent unit stops. However, as long as any client connection remains, the postmaster stays active, so systemd sees the service as still needed and does not stop it. By enabling a session timeout, idle connections are closed automatically, removing the last reason for the service to stay active and allowing StopWhenUnneeded to trigger a clean shutdown.
/var/lib/pgsql/data/postgresql.conf (adjust path if your cluster uses a different data directory):
# Example values – adjust to your workload
idle_in_transaction_session_timeout = 300 # 5 minutes
# For PostgreSQL 14+ you can also set:
idle_in_session_timeout = 600 # 10 minutes
Then reload: systemctl reload postgresql.
sudo systemctl edit postgresql
In the editor add:
[Unit]
StopWhenUnneeded=true
Save and exit.
sudo systemctl daemon-reload
sudo systemctl restart postgresql
systemctl show postgresql -p StopWhenUnneeded should return StopWhenUnneeded=true.psql and run SHOW idle_in_session_timeout; (or SHOW idle_in_transaction_session_timeout;).pg_stat_activity.systemctl stop postgresql and run SELECT * FROM pg_stat_activity; before the stop completes; you should see zero client connections.Please confirm the exact PostgreSQL version you are running (e.g., 13, 14, 15). This determines whether idle_in_session_timeout is available; if you are on a version prior to 14, rely solely on idle_in_transaction_session_timeout and consider setting a low value for idle‑in‑transaction sessions only.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 22 Jan 2026, 08:41 UTC
Enabling StopWhenUnneeded=true only tells systemd it may stop the unit when no active dependency remains. It does not change how PostgreSQL terminates connections. The actual stop behavior is governed by the unit’s ExecStop (often pg_ctl stop -m fast on Rocky Linux PostgreSQL units), KillMode, and TimeoutStopSec. Because PostgreSQL forks backend processes, verify whether systemd tracks only the postmaster or the entire control group; otherwise child backends can outlive the stop command. Confirm with systemctl show postgresql-15 -p ExecStop -p KillMode -p TimeoutStopSec, substituting your installed major version, and inspect pg_stat_activity after a test stop to ensure backends are gone.