Reimplement PostgreSQL COPY escaping from first principles
Instead of the brittle csv we used to use, use postgresql text mode to transfer
data in the copy operation. Reference all relevant bits of the PostgreSQL
documentation inline with the code.
This also adds test coverage for most supported types, as well as making sure
all escaping corner cases are covered.