postgresql - Django ORM leaves idle connections on Postgres DB -
recently, django app has been crashing due database connection errors:
operationalerror: fatal: sorry, many clients when go app database, see indeed there 100 open connections, same query (executed django orm) , in idle state.
i have been manually doing select pg_terminate_backend(pid) pg_stat_activity state = 'idle'; perplexed why happening. can shed insight happening here?
my django database settings not stray defaults (i have not defined conn_max_age or of nature).
what cause this? i'm not doing advanced django queries. can solved django setting or perhaps postgresql configuration? advice appreciated.
apparently don't disconnect. using db.close_connection() after query finishes help. if right conn_max_age short value help. , consider using session pooler, eg pgbouncer django connections. way if have many connections wait (or reuse previous, depending on config) instead of aborting execution error...
update: explanation why propose it
each thread maintains own connection, database must support @ least many simultaneous connections have worker threads.
so if have more threads postgres max_connections, mentioned error. each thread can reuse connection if conn_max_age has not passed. setting 0, connection should closed after query completion, see 100 idle connection. not closing. big number of connection means not reused either (logic: if have 100 parallel queries not idle, , if have many, not reused - opening new). think django not close them prommised - conn_max_age set 0 not work in code. propose using db.close_connection() force disconnect , setting conn_max_age small value can change behaviour.
Comments
Post a Comment