D
D
Denis Melnikov2021-12-09 21:47:06
PostgreSQL
Denis Melnikov, 2021-12-09 21:47:06

How to fix FATAL: remaining connection slots are reserved for non-replication superuser connections?

I rummaged through a bunch of information, I didn’t find the answer, or I’m looking badly.
There is Django 3.2, there is postgres, there are a bunch of direct SQL queries through

with connection.cursor() as cursor:
        cursor.execute

That is, closing the connection at the code level.
psql has 300 connections
configured Worth setting
SET SESSION idle_in_transaction_session_timeout = '2min';


I tried to put it in the setting I thought it might keep the connection ... But no, IDLE connections accumulate very quickly, reaching the limit ... It helps either to reset the connections, with idle > 5 minutes
CONN_MAX_AGE = 120



SELECT pg_terminate_backend(pid)
FROM inactive_connections
WHERE rank > 1;


or reboot psql, even if it doesn't work above ...

idle connections can hang for a long time, it feels like they are still not reset automatically.
How to check .. I do not know.
61b24f07d552d591762324.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-12-09
@Mi11er

That is, closing the connection at the code level..

Yes? Who told you this?
Connections can be used as context managers. Note that a context wraps a transaction: if the context exits with success the transaction is committed, if it exits with an exception the transaction is rolled back. Note that the connection is not closed by the context and it can be used for several contexts

https://www.psycopg.org/docs/connection.html
Especially since you have context on the cursor.
SET SESSION idle_in_transaction_session_timeout = '2min';

By again. IDLE and IDLE IN TRANSACTION are obviously different states.
Close connections properly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question