[not ready to merge] WIP on pgsql

A lot of improvment on the postgresql front, not safe for merge yet, but waiting for ongoing review.