Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long lasting 'COMMIT' queries with 'idle' state in pg_stat_activity

If I query:

select * from pg_stat_activity where application_name ~ 'example-application';

I get many rows which state is idle and query is COMMIT. They are long lasting and do not disappear. After some time, my application reach hibernate.c3p0.max_size (maximum number of JDBC connections in the pool) limit and stops working with database.

Some application implementation details are described in other SO thread: Guice DAO Provider in thread pool - queries become 'idle in transation'

Why does it happen? How to solve this problem?

like image 802
Justinas Jakavonis Avatar asked May 24 '17 10:05

Justinas Jakavonis


People also ask

What is idle state in Pg_stat_activity?

"idle" means the client is not currently executing a query nor in a transaction.

What does idle in transaction mean?

"Idle in Transaction" means that a transaction was started on a database connection and not completed and there is no longer any queries running.

What are idle connections in PostgreSQL?

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.

How do I turn off idle connections in PostgreSQL?

Kill an Idle Connection: We have to provide the process 'id' within the query in a terminate function. >> SELECT pg_terminate_backend(7408); The process has been magnificently killed.


1 Answers

If the session is "idle" the query column shows the last statement that connection has executed. It is not the "current" query, so the connection is not waiting for the commit to finish.

The query column only shows the current statement if status shows active.

An "idle" connection is not a problem and essentially the reason why a connection pool is used, so that it can be re-used. However, sessions that stay very long in "idle in transaction" are a problem. But you stated your connections are in "idle" state.

If your connection pool reaches the limit, this most probably means your application is not returning the connections properly to the pool. It is something you need to fix in your application.

like image 178
a_horse_with_no_name Avatar answered Sep 20 '22 14:09

a_horse_with_no_name