move middle_t::cleanup code into destructor

Fixes a whole bunch of leaking postgres connections.