Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres sessions are idle with query = COMMIT or ROLLBACK

Tags:

I have a Postgres 9.5 database for a web-based Java application. Sometimes, the number of sessions suddenly spikes up due to a long running query, and these sessions are not cleared immediately. When I check pg_stat_activity, query column shows COMMIT or ROLLBACK with a stage of idle. This is causing Postgres to hit high thresholds of max_connections, which can possibly cause Production outage.

query | ROLLBACK state | idle count | 167

query | COMMIT state | idle count | 280

  1. What are these COMMIT/ROLLBACK idle sessions?
  2. How can I get these idle sessions to clear immediately?
  3. How can I reduce the number of these idle sessions from being created?

Thanks

like image 934
Murali Avatar asked May 31 '17 04:05

Murali


People also ask

What is idle session in Postgres?

idle: This indicates that the connection is idle and we need to track these connections based on the time that they have been idle. idle in transaction: This indicates the backend is in a transaction, but it is currently not doing anything and could be waiting for an input from the end user.

What is commit and rollback in PostgreSQL?

The COMMIT command is the transactional command used to save changes invoked by a transaction to the database. The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.

How do I stop Postgres automatically idle?

Kill an Idle Connection: >> SELECT pg_terminate_backend(7408); The process has been magnificently killed. Now check the remaining idle connections from the below-appended query.

Does Postgres require commit?

The default value of commit is ON in PostgreSQL, which means we need not have to execute a commit statement to save the transaction; it will automatically save the transaction into the database. If we set auto-commit is off, then we need to write commit to save the transaction into the database.


1 Answers

These are connections waiting for another query. They aren't doing anything. That's why they're idle.

Your app uses a connection pooler to avoid having to disconnect and reconnect all the time. When it has more connections than active queries, some will be idle, and the query shown will be the last completed query.

This is all entirely normal, and nothing to worry about. There is nothing you need to change or fix.

like image 91
Craig Ringer Avatar answered Sep 18 '22 14:09

Craig Ringer