Skip to main content
SRE

PostgreSQL

PostgreSQL too many connections

warning

Overview

What does this alert mean?

Active connections exceed 80% of max_connections.

Contributing factors

  • PgBouncer not in use or misconfigured
  • Connection leak in application
  • Idle-in-transaction connections accumulating

Affected components

  • Application services opening new connections

Expected action

Identify and terminate idle or idle-in-transaction connections. Consider deploying PgBouncer.

Services

Metrics

Metric

sum(pg_stat_activity_count) / pg_settings_max_connections

Description & unit

Ratio of active connections to the configured maximum.

Unit: ratio (0–1), alert threshold at 0.8

Threshold rationale

At 80% there is still headroom to act before new connections are refused.

Normal behavior

Below 50% in healthy operation.

Screenshots

Dashboard when connection ratio exceeds 80%
Dashboard when connection ratio exceeds 80%
Dashboard after connections drop
Dashboard after connections drop

Alert Behavior

Silencing

Silence during load tests.

Expected frequency

Occasional.

Severities

Severity guidance

Warning. Escalate if connections reach 95%+ or application errors appear.

Impacted users

Users of services opening new connections.

Things to check to determine severity

  • Are idle-in-transaction sessions accumulating?
  • Is PgBouncer deployed and healthy?

Verification

Prometheus query

sum(pg_stat_activity_count) / pg_settings_max_connections > 0.8

Dashboards

  • PostgreSQL Connections — Fill in: dashboard URL

Log queries

  • Application connection errors — Fill in: log query URL

Troubleshooting

Steps

  1. 1Count connections: SELECT count(*) FROM pg_stat_activity;
  2. 2Find idle-in-transaction: SELECT pid, now() - state_change AS duration, query FROM pg_stat_activity WHERE state = 'idle in transaction' ORDER BY duration DESC;
  3. 3Terminate long idle sessions: SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND now() - state_change > interval '10 minutes';
  4. 4Check max_connections: SHOW max_connections;

Useful commands

SELECT count(*) FROM pg_stat_activity;SHOW max_connections;

Possible Resolutions

  • Deployed PgBouncer in transaction pooling mode — sustained connection count dropped by 80%.

Dependencies

  • PgBouncer (if applicable)
  • Application connection pools

Escalation

Guidance

Escalate if connections exceed 95% or new connection errors appear in application logs.

Slack channels

  • #database-oncall — Fill in: channel URL

Definitions

Alert rule

Fill in: link to the alert rule in _data/rules.yml on GitHub

Tuning notes

The 80% threshold is self-normalizing against max_connections.

Related Links

Related alerts

Documentation

Template adapted from https://runbooks.gitlab.com/template-alert-playbook/