Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 472 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Heroku Postgres: Too many connections. How do I kill these connections?

#1
I have an app running on Heroku. This app has an Postgres 9.2.4 (Dev) addon installed. To access my online database I use Navicat Postgres. Sometimes Navicat doesn't cleanly close connections it sets up with the Postgres database. The result is that after a while there are 20+ open connections to the Postgres database. My Postgres installs only allows 20 simultanious connections. So with the 20+ open connections my Postgress database is now unreachable (too many connections).

I know this is a problem of Navicat and I'm trying to solve this on that end. But if it happens (that there are too many connections), how can I solve this (e.g. close all connections).

I've tried all of the following things, without result.

- Closed Navicat & restarted my computer (OS X 10.9)
- Restarted my Heroku application (`heroku restart`)
- Tried to restart the online database, but I found out there is no option to do this
- Manually closed all connections from OS X to the IP of the Postgres server
- Restarted our router

I think it's obvious there are some 'dead' connections at the Postgres side. But how do I close them?
Reply

#2
As the superuser (eg. "postgres"), you can kill every session but your current one with a query like this:

select pg_cancel_backend(pid)
from pg_stat_activity
where pid <> pg_backend_pid();

If they do not go away, you might have to use a stronger "kill", but certainly test with `pg_cancel_backend()` first.

select pg_terminate_backend(pid)
from pg_stat_activity
where pid <> pg_backend_pid();
Reply

#3
From the [Heroku documentation][1] (emphasis is mine):

> FATAL: too many connections for role

FATAL: too many connections for role "[role name]"
This occurs on Starter Tier (dev and basic) plans, which have a max connection limit of 20 per user. **To resolve this error, close some connections to your database by stopping background workers, reducing the number of dynos, or restarting your application in case it has created connection leaks over time.** A discussion on handling connections in a Rails application can be found here.

Because Heroku [does not provide superuser access][2] your options are rather limited to the above.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
Maybe have a look at what `heroku pg:kill` can do for you?

[To see links please register here]

Reply

#5
`heroku pg:killall` will kill all open connections, but that may be a blunt instrument for your needs.
Interestingly, **you can actually kill specific connections** using heroku's dataclips.

To get a detailed list of connections, you can query via dataclips:

SELECT * FROM pg_stat_activity;

In some cases, you may want to kill all connections associated with an IP address (your laptop or in my case, a server that was now destroyed).

You can see how many connections belong to each client IP using:

SELECT client_addr, count(*)
FROM pg_stat_activity
WHERE client_addr is not null
AND client_addr <> (select client_addr from pg_stat_activity where pid=pg_backend_Tid())
GROUP BY client_addr;

which will list the number of connections per IP excluding the IP that dataclips itself uses.

To actually kill the connections, you pass their "pid" to pg_terminate_backend(). In the simple case:

SELECT pg_terminate_backend(1234)

where 1234 is the offending PID you found in pg_stat_activity.

In my case, I wanted to kill all connections associated with a (now dead) server, so I used:

SELECT pg_terminate_backend(pid), host(client_addr)
FROM pg_stat_activity
WHERE host(client_addr) = 'IP HERE'
Reply

#6
Restart server


heroku restart --app <app_name>

It will close all connection and restart.
Reply

#7
__1).__ First login into Heroku with your correct id (in case you have multiple accounts) using `heroku login`.
__2).__ Then, run `heroku apps` to get a list of your apps and copy the name of the one which is having the PostgreSQL db installed.
__3).__ Finally, run `heroku pg:killall --app appname` to get all the connections terminated.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through