Various bug fixing for PgSQL