Fixed segments SQL queries for PostgreSQL compatibility