Add new function pg_get_connection_safe() and use it instead of two calls to pg_get_connection() and pg_get_pgconn(). It is faster and less confusing.