karenc / db-migrator

GNU Affero General Public License v3.0
4 stars 5 forks source link

Explicitly close all postgres db connections #50

Closed karenc closed 7 years ago

karenc commented 7 years ago

The psycopg2 db connection with statement does not actually close the database connection causing lots of idle postgres connections on the production servers. An idle postgres connection can be found by doing ps aux on the database servers, for example you will see something like this:

postgres 25498  0.0  0.5 302652 10996 ?        Ss   09:31   0:00 postgres: cnxarchive cnxarchive 192.168.122.250(42646) idle

On http://initd.org/psycopg/docs/usage.html#with-statement

"Note that, unlike file objects or other resources, exiting the connection’s with block doesn't close the connection but only the transaction associated with it"

This change creates a db_connect function that is like psycopg2.connect but closes the db connection when exiting the with block.

coveralls commented 7 years ago

Coverage Status

Coverage increased (+0.02%) to 97.148% when pulling ed9189687cc850664c97bd217b6b91e28abc0bca on close-db-connections into 0333632d0469bc0eca5cd56c1abad90b05e9b43f on master.