[need review] PGSQL state/mod improvments

A lot of improvment on the postgresql front, not safe for merge yet, but waiting for ongoing review.

* [DONE]: Tests on changes
* [DONE]: pgsql extentions handling