pgx icon indicating copy to clipboard operation
pgx copied to clipboard

A point to document why to use the native PGX instead of database/sql interface

Open dansmill opened this issue 7 years ago • 1 comments

Just a note to maybe help someone else just starting out in using your package. I just spent hours trying to figure out why my postgresql text array was not mapping to a []string.

When I was implementing my Postgresql interface I was looking at database/sql first of course and seeing the different drivers available. I ran across an article at http://go-database-sql.org/importing.html and in the article they said:

You generally shouldn’t use driver packages directly, although some drivers encourage you to do so. (In our opinion, it’s usually a bad idea.) Instead, your code should only refer to types defined in database/sql, if possible. This helps avoid making your code dependent on the driver, so that you can change the underlying driver (and thus the database you’re accessing) with minimal code changes. It also forces you to use the Go idioms instead of ad-hoc idioms that a particular driver author may have provided.

So by reading this, I was using your stdlib driver with database/sql. I got it all working and then I ran into the problem with mapping the text array. That has been around so long in Postgres, I didnt really consider that an advanced area so figured it should work. Well, it didnt and I had a terrible time finding anything on it or what the problem could be.

And then I happen to find an issue in here from back in 2015 and your quote here that says:

No. This is one of the major reasons for the native pgx interface.

The database/sql interface only allows the underlying driver to return or receive the following types: int64, float64, bool, []byte, string, time.Time, or nil. The only way to handle other types is to implement the database/sql.Scanner and the database/sq/driver.Valuer interfaces. These interfaces require using the text format for transmitting values. The binary format can be substantially faster, and that is what the pgx native interface uses to encode and decode values for PostgreSQL. However, this means that the array (and other advanced types) support in pgx is not compatible with database/sql because it only understands the binary format -- not the text format that database/sql would use.

That explained everything and a very good reason on why I should be using the native pgx interface even though that other article said I shouldnt. Im actually surprised there wasnt more about it out there. Maybe I missed something obvious somewhere but whatever it was, it took a lot of searching to find that quote of yours. So I used the native interface and everything is working as I expect it to. So this is just to give you a perspective on a new user to your package and my experience with one aspect of it and maybe it will help someone else down the line. I hope this feedback helps.

dansmill avatar Aug 15 '19 11:08 dansmill

Thanks for the feedback. I added some of this information to the README: https://github.com/jackc/pgx#choosing-between-the-pgx-and-databasesql-interfaces.

jackc avatar Sep 16 '19 14:09 jackc