fixed pg query bug