Postgresql idle timeout

Description

При ошибке: remaining connection slots are reserved for non-replication superuser connections

Snippet code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
6

If you are using PostgreSQL >= 9.6 there is an even easier solution. Let's suppose you want to delete all idle connections every 5 minutes, just run the following:

alter system set idle_in_transaction_session_timeout='5min';
In case you don't have access as superuser (example on Azure cloud), try:

SET SESSION idle_in_transaction_session_timeout = '5min';
But this latter will work only for the current session, that most likely is not what you want.

To disable the feature,

alter system set idle_in_transaction_session_timeout=0;
or

SET SESSION idle_in_transaction_session_timeout = 0;
(by the way, 0 is the default value).

If you use alter system, you must reload configuration to start the change and the change is persistent, you won't have to re-run the query anymore if, for example, you will restart the server.

To check the feature status:

show idle_in_transaction_session_timeout;