Better initial query construction, fix for postgres (again).