Refactoring ext/pgsql (incompleted, only compilable)