Completed SQL generation for Postgres