Fix a PostgreSQL string escaping bug and use pg_escape_literal() when possible