query fixes for PostgreSQL