go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.52k stars 118 forks source link

Generator issue with pgbouncer #211

Closed krystofspl closed 1 year ago

krystofspl commented 1 year ago

Describe the bug

I set up the generator to connect to our PG database. However I'm getting these errors when running it:

jet: pq: bind message supplies 2 parameters, but prepared statement "" requires 1, retrying
jet: pq: unnamed prepared statement does not exist, retrying
jet: ERROR: prepared statement "stmtcache_1" already exists (SQLSTATE 42P05)

Sometimes re-running the generator overcomes it and everything is generated correctly.

After some digging, I found that the issue is with pgbouncer. Pgbouncer sends the prepared statement to one session and the select to another, so there is the mismatch.

It can be fixed by supplying the statement_cache_mode=describe as hinted here: https://github.com/jackc/pgx/issues/650

This also means using jackc/pgx instead of lib/pq as the driver, as the latter doesn't even offer this. This driver is usually recommended, since it is more up-to-date anyways.

This could be quite an easy fix (which I verified to be working locally), but this option works only in pgx version 4, the current one is 5. That's where I got lost. Do you have any thoughts around this?

I resolved this for our case by not connecting to pgbouncer and connecting directly instead.

Environment (please complete the following information):

Code snippet The errors happen in GetTableColumnsMetaData.

houten11 commented 1 year ago

It seems connection string option for pq is binary_parameters=yes

go-jet commented 1 year ago

I'll close it, as it seems pq has this options as well.