Fix SQL issues on Postgresql 8