Improve reliability of PostgreSQL connections

This patch adds support for detecting that a query failed due to
the DB connection being down and attempts to reconnect before
retrying the query.  It does this for opening transactions,
safeExec(), and prepareAndExecuteQuery().