Fix postgresql connection leaks

Return connection to pool on connection error (see #1708)
Fix leaks on persist_connection=false (#1711)

Includes testcase for #1711
Merge pull request #1764 from strk/master_connection_leaks

Fix postgresql connection leaks