Controlling number of idle sessions in postgresql 11.5


We recently moved from PostgreSQL 9.0 to PostgreSQL 11.5.
We are now having problems with hundreds of idle sessions consuming all the database connections.

We have tried defining idle_in_transaction_session_timeout=30000. That only managed to abruptly terminate legitimate processes while they were awaiting results from queries

We have tried defining tcp_keepalives_idle=300, tcp_keepalives_interval=30, and tcp_keepalives_count=3 Still, the number of idle sessions are in the hundreds.

When we kill idle sessions, or restart the database, the idle sessions reconnect, and stay idle.