Fixed issue with broken query on PostgreSQL, closes #748