psql:致命错误:剩余的连接插槽保留用于非复制超级用户连接 [英] psql: FATAL: remaining connection slots are reserved for non-replication superuser connections
问题描述
我想了解何时发生此错误以及如何解决。我检查了 pg_stat_activity
和 pg_locks
,但无法弄清哪个进程正在耗尽连接
I want to understand when this error occurs and how to resolve it. I checked pg_stat_activity
and pg_locks
but couldnt figure out which process is exhausing the connections
我们正在使用sqlalchemy如下连接数据库
We are using sqlalchemy to connect to database as below
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine_url = f'{dbms}://{username}:{password}@{hostname}:{port}/{database}'
engine = create_engine(engine_url, pool_size=20, max_overflow=10)
Session = sessionmaker(bind=engine)
db_session = Session()
这是 Heroku psql:致命:剩余的连接插槽保留用于非复制超级用户连接
推荐答案
I我在sqlalchemy引擎url的应用程序名称中添加了客户端进程ID。这将存储在 pg_stat_activity
中的 application_name
下,这对于从打开此连接的位置进行调试将很有帮助。 / p>
I am adding the client process id in application name of sqlalchemy engine url. This will be stored under application_name
in pg_stat_activity
and it will be helpful to debug from where did this connection was opened.
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
client_hostname = os.environ.get('HOSTNAME', 'UNKNOWN')
client_pid = os.getpid()
engine_url = f'{dbms}://{username}:{password}@{hostname}:{port}/{database}?application_name={client_hostname}_{client_pid}'
engine = create_engine(engine_url, pool_size=20, max_overflow=10)
Session = sessionmaker(bind=engine)
db_session = Session()
连接起源的地方,应用程序名称的限制为 64个字符,因此我们可以包含要传递给数据库统计信息的所有自定义信息
This shows where the connection was originated from and the application name has a limit of 64 chars, so we can include any custom information that you want to pass on to db stats
这篇关于psql:致命错误:剩余的连接插槽保留用于非复制超级用户连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!