Proper way to reset/cleanup a connection
Assuming libpq, you can call PQtransactionStatus to find out if there
's a transaction open.

If you only have SQL then you can call 

select xact_start &lt;&gt; query_start from pg_catalog.pg_stat_activity where pid=pg_backend_pid();


That round-trip will add latency to the cleanup process, but this will not be a problem so long as there's no process blocked waiting for the freed connection.