PostgresDB Connection Saturation

Too many connections open simultaneously, leading to connection pool exhaustion and preventing new connections.
  1. Identify Active Connections:Run the query to list current connections and their states:
  2. SELECT pid, usename, datname, state, query FROM pg_stat_activity;
  3. Terminate Long-Running Queries:If you identify any long-running queries that are not critical, consider terminating them to free up connections:
  4. SELECT pg_cancel_backend(pid); -- To gently cancel a query
    -- OR
    SELECT pg_terminate_backend(pid); -- To forcefully terminate a connection
  5. Replace pid with the process ID of the connection you want to cancel or terminate.
  6. Increase max_connections Temporarily:If your system has enough resources and you need an immediate but temporary solution, increase max_connections:
    • First, check the current setting:
    • SHOW max_connections;
    • Then, increase the max_connections value in postgresql.conf and reload the configuration. Note that this requires access to the server config and will affect memory usage, so it should be done with caution.
  7. Enable Connection Pooling:If not already in use, immediately set up a connection pooling solution like PgBouncer or use the built-in pooling of an application framework. This is more of an immediate mitigation rather than a direct action but can be crucial for preventing future saturation.
  8. Check for Locks:Sometimes connections are not released due to locks. Identify locks by running:
  9. SELECT a.datname, l.relation::regclass, l.mode, l.locktype, a.query, a.pid, age(now(), a.query_start) AS "age", a.usename
    FROM pg_stat_activity a
    JOIN pg_locks l ON l.pid = a.pid
    WHERE a.datname = 'your_database_name' AND NOT l.granted;
  10. Replace 'your_database_name' with your actual database name.
  11. Analyze and Optimize Queries:Identify slow queries that might be causing connection pile-ups:
  12. SELECT pid, query, state, start_time, now() - query_start AS duration
    FROM pg_stat_activity
    WHERE (now() - query_start) > interval '5 minutes';
  13. This helps in finding queries running longer than 5 minutes, which you might consider optimizing.

Remember, while the above actions can provide immediate relief, it’s crucial to investigate the root cause thoroughly to implement a long-term solution.

Master

PostgresDB

in Minutes — Grab the Ultimate Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Real-world configs/examples
Handy troubleshooting shortcuts
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the whitepaper on your email!
Oops! Something went wrong while submitting the form.

PostgresDB

Cheatsheet

(Perfect for DevOps & SREs)

Most-used commands
Your email is safe with us. No spam, ever.

Thankyou for your submission

We have sent the whitepaper on your email!
Oops! Something went wrong while submitting the form.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid