fixed postgres query