Fix postgres query bug?