lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
9.02k stars 910 forks source link

Document binary_parameters connection parameter #435

Open kevinburkeshyp opened 8 years ago

kevinburkeshyp commented 8 years ago

I'm trying to send/read UUID's from the database. My first thought was that UUID's were 16 bytes, so I should be able to send []byte to the database, but this failed with the 22021 error.

Reading through the tests, it seems like I can only do this if I pass binary_parameters=yes as part of the connection string. However, the consequences of doing so are not very clear; it seems like this goes down a branch that disables prepared statements?

It would be nice if there was more documentation/help around how to pass UUID's to the database; I'm just using a string for now.

johto commented 8 years ago

Reading through the tests, it seems like I can only do this if I pass binary_parameters=yes as part of the connection string. However, the consequences of doing so are not very clear; it seems like this goes down a branch that disables prepared statements?

No, it doesn't do that. It just uses the underlying protocol a bit differently.

But yeah, with that option byte slices are passed to the database as-is in binary, which means that passing a UUID as a 16-byte slice works directly.

It would be nice if there was more documentation/help around how to pass UUID's to the database; I'm just using a string for now.

Yup, that's how you do it. Every type not recognized by database/sql has to be passed as the text representation of the type, except for bytea values which are converted in the driver automatically for historical reasons. We can't possibly be expected to list how to pass every single type in the documentation, but we could probably do a bit better.

cbandy commented 8 years ago

Here's some relevant PostgreSQL documentation: http://www.postgresql.org/docs/current/static/protocol-overview.html#PROTOCOL-FORMAT-CODES

The binary format of nontrivial types is ... deep knowledge.